<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :page="pageTask"
        :data="tableDataTask"
        :table-loading="tableLoadingTask"
        @selection-change="selectionChange"
        :selectable="selectable"
        :option="tableOptionTask"
        @on-load="getListTask"
        @search-change="searchChangeTask"
        @refresh-change="refreshChangeTask"
        @size-change="sizeChangeTask"
        @current-change="currentChangeTask"
      >
        <template slot="menuLeft">
          <el-button
            type="primary"
            @click="exportTaskInfoAll()"
            :loading="infoExportAllLoading"
            >批量导出个人业绩报告<i class="el-icon-download el-icon--right"></i
          ></el-button>
        </template>

        <template slot="menu" slot-scope="scope">
          <el-button
            type="text"
            size="small"
            icon="el-icon-check"
            :disabled="scope.row.ysh <= 0"
            @click="showExportDia(scope.row, scope.index)"
            >导出
          </el-button>
        </template>
      </avue-crud>
    </basic-container>

    <el-dialog
      title="提示"
      :visible.sync="centerDialogVisible"
      width="40%"
      center
    >
      <span>
        <el-button
          @click="taskExport"
          :loading="taskExportLoading"
          style="margin-left: 50px;"
          >导出汇总报告</el-button
        >

        <el-button
          @click="taskExportInfo"
          :loading="taskExportInfoLoading"
          style="margin-left: 50px;"
          >导出报告详情</el-button
        >

        <el-button
          @click="taskExportJS"
          :loading="taskExportJsLoading"
          style="margin-left: 50px;"
          >导出结算表</el-button
        >
      </span>
      <span slot="footer" class="dialog-footer">
        注意： 1.报告汇总为封面+任务数据汇总+任务列表明细。<br />

        2.报告详情为每条任务的任务详情+图片。
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  taskExportZbbg,
  taskExportZbqybgInfo,
  infoExportJsInfo
} from "@/api/wmtask";
import { tableOptionT_JXS } from "@/const/crud/export";
import { getExportPackageZbTotalList } from "@/api/sysExport";
import { tableOptionTaskZbDrug } from "@/const/crud/export";
import { mapGetters } from "vuex";
import Config from "@/settings";

