<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="type_value">
          <el-select
            class="mr10"
            v-model="type_value"
            @change="getListData"
            placeholder="请选择任务"
          >
            <el-option
              v-for="item in rwList"
              :key="item.ddrw_id"
              :label="item.ddrw_name"
              :value="item.ddrw_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 label="序号" align="center" type="index" width="50">
      </el-table-column>
      <el-table-column
        label="所属任务"
        show-overflow-tooltip
        prop="ddrw_name"
        width="auto"
      ></el-table-column>
      <el-table-column
        label="创建人"
        width="200"
        prop="ddzj_cjr"
      ></el-table-column>
      <el-table-column
        label="创建时间"
        width="150"
        prop="ddzj_cjsj"
      ></el-table-column>
      <el-table-column label="负责人" prop="ddzj_fzr" width="150">
      </el-table-column>
      <el-table-column
        label="综述内容"
        show-overflow-tooltip
        prop="ddzj_fkdx"
        width="auto"
      >
        <template slot-scope="scope">
          <div v-html="scope.row.ddzj_fkdx"></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.ddzj_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="surw">
          <el-select
            v-model="dialogForm.surw"
            style="width: 100%"
            placeholder="请选择任务名称"
          >
            <el-option
              v-for="item in rwList"
              :key="item.ddrw_id"
              :label="item.ddrw_name"
              :value="{ value: item.ddrw_id, label: item.ddrw_name }"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="负责人" prop="fzr">
          <el-input v-model="dialogForm.fzr"></el-input>
        </el-form-item>

        <el-form-item label="创建时间" prop="cjsj">
          <el-date-picker
            v-model="dialogForm.cjsj"
            placeholder="创建日期"
            type="date"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="内容" prop="nr">
          <Tinymce
            v-if="dialogFormVisible"
            v-model="dialogForm.nr"
            :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 { useUserStore } from "@/stores/user";
const { real_name } = useUserStore();
import { gzzj_list, gzzj_add, gzzj_edit, gzzj_del, ddrw_list } from "./api";
export default {
  name: "index",
  components: {
    Tinymce,
    FileUpload,
  },
  data() {
    return {
      keyword: "",
      type_value: "",
      rwList: [],
      tableData: [],
      limit: 10,
      total: 12,
      cur_page: 1,
      dialogFormVisible: false,

      dialogForm: {
        surw: {
          label: "",
          value: "",
        },
        cjr: "",
        fzr: "",
        cjsj: "",
        nr: "",
      },
      title: "",
      isEdit: false,
      id: "",
      loading: false,
      timestamp: Date.now(),
    };
  },
  methods: {
    getrwList() {
      let data = {
        page: 1,
        limit: 999,
      };

      ddrw_list(data).then((res) => {
        this.rwList = res.data.page_data;

        let obj = {
          ddrw_id: "",
          ddrw_name: "全部",
        };
        this.rwList.unshift(obj);
      });
    },
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
        ddrw_name: this.type_value,
      };

      ddrw_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);
      });
    },
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
        ddrw_name: this.type_value,
      };

      gzzj_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 = {
        surw: {
          label: "",
          value: "",
        },
        cjr: "",
        fzr: "",
        cjsj: "",
        nr: "",
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    editData(item) {
      this.title = "编辑总结";
      this.isEdit = true;
      this.dialogFormVisible = true;
      this.id = item.ddzj_id;
      this.dialogForm.surw.value = item.ddrw_id.toString();
      this.dialogForm.surw.label = item.ddrw_name;
      this.dialogForm.cjr = item.ddzj_cjr;
      this.dialogForm.fzr = item.ddzj_fzr;
      this.dialogForm.cjsj = item.ddzj_cjsj;
      this.dialogForm.nr = item.ddzj_fkdx;
    },
    formSubmit() {
      let data = {
        ddrw_id: this.dialogForm.surw.value,
        ddrw_name: this.dialogForm.surw.label,
        ddzj_cjr: real_name,
        ddzj_fzr: this.dialogForm.fzr,
        ddzj_cjsj: this.dialogForm.cjsj,
        ddzj_fkdx: this.dialogForm.nr,
      };
      if (!this.isEdit) {
        gzzj_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.ddzj_id = this.id;
        gzzj_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 = {
        ddzj_id: id,
      };
      this.$confirm("确认删除该总结?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          gzzj_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.getListData();
    this.getrwList();
  },
};
</script>

<style scoped>
</style>
