<template>
  <el-dialog title="导出报告" center :visible.sync="visible" width="800px">
    <div class="export-content">
      <div class="export-item">
        <div class="export-list">
          <!-- word格式 -->
          <div class="lable">Word格式：</div>
          <div class="btn-cont">
            <div class="btns">
              <!-- 第一次导出 -->
              <template
                v-if="checkRow && checkRow.reportOpt.status === 'NOT_GENERATE'"
              >
                <el-button
                  @click="taskExport('word')"
                  :loading="wordLoading"
                  class="btn-a"
                  >生成报告</el-button
                >
              </template>
              <!-- 生成中 -->
              <template
                v-else-if="
                  checkRow && checkRow.reportOpt.status === 'GENERATING'
                "
              >
                <span class="generating">
                  报告正在生成中<i class="el-icon-loading"></i>
                </span>
              </template>
              <template
                v-else-if="
                  checkRow &&
                    (checkRow.reportOpt.status === 'GENERATED' ||
                      checkRow.reportOpt.status === 'ERROR')
                "
              >
                <el-button
                  @click="taskExport('word')"
                  :loading="wordLoading"
                  class="btn-b"
                  >重新导出</el-button
                >
                <template v-if="checkRow.reportOpt.status === 'GENERATED'">
                  <el-button @click="downloadTask('word')" class="btn-b"
                    >下载报告</el-button
                  >
                </template>
                <template v-else-if="checkRow.reportOpt.status === 'ERROR'">
                  <el-button
                    disabled
                    :title="checkRow.reportOpt.errorMsg"
                    class="btn-b"
                    >导出失败
                  </el-button>
                </template>
              </template>
            </div>
            <div class="title-msg">
              <p v-if="checkRow && checkRow.reportOpt.status === 'GENERATED'">
                下载截止日期：{{ checkRow.reportOpt.ttl }}
              </p>
              <p v-if="checkRow && checkRow.reportOpt.status === 'ERROR'">
                本次导出失败：{{ checkRow.reportOpt.errorMsg }}
              </p>
            </div>
          </div>
        </div>
        <!-- Excel格式： -->
        <div class="export-list">
          <div class="lable">Excel格式：</div>
          <div class="btn-cont">
            <div class="btns">
              <template
                v-if="
                  checkRow && checkRow.excelReportOpt.status === 'NOT_GENERATE'
                "
              >
                <el-button
                  :loading="excelLoading"
                  class="btn-a"
                  @click="taskExport('excel')"
                  >生成报告</el-button
                >
              </template>
              <template
                v-else-if="
                  checkRow && checkRow.excelReportOpt.status === 'GENERATING'
                "
              >
                <span class="generating">
                  报告正在生成中<i class="el-icon-loading"></i>
                </span>
              </template>
              <template
                v-else-if="
                  checkRow &&
                    (checkRow.excelReportOpt.status === 'GENERATED' ||
                      checkRow.excelReportOpt.status === 'ERROR')
                "
              >
                <el-button
                  :loading="excelLoading"
                  class="btn-b"
                  @click="taskExport('excel')"
                  >重新导出</el-button
                >
                <template v-if="checkRow.excelReportOpt.status === 'GENERATED'">
                  <el-button @click="downloadTask('excel')" class="btn-b">
                    下载报告</el-button
                  >
                </template>
                <template
                  v-else-if="checkRow.excelReportOpt.status === 'ERROR'"
                >
                  <el-button
                    disabled
                    :title="checkRow.excelReportOpt.errorMsg"
                    class="btn-b"
                    >导出失败
                  </el-button>
                </template>
              </template>
            </div>
            <div class="title-msg">
              <p
                v-if="
                  checkRow && checkRow.excelReportOpt.status === 'GENERATED'
                "
              >
                下载截止日期：{{ checkRow.excelReportOpt.ttl }}
              </p>
              <p v-if="checkRow && checkRow.excelReportOpt.status === 'ERROR'">
                本次导出失败：{{ checkRow.excelReportOpt.errorMsg }}
              </p>
            </div>
          </div>
        </div>
      </div>
      <el-divider></el-divider>
      <div class="export-item">
        <div class="export-list">
          <!-- word格式 -->
          <div class="lable">Word格式：</div>
          <div class="btn-cont">
            <div class="btns">
              <!-- 第一次导出 -->
              <template
                v-if="
                  checkRow && checkRow.newReportOpt.status === 'NOT_GENERATE'
                "
              >
                <el-button
                  @click="taskExport('newWord')"
                  :loading="newWordLoading"
                  class="btn-a"
                  >生成报告</el-button
                >
              </template>
              <!-- 生成中 -->
              <template
                v-else-if="
                  checkRow && checkRow.newReportOpt.status === 'GENERATING'
                "
              >
                <span class="generating">
                  报告正在生成中<i class="el-icon-loading"></i>
                </span>
              </template>
              <template
                v-else-if="
                  checkRow &&
                    (checkRow.newReportOpt.status === 'GENERATED' ||
                      checkRow.newReportOpt.status === 'ERROR')
                "
              >
                <el-button
                  @click="taskExport('newWord')"
                  :loading="newWordLoading"
                  class="btn-b"
                  >重新导出</el-button
                >
                <template v-if="checkRow.newReportOpt.status === 'GENERATED'">
                  <el-button @click="downloadTask('newWord')" class="btn-b"
                    >下载报告</el-button
                  >
                </template>
                <template v-else-if="checkRow.newReportOpt.status === 'ERROR'">
                  <el-button
                    disabled
                    :title="checkRow.newReportOpt.errorMsg"
                    class="btn-b"
                    >导出失败
                  </el-button>
                </template>
              </template>
            </div>
            <div class="title-msg">
              <p
                v-if="checkRow && checkRow.newReportOpt.status === 'GENERATED'"
              >
                下载截止日期：{{ checkRow.newReportOpt.ttl }}
              </p>
              <p v-if="checkRow && checkRow.newReportOpt.status === 'ERROR'">
                本次导出失败：{{ checkRow.newReportOpt.errorMsg }}
              </p>
            </div>
          </div>
        </div>
        <!-- Excel格式： -->
        <div class="export-list">
          <div class="lable">Excel格式：</div>
          <div class="btn-cont">
            <div class="btns">
              <template
                v-if="
                  checkRow &&
                    checkRow.newExcelReportOpt.status === 'NOT_GENERATE'
                "
              >
                <el-button
                  @click="taskExport('newExcel')"
                  :loading="newExcelLoading"
                  class="btn-a"
                  >生成报告</el-button
                >
              </template>
              <!-- 生成中 -->
              <template
                v-else-if="
                  checkRow && checkRow.newExcelReportOpt.status === 'GENERATING'
                "
              >
                <span class="generating">
                  报告正在生成中<i class="el-icon-loading"></i>
                </span>
              </template>
              <template
                v-else-if="
                  checkRow &&
                    (checkRow.newExcelReportOpt.status === 'GENERATED' ||
                      checkRow.newExcelReportOpt.status === 'ERROR')
                "
              >
                <el-button
                  @click="taskExport('newExcel')"
                  :loading="newExcelLoading"
                  class="btn-b"
                  >重新导出</el-button
                >
                <template
                  v-if="checkRow.newExcelReportOpt.status === 'GENERATED'"
                >
                  <el-button @click="downloadTask('newExcel')" class="btn-b"
                    >下载报告</el-button
                  >
                </template>
                <template
                  v-else-if="checkRow.newExcelReportOpt.status === 'ERROR'"
                >
                  <el-button
                    disabled
                    :title="checkRow.newExcelReportOpt.errorMsg"
                    class="btn-b"
                    >导出失败
                  </el-button>
                </template>
              </template>
            </div>
            <div class="title-msg">
              <p
                v-if="
                  checkRow && checkRow.newExcelReportOpt.status === 'GENERATED'
                "
              >
                下载截止日期：{{ checkRow.newExcelReportOpt.ttl }}
              </p>
              <p
                v-if="checkRow && checkRow.newExcelReportOpt.status === 'ERROR'"
              >
                本次导出失败：{{ checkRow.newExcelReportOpt.errorMsg }}
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
      注意： 1.报告将于24小时之内生成完毕，请注意下载。<br />
      <i style="margin-left:62px"></i
      >2.报告汇总为封面+任务数据汇总+任务列表明细。
      <br />
      <i style="margin-left:10px"></i>3.报告详情为每条任务的任务详情+图片。
    </span>
  </el-dialog>
