<template>
  <div class="app-container">
    <div class="cell clearfix">
      <label class="title">
        <span class="name">日期查询</span> :
      </label>
      <el-date-picker
        v-model="listQuery.start_time"
        prefix-icon="el-icon-date"
        @change="changeTime"
        :editable="false"
        style="width: 196px;"
        type="date"
        placeholder="选择日期时间"
        align="right"
        :picker-options="pickerOptions"
        format="yyyy-MM-dd"
        value-format="yyyy-MM-dd"
      ></el-date-picker>
      <span class>-</span>
      <el-date-picker
        v-model="listQuery.end_time"
        prefix-icon="el-icon-date"
        @change="changeTime"
        :editable="false"
        style="width: 196px;"
        type="date"
        placeholder="选择日期时间"
        align="right"
        :picker-options="pickerOptions"
        format="yyyy-MM-dd"
        value-format="yyyy-MM-dd"
      ></el-date-picker>
    </div>
    <div class="cell clearfix">
      <label class="title">
        <span class="name">透析龄</span> :
      </label>
      <div class="time">
        <ul class>
          <li
            :class="item.value==dialysisAge?'active':''"
            @click="selectdialysisAges(item.value)"
            v-for="item in dialysisAgeOptions"
            :key="item.value"
          >{{item.label}}</li>
        </ul>
      </div>
    </div>
    <div class="cell clearfix">
      <label class="title">
        <span class="name">转归状态</span> :
      </label>
      <div class="time">
        <ul class>
          <li
            :class="item.lapseto==lapsetoType?'active':''"
            @click="selectLapseTo( item.lapseto)"
            v-for="item in lapsetoState"
            :key="item.value"
          >{{item.label}}</li>
        </ul>
      </div>
    </div>

    <div class="cell clearfix">
      <label class="title">
        <span class="name">检验项目</span> :
      </label>
      <el-select v-model="listQuery.project_id" placeholder="请选择" @change="changeModel()">
        <el-option label="全部" value=""></el-option>
        <el-option v-for="item in projectOptions" :key="item.project_id" :label="item.project_name" :value="item.project_id"></el-option>
      </el-select>
    </div>

    <h4 class="hui">统计表</h4>
    <hr class="huixiao">
    <el-table
      ref="multipleTable"
      :data="tableData"
      :span-method="mergeSpan"
      border
      fit
      highlight-current-row
      style="width: 100%;margin-top: 10px;"
    >
      <el-table-column label="透析号" min-width="80" property="patient_id" align="center">
         <template slot-scope="scope">{{getPatientDialysisNo(scope.row.patient_id)}}</template>
      </el-table-column>
      <el-table-column label="姓名" min-width="80" property="patient_id" align="center">
         <template slot-scope="scope">{{getPatientName(scope.row.patient_id)}}</template>
      </el-table-column>
      <el-table-column label="检查项目" min-width="80" property="project_id" align="center">
         <template slot-scope="scope">{{getProjectName(scope.row.project_id)}}</template>
      </el-table-column>
      <el-table-column label="检查日期" min-width="80" property="inspect_date" align="center">
        <template slot-scope="scope">{{getTime(scope.row.inspect_date, '{y}{m}{d}')}}</template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="listQuery.pate"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="10"
      background
      style="margin-top:20px;"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>


