<template>
  <div class="approval-cooperation ele-body">
    <el-card shadow="never" element-loading-background="rgba(255, 255, 255, 1)">
      <search @search:task="updateParams($event)" @export="exportData($event)" :show-export="true"/>
      <el-table
        border
        highlight-current-row
        :data="dataList"
        :height="tableHeight"
        v-loading="loading"
        ref="table"
      >
        <el-table-column
          type="index"
          width="50"
          align="center"
          label="序号"
        ></el-table-column>
        <el-table-column
          label="申请部门"
          align="center"
          width="150"
          prop="applyDeptName"
        ></el-table-column>
        <el-table-column
          label="申请时间"
          align="center"
          width="160"
          prop="createTime"
        ></el-table-column
        ><el-table-column
          label="申请理由"
          align="center"
          width="150"
          prop="note"
        ></el-table-column>
        <el-table-column
          label="产业链名称"
          align="center"
          width="150"
          prop="categoryName"
        ></el-table-column>
        <el-table-column
          label="类型"
          align="center"
          width="100"
          prop="typeName"
        ></el-table-column>
        <el-table-column
          label="指标名称"
          align="center"
          min-width="200"
          prop="content"
        ></el-table-column>
        <el-table-column
          label="分值"
          align="center"
          width="80"
          prop="points"
        ></el-table-column>
        <el-table-column
          label="第一牵头部门"
          align="center"
          width="130"
          prop="leaderDeptName"
        >
          <template slot-scope="{ row }">
            <pre>{{ row.leaderDeptList | formatList }}</pre>
          </template>
        </el-table-column>
        <el-table-column
          label="其它牵头部门"
          align="center"
          width="130"
          prop="otherDeptName"
        >
          <template slot-scope="{ row }">
            <pre>{{ row.otherDeptList | formatList }}</pre>
          </template>
        </el-table-column>
        <el-table-column
          label="配合部门"
          align="center"
          width="130"
          prop="cooperateDeptName"
        >
          <template slot-scope="{ row }">
            <pre>{{ row.cooperateDeptList | formatList }}</pre>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" min-width="120">
          <template slot-scope="{ row }">
            <el-link
              @click="edit(row, true)"
              type="primary"
              :underline="false"
              :disabled="judge(row)"
              >同意</el-link
            >
            <el-link
              @click="edit(row, false)"
              type="primary"
              :underline="false"
              :disabled="judge(row)"
              >不同意</el-link
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- <el-pagination
        :current-page.sync="page.page"
        :page-size="page.limit"
        :total="count"
        :background="true"
        layout="total, prev, pager, next, jumper"
        :pager-count="5"
        class="ele-pagination-circle"
        @current-change="getDataList()"
      /> -->
      <el-dialog
        title="审核配合申请"
        :visible.sync="showEdit"
        width="400px"
        custom-class="ele-dialog-form"
        :lock-scroll="true"
        :destroy-on-close="true"
        @closed="form = {}"
      >
        <div class="warning">
          <span
            class="el-icon-warning-outline"
            style="color: #f7ac2c; font-size: 25px; margin-right: 20px"
          ></span>
          <div class="right">
            <h5>{{ warning.title }}</h5>
            <p>{{ warning.info }}</p>
          </div>
        </div>
        <div slot="footer">
          <el-button @click="showEdit = false">取消</el-button>&nbsp;
          <el-button type="primary" @click="save">保存</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import search from "../search";
import { mapGetters } from "vuex";
import api from "@/api/chain/coordinationManage/approvalCooperation";
export default {
  components: {
    search,
  },
  data() {
    return {
      searchParams: {},
      dataList: [],
      count: 0,
      tableHeight: document.documentElement.clientHeight - 220,
      page: {
        page: 1,
        limit: Math.floor(
          (document.documentElement.clientHeight - 220 - 45) / 45
        ),
      },
      loading: false,
      form: {},
      showEdit: false,
      warning: {
        title: "确定同意此申请？",
        info: "点击确定后，该部门将添加至考核任务配合部门中",
      },
    };
  },
  computed: {
    ...mapGetters(["user"]),
  },
  created() {
    this.getDataList();
  },
  methods: {
    exportData(data){
      this.loading=true;
      api.exportList(Object.assign({sheetName:"链长制配合申请审核记录", isChecked: 1},data)).then(res=>{
        if (res.data.code == 0) {
            window.open(res.data.data);
            this.$message({
              type: "success",
              message: "导出成功",
            });
          }else{
            this.$message({
              type:"warning",
              message:"暂无数据"
            })
          }
      }).catch(e=>{
        console.log(e)
        this.$message.error("暂无数据")
      }).finally(()=>{
        this.loading=false;
      })
    },
    updateParams(data) {
      this.searchParams = data;
      this.getDataList();
    },
    judge(row) {
      if (row.status == 0) {
        return false;
      } else if (row.status == 1 || row.status == 2) {
        return true;
      }
    },
    updateParams(data) {
      this.searchParams = data;
      this.resetCurrentPage();
      this.getDataList();
    },
    resetCurrentPage() {
      let page = this.page;
      page.page = 1;
      this.page = page;
    },
    getDataList() {
      try {
        this.$refs.table.bodyWrapper.scrollTop = 0;
      } catch (e) {}
      this.loading = true;
      api
        .getDataList(Object.assign({isChecked: 1}, this.page, this.searchParams))
        .then((res) => {
          let data = res.data.data;
          this.count = data.total;
          this.dataList = data.records;
        })
        .catch((e) => {
          this.dataList = [];
          this.count = 0;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    edit(row, flag) {
      this.showEdit = true;
      this.form.id = row.id;
      this.form.itemId = row.id;
      this.form.leaderDept = row.leaderDept;
      this.form.cooperateDept = row.cooperateDept;
      if (flag) {
        this.form.status = 1;
        this.warning = {
          title: "确定同意此申请？",
          info: "点击确定后，该部门将添加至考核任务配合部门中",
        };
      } else {
        this.form.status = 2;
        this.warning = {
          title: "确定不同意此申请？",
          info:
            "点击确定后，该部门申请将驳回，如需再次添加，则由管理员确认后增加",
        };
      }
    },
    save() {
      api
        .editItem(Object.assign({}, this.form))
        .then((res) => {
          this.showEdit = false;
          this.$message({
            type: "success",
            message: "提交成功",
          });
          this.getDataList();
        })
        .catch((e) => {
          this.showEdit = false;
          this.$message({
            type: "error",
            message: "提交失败",
          });
        });
    },
  },
  filters: {
    formatList(val) {
      if (val) {
        return val.join("\n");
      }
    },
  },
};
</script>

<style scoped>
.warning {
  display: flex;
  margin-bottom: 20px;
}
</style>