</template>

<script>
import {
  taskExportNbqybg,
  taskExportExcelNbqybg,
  taskAssignPointsExcel,
  taskNewExport,
  taskNewExcel,
  taskAssignPointsWord,
  taskAssignPointsNewWord
} from "@/api/exportApi.js";
export default {
  props: ["dialogShow", "type", "checkRow", "packageIds"],
  data() {
    return {
      wordLoading: false,
      excelLoading: false,
      newWordLoading: false,
      newExcelLoading: false
    };
  },
  computed: {
    visible: {
      get() {
        return this.dialogShow;
      },
      set(value) {
        this.$emit("update:dialogShow", false);
      }
    }
  },
  methods: {
    taskExport(type) {
      switch (this.type) {
        // 是
        case "1":
          this.pointsTaskExportFn(type);
          break;
        case "2":
          this.assignPointsExportFn(type);
          break;
        case "3":
          this.pointsTaskExportFn(type);
          break;
        case "4":
          this.assignPointsExportFn(type);
          break;
        default:
          break;
      }
    },
    // 企业承接任务导出
    async pointsTaskExportFn(type) {
      const obj = {
        id: this.checkRow.id
      };
      let res;
      if (type === "word") {
        this.wordLoading = true;
        res = await taskExportNbqybg(obj);
      }
      if (type === "excel") {
        this.wordLoading = true;
        res = await taskExportExcelNbqybg(obj);
      }
      if (type === "newWord") {
        obj.taskStatus = 3;
        res = await taskNewExport(obj);
      }
      if (type === "newExcel") {
        obj.taskStatus = 3;
        res = await taskNewExcel(obj);
      }
      if (res.data && res.data.data) {
        this.$message.success(res.data.data);
        this.wordLoading = false;
        this.$emit("downSuccess");
      } else {
        this.$message.error("导出失败");
      }
      this.visible = false;
    },
    // 个人承接任务
    async assignPointsExportFn(type) {
      const obj = {
        id: this.checkRow.id
      };
      if (this.packageIds) {
        const arr = this.packageIds
          .split(",")
          .sort((a, b) => Number(a) - Number(b));
        obj.id = arr.join(",");
      }
      let res;
      if (type === "word") {
        const obj = {
          scorePackageDrugId: this.checkRow.id
        };
        if (this.packageIds) {
          const arr = this.packageIds
            .split(",")
            .sort((a, b) => Number(a) - Number(b));
          obj.scorePackageDrugId = arr.join(",");
        }
        this.wordLoading = true;
        res = await taskAssignPointsWord(obj);
      }
      if (type === "excel") {
        const obj = {
          scorePackageDrugId: this.checkRow.id
        };
        if (this.packageIds) {
          const arr = this.packageIds
            .split(",")
            .sort((a, b) => Number(a) - Number(b));
          obj.scorePackageDrugId = arr.join(",");
        }
        this.excelLoading = true;
        res = await taskAssignPointsExcel(obj);
      }
      if (type === "newWord") {
        res = await taskAssignPointsNewWord(obj);
      }
      if (type === "newExcel") {
        res = await taskNewExcel(obj);
      }
      if (res.data && res.data.data) {
        this.$message.success(res.data.data);
        this.wordLoading = false;
        this.$emit("downSuccess");
      } else {
        this.$message.error("导出失败");
      }
      this.visible = false;
    },

    downloadTask(type) {
      let latestUrl;
      if (type === "word") {
        latestUrl = this.checkRow.reportOpt.latestUrl;
      } else if (type === "excel") {
        latestUrl = this.checkRow.excelReportOpt.latestUrl;
      } else if (type === "newWord") {
        latestUrl = this.checkRow.newReportOpt.latestUrl;
      } else if (type === "newExcel") {
        latestUrl = this.checkRow.newExcelReportOpt.latestUrl;
      }
      let a = document.createElement("a");
      a.href = latestUrl;
      a.click();
      this.$message.success("导出汇总报告成功");
      this.visible = false;
    }
  }
};
</script>

<style lang="scss" scoped>
.export-content {
  text-align: center;
  display: flex;
  flex-direction: column;

  .export-item {
    margin: 0 auto;
    width: 360px;

    .export-list {
      margin-bottom: 10px;
      display: flex;

      .lable {
        width: 90px;
        line-height: 32px;
        text-align: left;
      }

      .btn-cont {
        .btns {
          .generating {
            line-height: 32px;
          }

          width: 240px;
          display: flex;
          justify-content: space-between;

          .btn-a {
            width: 240px;
          }

          .btn-b {
            width: 110px;
          }
        }
      }
    }
  }

  .line {
    margin: 20px auto;
    margin-bottom: 30px;
    width: 360px;
    text-align: left;
    display: flex;
    align-items: center;
    .line-title {
      line-height: 32px;
      display: inline-block;
      font-size: 14px;
      color: #000;
      font-weight: 600;
    }

    .line-text {
      font-size: 12px;
      color: #666;
    }
  }
}

.title-msg {
  text-align: center;
  margin-top: 10px;
  font-size: 12px;
}
</style>
