<template>
  <div style="display: flex;width:100%;">  
    <div style="width:180;">
      <el-table :data="RegisterCheckList" width="180" 
        :height="window.pageHeight < 600 ? 300:window.pageHeight-300"
        border highlight-current-row
        :row-style="{ height: '40px' }" :row-class-name="tableRowClassName" @row-click="rowClick">
        <el-table-column prop="asbitemName" label="组合项目" width="180" />
      </el-table>
    </div>
    <div :style="'width:' + (window.pageWidth - 330 - 80) + 'px;'">
      <el-table :data="tableData" :width="(window.pageWidth - 330 - 80)" 
        :height="window.pageHeight < 600 ? 300:window.pageHeight-300"
        border :row-style="{ height: '40px' }">
        <el-table-column prop="itemName" label="项目" width="150" />
        <el-table-column prop="unitName" label="单位" width="90" />
        <el-table-column prop="referenceRangeValue" label="参考范围" width="120" />
        <el-table-column v-for="(item, index) of tableCols" :label="item" :prop="item" :key="index" width="150">
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script lang="ts">
import { mapState } from 'vuex';
import Sortable from "sortablejs";
import { getapi, postapi, putapi, deletapi } from "@/api/api";
import { arrayExistObj } from '@/utlis/proFunc';
export default {
  components: {},
  data() {
    return {
      RegisterCheckList: [],
      tableData: [],   //显示数据
      tableRows: [],   //动态行
      tableCols: [],   //动态列
    };
  },

  created() { },

  //挂载完成
  mounted() {
    if(this.sumDoctorCheck.sumPREdit.patientId){
      this.registerCheckList(this.sumDoctorCheck.sumPREdit.patientId);
    }
  },

  computed: {
    ...mapState(['window','dict', 'doctorCheck', 'sumDoctorCheck']),
  },

  methods: {
    //组合项目颜色标识
    tableRowClassName({ row, rowIndex }) {
      //console.log('tableRowClassName',rowIndex,row)
      if (row.completeFlag === '0') {
        return 'warning'; //未检
      } else if (row.completeFlag === '2') {
        return 'info'; //弃检
      }
      return '';      
    },

    //获取检查组合项目
    registerCheckList(patientId) {
      console.log(`/api/app/sumsummaryreport/gethorizontalcomparisonasbitemlist?PatientId=${patientId}`)
      getapi(`/api/app/sumsummaryreport/gethorizontalcomparisonasbitemlist?PatientId=${patientId}`)
        .then((res) => {
          console.log("获取项目对比 SumItems", res.data);
          if (res.code != -1) {
            this.RegisterCheckList = res.data;
            if (res.data.length > 0) this.rowClick(res.data[0])
          }
        })
        .catch((err) => {
          this.$message({ type: "error", message: `操作失败，原因：${err}` });
        });

    },

    rowClick(row) {
      //console.log('row',row) //asbitemId
      this.SumItems(this.sumDoctorCheck.sumPREdit.patientId, row.asbitemId)
    },



    //获取结果明细 3a0c6589-9989-4d7f-ba54-61e5b0138220
    // "itemName": "乙肝表面抗原(HBsAg)",
    // "unitName": "次/分",
    // "referenceRangeValue": "",
    // "checkDate": "2023-07-14",
    // "resultValue": "阴性"
    SumItems(patientId, asbitemId) {
      console.log(`/api/app/sumsummaryreport/gethorizontalcomparisonlist?PatientId=${patientId}&AsbitemId=${asbitemId}`)
      getapi(`/api/app/sumsummaryreport/gethorizontalcomparisonlist?PatientId=${patientId}&AsbitemId=${asbitemId}`)
        .then((res) => {
          console.log("获取项目对比 SumItems", res.data);
          if (res.code != -1) {
            this.crossTable(res.data);
          }
        })
        .catch((err) => {
          this.$message({ type: "error", message: `操作失败，原因：${err}` });
        });
    },

    //交叉报表

    crossTable(tableData) {
      this.tableData = []   //显示数据
      this.tableRows = []   //动态行
      this.tableCols = []   //动态列

      console.log('tableData', tableData)
      tableData.forEach(e => {
        if (this.tableRows.indexOf(e.itemName) == - 1) this.tableRows.push(e.itemName)
        if (this.tableCols.indexOf(e.checkDate) == - 1) this.tableCols.push(e.checkDate)
      });
      this.tableCols.sort();

      this.tableRows.forEach(r => {
        let item = {}
        let resultValue = ''
        item['itemName'] = r
        this.tableCols.forEach(c => {
          for (let i = 0; i < tableData.length; i++) {
            if (tableData[i].itemName == r) {
              item['unitName'] = tableData[i].unitName
              item['referenceRangeValue'] = tableData[i].referenceRangeValue
            }
            if (tableData[i].itemName == r && tableData[i].checkDate == c) {
              resultValue = tableData[i].resultValue
              break;
            }
          }
          item[c] = resultValue
        });
        this.tableData.push(item)
      });
      console.log('this.tableRows', this.tableRows)
      console.log('this.tableCols', this.tableCols)
      console.log('this.tableData', this.tableData)
    },



  },

  //监听事件
  watch: {
    //检查项目切换
    "sumDoctorCheck.sumPREdit.patientId"(newVal, oldVal) {
      console.log("watch sumDoctorCheck.sumPREdit.patientId newVal：", newVal, " oldVal：", oldVal);
      if (newVal != oldVal && newVal != '') {
        this.registerCheckList(newVal)
      }
    },
  },
};
</script>
<style scoped>
::v-deep .el-table td.el-table__cell,
.el-table th.el-table__cell.is-leaf {
  padding: 0;
}
</style>
  