<template>
  <basic-container>
    <div class="content">
      <leftMenu @select="select" />
      <div class="right-content">
        <avue-crud
          :data="tableData"
          :option="option"
          :page.sync="page"
          :table-loading="tableLoading"
          @size-change="sizeChange"
          @current-change="currentChange"
          @search-change="searchVal"
          @search-reset="searchReset"
        >
          <!-- 操作 -->
          <template slot="menu" slot-scope="scope">
            <el-button
              type="text"
              size="small"
              icon="el-icon-view"
              @click="viewBtn(scope.row, scope.index)"
              >查看
            </el-button>
            <el-button
              type="text"
              size="small"
              icon="el-icon-document"
              @click="taskStatisticsFn(scope.row, scope.index)"
            >
              任务统计
            </el-button>
            <el-button
              type="text"
              size="small"
              icon="el-icon-folder-checked"
              :disabled="scope.row.ysh == 0 || scope.row.ysh == null"
              @click="exportBtn(scope.row, scope.index)"
            >
              导出
            </el-button>
          </template>
          <!-- 搜索 -->
          <template slot="allocationDateSearch">
            <el-date-picker
              v-model="dateTime"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
              @change="startDateBtn"
            />
          </template>
          <template slot="acceptSugSearch">
            <el-select v-model="acceptSug" placeholder="接包对象">
              <el-option
                :label="item.gsmc"
                :value="item.id"
                v-for="item in csoList"
                :key="item.id"
              ></el-option>
            </el-select>
          </template>
          <!-- 自定义列 -->
          <template slot="acceptSug" slot-scope="scope">
            {{ getAcceptSugName(scope.row) }}
          </template>
          <template slot="relationScoreId" slot-scope="scope">
            {{ getRelationScoreName(scope.row) }}
          </template>
        </avue-crud>
      </div>
    </div>
    <!-- 查看 -->
    <enterprisehandlestasksDialog
      :dialogShow.sync="dialogShow"
      ref="enterprisehandlestasksDialogRef"
    />
    <!-- 任务统计 -->
    <enterprisehandlesTaskStatistics
      :taskStatisticsShow.sync="taskStatisticsShow"
      :currentId.sync="currentId"
    />
    <!-- 导出 -->
    <exportDialog
      :dialogShow.sync="exportDialogShow"
      type="1"
      :checkRow="checkRow"
      @downSuccess="downSuccess"
    />
  </basic-container>
</template>

<script>
import {
  selectWmScorePackageListsForBCApi,
  listCsoApi,
  wmscorepackageListApi
} from "@/api/csmpages/index.js";
import { enterprisehandlestasksOption } from "@/const/crud/csmpages/enterprisehandlestasks.js";
import leftMenu from "@/views/components/teamManagement/leftMenu";
import exportDialog from "@/components/exportDialog/index";
import enterprisehandlestasksDialog from "./components/enterprisehandlestasksDialog";
import enterprisehandlesTaskStatistics from "./components/enterprisehandlesTaskStatistics";
export default {
  components: {
    leftMenu,
    exportDialog,
    enterprisehandlestasksDialog,
    enterprisehandlesTaskStatistics
  },
  data() {
    return {
      deptId: "",
      csoList: [],
      wmscorepackageList: [],
      acceptSug: "",
      tableData: [],
      tableLoading: false,
      option: enterprisehandlestasksOption,
      searchForm: {},
      page: {
        total: 0,
        currentPage: 1,
        pageSize: 10
      },
      dateTime: "",
      allocationDate: "",
      endLabelDate: "",
      dialogShow: false,
      taskStatisticsShow: false,
      currentId: "",
      exportDialogShow: false,
      checkRow: null
    };
  },
  methods: {
    async select(info) {
      if (this.deptId !== info.deptId) {
        this.deptId = info.deptId;
        this.init();
        await this.getDictList();
        this.getList(this.page);
      }
    },
    init() {
      this.page = {
        total: 0,
        currentPage: 1,
        pageSize: 10
      };
      this.searchReset();
      this.tableData = [];
    },
    async getDictList() {
      const res = await listCsoApi({
        deptId: this.deptId
      });
      this.csoList = res.data.data;
      const resA = await wmscorepackageListApi({
        deptId: this.deptId
      });
      this.wmscorepackageList = resA.data.data;
    },
    async getList(page, params) {
      this.tableLoading = true;
      const obj = Object.assign(
        {
          current: page.currentPage,
          size: page.pageSize,
          deptId: this.deptId
        },
        params,
        this.searchForm
      );
      const res = await selectWmScorePackageListsForBCApi(obj);

      this.tableData = res.data.data.records;
      this.page.total = res.data.data.total;
      this.tableLoading = false;
    },
    getAcceptSugName(row) {
      const curr = this.csoList.find(item => item.id === row.acceptSug);
      return curr && curr.gsmc;
    },
    getRelationScoreName(row) {
      const curr = this.wmscorepackageList.find(
        item => item.relationScoreId === row.relationScoreId
      );
      return curr && curr.scorePackageName;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.getList(this.page);
    },
    currentChange(current) {
      this.page.currentPage = current;
      this.getList(this.page);
    },
    searchVal(form, done) {
      this.searchForm = form;
      this.searchForm.allocationDate = this.allocationDate;
      this.searchForm.endLabelDate = this.endLabelDate;
      this.searchForm.acceptSug = this.acceptSug;
      this.page.currentPage = 1;
      this.getList(this.page, form);
      done();
    },
    // 搜索清空按钮
    searchReset() {
      this.dateTime = "";
      this.acceptSug = "";
      this.allocationDate = "";
      this.endLabelDate = "";
      this.searchForm = {};
    },
    startDateBtn(date) {
      (this.allocationDate = date[0]), (this.endLabelDate = date[1]);
    },
    // 查看
    async viewBtn(row) {
      await this.$refs.enterprisehandlestasksDialogRef.lookDetail(row.id);
      this.dialogShow = true;
    },
    // 任务统计
    taskStatisticsFn(row) {
      this.taskStatisticsShow = true;
      this.currentId = row.id;
    },
    exportBtn(row) {
      this.checkRow = row;
      this.exportDialogShow = true;
    },
    // 导出成功
    downSuccess() {
      this.getList(this.page);
    }
  }
};
</script>

<style lang="scss" scoped>
.content {
  display: flex;
}
.right-content {
  margin-left: 10px;
  flex: 1;
  overflow: auto;
}
</style>
