<template>
  <basic-container>
    <div class="content">
      <leftMenu @select="select" />
      <div class="right-content">
        <avue-crud
          ref="crud"
          :page.sync="page"
          :data="tableData"
          :table-loading="tableLoading"
          :option="tableOption"
          @selection-change="selectionChange"
          @search-change="searchChange"
          @refresh-change="refreshChange"
          @size-change="sizeChange"
          @current-change="currentChange"
        >
          <template slot="menuLeft">
            <el-button type="primary" @click="exportTaskInfoAll()"
              >合并导出<i class="el-icon-download el-icon--right"></i
            ></el-button>
          </template>

          <template slot="menu" slot-scope="scope">
            <el-button
              v-show="scope.row.packageType2 == '1'"
              type="text"
              size="small"
              icon="el-icon-check"
              :disabled="scope.row.ysh <= 0"
              @click="exportBtn(scope.row, scope.index)"
            >
              导出
            </el-button>

            <el-button
              type="text"
              size="small"
              icon="el-icon-check"
              :disabled="scope.row.ysh <= 0"
              @click="lookDetail(scope.row, scope.index)"
              >查看
            </el-button>
          </template>
        </avue-crud>
      </div>
    </div>
    <!-- 导出 -->
    <exportDialog
      :dialogShow.sync="exportDialogShow"
      type="4"
      :packageIds="packageIds"
      :checkRow="checkRow"
      @downSuccess="downSuccess"
    />
  </basic-container>
</template>

<script>
import leftMenu from "@/views/components/teamManagement/leftMenu";
import exportDialog from "@/components/exportDialog/index";

import {
  getExportPackageZbTotalListForBCApi,
  allExportStatusApi
} from "@/api/csmpages/index.js";
import { individualreportexportOption } from "@/const/crud/csmpages/individualreportexport.js";

export default {
  components: {
    leftMenu,
    exportDialog
  },
  data() {
    return {
      deptId: "",
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableData: [],
      tableLoading: false,
      tableOption: individualreportexportOption,
      searchForm: {},
      packageIds: "",
      exportDialogShow: false,
      checkRow: false
    };
  },
  activated() {
    if (this.deptId) {
      this.getList(this.page);
    }
  },
  methods: {
    async select(info) {
      if (this.deptId !== info.deptId) {
        this.deptId = info.deptId;
        this.init();
        this.getList(this.page);
      }
    },
    init() {
      this.page = {
        total: 0,
        currentPage: 1,
        pageSize: 20
      };

      this.tableData = [];
    },
    async getList(page, params) {
      this.tableLoading = true;
      const obj = Object.assign(
        {
          current: page.currentPage,
          size: page.pageSize,
          sendPackageDeptId: this.deptId
        },
        params,
        this.searchForm
      );
      const res = await getExportPackageZbTotalListForBCApi(obj);
      this.tableData = res.data.data.records;
      this.$set(this.page, "total", res.data.data.total);
      this.$forceUpdate();
      this.tableLoading = false;
    },
    selectionChange(selection) {
      if (selection.length) {
        const str = selection.map(item => item.id).join(",");
        this.packageIds = str;
      }
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.getList(this.page);
    },
    currentChange(current) {
      this.page.currentPage = current;
      this.getList(this.page);
    },
    // 查询
    searchChange(form, done) {
      this.searchForm = form;
      this.page.currentPage = 1;
      this.getList(this.page);
      done();
    },
    // 刷新
    refreshChange() {
      this.getList(this.page);
    },

    // 批量导出
    async exportTaskInfoAll() {
      if (!this.packageIds) {
        return this.$message.info("请先选择数据");
      }
      this.checkRow = null;
      await this.getExportZbStatus();
    },

    async getExportZbStatus() {
      let rowid = "";
      const arr = this.packageIds
        .split(",")
        .sort((a, b) => Number(a) - Number(b));

      rowid = arr.join(",");
      const obj = {
        id: rowid
      };
      const res = await allExportStatusApi(obj);

      if (res.data.code === 0) {
        this.checkRow = res.data.data;
        this.exportDialogShow = true;
      }
    },

    lookDetail(row) {
      this.$router.push({
        path: "/admin/export/reportExportlook?id=" + 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>
