<template>
  <div class="main-contain new-main-contain">
    <div class="position">
      <bread-crumb :crumbs="crumbs"></bread-crumb>
    </div>
    <div
      class="app-container"
      style="display: flex; flex: 1; padding: 10px 20px 0px 20px"
    >
      <div class="mainLeft">
        <div>
          <div
            style="
              margin: 10px 0;
              display: flex;
              justify-content: space-between;
            "
          >
          <el-date-picker
            size="small"
            v-model="start_time"
            prefix-icon="el-icon-date"
            :editable="false"
            style="width: 196px;"
            type="date"
            placeholder="选择日期时间"
            align="right"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            @change="startTimeChange"
          ></el-date-picker>

        <!-- <el-input @keyup.enter.native='searchAction' v-model="keywords"
                  class="filter-item"/>
        <el-button  class="filter-item" type="primary" icon="el-icon-search"
                   @click="searchAction">搜索</el-button> -->
          </div>

          <el-table
            :row-class-name="tableRowClassName"
            @row-click="onRowClick"
            :data="patientTableData"
            :height="tableHeight"
            border
            style="width: 100%"
            :row-style="{ color: '#303133' }"
            :header-cell-style="{
              backgroundColor: 'rgb(245, 247, 250)',
              color: '#606266',
            }"
            highlight-current-row
            ref="singleTable"
            @current-change="handleCurrentChange"
          >
            <el-table-column align="center" prop="name" label="名称" wdith="89">
              <template slot-scope="scope">
                {{scope.row.drug_name}}
              </template>
            </el-table-column>
            <el-table-column align="center" prop="name" label="规格" wdith="89">
              <template slot-scope="scope">
                {{scope.row.dose}}{{scope.row.dose_unit}}*{{scope.row.min_number}}{{scope.row.min_unit}}/{{scope.row.max_unit}}
              </template>
            </el-table-column>
            <el-table-column align="center" prop="name" label="库存" wdith="89">
              <template slot-scope="scope">
                {{getWarehoseInfo(scope.row.drug_warehouse_info,scope.row.max_unit,scope.row.min_unit,scope.row.min_number)}}
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <div class="mainRight">
        <el-tabs v-model="titleType">
           <el-button type="primary" @click="toSend">发药</el-button>
          <el-tab-pane label="明细" name="明细">
            
           <el-table
            :data="tableData"
            border
            style="width: 100%">
            <el-table-column prop="date" label="序号" width="180" align="center">
              <template slot-scope="scope">
                 {{scope.$index + 1}}
              </template>
            </el-table-column>
           
            <el-table-column prop="address" label="患者姓名" align="center">
               <template slot-scope="scope">
                 {{getPatientName(scope.row.patient_id)}}
              </template>
            </el-table-column>
            <el-table-column prop="address" label="单次用量" align="center">
              <template  slot-scope="scope">
                 {{scope.row.single_dose}}
               </template>
            </el-table-column>
             <el-table-column prop="address" label="用法" align="center">
               <template  slot-scope="scope">
                 {{scope.row.delivery_way}}
               </template>
            </el-table-column>
             <el-table-column prop="address" label="频率" align="center">
               <template  slot-scope="scope">
                 {{scope.row.execution_frequency}}
               </template>
            </el-table-column>
             <el-table-column prop="address" label="天数" align="center">
                <template  slot-scope="scope">
                 {{scope.row.day}}
               </template>
            </el-table-column>
            <el-table-column prop="address" label="总量" align="center">
               <template  slot-scope="scope">
                 {{scope.row.prescribing_number}}
               </template>
            </el-table-column>
            <el-table-column  prop="name" label="数据来源" width="180" align="center">
                <template  slot-scope="scope">
                 <span v-if="scope.row.type == 1">血透</span>
                 <span v-if="scope.row.type == 2">HIS</span>
               </template>
            </el-table-column>
          </el-table>

          </el-tab-pane>
         
        </el-tabs>
      </div>
    </div>
  </div>
</template>

