<template>
 <div class="main-contain">
    <div class="position">
      <bread-crumb :crumbs='crumbs'></bread-crumb>
    </div>
    <div class="app-container" id="outcome-indicators-query">
      <person-info :patient="patientInfo"></person-info>
      <person-nav activeName="oralmedicine" :patientID="patientID"></person-nav>


      <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"
          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"
          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==adviceType?'active':''"
              @click="selectAdviceType( item.value)"
              v-for="item in adviceTypeOptions"
              :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.advice_type" filterable clearable placeholder="请选择">
          <el-option
            v-for="item in medicalOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </div> -->

      <!-- <div class="cqd-dataTitle">统计图</div>
      <el-row :gutter="32">
        <el-col :xs="24" :sm="24" :lg="24">
          <div class="chart-wrapper">
              <line-chart title="曲线图(Kg)" :options="CompletionOptions" />
          </div>
        </el-col>
      </el-row>

      <div style="height:20px">&nbsp;</div> -->
      <div class="cqd-dataTitle">统计表</div>
      <el-table
        ref="multipleTable"
        :data="tableData"
        border
        fit
        highlight-current-row
        :cell-style="cellstyle"
        style="width: 100%;margin-top: 10px;"
        id="qcd-person-oral-medical"
        :span-method="mergeSpan"
        :header-cell-style="{ backgroundColor: 'rgb(245, 247, 250)'}"
      >
        <el-table-column  label="医嘱类型" width="80" property="advice_type" align="center" >
            <template slot-scope="scope">
                <span v-if="scope.row.advice_type==1">长期</span>
                <span v-else-if="scope.row.advice_type==2">临时</span>
                <span v-else></span>
            </template>
        </el-table-column>

        <el-table-column  label="医嘱内容" property="advice_name" min-width="180" align="center" >
            <template slot-scope="scope">
               {{scope.row.parent_id>0?'&nbsp;&nbsp;':''}} {{scope.row.advice_name }}
            </template>
        </el-table-column>

        <el-table-column  label="开始时间" width="180" property="start_time" align="center">
            <template slot-scope="scope">
                {{scope.row.start_time | parseTime("{y}-{m}-{d} {h}:{i}")}}
            </template>
        </el-table-column>
        <el-table-column label="停止时间" width="180"  property="stop_time" align="center">
            <template slot-scope="scope">
                <span v-if="scope.row.advice_type==2&&scope.row.execution_state==1"> {{scope.row.execution_time | parseTime("{y}-{m}-{d} {h}:{i}")}}</span>
                <span v-if="scope.row.advice_type==1&&scope.row.stop_state==1"> {{scope.row.stop_time | parseTime("{y}-{m}-{d} {h}:{i}")}}</span>
            </template>
        </el-table-column>

        <el-table-column label="开嘱医生" width="180"  property="advice_doctor" align="center">
            <template slot-scope="scope">
                {{getXuserName(scope.row.advice_doctor)}}
            </template>
        </el-table-column>

        <el-table-column  label="执行护士" width="180"  property="execution_staff" align="center">
            <template slot-scope="scope">
                <span v-if="scope.row.parent_id==0">{{getXuserName(scope.row.execution_staff)}}</span>
            </template>
        </el-table-column>

      </el-table>

      <!-- <pagi-nation title="分页"></pagi-nation> -->

      <el-pagination
      align="right"
        @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>
  </div>
</template>


