<template>
  <div class="reward" v-loading="loading">
    <search
      :type="2"
      @search:task="updateParams($event)"
      @export="exportData($event)"
      :show-export="true"
    />
    <el-table
      :data="dataList"
      border
      highlight-current-row
      :height="tableHeight"
      ref="table"
      :span-method="arraySpanMethod"
    >
      <el-table-column type="index" label="序号" align="center" width="50">
      </el-table-column>
      <el-table-column
        label="一级指标"
        min-width="80"
        align="center"
        prop="quota1"
      ></el-table-column>

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

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

      <el-table-column
        label="区牵头单位"
        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="170" align="center">
        <template slot-scope="{ row }">
          <el-link
            @click="edit(row)"
            icon="el-icon-edit"
            type="primary"
            :underline="false"
            :disabled="row.isApply || !row.id"
            >申请成为配合部门</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 = {}"
    >
      <el-form
        :model="form"
        label-width="82px"
        width="400px"
        :destroy-on-close="true"
        @closed="form = {}"
      >
        <el-form-item label="申请理由：">
          <el-input
            v-model="form.note"
            type="textarea"
            placeholder="请输入申请理由"
            :rows="5"
            :maxlength="50"
            :show-word-limit="true"
          ></el-input>
        </el-form-item>
      </el-form>
      <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/taskSummary";
import { mergeSpan } from "@/utils/mixin";
import applyApi from "@/api/second/coordinationManage/applyCooperation";
export default {
  components: {
    search,
  },
  mixins: [
    mergeSpan([
      { name: "quota1", col: [1] },
      { name: "quota2", col: [2] },
      { name: "content", col: [3] },
    ]),
  ],
  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: {},
    };
  },
  created() {
    this.getDataList();
  },
  computed: {
    ...mapGetters(["formatDep", "user"]),
  },
  methods: {
    exportData(data) {
      this.loading = true;
      api
        .exportRewardList(
          Object.assign({ sheetName: "重点专项细分小条", isApply: 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) {
      let arr = [].concat(
        row.cooperateDept
          ? row.cooperateDept.split(",").map((item) => parseInt(item))
          : [],
        row.leaderDept
          ? row.leaderDept.split(",").map((item) => parseInt(item))
          : []
      );
      let bool = arr.includes(this.user.user.deptId);
      return bool;
    },
    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) {}
      api
        .getRewardList(
          Object.assign({ isApply: 1 }, this.page, this.searchParams)
        )
        .then((res) => {
          let data = res.data.data;
          this.count = data.total;
          // data = data.records.filter((item) => {
          //   return !this.judge(item);
          // });
          data = data.records
          this.dataList = this.reduceData(data);
        })
        .catch((e) => {
          this.dataList = [];
          this.count = 0;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    edit(row) {
      console.log(row);
      this.showEdit = true;
      this.form.type = 2;
      this.form.itemId = row.id;
      this.form.leaderDept = row.leaderDept;
      // this.form.isApply = 1
    },
    save() {
      if (this.form.note) {
        applyApi
          .applyCooperate(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: "提交申请失败",
            });
          });
      } else {
        this.$message({
          type: "error",
          message: "申请理由不能为空",
        });
      }
    },
  },
  filters: {
    formatList(val) {
      if (val) {
        return val.join("\n");
      }
    },
  },
};
</script>

<style>
</style>