<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="ddjh_name"
        width="auto"
      ></el-table-column>
      <el-table-column
        label="创建人"
        width="100"
        prop="ddjh_cjr"
      ></el-table-column>
      <el-table-column
        label="计划时间"
        width="150"
        prop="ddjh_jhsj"
      ></el-table-column>
      <el-table-column label="负责人" prop="ddjh_fzr" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.ddjh_fzr == 1">张三</span>
          <span v-if="scope.row.ddjh_fzr == 2">李四</span>
          <span v-if="scope.row.ddjh_fzr == 3">王五</span>
        </template>
      </el-table-column>
      <el-table-column label="所属任务" prop="ddrw_name" width="100">
      </el-table-column>
      <el-table-column
        label="计划内容"
        prop="ddjh_jhnr"
        show-overflow-tooltip
        width="auto"
      >
        <template slot-scope="scope">
          <div v-html="scope.row.ddjh_jhnr"></div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="250">
        <template slot-scope="scope">
          <el-button type="text" @click="detailData(scope.row)"
            >详情
          </el-button>
          <el-button type="text" @click="editData(scope.row)">编辑 </el-button>
          <el-button type="text" @click="ddjlData(scope.row)"
            >督导记录
          </el-button>
          <el-button type="text" @click="delData(scope.row.ddjh_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="editDialogFormVisible"
      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="jhmc">
          <el-input v-model="dialogForm.jhmc"></el-input>
        </el-form-item>

        <el-form-item label="负责人" prop="fzr">
          <el-select v-model="dialogForm.fzr" style="width: 100%">
            <el-option
              v-for="item in fzr_list"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="截止时间" prop="jhsj">
          <el-date-picker
            v-model="dialogForm.jhsj"
            type="date"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="消息内容" prop="jhnr">
          <Tinymce
            v-if="editDialogFormVisible"
            v-model="dialogForm.jhnr"
            :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>

    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="750px">
      <div class="pop-content">
        <div class="mt15 d-flex">
          <div class="font-size-16">计划名称：</div>
          <div class="flex-1 font-size-16">{{ detailInfo.ddjh_name }}</div>
        </div>
        <div class="mt15 d-flex">
          <div class="font-size-16">创建人：</div>
          <div class="flex-1 font-size-16">{{ detailInfo.ddjh_cjr }}</div>
        </div>
        <div class="mt15 d-flex">
          <div class="font-size-16">计划时间：</div>
          <div class="flex-1 font-size-16">{{ detailInfo.ddjh_jhsj }}</div>
        </div>
        <div class="mt15 d-flex">
          <div class="font-size-16">负责人：</div>
          <div class="flex-1 font-size-16">{{ detailInfo.ddjh_fzr }}</div>
        </div>
        <div class="mt15 d-flex">
          <div class="font-size-16">所属任务：</div>
          <div class="flex-1 font-size-16">{{ detailInfo.ddrw_name }}</div>
        </div>
        <div class="mt15 d-flex">
          <div class="font-size-16">计划内容：</div>
          <div class="flex-1 font-size-16" v-html="detailInfo.ddjh_jhnr">
            {{ detailInfo.ddrw_ddnr }}
          </div>
        </div>
      </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 {
  ddgzjh_list,
  ddgzjh_add,
  ddgzjh_edit,
  ddgzjh_del,
  ddgzjh_detail,
  ddrw_list,
} from "./api";
export default {
  name: "index",
  components: {
    Tinymce,
    FileUpload,
  },
  data() {
    return {
      keyword: "",
      type_value: "",
      fzr_list: [
        {
          label: "全部",
          value: "0",
        },
        {
          label: "张三",
          value: "1",
        },
        {
          label: "李四",
          value: "2",
        },
        {
          label: "王五",
          value: "3",
        },
      ],
      tableData: [],
      detailInfo: {},
      limit: 10,
      total: 12,
      cur_page: 1,
      dialogFormVisible: false,
      editDialogFormVisible: false,
      dialogForm: {
        jhmc: "",
        cjr: "",
        jhsj: "",
        fzr: "",
        surw: {
          label: "",
          value: "",
        },
        jhnr: "",
      },
      rwList: [],
      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,
      };

      ddgzjh_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 = {
        jhmc: "",
        cjr: "",
        jhsj: "",
        fzr: "",
        surw: {
          label: "",
          value: "",
        },
        jhnr: "",
      };
      this.isEdit = false;
      this.editDialogFormVisible = true;
    },
    editData(item) {
      this.title = "编辑计划";
      this.isEdit = true;
      this.editDialogFormVisible = true;
      this.id = item.ddjh_id;
      this.dialogForm.jhmc = item.ddjh_name;
      this.dialogForm.cjr = item.ddjh_cjr;
      this.dialogForm.jhsj = item.ddjh_jhsj;
      this.dialogForm.fzr = item.ddjh_fzr;
      this.dialogForm.surw.value = item.ddrw_id;
      this.dialogForm.surw.label = item.ddrw_name;
      this.dialogForm.jhnr = item.ddjh_jhnr;
      this.timestamp = Date.now();
    },
    formSubmit() {
      let data = {
        ddjh_name: this.dialogForm.jhmc,
        ddjh_cjr: real_name,
        ddjh_jhsj: this.dialogForm.jhsj,
        ddjh_fzr: this.dialogForm.fzr,
        ddrw_id: this.dialogForm.surw.value,
        ddrw_name: this.dialogForm.surw.label,
        ddjh_jhnr: this.dialogForm.jhnr,
      };
      if (!this.isEdit) {
        ddgzjh_add(data).then((res) => {
          if (res.code == 1) {
            this.$message({
              message: "计划添加成功！",
              type: "success",
            });

            this.editDialogFormVisible = false;
            this.getListData();
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        });
      } else {
        data.ddjh_id = this.id;
        ddgzjh_edit(data).then((res) => {
          if (res.code == 1) {
            this.$message({
              message: "计划编辑成功！",
              type: "success",
            });

            this.editDialogFormVisible = false;
            this.getListData();
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        });
      }
    },
    formCancel() {
      this.dialogFormVisible = false;
      this.editDialogFormVisible = false;
    },
    delData(id) {
      let data = {
        ddjh_id: id,
      };
      this.$confirm("确认删除该计划?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          ddgzjh_del(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "计划删除成功！",
                type: "success",
              });
              this.getListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => {});
    },
    detailData(item) {
      this.title = "计划详情";
      this.dialogFormVisible = true;
      let data = {
        ddjh_id: item.ddjh_id,
      };
      ddgzjh_detail(data).then((res) => {
        if (res.code == 1) {
          this.detailInfo = res.data.one_info;
        }
      });
    },
    ddjlData(id) {
      this.$router.push({
        name: "dddx_ddgzjl",
        params: { id: id },
      });
    },
  },
  mounted() {
    this.getListData();
    this.getrwList();
  },
};
</script>

<style scoped>
</style>
