<template>
  <div>
    <div class="d-flex flex-between">
      <div>
        <el-button type="primary" size="medium" @click="addData">
          <span class="d-in-block v-mid el-icon-plus font-size-16"></span>
          <span class="d-in-block v-mid font-size-16">新建活动</span>
        </el-button>
      </div>
      <el-form label-width="100px" :inline="true">
        <el-form-item prop="state_value">
          <el-select class="mr10" v-model="type_value" placeholder="请选择类别">
            <el-option
              v-for="item in type_list"
              :key="item.kylb_id"
              :label="item.kylb_title"
              :value="item.kylb_id"
            ></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"
      v-loading="loading"
      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="活动名称"
        show-overflow-tooltip
        prop="kyhd_name"
        width="auto"
      ></el-table-column>
      <el-table-column
        label="类别"
        show-overflow-tooltip
        prop="kylb_id"
        width="150"
      >
      </el-table-column>
      <el-table-column
        label="创建人"
        width="100"
        prop="kyhd_cyry"
      ></el-table-column>
      <el-table-column
        label="创建时间"
        width="150"
        prop="kyhd_hdsj"
      ></el-table-column>
      <el-table-column label="活动内容" width="200">
        <template slot-scope="scope">
          <div v-html="scope.row.kyhd_hdnr"></div>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" @click="editData(scope.row)">编辑 </el-button>
          <el-button type="text" @click="delData(scope.row.kyhd_id)"
            >删除
          </el-button>
        </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"
      width="750px"
      destory-on-close
    >
      <el-form :model="dialogForm" label-width="80px">
        <el-form-item label="科研类别" prop="kylb">
          <el-select
            v-model="dialogForm.kylb"
            style="width: 100%"
            placeholder="请选择科研类别类型"
          >
            <el-option
              v-for="item in type_list"
              :key="item.kylb_id"
              :label="item.kylb_title"
              :value="item.kylb_id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="活动名称" prop="hdmc">
          <el-input
            v-model="dialogForm.hdmc"
            placeholder="请输入活动名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="参与人员" prop="cyry">
          <el-input
            v-model="dialogForm.cyry"
            placeholder="请输入参与人员"
          ></el-input>
        </el-form-item>
        <el-form-item label="活动时间" prop="hdsj">
          <el-date-picker
            v-model="dialogForm.hdsj"
            placeholder="选择日期"
            type="date"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="活动内容" prop="hdnr">
          <Tinymce
            v-if="dialogFormVisible"
            v-model="dialogForm.hdnr"
            :height="300"
            :key="timestamp"
          ></Tinymce>
        </el-form-item>

        <el-form-item label="其他附件" prop="qtfj">
          <FileUpload
            :limit="5"
            :full.sync="dialogForm.qtfj"
            :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" @click="formSubmit">发 布</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Tinymce from "@/components/TinymceVue/index.vue";
import FileUpload from "@/components/FileUpload/index.vue";
import { kyhd_list, kyhd_add, kyhd_edit, kyhd_del, kylb_list } from "./api";
import { useUserStore } from "@/stores/user";
const { real_name } = useUserStore();

export default {
  name: "index",
  components: {
    Tinymce,
    FileUpload,
  },
  data() {
    return {
      type_value: "",
      type_list: [],
      keyword: "",
      tableData: [],
      limit: 10,
      total: 12,
      cur_page: 1,
      dialogFormVisible: false,
      dialogForm: {
        kylb: "",
        hdmc: "",
        cyry: "",
        hdsj: "",
        hdnr: "",
        qtfj: "",
      },
      title: "",
      isEdit: false,
      id: "",
      loading: false,
      timestamp: Date.now(),
    };
  },

  methods: {
    getkylbList() {
      let data = {
        page: 1,
        limit: 999,
      };

      kylb_list(data).then((res) => {
        this.type_list = res.data.page_data;

        let obj = {
          kylb_id: "",
          kylb_title: "全部",
        };
        this.type_list.unshift(obj);
      });
    },
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
      };

      kyhd_list(data).then((res) => {
        this.loading = false;
        this.tableData = res.data.page_data;
        this.cur_page = Number(res.data.page_now);
        this.total = Number(res.data.total_rows);
      });
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    addData() {
      this.title = "新建活动";
      this.dialogForm = {
        kylb: "",
        hdmc: "",
        cyry: "",
        hdsj: "",
        hdnr: "",
        qtfj: "",
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    editData(item) {
      this.title = "编辑活动";
      this.isEdit = true;
      this.id = item.kyhd_id;
      this.dialogForm.kylb = item.kylb_id;
      this.dialogForm.hdmc = item.kyhd_name;
      this.dialogForm.cyry = item.kyhd_cyry;
      this.dialogForm.hdsj = item.kyhd_hdsj;
      this.dialogForm.hdnr = item.kyhd_hdnr;
      this.timestamp = Date.now();
      this.dialogForm.qtfj = item.kyhd_qtfj;

      this.dialogFormVisible = true;
    },
    formSubmit() {
      let data = {
        kylb_id: this.dialogForm.kylb,
        kyhd_name: this.dialogForm.hdmc,
        kyhd_cyry: real_name,
        kyhd_hdsj: this.dialogForm.hdsj,
        kyhd_hdnr: this.dialogForm.hdnr,
        kyhd_qtfj: this.dialogForm.qtfj,
      };
      if (!this.isEdit) {
        kyhd_add(data).then((res) => {
          if (res.code == 1) {
            this.$message({
              message: "活动添加成功！",
              type: "success",
            });

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

            this.dialogFormVisible = false;
            this.getListData();
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        });
      }
    },
    formCancel() {
      this.dialogFormVisible = false;
    },
    delData(id) {
      let data = {
        kyhd_id: id,
      };
      this.$confirm("确认删除该活动?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          kyhd_del(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "活动删除成功！",
                type: "success",
              });
              this.getListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => {});
    },
  },
  mounted() {
    this.getkylbList();
    this.getListData();
  },
};
</script>

<style scoped>
</style>