<script>
import { getTodayAdviceList,getPharmacyBaseDrug,updatePharmacyBaseDrug } from "@/api/his/advice";
import BreadCrumb from "@/xt_pages/components/bread-crumb";
const moment = require('moment')
export default {
  components: {
    BreadCrumb,
  },
  data() {
    return {
      currentRowIndex: 0,
      patientTableData: [],
      tableHeight: 400,
      crumbs: [
        { path: false, name: "His工具" },
        { path: false, name: "明细汇总统计" },
      ],
      titleType: "明细",
      patient_id: 0,
      keywords: "",
      start_time:moment(new Date()).add('year',0).format("YYYY-MM-DD"),
      tableData:[],
      patientList:[],
    };
  },


  //判断列表页是否刷新
  beforeRouteLeave(to, from, next) {
    console.log(to, "beforeRouteEnter");
    if (to.path != "/hisTool/detailPrint"&&to.path != "/hisTool/gatherPrint") {
      this.$store.getters.pagedata.list.table_id = 0
      next()
    } else {
     next()
    }
  },

  watch:{
    'titleType':{
      handler(val){
        this.$store.commit('SET_PAGENAME',val)
      }
    }
  },

  methods: {
   

    tableRowClassName({ row, rowIndex }) {
      row.row_index = rowIndex;
    },

    onRowClick(row, event, column) {
      this.currentRowIndex = row.row_index;
      console.log(this.currentRowIndex, "this.currentRowIndex");
      this.$store.commit("SET_PAGEDATA", {
        table_id: this.currentRowIndex,
      });
      console.log(this.$store.getters.pagedata.list.table_id, "kk");
    },
    searchAction() {
      this.getPatientList();
    },
    handleCurrentChange(row) {
    
      this.getPharmacyBaseDrug(row.id)
    },
    getlist(){
      var params = {
        start_time:this.start_time,
        keywords:this.keywords,
      }  
      getTodayAdviceList(params).then(response=>{
         if(response.data.state == 1){
           var list =  response.data.data.list
           this.$refs.singleTable.setCurrentRow(list[0])
           this.patientTableData = list
         }
      }) 
    }, 
     getWarehoseInfo(arr, max_unit, min_unit, min_number) {
      var total = 0;
      var max_str = "";
      var min_str = "";
      if (arr.length > 0) {
        for (let i = 0; i < arr.length; i++) {
          total += parseInt(arr[i].stock_max_number+ arr[i].stock_min_number);
        }
      }
      if (total < min_number) {
        min_str = total + min_unit;
      }
      if (total == 0) {
        min_str = "";
        max_str = "";
      }
      if (total >= min_number) {
        if (parseInt(total / min_number) != 0) {
          max_str = parseInt(total / min_number) + max_unit;
        }
        if (total % min_number != 0) {
          min_str = (total % min_number) + min_unit;
        }
      }
      return max_str + min_str;
    },
    startTimeChange(){

    },
    getPharmacyBaseDrug(drug_id){
      var params = {
        start_time:this.start_time,
        drug_id:drug_id,
      }
      console.log("param23232323322332",params)
      getPharmacyBaseDrug(params).then(response=>{
        if(response.data.state == 1){
           var advicelist = response.data.data.advicelist
           console.log("advicelist23323232323323",advicelist)
           for(let i=0;i<advicelist.length;i++){
             advicelist[i].type = 1
             advicelist[i].day = " "
             this.tableData.push(advicelist[i])
           }
           var hisAdviceList = response.data.data.hisAdviceList
           console.log("hisadvicelist23233232233",hisAdviceList)
           for(let i=0;i<hisAdviceList.length;i++){
             hisAdviceList[i].type = 2
             this.tableData.push(hisAdviceList[i])
           }
           var patient = response.data.data.patient
           console.log("patient23322323232332",patient)
           console.log("tabedaadddddddd",this.tableData)
           this.patientList = patient
        }
      })
    },
    getPatientName(id){
       var name = ""
       for(let i=0;i<this.patientList.length;i++){
          if(id == this.patientList[i].id){
             name = this.patientList[i].name
          }
       }
       return name
    },
    toSend(){
      var blood_ids = []
      var his_ids = []
      var bloodStr = ""
      var hisStr = ""
      console.log("table3223233232322323",this.tableData)
      for(let i=0;i<this.tableData.length;i++){
        if(this.tableData[i].type == 1){
          blood_ids.push(this.tableData[i].id)
        }
        if(this.tableData[i].type == 2){
          his_ids.push(this.tableData[i].id)
        }
      }

      console.log("blood_ids223我",blood_ids)
      console.log("his_ids22222我",his_ids)
      bloodStr = blood_ids.join(",")
      hisStr =  his_ids.join(",")
       var params = {
         bloodStr:bloodStr,
         hisStr:hisStr,
       }
      updatePharmacyBaseDrug(params).then(response=>{
         if(response.data.state == 1){
           var msg = response.data.data.msg
           var medical =  response.data.data.medical
           if(msg === 1){
             this.$message.success("发药成功!")
           }
           if(msg == 2 || msg ===3){
             this.$message.error("库存不足")
           }
         }
      })
    }
  },
  created() {

    let type = this.$store.getters.pagedata.pagestate
    if(type == ""){
      this.titleType = "明细"
    }else{
      this.titleType = type
    }
    const tableHeight = document.body.clientHeight - 290;
    this.tableHeight = tableHeight;
    
    this.getlist()
    
  },

  mounted() {}
};
</script>