export default {
  name: "wmtask",
  data() {
    return {
      taskExportJsLoading: false,
      infoExportAllLoading: false,
      taskExportLoading: false,
      taskExportInfoLoading: false,
      infoExportLoading: false,
      centerDialogVisible: false,
      checkRow: null,
      entLevel: 0,
      packageIds: "",
      isAllCheck: false,
      rateForm: {
        socreShtgRate: 0,
        socreYtj: 0,
        socreYjwc: 0,
        dqyyjfzz: 0
      },
      roleSubmitType: true,
      roleAudioType: true,
      map: null,
      getAddress: null,
      getAddCode: null,
      addressKeyword: "",
      shopInfo: {
        lng: null,
        lat: null
      },
      city: "请选择区域",
      addInp: false,
      mask: false,
      form: {},
      searchForm: {},
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOptionT_JXS,

      isHasAudit: false,
      formTask: {},
      searchFormTask: {},
      tableDataTask: [],
      pageTask: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoadingTask: false,
      tableOptionTask: tableOptionTaskZbDrug
    };
  },
  computed: {
    ...mapGetters(["permissions", "userInfo"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.admin_wmtask_add, false),
        delBtn: this.vaildData(this.permissions.admin_wmtask_del, false),
        editBtn: this.vaildData(this.permissions.admin_wmtask_edit, false)
      };
    }
  },

  methods: {
    selectable(row, index) {
      if (row.ysh > 0) {
        return true;
      }
      return false;
    },
    sizeChangeTask(pageSize) {
      this.pageTask.pageSize = pageSize;
    },
    currentChangeTask(current) {
      this.pageTask.currentPage = current;
    },
    searchChangeTask(form, done) {
      this.searchFormTask = form;
      this.pageTask.currentPage = 1;
      this.getListTask(this.pageTask, form);
      done();
    },
    refreshChangeTask() {
      this.getListTask(this.pageTask);
    },
    getListTask(page, params) {
      this.tableLoadingTask = true;
      this.searchFormTask.typeid = 4;

      getExportPackageZbTotalList(
        Object.assign(
          {
            current: page.currentPage,
            size: page.pageSize
          },
          params,
          this.searchFormTask
        )
      )
        .then(response => {
          this.tableDataTask = response.data.data.records;
          this.pageTask.total = response.data.data.total;
          this.tableLoadingTask = false;
        })
        .catch(() => {
          this.tableLoadingTask = false;
        });
    },
    showTaskInfo(row, index) {
      window.open(
        Config.outsideUrl + "/h5/#/pages/task/task-detail/index?id=" + row.id,
        "newwindow",
        "height=750, width=400, top=200, left=300, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no"
      );
    },
    showExportDia(row, index) {
      this.checkRow = row;
      this.centerDialogVisible = true;
      this.isAllCheck = false;
    },
    taskExport() {
      this.taskExportLoading = true;
      let rowid = "";
      if (this.isAllCheck) {
        rowid = this.packageIds;
      } else {
        let row = this.checkRow;
        rowid = row.id;
      }
      var obj = {
        scorePackageDrugId: rowid
      };
      taskExportZbbg(obj)
        .then(response => {
          if (undefined == response.data || undefined == response.data.data) {
            this.$message.error("导出失败");
            return false;
          }
          let obj = response.data.data;
          let a = document.createElement("a");
          a.href = obj.url; // 这里的请求方式为get，如果需要认证，接口上需要带上token
          a.click();
          this.$message.success("导出众包代表业绩报告成功");
          this.taskExportLoading = false;
        })
        .catch(e => {
          this.taskExportLoading = false;
        });
    },
    taskExportInfo() {
      this.taskExportInfoLoading = true;
      let rowid = "";
      if (this.isAllCheck) {
        rowid = this.packageIds;
      } else {
        let row = this.checkRow;
        rowid = row.id;
      }
      var obj = {
        scorePackageDrugId: rowid
      };
      taskExportZbqybgInfo(obj).then(response => {
        if (undefined == response.data || undefined == response.data.data) {
          this.$message.error("导出失败");
          return false;
        }
        let obj = response.data.data;
        let a = document.createElement("a");
        a.href = obj.url; // 这里的请求方式为get，如果需要认证，接口上需要带上token
        a.click();
        this.taskExportInfoLoading = false;
      });
    },
    taskExportJS() {
      this.taskExportJsLoading = true;
      let rowid = "";
      if (this.isAllCheck) {
        rowid = this.packageIds;
      } else {
        let row = this.checkRow;
        rowid = row.id;
      }
      var obj = {
        scorePackageDrugId: rowid
      };
      infoExportJsInfo(obj)
        .then(response => {
          if (undefined == response.data || undefined == response.data.data) {
            this.$message.error("导出失败");
            return false;
          }
          let obj = response.data.data;
          let a = document.createElement("a");
          a.href = obj.url; // 这里的请求方式为get，如果需要认证，接口上需要带上token
          a.click();
          this.$message.success("导出众包代表业绩报告-结算表成功");
          this.taskExportJsLoading = false;
        })
        .catch(() => {
          this.taskExportJsLoading = false;
        });
    },
    exportTaskInfoAll() {
      if ("" == this.packageIds) {
        this.$message.error("请选择需要导出的列");
        return false;
      }

      if (this.isHasAudit == false) {
        this.$message.error("选择的任务包中没有已审核通过的任务");
        return false;
      }

      this.isAllCheck = true;
      this.centerDialogVisible = true;
    },
    selectionChange(selection, row) {
      this.isHasAudit = false;
      this.packageIds = "";
      if (undefined != selection) {
        for (var i = 0; i < selection.length; i++) {
          if (selection[i].ysh > 0) {
            this.isHasAudit = true;
          }

          this.packageIds += selection[i].id;
          if (i < selection.length - 1) {
            this.packageIds += ",";
          }
        }
      }
    }
  }
};
</script>
