<template>
    <div>
      <div class="print_btn">
      <el-button type="primary" @click="printtpage">打印</el-button>
    </div>
        <div id="list-print" class="list-print">
        <div class="listTitle">{{$store.getters.xt_user.org.org_name}}费用清单</div>
         <div class="listInfo" style="border-bottom: 2px solid;margin-bottom: 20px;padding-bottom: 10px;">
            <div>医疗机构名称：{{$store.getters.xt_user.org.org_name}}</div>
        </div>
        <div class="listInfo">
            <div>科别：{{getDepartment(his_patient.departments)}}</div>
            <div>床号：</div>
            <div>门诊号：{{his_patient.number}}</div>
            <div>姓名：{{patient.name}}</div>
        </div>
         <div class="listInfo" style="border-bottom: 2px solid;margin-bottom: 20px;padding-bottom: 20px;">
            <div>预交款：</div>
            <div>总费用：{{getAllPice()}}</div>
            <div>门诊日期：</div>
        </div>
        <div class="listInfo">
            <div>西药：</div>
            <div>中成药：{{chineseTraditionalMedicineCostTotal}}</div>
            <div>中草药：</div>
        </div>
        <div class="listInfo">
            <div>检查费：{{checkCostTotal}}</div>
            <div>输氧费：</div>
            <div>手术费：</div>
        </div>
        <div class="listInfo">
            <div>化验费：{{laboratoryCostTotal}}</div>
            <div>输血费：</div>
            <div>诊断费：{{zhenChaCostTotal}}</div>
        </div>
         <div class="listInfo">
            <div>治疗费：</div>
            <div>护理费：</div>
            <div>床位费：{{bedCostTotal}}</div>
        </div>
        <div class="listInfo" style="margin-bottom: 20px;">
            <div>麻醉费：</div>
            <div>材料费：{{materialCostTotal}}</div>
            <div>其他：{{otherCostTotal}}</div>
        </div>
        <table class="listTable" border="1">
            <tr>
                <td style="width:14%;text-align:center" >费用分类</td>
                <td style="width:22%;text-align:center">项目名称</td>
                <td style="width:15%;text-align:center">规格</td>
                <td style="width:9%;text-align:center">单价</td>
                <td style="width:10%;text-align:center" >数量</td>
                <td style="width:10%;text-align:center">金额</td>
                <td style="width:10%;text-align:center" >医保类型</td>
            </tr>
            <tr v-for="(item, index) in list" :key="index">
                <td style="width:19%;text-align:center" >
                 {{ item.med_chrgitm_type }}
                </td>
                <td style="width:10%;text-align:center" >
                  <span>{{item.name}}</span>  
                </td>
                <td style="width:31%;text-align:center" >
                  {{ item.spec }}
                </td>
                <td style="width:14%;text-align:center" >
                  {{item.price}}  
                </td>
                <td style="width:10%;text-align:center" >
                   {{item.count }}{{item.unit }}
                </td>
                <td style="width:6%;text-align:center" >
                    <span v-if="item.is_total == 1">{{item.total}}</span>  
                    <span v-if="item.is_total == 2">{{(item.price * item.count).toFixed(2) }}</span>  
                </td>
                <td style="width:10%;text-align:center" >
                  <span v-if="item.chrgitm_lv== '01'">甲类</span>
                  <span v-if="item.chrgitm_lv== '02'">乙类</span>
                  <span v-if="item.chrgitm_lv== '03'">自费</span>
                </td>
            </tr>
        </table>
        </div>
    </div>
</template>

