<template>
  <div class="execution">
    <Tabs value="name1">
      <TabPane label="内部企业" name="name1">
        <basic-container>
          <avue-crud
            ref="crud"
            :page="pageTask"
            :data="tableDataTask"
            :table-loading="tableLoadingTask"
            :option="tableOptionTask"
            @on-load="getListTask"
            @search-change="searchChangeTask"
            @refresh-change="refreshChangeTask"
            @size-change="sizeChangeTask"
            @current-change="currentChangeTask"
            @row-update="handleUpdateTask"
            @row-save="handleSaveTask"
            @row-del="rowDelTask"
          >
            <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>
      </TabPane>
      <TabPane label="外部企业" name="name2">
        <basic-container>
          <avue-crud
            ref="crud"
            :page="page"
            v-model="form"
            :data="tableData"
            :permission="permissionList"
            :table-loading="tableLoading"
            :option="tableOption"
            @on-load="getList"
            @search-change="searchChange"
            @refresh-change="refreshChange"
            @size-change="sizeChange"
            @current-change="currentChange"
            @row-update="handleUpdate"
            @row-save="handleSave"
            @row-del="rowDel"
          >
            <template slot="menuLeft">
              <el-button
                type="primary"
                @click="exportTaskInfo(row)"
                :loading="infoExportLoading"
                >导出学术推广报告<i class="el-icon-download el-icon--right"></i
              ></el-button>
            </template>
            <template slot="menu" slot-scope="scope">
              <el-button
                v-if="roleSubmitType"
                type="text"
                size="small"
                icon="el-icon-edit"
                @click="showTaskInfo(scope.row, scope.index)"
                >查看详情
              </el-button>
            </template>
          </avue-crud>
        </basic-container>
      </TabPane>
    </Tabs>

    <el-dialog
      title="提示"
      :visible.sync="centerDialogVisible"
      width="30%"
      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
        >
      </span>
      <span slot="footer" class="dialog-footer">
        注意： 1.报告汇总为封面+任务数据汇总+任务列表明细。<br />

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

<script>
import {
  fetchList,
  getObj,
  addObj,
  putObj,
  delObj,
  taskExportYq,
  taskExportNbqybg,
  taskExportNbqybgInfo,
  infoExportNbqybgInfo,
  pageExtList
} from "@/api/wmtask";
import { tableOptionT_JXS } from "@/const/crud/export";
import { getExportPackageTotalList } from "@/api/sysExport";
import {
  fetchListTask,
  getObjTask,
  addObjTask,
  putObjTask,
  delObjTask,
  getTotalData,
  getWmScorePackagePageTask,
  getWmScorePackageTask
} from "@/api/wmscorepackage";
import { tableOptionTaskJsx } from "@/const/crud/export";
import { getObjDept } from "@/api/admin/dept";
import { mapGetters } from "vuex";
import VDistpicker from "v-distpicker";
import maps from "qqmap";
import Config from "@/settings";

