<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="ob_value" @change="getListData">
          <el-select
            class="mr5"
            v-model="ob_value"
            placeholder="请选择反馈对象"
          >
            <el-option
              v-for="item in ob_list"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </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="ddqk_name"
        width="auto"
      ></el-table-column>
      <el-table-column
        label="创建人"
        width="200"
        prop="ddjh_cjr"
      ></el-table-column>
      <el-table-column
        label="创建时间"
        width="150"
        prop="ddjh_cjsj"
      ></el-table-column>
      <el-table-column label="反馈对象" prop="ddjh_fkdx" width="150">
      </el-table-column>
      <el-table-column label="所属任务" prop="ddrw_name" width="150">
      </el-table-column>
      <el-table-column
        label="内容"
        show-overflow-tooltip
        prop="ddqk_content"
        width="auto"
      >
        <template slot-scope="scope">
          <div v-html="scope.row.ddqk_content"></div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" @click="delData(scope.row.ddqk_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="bt">
          <el-input v-model="dialogForm.bt"></el-input>
        </el-form-item>
        <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="fkdx">
          <el-input v-model="dialogForm.fkdx"></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="fknr">
          <Tinymce
            v-if="dialogFormVisible"
            v-model="dialogForm.fknr"
            :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 { ddqkfk_list, ddqkfk_add, ddqkfk_del, ddrw_list } from "./api";
export default {
  name: "index",
  components: {
    Tinymce,
    FileUpload,
  },
  data() {
    return {
      keyword: "",
      type_value: "",
      rwList: [],
      ob_value: "",
      ob_list: [
        {
          label: "全部",
          value: "0",
        },
        {
          label: "对象1",
          value: "1",
        },
        {
          label: "对象2",
          value: "2",
        },
        {
          label: "对象3",
          value: "3",
        },
      ],
      tableData: [],
      limit: 10,
      total: 12,
      cur_page: 1,
      dialogFormVisible: false,
      dialogForm: {
        bt: "",
        fknr: "",
        cjsj: "",
        fkdx: "",
        surw: {
          label: "",
          value: "",
        },
      },
      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,
      };

      ddqkfk_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 = {
        bt: "",
        fknr: "",
        cjsj: "",
        fkdx: "",
        surw: {
          label: "",
          value: "",
        },
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },

    formSubmit() {
      let data = {
        ddqk_name: this.dialogForm.bt,
        ddjh_cjr: real_name,
        ddqk_content: this.dialogForm.fknr,
        ddjh_cjsj: this.dialogForm.cjsj,
        ddjh_fkdx: this.dialogForm.fkdx,
        ddrw_id: this.dialogForm.surw.value,
        ddrw_name: this.dialogForm.surw.label,
      };

      ddqkfk_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",
          });
        }
      });
    },
    formCancel() {
      this.dialogFormVisible = false;
    },
    delData(id) {
      let data = {
        ddqk_id: id,
      };
      this.$confirm("确认删除该反馈?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          ddqkfk_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>