<script>
  import print from "print-js";
  import { uParseTime } from '@/utils/tools'
  import { getGatherDetailList } from '@/api/his/his_tools'
  export default {
    data(){
        return{
          patient:{},
          tableData:[],
          list:[],
          order:[],
          his_patient:{},
          hisDepatment:[],
          chineseTraditionalMedicineCostTotal:"",//中成药
          checkCostTotal:"",//检查费
          materialCostTotal:"",//材料费
          laboratoryCostTotal:"",//化验费
          bedCostTotal:"",//床位费,
          operationCostTotal:"",//材料费
          zhenChaCostTotal:"",//诊查费
          otherCostTotal:"",//其他费用
        }
    },
    methods:{
      printtpage() {
      const style =
        '@media print {.list-print{width:960px;margin:0 auto} .listTitle {font-size: 24px;text-align: center;font-weight: bold;margin-bottom: 10px;}.listInfo {display: flex;font-size: 16px;justify-content: space-between;margin: 10px 0;}.listTable {width: 100%;text-align: center;border-collapse: collapse;line-height: 25px;font-size: 14px;border-color: #000;text-align: left;} .listTable tr td {padding: 0 5px;}.tableBottom {font-size: 16px;display: flex;margin-top: 20px;}.tableBottomOne {margin-right: 40px;}';
      printJS({
        printable: "list-print",
        type: "html",
        style: style,
        scanStyles: false,
      });
    },

      getNowTime: function () {
        let dateTime;
        let yy = new Date().getFullYear();
        let mm = new Date().getMonth() + 1;
        let dd = new Date().getDate();
        let hh = new Date().getHours();
        let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes()
          :
          new Date().getMinutes();
        let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds()
          :
          new Date().getSeconds();
        dateTime = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss;
        return dateTime
      },
      getTimes(time) {
        return uParseTime(time, '{y}-{m}-{d}')
      },
    getGatherDetailList() {
      let start_time = this.$route.query.start_time
      let end_time = this.$route.query.end_time
      let params = {
        patient_id:this.$route.query.patient_id,
        start_time: start_time,
        end_time: end_time,
        type: this.$route.query.type,
        keyword: this.$route.query.keyword,
      }
     getGatherDetailList(params).then(response=>{
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {
            this.order = response.data.data.order
          
            this.patient = response.data.data.patient
            this.admin = response.data.data.admin_info
            this.his_hospital = response.data.data.his_hospital
            var order_info = response.data.data.order_info
            this.his_patient = response.data.data.his_patient
            this.hisDepatment = response.data.data.hisDepatment
            console.log("order_info2332332232332232323",order_info)
            if(this.$store.getters.xt_user.org_id == 10215 || this.$store.getters.xt_user.org_id == 0){
              //获取所有项目类型进行去重
              let med_chrgitm_types = []
              for (let i = 0; i < order_info.length; i++) {
                med_chrgitm_types.push(order_info[i].med_chrgitm_type)
              }
              const obj = {}
              med_chrgitm_types = med_chrgitm_types.reduce((cur, next) => {
                obj[next] ? '' : obj[next] = true && cur.push(next)
                return cur
              }, []) // 设置cur默认类型为数组，并且初始值为空的数组

              let tempOrderInfo = []

              for (let i = 0; i < med_chrgitm_types.length; i++) {
                let obj = {
                  total: 0,
                  details: [],
                  is_total:0,
                }
                let tempDetails = []
                for (let b = 0; b < order_info.length; b++) {
                  if (med_chrgitm_types[i] == order_info[b].med_chrgitm_type) {
                    tempDetails.push(order_info[b])
                  }
                }
                obj.details = this.setNewData(tempDetails)
               
                this.list =  this.list.concat(obj.details)
                for(let i=0;i<this.list.length;i++){
                  this.list[i].index = i
                }
              }
              let newobj = {}
              newobj['total'] = this.order.medfee_sumamt
              newobj['is_total'] = 1
              
              this.list.push(newobj)

              var new_arr = []
              if(this.$route.query.keyword!=""){
       
                   for(let i=0;i<this.list.length;i++){
                     if(this.list[i].is_total == 2){
                        if(this.list[i].name.indexOf(this.$route.query.keyword)>-1){
                           new_arr.push(this.list[i])
                       }
                     }
                   
                  }
                  var new_arr_two = []
                  for(let i=0;i<tempDetails.length;i++){
                    if(tempDetails[i].advice_id >0){
                       tempDetails[i].name = tempDetails[i].advice.advice_name
                    }
                    if(tempDetails[i].project_id > 0){
                       if(tempDetails[i].type == 2 ){
                         tempDetails[i].name = tempDetails[i].project.project_name
                       }
                       if(tempDetails[i].type == 3){
                         tempDetails[i].name = tempDetails[i].good_info.good_name
                       }
                    }
                    if(tempDetails[i].name.indexOf(this.$route.query.keyword) > -1){
                       new_arr_two.push(tempDetails[i])
                    }
                  }
                   this.list = []
                
                   
                   this.list = new_arr
                    obj.details = []
                    obj.details = this.setNewData(new_arr_two)

                    obj.total = this.getTotal(obj.details)
                    obj.details.push({
                      total: obj.total,
                      is_total: 1,
                    })
                    for(let i=0;i<obj.details.length;i++){
                       if(obj.details[i].is_total == 1){
                          obj.details[i].index = "合计"
                       }else{
                         obj.details[i].index = i+1
                       }
                    }
                 
                    this.list = obj.details
                    this.chineseTraditionalMedicineCostTotal = response.data.data.chineseTraditionalMedicineCostTotal
                    this.checkCostTotal = response.data.data.checkCostTotal
                    this.materialCostTotal = response.data.data.materialCostTotal
                    this.laboratoryCostTotal = response.data.data.laboratoryCostTotal
                    this.bedCostTotal = response.data.data.bedCostTotal
                    this.operationCostTotal = response.data.data.operationCostTotal
                    this.zhenChaCostTotal = response.data.data.zhenChaCostTotal
                    this.otherCostTotal = response.data.data.otherCostTotal

                }
            }else{
              //获取所有项目类型进行去重
              let med_chrgitm_types = []
              for (let i = 0; i < order_info.length; i++) {
                med_chrgitm_types.push(order_info[i].med_chrgitm_type)
              }
             
              const obj = {}
              med_chrgitm_types = med_chrgitm_types.reduce((cur, next) => {
                obj[next] ? '' : obj[next] = true && cur.push(next)
                return cur
              }, []) // 设置cur默认类型为数组，并且初始值为空的数组
              let tempOrderInfo = []
              for (let i = 0; i < med_chrgitm_types.length; i++) {
                let obj = {
                  total: 0,
                  details: []
                }
                let tempDetails = []
                for (let b = 0; b < order_info.length; b++) {
                  if (med_chrgitm_types[i] == order_info[b].med_chrgitm_type) {
                    tempDetails.push(order_info[b])
                  }
                }
                
                obj.details = this.setNewData(tempDetails)

                obj.total = this.getTotal(obj.details)
                obj.details.push({
                  total: obj.total,
                  is_total: 1,
                })
               
                this.list =  this.list.concat(obj.details)
              
                for(let i=0;i<this.list.length;i++){
                  this.list[i].index = i+1
                }
              var new_arr = []
              if(this.$route.query.keyword!=""){
                console.log("锦鲤3332232332233223322332",this.list)
                for(let i=0;i<this.list.length;i++){
                     if(this.list[i].is_total == 2){
                        if(this.list[i].name.indexOf(this.$route.query.keyword)>-1){
                           new_arr.push(this.list[i])
                       }
                     }
                   
                  }
                  var new_arr_two = []
                  for(let i=0;i<tempDetails.length;i++){
                    if(tempDetails[i].advice_id >0){
                       tempDetails[i].name = tempDetails[i].advice.advice_name
                    }
                    if(tempDetails[i].project_id > 0){
                       if(tempDetails[i].type == 2 ){
                         tempDetails[i].name = tempDetails[i].project.project_name
                       }
                       if(tempDetails[i].type == 3){
                         tempDetails[i].name = tempDetails[i].good_info.good_name
                       }
                    }
                    if(tempDetails[i].name.indexOf(this.$route.query.keyword) > -1){
                       new_arr_two.push(tempDetails[i])
                    }
                  }
                  this.list = []
                  console.log("new_arr2323232333333232",new_arr)
                   
                   this.list = new_arr
                    obj.details = []
                    obj.details = this.setNewData(new_arr_two)

                    obj.total = this.getTotal(obj.details)
                    obj.details.push({
                      total: obj.total,
                      is_total: 1,
                    })
                    for(let i=0;i<obj.details.length;i++){
                       if(obj.details[i].is_total == 1){
                          obj.details[i].index = "合计"
                       }else{
                         obj.details[i].index = i+1
                       }
                    }
                    console.log("obj23323232323232323232",obj.details)
                    this.list = obj.details
              }
                this.chineseTraditionalMedicineCostTotal = response.data.data.chineseTraditionalMedicineCostTotal
                this.checkCostTotal = response.data.data.checkCostTotal
                this.materialCostTotal = response.data.data.materialCostTotal
                this.laboratoryCostTotal = response.data.data.laboratoryCostTotal
                this.bedCostTotal = response.data.data.bedCostTotal
                this.operationCostTotal = response.data.data.operationCostTotal
                this.zhenChaCostTotal = response.data.data.zhenChaCostTotal
                this.otherCostTotal = response.data.data.otherCostTotal

              
             }
          }
        }
     })
    },
     getAdviceName(id){
      var drug_name = ""
      for(let i= 0;i<this.tableData.length;i++){
         if(id == this.tableData[i].advice_id){
           drug_name = this.tableData[i].advice.advice_name
         }
      }
      return drug_name
    },
    getProjectName(id){
     var project_name = ""
     for(let i=0;i<this.tableData.length;i++){
       if(id == this.tableData[i].project_id){
         project_name = this.tableData[i].project.project.project_name
       }
     }
     return project_name
    },
    setNewData(details) {
        let drug_ids = []
        let project_ids = []

        for (let i = 0; i < details.length; i++) {
          if (details[i].advice && details[i].advice.id > 0 && details[i].advice.prescription && details[i].advice.prescription.type == 1) { //药品
            let obj = {
              id: details[i].advice.drug_id,
              price: details[i].advice.price,
              record_date:details[i].advice.advice_date
              
            }
            drug_ids.push(obj)
          } else if (details[i].project && details[i].project.id > 0 && details[i].project.prescription && details[i].project.prescription.type == 2) { //项目
            let obj = {
              id: details[i].project.project_id,
              price: details[i].project.price,
              record_date:details[i].advice.record_date
            }
            project_ids.push(obj)

          }

        }
      
        let new_drug_ids = this.unique(drug_ids)
        let new_project_ids = this.unique(project_ids)
       
        let list = []
        if (new_drug_ids.length > 0 && new_project_ids.length == 0) {
          for (let i = 0; i < new_drug_ids.length; i++) {
            let obj = {}
            let count = 0
            for (let a = 0; a < details.length; a++) {
              if (new_drug_ids[i].id == details[a].advice.drug_id && new_drug_ids[i].price == details[a].advice.price) {
                obj['name'] = details[a].advice.advice_name
                obj['spec'] =   details[a].advice.drug.dose +   details[a].advice.drug.dose_unit+"*" +  details[a].advice.drug.min_number +   details[a].advice.drug.min_unit+"/"+ details[a].advice.drug.max_unit
                obj['unit'] = details[a].advice.drug.min_unit
                obj['medicine_insurance_kind'] = this.getMedicineInsuranceType(details[a].chrgitm_lv)
                obj['med_chrgitm_type'] = this.getType(details[a].med_chrgitm_type)
                obj['price'] = parseFloat(details[a].pric)
                obj['is_total'] = 2
                obj['record_date'] = details[a].advice.record_date
                obj['chrgitm_lv'] = details[a].chrgitm_lv
                count = count + details[a].cnt
              }
            }
            obj['count'] = count
            list.push(obj)

          }
        }
        if (new_drug_ids.length == 0 && new_project_ids.length > 0) {
          for (let i = 0; i < new_project_ids.length; i++) {
            let obj = {}
            let count = 0
            for (let a = 0; a < details.length; a++) {
              if (new_project_ids[i].id == details[a].project.project_id && new_project_ids[i].price == details[a].project.price) {
               if( details[a].project.type == 2){
                 obj['name'] = details[a].project.project.project_name
                 obj['spec'] = details[a].project.project.project_name
                 obj['unit'] = details[a].project.project.unit
                 obj['record_date'] = details[a].project.record_date

               }else if(details[a].project.type == 3){
                 obj['name'] = details[a].project.good_info.good_name
                 obj['spec'] =  details[a].project.good_info.specification_name
                 obj['unit'] =  details[a].project.good_info.packing_unit
                 obj['record_date'] = details[a].project.record_date

               }
                obj['medicine_insurance_kind'] = this.getMedicineInsuranceType(details[a].chrgitm_lv)
                obj['med_chrgitm_type'] = this.getType(details[a].med_chrgitm_type)
                obj['price'] = parseFloat(details[a].pric)
                obj['is_total'] = 2
                obj['chrgitm_lv'] = details[a].chrgitm_lv

                count = count + details[a].cnt
              }
            }
            obj['count'] = count
            list.push(obj)
          
          }
        }

         if (new_drug_ids.length > 0 && new_project_ids.length > 0) {
           for (let i = 0; i < new_drug_ids.length; i++) {
            let obj = {}
            let count = 0
            for (let a = 0; a < details.length; a++) {
              if (new_drug_ids[i].id == details[a].advice.drug_id && new_drug_ids[i].price == details[a].advice.price) {
                obj['name'] = details[a].advice.advice_name
                obj['spec'] =   details[a].advice.drug.dose +   details[a].advice.drug.dose_unit+"*" +  details[a].advice.drug.min_number +   details[a].advice.drug.min_unit+"/"+ details[a].advice.drug.max_unit
                obj['unit'] = details[a].advice.drug.min_unit
                obj['medicine_insurance_kind'] = this.getMedicineInsuranceType(details[a].chrgitm_lv)
                obj['med_chrgitm_type'] = this.getType(details[a].med_chrgitm_type)
                obj['price'] = parseFloat(details[a].pric)
                obj['is_total'] = 2
                obj['record_date'] = details[a].advice.record_date
                obj['chrgitm_lv'] = details[a].chrgitm_lv
                count = count + details[a].cnt
              }
            }
            obj['count'] = count
            list.push(obj)

          }
         
          for (let i = 0; i < new_project_ids.length; i++) {
            let obj = {}
            let count = 0
            for (let a = 0; a < details.length; a++) {
             if (new_project_ids[i].id == details[a].project.project_id && new_project_ids[i].price == details[a].project.price) {
              if( details[a].project.type == 2){
                 obj['name'] = details[a].project.project.project_name
                 obj['spec'] = ''
                 obj['unit'] = details[a].project.project.unit

               }else if(details[a].project.type == 3){
                 obj['name'] = details[a].project.good_info.good_name
                 obj['spec'] = ''
                 obj['unit'] = details[a].project.good_info.packing_unit
               }
                obj['medicine_insurance_kind'] = this.getMedicineInsuranceType(details[a].chrgitm_lv)
                obj['med_chrgitm_type'] = this.getType(details[a].med_chrgitm_type)
                obj['price'] = parseFloat(details[a].pric)
                obj['is_total'] = 2
                obj['record_date'] = details[a].project.record_date
                 obj['chrgitm_lv'] = details[a].chrgitm_lv
                count = count + details[a].cnt
              }
            }
            obj['count'] = count
            list.push(obj)
          
          }
         }
         console.log("list232332322323332233223232323",list)
        return list
      }, 
    
      getMedicineInsuranceType(type) {
        switch (type) {
          case "01":
            return '甲类'
            break
          case "02":
            return '乙类'

            break
          case "03":
            return '自费'
            break

        }

      },
       getType(med_chrgitm_type){
        switch (med_chrgitm_type) {
          case '01':
            return '床位费'
            break
          case '02':
            return '诊察费'

            break
          case '03':
            return '检查费'

            break
          case '04':
            return '化验费'
            break
          case '05':
            return '治疗费'

            break
          case '06':
            return '手术费'

            break
          case '07':
            return '护理费'

            break
          case '08':
            return '材料费'

            break
          case '09':
            return '西药费'

            break
          case '10':
            return '中药饮片费'

            break
          case '11':
            return '中成药费'

            break
          case '12':
            return '一般诊疗费'

            break
          case '13':
            return '挂号费'

            break
          case '14':
            return '其他费'

            break

        }
      },
      unique(array) {
        // res用来存储结果
        var res = []
        for (var i = 0, arrayLen = array.length; i < arrayLen; i++) {
          for (var j = 0, resLen = res.length; j < resLen; j++) {
            if (array[i].id === res[j].id && array[i].price === res[j].price) {
              break
            }
          }
          // 如果array[i]是唯一的，那么执行完循环，j等于resLen
          if (j === resLen) {
            res.push(array[i])
          }
        }
        return res
      },
      getTotal:function(items){
        let total = 0
        for(let i = 0; i < items.length; i++){
          total = Number(total) + Number((parseFloat(items[i].count) * parseFloat(items[i].price)).toFixed(2))
        }
        return total.toFixed(2)
      },
      getTimes(time) {
       if(time < 0){
         return ""
       }else{
        return uParseTime(time, '{y}-{m}-{d}')
       }
     },
    getDepartment(id){
      var name = ""
      for(let i=0;i<this.hisDepatment.length;i++){
        if(id == this.hisDepatment[i].id){
           name = this.hisDepatment[i].name
        }
      }
      return name
    },
    getAllPice(){
      console.log("liust23322332323223",this.list)
      var total_price = 0
      for(let i=0;i<this.list.length;i++){
        if(this.list[i].is_total ==2){
          total_price += (this.list[i].price * parseInt(this.list[i].count))
        }
      }
      if(total_price > 0){
        return total_price.toFixed(2)
      }else{
        return total_price
      }
     
    }
    },
    created(){
      this.getGatherDetailList()   
    }

  }


</script>

<style lang="scss" scoped>
.list-print{
    -webkit-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.27), 0 0 60px rgba(0, 0, 0, 0.06) inset;
    -moz-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.27), 0 0 40px rgba(0, 0, 0, 0.06) inset;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.27), 0 0 40px rgba(0, 0, 0, 0.06) inset;
    margin-bottom: 20px;
    padding:20px 10px;
}
.listTitle{
    font-size: 24px;
    text-align: center;
    font-weight: bold;
    margin-bottom: 10px;
}
.listInfo{
    display: flex;
    font-size: 16px;
    justify-content: space-between;
    margin: 10px 0;
}
.listTable{
    width: 100%;
    text-align: center;
    border-collapse: collapse;
    line-height: 40px;
    font-size: 14px;
    border-color: #000;
    text-align: left;
}
.listTable tr td {
    padding: 0 5px;
}
.tableBottom{
    font-size: 16px;
    display: flex;
    margin-top: 20px;
}
.tableBottomOne{
    margin-right: 40px;
}
.print_btn {
  display: flex;
  justify-content: flex-end;
}
</style>
