<template>
  <div>
    <div class="d-flex flex-between">
      <div>
        <el-button type="success" size="medium" @click="addData">
          <span class="d-in-block el-icon-plus v-mid font-size-16">新建比赛</span>
        </el-button>
      </div>
      <el-form label-width="100px" :inline="true">
        <el-form-item prop="bs_type_name">
          <el-select class="mr10" v-model="bs_type_name" placeholder="全部" @change="getListData">
            <el-option v-for="(item,index) in typeData" :key='index' :value="item">
              {{item}}
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="keyword">
          <el-input placeholder="请输入搜索关键字" v-model="keyword" clearable @clear="getListData">
          </el-input>
        </el-form-item>
        <el-button type="primary" class="ml5" @click="getListData">搜索</el-button>
      </el-form>
    </div>
    <el-table :data="tableData" tooltip-effect="dark" style="width: 100%">
      <el-table-column type="selection" align="center" width="55">
      </el-table-column>
      <el-table-column label="序号" align="center" type="index" width="50">
      </el-table-column>
      <el-table-column label="标题" width="" prop="bs_name"></el-table-column>
      <el-table-column label="比赛类型" width="" prop="bs_type_name"></el-table-column>
      <el-table-column label="报名开始时间" width="" prop="bs_bmsj"></el-table-column>
      <el-table-column label="报名结束时间" width="" prop="bs_end_bmsj"></el-table-column>
      <el-table-column label="比赛内容" show-overflow-tooltip width="" prop="bs_content">
        <template slot-scope="scope"><span v-html="scope.row.bs_content"></span></template>
      </el-table-column>
      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-popover placement="right" :width="150" trigger="click">
            <i style="transform: rotate(90deg); cursor: pointer" slot="reference" class="el-icon-more"></i>
            <!-- 2是没设置呢 1是已设置 -->
            <el-button type="text" v-show="scope.row.is_set_ry==2" @click="editData(scope.row)">编辑 </el-button>
            <el-button type="text" v-show="scope.row.is_set_ry==2" @click="delData(scope.row.bs_id)">删除 </el-button>
            <el-button type="text" @click="toDetail(scope.row.bs_id)">查看 </el-button>
          </el-popover>
        </template>
      </el-table-column>

    </el-table>
    <footer class="flex-item-none" style="display: flex; justify-content: flex-end; margin-top: 30px">
      <el-pagination background layout="total,prev, pager, next" :page-size="limit" :total="total" :current-page.sync="cur_page" @current-change="handleCurrentChange"></el-pagination>
    </footer>

    <el-dialog :title="title" :visible.sync="dialogFormVisible" v-if="dialogFormVisible" width="700px" destory-on-close>
      <el-form :model="dialogForm" ref="dialogForm" :rules="rules" label-width="110px">
        <el-form-item label="标题" prop="bs_name">
          <el-input v-model="dialogForm.bs_name"></el-input>
        </el-form-item>

        <el-form-item label="比赛类型" prop="bs_type_name">
          <el-input v-model="dialogForm.bs_type_name"></el-input>
        </el-form-item>

        <el-form-item label="报名开始时间" prop="bs_bmsj">
          <el-date-picker v-model="dialogForm.bs_bmsj" type="datetime" value-format="yyyy-MM-dd HH:mm:ss">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报名结束时间" prop="bs_end_bmsj">
          <el-date-picker v-model="dialogForm.bs_end_bmsj" type="datetime" value-format="yyyy-MM-dd HH:mm:ss">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="内容" prop="bs_content">
          <Tinymce v-if="dialogFormVisible" v-model="dialogForm.bs_content" :height="300" :key="timestamp"></Tinymce>
        </el-form-item>

        <el-form-item label="附件" prop="bs_file">
          <FileUpload v-if="dialogFormVisible" :limit="5" :full.sync="dialogForm.bs_file" :key="timestamp" multiple>
            <div slot="tip" class="el-upload__tip">
              支持文件类型:doc(.docx)、.ppt(.pptx)、.xls(.xlsx)、.pps、.wps、.pdf、.txt文件格式。
            </div>
          </FileUpload>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="formCancel">取 消</el-button>
        <el-button type="primary" :disabled="isButton" @click="formSubmit('dialogForm')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { cjbs_list, cjbs_add, cjbs_edit, cjbs_del, type_list } from "./api";
