<template>
  <div>
    <crumbs
      :title="
        type === 'success'
          ? '高风险提示-高危手术整合规则测试-成功条数'
          : '高风险提示-高危手术整合规则测试-失败条数'
      "
      class="topBack"
      :param="$route.params"
      linkTo="KnowledgeMapRuleTest"
    >
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item style="marginbottom: 0px">
          <el-button size="mini" @click="exportData">导出</el-button>
        </el-form-item>
      </el-form>
    </crumbs>
    <div style="margin: 60px 20px 0">
      <el-table :data="list" border>
        <el-table-column
          :resizable="false"
          type="index"
          :index="indexMethod"
          label="编号"
          width="80"
        ></el-table-column>
        <el-table-column
          :resizable="false"
          prop="gmtModified"
          label="测试时间"
        ></el-table-column>
        <el-table-column
          :resizable="false"
          prop="highriskItemName"
          label="手术名称（标准术语）"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          :resizable="false"
          prop="highriskItemHisName"
          label="手术名称（医院术语）"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          :resizable="false"
          prop="level"
          label="手术级别"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          :resizable="false"
          prop="factorItemType"
          label="禁忌项类型"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          :resizable="false"
          prop="factorItemName"
          label="禁忌项（标准术语）"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          :resizable="false"
          prop="factorItemHisName"
          label="禁忌项（医院术语）"
          show-overflow-tooltip
        ></el-table-column>
          <el-table-column
          :resizable="false"
          prop="factorItemHisDetailName"
          label="禁忌项-细项（医院术语）"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          :resizable="false"
          prop="inputValue"
          label="禁忌项输入值/结果"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          :resizable="false"
          prop="output"
          label="实际结果"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          :resizable="false"
          prop="expectedOutput"
          label="预期结果"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="测试结果">
          <template slot-scope="scope">{{
            scope.row.success === 1 ? '成功' : '失败'
          }}</template>
        </el-table-column>
        <el-table-column
          v-if="type !== 'success'"
          :resizable="false"
          prop="message"
          label="失败原因"
          show-overflow-tooltip
        ></el-table-column>
      </el-table>
      <div class="pagination pagepage">
        <el-pagination
          :current-page.sync="currentPage"
          @current-change="currentChange"
          background
          :page-size="pageSize"
          :page-sizes="pageSizeArr"
          @size-change="handleSizeChange"
          :layout="pageLayout"
          :total="total"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import api from '@api/cdss.js';
import config from '@api/config.js';
import utils from '@api/utils.js';
export default {
  data() {
    return {
      list: [],
      currentPage: 1,
      pageSize: config.pageSize,
      pageSizeArr: config.pageSizeArr,
      pageLayout: config.pageLayout,
      total: 0,
      type: '',
    };
  },
  created() {
    const params = this.handleInitData();
    this.getDataList();
    // 非首页 编辑页返回 设置 this.currentPage
    if (Object.keys(this.$route.params).length !== 0) {
      this.currentPage = this.$route.params.currentPage;
    }
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      Object.assign(vm, to.params);
      vm.inCurrentPage = to.params.currentPage;
    });
  },
  methods: {
    handleInitData() {
      const { data, type } = this.$route.params;
      this.type = type;
      return {
        current: this.inCurrentPage || this.currentPage,
        size: this.pageSize,
        resultId: data && data.resultId,
        success: type === 'success' ? 1 : 0,
      };
    },
    indexMethod(index) {
      return (this.currentPage - 1) * this.pageSize + index + 1;
    },
    currentChange(next) {
      this.currentPage = next;
      this.getDataList(true);
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = utils.getCurrentPage(
        this.currentPage,
        this.total,
        this.pageSize
      );
      this.getDataList();
    },

    getDataList() {
      const params = this.handleInitData();
      this.searched = true;
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)',
      });
      api.getResultHighriskOperationPage(params).then((res) => {
        loading.close();
        if (res.data.code === '0') {
          this.list = res.data.data && res.data.data.records;
        }
        this.total = res.data.data && res.data.data.total;
        if (this.inCurrentPage !== undefined) {
          this.currentPage = this.inCurrentPage;
          this.inCurrentPage = undefined;
        }
      });
    },
    // 导出
    exportData() {
      const { data, hospitalId, hospitalName, type } = this.$route.params;
      let params = {
        success: type === 'success' ? 1 : 0,
        resultId: data.resultId,
      };
      this.$alert('确定要导出规则测试结果吗？', '', {
        confirmButtonText: '确定',
        title: '提示',
        type: 'warning',
      })
        .then(() => {
          api.highriskOperationExportExcel(params).then((res) => {
            if (res.status === 200) {
              this.$message({ message: '导出成功', type: 'success' });
              utils.downloadExportedData(
                res.data,
                `${hospitalName}-高风险提示_高危手术整合规则测试结果.xls`
              );
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="less" scored>
@import '../../../less/admin.less';
</style>