<template>
  <div class="basic-target" v-loading="loading">
    <search
      :type="1"
      @search:task="updateParams($event)"
      @export="exportData($event)"
      :show-export="true"
      :approval="false"
    />
    <el-table
      :data="dataList"
      border
      highlight-current-row
      :height="tableHeight"
      ref="table"
    >
      <el-table-column type="index" label="序号" align="center" width="50">
      </el-table-column>
      <el-table-column
        label="申请部门"
        align="center"
        width="150"
        prop="applyDeptName"
      ></el-table-column
      ><el-table-column
        label="申请时间"
        align="center"
        width="120"
        prop="createTime"
      ></el-table-column
      ><el-table-column
        label="申请理由"
        align="center"
        width="100"
        prop="note"
      ></el-table-column>
      <el-table-column
        label="一级指标"
        width="100"
        align="center"
        prop="quota1"
      ></el-table-column>

      <el-table-column
        label="二级指标"
        width="120"
        align="center"
        prop="quota2"
      ></el-table-column>

      <el-table-column
        label="考核内容和计分细则"
        align="center"
        width="400"
        prop="content"
      ></el-table-column>
      <el-table-column
        label="小条内容"
        align="center"
        width="220"
        prop="itemContent"
      ></el-table-column>
      <el-table-column
        label="分值"
        min-width="60"
        align="center"
        prop="points"
      ></el-table-column>

      <el-table-column
        label="区牵头单位"
        min-width="95 "
        align="center"
        prop="leaderUnit"
      >
        <template slot-scope="{ row }">
          <pre>{{ row.leaderUnit }}</pre>
        </template>
      </el-table-column>
      <el-table-column label="第一牵头部门" width="120" align="center">
        <template slot-scope="{ row }">
          <pre>{{ row.leaderDeptList | formatList }}</pre>
        </template>
      </el-table-column>
      <el-table-column label="第一牵头部门责任领导" width="170" align="center">
        <template slot-scope="{ row }">
          <pre>{{ row.leaderPrincipalList | formatList }}</pre>
        </template>
      </el-table-column>
      <el-table-column label="其他牵头部门" width="120" align="center">
        <template slot-scope="{ row }">
          <span>
            <pre>{{ row.leaderDeptList | formatList }}</pre>
          </span>
        </template>
      </el-table-column>

      <el-table-column label="其他牵头部门责任领导" width="170" align="center">
        <template slot-scope="{ row }">
          <pre>{{ row.leaderPrincipalList | formatList }}</pre>
        </template>
      </el-table-column>
      <el-table-column label="配合部门" width="100" align="center">
        <template slot-scope="{ row }">
          <span>
            <pre>{{ row.cooperateDeptList | formatList }}</pre>
          </span>
        </template>
      </el-table-column>

      <el-table-column label="配合部门责任领导" width="150" align="center">
        <template slot-scope="{ row }">
          <pre>{{ row.cooperatePrincipalList | formatList }}</pre>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="125" align="center">
        <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"
      :flat="true"
      @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>
  </div>
</template>

<script>
import search from "../../search";
import { mapGetters } from "vuex";
import api from "@/api/second/coordinationManage/approvalCooperation";
export default {
  components: {
    search,
  },
  data() {
    return {
      searchParams: {},
      dataList: [],
      tableHeight: document.documentElement.clientHeight - 305,
      page: {
        page: 1,
        limit: Math.floor(
          (document.documentElement.clientHeight - 305 - 45) / 45
        ),
      },
      count: 0,
      showEdit: false,
      loading: false,
      form: {},
      warning: {
        title: "确定同意此申请？",
        info: "点击确定后，该部门将添加至考核任务配合部门中",
      },
    };
  },
  computed: {
    ...mapGetters(["formatDep", "user"]),
  },
  created() {
    this.getDataList();
  },
  methods: {
    exportData(data) {
      this.loading = true;
      api
        .exportBasicList(
          Object.assign(
            { sheetName: "基础目标任务配合申请审核列表", isChecked: 1 },
            data
          )
        )
        .then((res) => {
          if (res.data.code == 0) {
            // window.open(res.data.data);
            window.location.href=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;
        });
    },
    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.searchParams.leaderDept = this.user.user.deptId
      api
        .getBasicDataList(
          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.type = 1;
      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>