import Tinymce from "@/components/TinymceVue/index.vue";
import FileUpload from "@/components/FileUpload/index.vue";
export default {
  name: "index",
  components: {
    Tinymce,
    FileUpload,
  },
  data() {
    return {
      isButton: false,
      keyword: "",
      bs_type_name: "",
      tableData: [],
      typeData: {},
      limit: 10,
      total: 0,
      cur_page: 1,
      dialogFormVisible: false,
      dialogForm: {
        bs_name: "",
        bs_type_name: "",
        bs_bmsj: "",
        bs_content: "",
        bs_file: "",
        bs_end_bmsj: ""
      },
      isEdit: false,
      timestamp: Date.now(),
      title: "",
      rules: {
        bs_name: [
          { required: true, message: "请输入标题", trigger: "blur" },
        ],
        bs_type_name: [
          { required: true, message: "请输入比赛类型", trigger: "blur" },
        ],
        bs_bmsj: [
          { required: true, message: "请选择报名开始时间", trigger: "blur" },
        ],
        bs_end_bmsj: [
          { required: true, message: "请选择报名结束时间", trigger: "blur" },
        ],
        bs_content: [
          { required: true, message: "请输入内容", trigger: "blur" },
        ],
        bs_file: [
          { required: true, message: "请上传附件", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    getListData() {
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
        bs_type_name: this.bs_type_name
      };
      cjbs_list(data).then((res) => {
        this.tableData = res.data.page_data;
        this.cur_page = Number(res.data.page_now);
        this.total = Number(res.data.total_rows);
      });
    },
    typeListData() {
      type_list().then((res) => {
        this.typeData = res.data;
      });
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    addData() {
      this.title = "新建比赛";
      this.isButton = false;
      this.dialogForm = {
        bs_name: "",
        bs_type_name: "",
        bs_bmsj: "",
        bs_content: "",
        bs_file: "",

      };

      this.dialogFormVisible = true;
    },
    editData(item) {
      this.title = "编辑比赛";
      this.isEdit = true;
      this.dialogFormVisible = true;
      this.id = item.bs_id;
      this.dialogForm.bs_name = item.bs_name;
      this.dialogForm.bs_type_name = item.bs_type_name;
      this.dialogForm.bs_bmsj = item.bs_bmsj;
      this.dialogForm.bs_content = item.bs_content;
      this.dialogForm.bs_file = item.bs_file;
      this.dialogForm.bs_end_bmsj = item.bs_end_bmsj;
    },

    formSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.isButton = true;
          let data = {
            bs_name: this.dialogForm.bs_name,
            bs_type_name: this.dialogForm.bs_type_name,
            bs_bmsj: this.dialogForm.bs_bmsj,
            bs_content: this.dialogForm.bs_content,
            bs_file: this.dialogForm.bs_file,
            bs_end_bmsj: this.dialogForm.bs_end_bmsj

          };
          if (!this.isEdit) {
            cjbs_add(data).then((res) => {
              if (res.code == 1) {
                this.$message({
                  message: "添加成功！",
                  type: "success",
                });

                this.dialogFormVisible = false;
                this.getListData();
                this.typeListData();
              } else {
                this.isButton = false;
                this.$message({
                  message: res.msg,
                  type: "error",
                });
              }
            });
          } else {
            data.bs_id = this.id;
            cjbs_edit(data).then((res) => {
              if (res.code == 1) {
                this.$message({
                  message: "编辑成功！",
                  type: "success",
                });
                this.dialogFormVisible = false;
                this.getListData();
                this.typeListData();
              } else {

                this.$message({
                  message: res.msg,
                  type: "error",
                });
              }
            });
          }
        }
      });
    },
    formCancel() {
      this.dialogFormVisible = false;
    },
    toDetail(id) {
      this.$router.push({ name: "cjbs_detail", params: { id: id } });
    },
    delData(id) {
      let data = {
        bs_id: id,
      };
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          cjbs_del(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "删除成功！",
                type: "success",
              });
              this.getListData();
              this.typeListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => { });
    },

  },
  mounted() {
    this.getListData();
    this.typeListData();
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__body {
  border-top: 1px solid #f0f3f8;
}
</style>
