<template>
  <div>
    <crumbs
      :title="title"
      class="topBack"
      :param="$route.params"
      linkTo="StaticKnowledgeMapTest"
    >
      <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="hisName"
          :label="caseName === '检验' ? '医院术语(检验套餐)' : '医院术语'"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          :resizable="false"
          prop="hisDetailName"
          label="医院术语(检验细项)"
          show-overflow-tooltip
          v-if="caseName === '检验'"
        ></el-table-column>
        <el-table-column
          :resizable="false"
          prop="name"
          label="标准术语"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          v-if="type === 'lessStatic'"
          :resizable="false"
          prop="hasStaticKnowledge"
          label="静态知识标识"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ scope.row.hasStaticKnowledge === 0 ? '无' : '有' }}
          </template>
        </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,
      caseName: '',
      type: '', // 来自什么页面   noMap/lessStatic
    };
  },
  created() {
    this._initData();
    this.getDataList();
    // 非首页 编辑页返回 设置 this.currentPage
    if (Object.keys(this.$route.params).length !== 0) {
      this.currentPage = this.$route.params.currentPage;
    }
  },
  computed: {
    title() {
      let text = '';
      if (this.type === 'noMap') {
        text = '未映射到标准术语的医院术语-';
      } else if (this.type === 'lessStatic') {
        text = '缺少静态知识的医院术语-';
      }
      return text + this.caseName;
    },
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      Object.assign(vm, to.params);
      vm.inCurrentPage = to.params.currentPage;
    });
  },
  methods: {
    _initData() {
      const { data, type } = this.$route.params;
      this.caseName = (data && data.caseName) || '';
      this.type = type;
    },
    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();
    },

    handleParams() {
      const { data, type } = this.$route.params;
      let params = {
        resultId: data.resultId, //结果id
      };
      if (type === 'noMap') {
        //是否映射到标准术语（0：否，1：是）
        params = { ...params, hasStandName: 0 };
      } else if (type === 'lessStatic') {
        //是否有静态知识（0：无，1：有）
        params = { ...params, hasStaticKnowledge: 0 };
      }
      return params;
    },
    getDataList() {
      const params = this.handleParams();
      this.searched = true;
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)',
      });
      api.getStaticPage(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;
        }
      });
    },
    // excel文件名处理
    handleExcelName() {
      const { data, hospitalId, hospitalName, type } = this.$route.params;
      let exportText = ''; // excel文件名

      return exportText;
    },
    // 导出
    exportData() {
      const { data, hospitalId, hospitalName, type } = this.$route.params;
      let requestAjax;
      let exportText; // excel文件名
      let params = {
        resultId: data.resultId,
      };
      if (data.caseName === '检验' && type === 'noMap') {
        requestAjax = 'exportStaticLisExcel';
        exportText = `${hospitalName}-未映射到标准术语的${data.caseName}医院术语.xls`;
        params = { ...params, hasStandName: 0 };
      } else if (data.caseName !== '检验' && type === 'noMap') {
        requestAjax = 'exportStaticCommonExcel';
        exportText = `${hospitalName}-未映射到标准术语的${data.caseName}医院术语.xls`;
        params = { ...params, hasStandName: 0 };
      } else if (data.caseName === '检验' && type === 'lessStatic') {
        requestAjax = 'exportStaticLisStaticExcel';
        exportText = `${hospitalName}-缺少静态知识的${data.caseName}医院术语.xls`;
        params = { ...params, hasStaticKnowledge: 0 };
      } else if (data.caseName !== '检验' && type === 'lessStatic') {
        requestAjax = 'exportStaticCommonStaticExcel';
        exportText = `${hospitalName}-缺少静态知识的${data.caseName}医院术语.xls`;
        params = { ...params, hasStaticKnowledge: 0 };
      }
      this.$alert('确定要导出静态知识映射测试结果吗？', '', {
        confirmButtonText: '确定',
        title: '提示',
        type: 'warning',
      })
        .then(() => {
          api[requestAjax](params).then((res) => {
            if (res.status === 200) {
              this.$message({ message: '导出成功', type: 'success' });
              utils.downloadExportedData(res.data, exportText);
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>

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