<style lang="scss" scoped>
.new-main-contain {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.app-container {
  height: 100%;
}

.mainCell {
  height: 36px;
  display: flex;
  align-items: center;
}

.mainLeft {
  width: 200px;
  height: 100%;
  display: flex;
  flex-direction: column;

  .el-radio {
    margin-right: 5px;
  }
}
.mainRight {
  margin-left: 10px;
  flex: 1;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow-y: auto;

  .cellSpan {
    min-width: 80px;
    display: inline-block;
    margin-right: 10px;
  }
}
.mainCenter {
  display: flex;
  flex: 1;
}

.centerLeft {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;

  .el-form-item {
    width: 32%;
    margin-right: 1%;
    float: left;
  }

  .el-form-item__label {
    text-align: left;
  }
}
.backColor {
  background: #f6f8f9;
  height: 5px;
  margin-bottom: 5px;
}

.tabsBox {
  position: relative;
  height: 76%;
  overflow-y: auto;
  margin-bottom: 60px;

  .el-tabs__item {
    padding: 0 10px;
  }
}
.preTabs {
  height: 100%;
  display: flex;
  flex-direction: column;

  .el-tabs__content {
    flex: 1;
    overflow-y: auto;
  }
}

.costBox {
  width: 100%;
  height: 60px;
  background: #fff;
  position: absolute;
  bottom: 0;
  display: flex;
  align-items: center;
}

.addTab {
  position: absolute;
  right: 0;
  top: 14px;
  z-index: 20;
}

.centerRight {
  width: 300px;
  margin-left: 10px;
  display: flex;
  flex-direction: column;
  position: relative;
}

.rightTab {
  height: 40px;
  width: 100%;
  border: 1px solid #d2d2d2;
  box-sizing: border-box;

  p {
    width: 50%;
    height: 40px;
    line-height: 40px;
    text-align: center;
    background: #eee;
    float: left;
  }

  > p:last-child {
    border-left: 1px solid #d2d2d2;
    float: right;
  }

  .activeP {
    background: #409eff;
    color: #fff;
  }
}
.comfirmBox {
  width: 100%;
  height: 60px;
  background: #fff;
  position: absolute;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.mainHeader {
  width: 100%;
  background: #fff;
  position: fixed;
  z-index: 100;
  height: 36px;
}

.titleBox {
  position: fixed;
  z-index: 99;
  background: #fff;
}

.fixedCell {
  position: fixed;
  z-index: 99;
  right: 30px;
  background: #fff;
  width: 300px;
  display: flex;
  justify-content: space-between;
}
</style>

<style lang="scss">
.centerLeft {
  .el-form-item__label {
    text-align: left;
  }
}
.tabsBox {
  .el-tabs__item {
    padding: 0 10px;
  }

  .el-tabs--bottom
    .el-tabs--left
    > .el-tabs__header
    .el-tabs__item:nth-child(2),
  .el-tabs--bottom
    .el-tabs--right
    > .el-tabs__header
    .el-tabs__item:nth-child(2),
  .el-tabs--bottom.el-tabs--border-card
    > .el-tabs__header
    .el-tabs__item:nth-child(2),
  .el-tabs--bottom.el-tabs--card > .el-tabs__header .el-tabs__item:nth-child(2),
  .el-tabs--top .el-tabs--left > .el-tabs__header .el-tabs__item:nth-child(2),
  .el-tabs--top .el-tabs--right > .el-tabs__header .el-tabs__item:nth-child(2),
  .el-tabs--top.el-tabs--border-card
    > .el-tabs__header
    .el-tabs__item:nth-child(2),
  .el-tabs--top.el-tabs--card > .el-tabs__header .el-tabs__item:nth-child(2) {
    padding: 0 10px;
  }

  .el-tabs--bottom .el-tabs--left > .el-tabs__header .el-tabs__item:last-child,
  .el-tabs--bottom .el-tabs--right > .el-tabs__header .el-tabs__item:last-child,
  .el-tabs--bottom.el-tabs--border-card
    > .el-tabs__header
    .el-tabs__item:last-child,
  .el-tabs--bottom.el-tabs--card > .el-tabs__header .el-tabs__item:last-child,
  .el-tabs--top .el-tabs--left > .el-tabs__header .el-tabs__item:last-child,
  .el-tabs--top .el-tabs--right > .el-tabs__header .el-tabs__item:last-child,
  .el-tabs--top.el-tabs--border-card
    > .el-tabs__header
    .el-tabs__item:last-child,
  .el-tabs--top.el-tabs--card > .el-tabs__header .el-tabs__item:last-child {
    padding: 0 10px;
  }

  .el-tabs--card > .el-tabs__header .el-tabs__item.is-active.is-closable {
    padding: 0 10px;
  }
}
.centerRight {
  .el-tabs__nav-scroll {
    display: flex;
  }

  .el-tabs__nav {
    margin: 0 auto;
  }

  .el-table th .cell,
  .el-table td .cell {
    padding: 0 2px;
  }
}
.preTabs {
  .el-tabs__content {
  }
}
.rightTabs {
  height: 100%;

  .el-tabs__content {
  }
}
.centerDialog {
  .el-dialog__body {
    max-height: calc(100vh - 100px) !important;
    padding: 0 20px;
  }
}
</style>