<script>
import { GetProcessIndicatorsData } from "@/api/qcd";
import {uParseTime} from "@/utils/tools";
export default {
  name: "processIndicators",
  data() {
    return {
      total: 0,
      modesData: {
        xAxis:[],
        series:[],
      },
      listQuery: {
        start_time: "",
        end_time: "",
        page: 1,
        limit: 10,
        lapseto: 0,
        dialysis_age: 0,
        project_id:0,
      },
      projectOptions: {},
      patientMap:{},
      tableData: [],
      tableRowData:[],
      dialysisAge: 0,
      dialysisAgeOptions: [
        { value: 0, label: "全部" },
        { value: 1, label: "大于三个月" },
        { value: 2, label: "小于三个月" }
      ],
      lapsetoType: 0,
      lapsetoState: [
        { value: 0, label: "全部", source: 0, lapseto: 0 },
        { value: 1, label: "转出", source: 0, lapseto: 2 },
        { value: 2, label: "留治", source: 0, lapseto: 1 }
      ],
      pickerOptions: {
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            }
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            }
          },
          {
            text: "一周前",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            }
          },
          {
            text: "清空",
            onClick(picker) {
              picker.$emit("pick");
            }
          }
        ]
      }
    };
  },
  created() {
    var nowDate = new Date();
    var nowYear = nowDate.getFullYear();
    var nowMonth = nowDate.getMonth() + 1;
    var nowDay = nowDate.getDate();
    this.listQuery.end_time = nowYear + "-" + (nowMonth < 10 ? "0" + nowMonth : nowMonth) + "-" + (nowDay < 10 ? "0" + nowDay : nowDay);
    nowDate.setMonth(nowDate.getMonth() - 3);
    nowYear = nowDate.getFullYear();
    nowMonth = nowDate.getMonth() + 1;
    nowDay = nowDate.getDate();
    this.listQuery.start_time =  nowYear + "-" + (nowMonth < 10 ? "0" + nowMonth : nowMonth) + "-" + (nowDay < 10 ? "0" + nowDay : nowDay);
    this.getList();
  },
  methods: {
    getList() {
      GetProcessIndicatorsData(this.listQuery).then(response => {
        if (response.data.state == 0) {
          this.$message.error(response.data.msg);
          return false;
        } else {
          
          this.tableData = [];
          this.tableRowData = []


          var references = response.data.data.references;
          for (const index in references) {
            if (references[index].project_id in this.projectOptions) {
              continue;  
            }
            this.$set(this.projectOptions, references[index].project_id, references[index]);
          }

          var patients = response.data.data.patients;
          for (const index in patients) {
            if (patients[index].id in this.patientMap) {
              continue;  
            }
            this.$set(this.patientMap, patients[index].id, patients[index]);
          }

          //
          var temInspections = {};
          var inspections = response.data.data.inspections;
          for (const index in inspections) {
            var key = inspections[index].inspect_date + '_' + inspections[index].patient_id;
            if (!(key in temInspections)) {
              temInspections[key] = {};
            }
            if (inspections[index].project_id in temInspections[key]) {
              continue;
            }
            temInspections[key][inspections[index].project_id] = inspections[index];
          }
          this.total = response.data.data.total;
          var rowIndex = 0;
          var childIndex = 0;
          for (const key in temInspections) {
            this.tableRowData.push(1);
            var parentIndex = rowIndex;
            childIndex = 0;
            for (const child in temInspections[key]) {
              this.tableData.push(temInspections[key][child]);
              if(childIndex>0) {
                this.tableRowData[parentIndex] += 1;
                this.tableRowData.push(0);
              }
              childIndex++;
              rowIndex++;
            }
          }
        }
      });
    },
    mergeSpan({row, column, rowIndex, columnIndex}) {
      if (columnIndex === 0 || columnIndex === 1 || columnIndex === 3) {
        const _row = this.tableRowData[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        return {
          rowspan: _row,
          colspan: _col
        }
      }
    },
    changeTime() {
      this.getList();
    },
    selectdialysisAges(type) {
      this.dialysisAge = type;
      this.listQuery.dialysis_age = type;
      this.getList();
    },
    selectLapseTo(lapseto) {
      this.lapsetoType = lapseto;
      this.listQuery.lapseto = lapseto;
      this.getList();
    },
    changeModel() {
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    getTime(value, temp) {
        if (value != undefined) {
          return uParseTime(value, temp)
        }
        return ""
    },
    getProjectName(id) {
      return (id in this.projectOptions) ? this.projectOptions[id].project_name:'';
    },
    getPatientName(id) {
      return (id in this.patientMap) ? this.patientMap[id].name:'';
    },
    getPatientDialysisNo(id) {
      return (id in this.patientMap) ? this.patientMap[id].dialysis_no:'';
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.app-container {
//   margin: 20px;
    margin-bottom: 50px;
  font-size: 15px;
  .filter-container {
    padding-bottom: 5px;
  }
  .hui {
    color: #909399;
    margin-bottom: 5px;
  }
  .huixiao {
    border: solid 1px #ebeef5;
  }
  .el-pagination {
    float: right !important;
  }
  .search-component {
    width: 500px;
    .searchBox {
      width: 300px;
      height: 36px;
      line-height: 36px;
      padding-left: 15px;
      border: 1px #dcdfe6 solid;
      border-right: none;
      outline: none;
      float: left;
      border-radius: 6px 0 0 6px;
      font-size: 14px;
      color: #333;
      background: #fff;
      box-shadow: 3px 3px 4px rgba(135, 135, 135, 0.05);
    }
    .searchBtn {
      background-color: #409eff;
      color: #fff;
      font-size: 15px;
      text-align: center;
      height: 36px;
      line-height: 36px;
      float: left;
      outline: none;
      width: 70px;
      border: none;
      border-radius: 0 6px 6px 0;
      font-family: "Microsoft Yahei";
      cursor: pointer;
    }
  }
  .amount {
    font-weight: normal;
    padding: 10px 0 0 0;
    color: #606266;
    font-size: 14px;
    span {
      color: #ef2525;
      font-family: "Arial";
      padding: 0 2px;
    }
  }
}
</style>