<script>
import { GetPersonAdviceData  } from "@/api/qcd";
import {jsGetAge, uParseTime} from "@/utils/tools";
import personNav from './components/personNav'
import personInfo from './components/personInfo'
import BreadCrumb from "@/xt_pages/components/bread-crumb";
import LineChart from "../../components/LineChart";
export default {
  name: "totalAnalysis",
  data() {
    return {
      crumbs:[
        {path:false, name:'质控管理'},
        {path:false, name:'患者分析'},
        {path:false, name:'统计'},
        {path:false, name:'口服药'},
      ],
      total: 0,
      patientMap:{},
      listQuery: {
        start_time: "",
        end_time: "",
        page: 1,
        limit: 10,
      },
      CompletionOptions:{
          xAxis:[],
          legend:[],
          series:[],
      },
      medicalOptions:[],
      tableData: [],
      pieData:[],
      patientInfo:null,
      patientID:0,
      adviceType:0,
      adviceTypeOptions: [
        { value: 0, label: "全部",  },
        { value: 2, label: "临时医嘱", },
        { value: 1, label: "长期医嘱",  },
      ],
      adminUserOptions: {},
      tableRow: [],
    };
  },
  created() {
    var patientID = this.$route.query && this.$route.query.id;
    if(typeof(patientID) == 'undefined' || !patientID || patientID<=0) {
      this.$message.error('参数错误，无法请求');
      return false;
    }
    patientID = parseInt(patientID);
    this.patientID = patientID;
    this.listQuery.patient_id = patientID;

    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() {
      GetPersonAdviceData(this.listQuery).then(response => {
        if (response.data.state == 0) {
          this.$message.error(response.data.msg);
          return false;
        } else {
            this.tableData = [];
            this.total = response.data.data.total;
            this.patientInfo = response.data.data.patient;
            var advices = response.data.data.advices;
            for (const index in advices) {
                this.tableData.push(advices[index]);
            }
            var adminUserOptions = response.data.data.users;
            for (const index in adminUserOptions) {
              this.adminUserOptions[adminUserOptions[index].id] = adminUserOptions[index];
            }
            // var pieData = response.data.data.pie;
            // for (const index in pieData) {
            //     this.pieData.push(pieData[index]);
            // }
        }
      });
    },
    cellstyle(row){
      if (row.columnIndex == 1) {
        return 'text-align:left;';
      }
      return '';
    },

    handleTableRow() {
      this.tableRow = [];
      var pos = 0;
      for (const index in this.tableData) {
        if (index==0) {
          this.tableRow.push(1);
          pos = 0;
        }else {
          if (this.tableData[index].groupno === this.tableData[index-1].groupno) {
            this.tableRow[pos] += 1;
            this.tableRow.push(0);
          }else {
            this.tableRow.push(1);
            pos = index;
          }
        }
      }
    },
    mergeSpan({ row, column, rowIndex, columnIndex }) {
      if (columnIndex !== 1 ) {
        const _row = this.tableRow[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        return {
          rowspan: _row,
          colspan: _col
        };
      }
    },
    changeTime() {
      this.getList();
    },
    onSearch(){
      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 ""
    },

    getXuserName(id) {
      if (id <= 0) {
        return "";
      }
      if (id in this.adminUserOptions) {
        return this.adminUserOptions[id].name;
      }
      return ''
    },
    getProjectName(id) {
      return (id in this.projectOptions) ? this.projectOptions[id].project_name:'';
    },
    getPatientName(id) {
      return (id in this.patientMap) ? this.patientMap[id].name:'';
    },
    getPatientLapseto(lapseto) {
      if(lapseto==1) {
        return '留治';
      }else if(lapseto == 2) {
        return '转出';
      }
      return '--';
    },
    getPatientSource(srouce){
      if(srouce==1) {
        return '门诊';
      }else if(srouce == 2) {
        return '住院';
      }
      return '其他';
    },
    getPatientGender(gender) {
      if (gender == 1) {
        return '男';
      } else if(gender == 2) {
        return '女';
      }
      return '';
    },
    tranAge(birthday) {
        var birth = uParseTime(birthday, '{y}-{m}-{d}');
        return jsGetAge(birth, '-');
    },
    getPatientDialysisNo(id) {
      return (id in this.patientMap) ? this.patientMap[id].dialysis_no:'';
    },
    getItemVulue(project_id, item_id, row) {
        var key = project_id + '_' + item_id;
        return (typeof(row.items) !== 'undefined' && (key in row.items)) ? row.items[key].inspect_value:'';
    },
    renderheader(h, { column, $index }) {
        return h('span', {}, [
            h('span', {}, column.label.split('//')[0]),
            h('br'),
            h('span', {}, column.label.split('//')[1])
        ])
    },
    selectAdviceType(type) {
      this.adviceType = type;
      this.listQuery.advice_type = type;
      this.getList();
    },
    drawPie(){
      this.CompletionOptions = {
          xAxis:[],
          legend:[],
          series:[],
      };


      if(this.diagramType !== 1 && this.diagramType !== 2) {
        return false;
      }

      if (this.diagramType == 1) {
        this.CompletionOptions.legend = ['透前称重', '透后称重', '干体重'];
        this.CompletionOptions.series = [
          { name:'透前称重', type:'line', data:[] },
          { name:'透后称重', type:'line', data:[] },
          { name:'干体重', type:'line', data:[] },
        ];
      }else {
        this.CompletionOptions.legend = ['体重增加'];
        this.CompletionOptions.series = [
          // { name:'透前体重', type:'line', data:[] },
          { name:'体重增加', type:'line', data:[] },
          // { name:'干体重', type:'line', data:[] },
        ];
      }
      for (const index in this.pieData) {

        var dateDay = uParseTime(this.pieData[index].schedule_date, '{y}-{m}-{d}');
        if (this.diagramType == 1) {
          this.CompletionOptions.xAxis.push(dateDay);
          if (this.pieData[index].assessment_after_dislysis) {
            this.CompletionOptions.series[1].data.push(this.pieData[index].assessment_after_dislysis.weight_after);
          } else {
            this.CompletionOptions.series[1].data.push(0);
          }
          if (this.pieData[index].assessment_before_dislysis) {
            this.CompletionOptions.series[0].data.push(this.pieData[index].assessment_before_dislysis.weight_before);
            this.CompletionOptions.series[2].data.push(this.pieData[index].assessment_before_dislysis.dry_weight);
          } else {
            this.CompletionOptions.series[0].data.push(0);
            this.CompletionOptions.series[2].data.push(0);
          }
        } else {
          this.CompletionOptions.xAxis.push(dateDay);
          if (this.pieData[index].assessment_before_dislysis) {
            var weightTem = this.pieData[index].assessment_before_dislysis.weight_before - this.pieData[index].assessment_before_dislysis.dry_weight;
            // this.CompletionOptions.series[0].data.push(this.pieData[index].assessment_before_dislysis.weight_before);
            this.CompletionOptions.series[0].data.push(weightTem);
            // this.CompletionOptions.series[2].data.push(this.pieData[index].assessment_before_dislysis.dry_weight);
          } else {
            this.CompletionOptions.series[0].data.push(0);
          }
        }
      }
    },
  },
  components: {
      personNav,
      BreadCrumb,
      personInfo,
      LineChart
  },
  watch:{
    "diagramType":function(){
      this.drawPie();
    },
    "pieData":function (){
      this.drawPie();
    },
    'tableData':function (){
      this.handleTableRow();
    }
  }
};
</script>
<style>
#oictable ::-webkit-scrollbar {
  height: 15px;
}
#outcome-indicators-query .el-form-item {
    margin-bottom: 0px !important;
}
</style>


<style rel="stylesheet/scss" lang="scss" scoped>
.app-container {
//   margin: 20px;
  font-size: 15px;
  .filter-container {
    padding-bottom: 5px;
  }
  .cqd-dataTitle{
    color: #303133;
    font-size: 14px;
    border-bottom: 2px #E4E7ED solid;
    height: 36px;
    line-height: 36px;
    margin: 0 0 25px 0;
    position: relative;
  }
  .cqd-dataTitle::before {
      position: absolute;
      left: 0;
      bottom: -2px;
      content: "";
      width: 42px;
      height: 2px;
      background: #409eff;
  }
  .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>