export default {
  name: "wmtask",
  data() {
    return {
      taskExportLoading: false,
      taskExportInfoLoading: false,
      infoExportLoading: false,
      centerDialogVisible: false,
      checkRow: null,
      entLevel: 0,
      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,

      formTask: {},
      searchFormTask: {},
      tableDataTask: [],
      pageTask: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoadingTask: false,
      tableOptionTask: tableOptionTaskJsx
    };
  },
  components: {
    VDistpicker
  },
  watch: {
    entLevel: function(val, oldVal) {
      if (val == "2") {
        this.roleSubmitType = false;
      }
    }
  },
  mounted() {
    this.getWmScorePackageTask();
    this.getObjDept1();
  },
  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)
      };
    },
    permissionListList() {
      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: {
    getList(page, params) {
      let searchFrom = this.searchForm;
      this.tableLoading = true;
      pageExtList(
        Object.assign(
          {
            current: page.currentPage,
            size: page.pageSize
          },
          params,
          this.searchForm
        )
      )
        .then(response => {
          this.tableData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    rowDel: function(row, index) {
      this.$confirm("是否确认删除ID为" + row.id, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delObj(row.id);
        })
        .then(data => {
          this.$message.success("删除成功");
          this.getList(this.page);
        });
    },
    handleUpdate: function(row, index, done, loading) {
      row.address = this.addressKeyword;
      putObj(row)
        .then(data => {
          this.$message.success("修改成功");
          done();
          this.getList(this.page);
        })
        .catch(() => {
          loading();
        });
    },
    handleSave: function(row, done, loading) {
      row.address = this.addressKeyword;
      addObj(row)
        .then(data => {
          this.$message.success("添加成功");
          done();
          this.getList(this.page);
        })
        .catch(() => {
          loading();
        });
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    currentChange(current) {
      this.page.currentPage = current;
    },
    searchChange(form, done) {
      this.searchForm = form;
      this.page.currentPage = 1;
      this.getList(this.page, form);
      done();
    },
    refreshChange() {
      this.getList(this.page);
    },

    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.agentTypeId=1;
      getExportPackageTotalList(
        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;
        });
    },
    rowDelTask: function(row, index) {
      this.$confirm("是否确认删除ID为" + row.id, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delObjTask(row.id);
        })
        .then(data => {
          this.$message.success("删除成功");
          this.getListTask(this.pageTask);
        });
    },
    handleUpdateTask: function(row, index, done, loading) {
      putObjTask(row)
        .then(data => {
          this.$message.success("修改成功");
          done();
          this.getListTask(this.pageTask);
        })
        .catch(() => {
          loading();
        });
    },
    handleSaveTask: function(row, done, loading) {
      addObjTask(row)
        .then(data => {
          this.$message.success("添加成功");
          done();
          this.getListTask(this.pageTask);
        })
        .catch(() => {
          loading();
        });
    },
    taskTj(row, index) {
      this.$router.push({
        path: "/admin/wmtask/indexJxsTj?taskStatus=1&packageId=" + row.id
      });
    },
    taskTjCk(row, index) {
      this.$router.push({
        path: "/admin/wmtask/indexJxsTj?packageId=" + row.id + "&type=show"
      });
    },
    getWmScorePackageTask() {
      var obj = {
        agentTypeId: 1
      };
      getWmScorePackageTask(obj)
        .then(response => {
          let answer = response.data.data;
          this.rateForm.dqyyjfzz = answer.dqf;
          this.rateForm.socreYtj = answer.ytj;
          this.rateForm.socreYjwc = answer.ywc;
          if (undefined != answer.ywc && 0 != answer.ywc) {
            this.rateForm.socreShtgRate = parseInt(
              (answer.ytj * 100) / answer.ywc
            );
          }
        })
        .catch(() => {});
    },
    getObjDept1() {
      let deptId = this.userInfo.deptId;
      getObjDept(deptId)
        .then(data => {
          var deptInfo = data.data.data;
          if (deptInfo.level == 3) {
            this.entLevel = 1;
          }

          if (deptInfo.level == 4) {
            this.entLevel = 2;
          }
        })
        .catch(() => {});
    },
    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;
    },
    taskExport() {
      this.taskExportLoading = true;
      let row = this.checkRow;
      var obj = {
        scorePackageDrugId: row.id
      };
      taskExportNbqybg(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(() => {
          this.taskExportLoading = false;
        });
    },
    taskExportInfo() {
      this.taskExportInfoLoading = true;
      let row = this.checkRow;
      var obj = {
        scorePackageDrugId: row.id
      };
      taskExportNbqybgInfo(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.taskExportInfoLoading = false;
        })
        .catch(() => {
          this.taskExportInfoLoading = false;
        });
    },
    exportTaskInfo() {
      this.infoExportLoading = true;
      infoExportNbqybgInfo(this.searchForm)
        .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.infoExportLoading = false;
        })
        .catch(() => {
          this.infoExportLoading = false;
        });
    }
  }
};
</script>
