<template>
  <div>
    <crumbs title="知识图谱规则测试" class="topBack">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item
          label=""
          class="selectMedicine"
          style="marginbottom: -1px"
        >
          <el-select
            size="mini"
            v-model="hospitalId"
            placeholder="选择医院"
            @change="handleChange"
          >
            <el-option
              v-for="item in hospitalData"
              :label="item.name"
              :value="item.id"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="marginbottom: 0px">
          <el-button size="mini" @click="exportData"
            >导出图谱中规则未涉及到的医院术语</el-button
          >
        </el-form-item>
        <el-form-item style="marginbottom: 0px">
          <el-button size="mini" @click="handleAllTest">所有规则测试</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="caseName"
          label="规则类型"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          :resizable="false"
          prop="gmtModified"
          label="测试时间"
        ></el-table-column>

        <el-table-column
          :resizable="false"
          prop="ruleNum"
          label="总条数"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="成功条数">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="goToFailedOrSuccessPage(scope.row, 'success')"
              >{{ scope.row.ruleSuccessNum }}</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="失败条数">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="goToFailedOrSuccessPage(scope.row, 'failed')"
              >{{ scope.row.ruleFailedNum }}</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="handleTest(scope.row)"
              >执行测试</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import api from '@api/cdss.js';
import config from '@api/config.js';
import utils from '@api/utils.js';
export default {
  name: 'KnowledgeMapRuleTest',
  data() {
    return {
      list: [],
      hospitalData: [],
      hospitalId: '', //选中医院
    };
  },
  created() {
    this._getHospitalInfoCDSS();
    // this.getDataList();
  },
  methods: {
    indexMethod(index) {
      return index + 1;
    },
    // 执行测试
    handleTest(row) {
      const { caseName, caseId } = row;
      let billType; // 开单合理性规则类型  1  通用  2 输血
      if (caseName === '开单合理性提醒_通用规则') {
        billType = 1;
      } else if (caseName === '开单类型提醒_输血规则') {
        billType = 2;
      }
      let params = {
        caseId,
        billType,
        hospitalId: this.hospitalId,
      };
      api.billRuleTest(params).then((res) => {
        console.log(res, '测试结果');
      });
    },
    // 所有规则测试
    handleAllTest() {
      if (this.hospitalId === '') {
        this.$message({
          message: '请先选择医院',
          type: 'warning',
        });
        return;
      }
      api.ruleAllTest({ hospitalId: this.hospitalId }).then((res) => {
        console.log(res, '所有规则测试测试结果');
      });
    },

    // 跳转至失败/成功条数页面
    goToFailedOrSuccessPage(row, type) {
      const page = this.handleGoPage(row.caseName)
      this.$router.push({
        name: page,
        params: { data: { ...row }, type },
      });
    },
    // 处理跳转到的页面
    handleGoPage(caseName){
      switch(caseName){
        case '开单合理性提醒_通用规则':
          return 'BillCommonTest'
        case '开单类型提醒_输血规则':
          return 'BillFusionTest'
        default:
          return null
      }
    },

    // 获取医院信息
    _getHospitalInfoCDSS() {
      api.getHospitalInfo().then((res) => {
        if (res.data.code === '0') {
          this.hospitalData = res.data && res.data.data;
        }
      });
    },

    // 选中医院
    handleChange(val) {
      if (val === '') return;
      this.getDataList(val);
    },

    // 获取列表数据
    getDataList(id) {
      const params = {
        hospitalId: id,
      };
      this.searched = true;
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)',
      });
      api.getCaseResultList(params).then((res) => {
        loading.close();
        if (res.data.code === '0') {
          this.list = res.data && res.data.data;
        }
      });
    },
    // 导出
    exportData() {
      if (this.hospitalId === '') {
        this.$message({
          message: '请先选择医院',
          type: 'warning',
        });
        return;
      }
      this.$alert('确定要导出规则未使用映射关系吗？', '', {
        confirmButtonText: '确定',
        title: '提示',
        type: 'warning',
      })
        .then(() => {
          api.exportRuleExcel({ hospitalId: this.hospitalId }).then((res) => {
            if (res.status === 200) {
              this.$message({ message: '导出成功', type: 'success' });
              utils.downloadExportedData(res.data, '规则未使用映射关系.xls');
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>

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