<template>
  <div>
    <template>
      <el-button
        style="position:fixed;right:25px;z-index:999"
        :loading="loading"
        size="small"
        icon="el-icon-printer"
        @click="printThisPage"
        type="primary"
      >打印
      </el-button
      >
    </template>

    <div class='dialysisPage' style="padding-top:40px;">
      <printOne v-if="org_id != 10106 && org_id != 10215  && org_id != 10265" :list="list" :patient="patient" :order="order" :admin="admin"></printOne>
      <printTwo v-if="org_id == 10106 && this.paramsObj.balance_accounts_type != 2" :info="info" :order_infos="order_infos" :p_admin="p_admin" :charge_admin="charge_admin"></printTwo>
      <print-three v-if="org_id == 10106 && this.paramsObj.balance_accounts_type == 2" :info="info" :order_infos="order_infos" :p_admin="p_admin" :charge_admin="charge_admin"></print-three>
      <list-print-two v-if="org_id == 10215" :list="list" :patient="patient" :order="order" :admin="admin"></list-print-two>
      <print-two10265  v-if="org_id == 10265" :list="list" :patient="patient" :order="order" :admin="admin" :hospital_record="hospital_record"></print-two10265>
    </div>
  </div>
</template>

<script>
  import printOne from './listTemplate/printOne'
  import printTwo from './listTemplate/printTwo'
  // import printThree from './listTemplate/printThree'
  import print from "print-js";

  import { getHisOrderDetail,getPrivateExpensesOrder } from '@/api/his/his'
  import { uParseTime } from '@/utils/tools'
  import axios from 'axios'
  import PrintThree from './listTemplate/printThree'
  import ListPrintTwo from './listTemplate/listPrintTwo'
  import PrintTwo10265 from "./listTemplate/printTwo10265";

  export default {
    name: 'listPrint',
    components: {
      PrintTwo10265,
      ListPrintTwo,
      PrintThree,
      printOne,
      printTwo
    },
    props: {
      paramsObj: Object
    },
    data() {
      return {
        list: [],
        patient:{},
        order:{},
        admin:{},
        order_infos:null,
        info:null,
        p_admin:{},
        charge_admin:{},
        org_id:'',
        hospital_record:null
      }
    },
    created() {
      this.getInfo(this.paramsObj.id)
      this.org_id = this.$store.getters.xt_user.org_id
      if(this.org_id == 10106){
        if(this.paramsObj.balance_accounts_type == 2){

          this.getInfoPrivateExpenses10106(this.paramsObj.id)

        }else{
          this.getInfo10106(this.paramsObj.id)



        }
      }

    },

    methods: {
      getTimes(time) {
        return uParseTime(time, '{y}-{m}-{d}')
      },
      getInfo(order_id) {
        let params = {
          id: order_id
        }
        getHisOrderDetail(params).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {
            if(this.org_id == 10265  ){
              this.order = response.data.data.order
              this.patient = response.data.data.patient
              this.admin = response.data.data.admin_info
              this.hospital_record = response.data.data.hospital_record


              //获取所有项目类型进行去重
              let med_chrgitm_types = []
              for (let i = 0; i < response.data.data.order_info.length; i++) {
                med_chrgitm_types.push(response.data.data.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 = {
                  details: [],
                  med_chrgitm_name:this.getType(med_chrgitm_types[i]),
                  jiaTotal:0,
                  total:0,
                  yiTotal:0,
                  noMedicineTotal:0,
                  bingTotal:0,
                  otherTotal:0,
                }

                let tempDetails = []
                for (let b = 0; b < response.data.data.order_info.length; b++) {
                  if (med_chrgitm_types[i] == response.data.data.order_info[b].med_chrgitm_type) {
                    tempDetails.push(response.data.data.order_info[b])
                  }
                }
                obj.total = this.getTotal(tempDetails)
                obj.jiaTotal = this.getJiaTotal(tempDetails)
                obj.yiTotal = this.getYiTotal(tempDetails)
                obj.noMedicineTotal = this.getNoMedicineTotal(tempDetails)
                obj.bingTotal = this.getBingTotal(tempDetails)
                obj.otherTotal = this.getOtherTotal(tempDetails)

                obj.details = this.setNewData(tempDetails)
                this.list.push(obj)
              }
              console.log(this.list)
            }else{
              this.order = response.data.data.order
              this.patient = response.data.data.patient
              console.log(this.patient)
              this.admin = response.data.data.admin_info
              for (let i = 0; i < response.data.data.order_info.length; i++) {
                var infos = response.data.data.order_info[i]
                let obj = {
                  med_chrgitm_type: this.getType(infos.med_chrgitm_type),
                  price: infos.pric.toFixed(4),
                  count: infos.cnt
                }
                if (infos.advice && infos.advice.id == 0 && infos.project && infos.project.id > 0) {
                  obj['p_time'] = this.getTimes(infos.project.prescription.ctime)
                  obj['feedetl_sn'] = infos.feedetl_sn

                  if(infos.project.type == 2){
                    obj['name'] = infos.project.project.project_name
                    obj['spec'] = ''
                    obj['unit'] =  infos.project.project.unit
                    obj['is_total']= 0


                  }else if (infos.project.type == 3){
                    obj['name'] = infos.project.good_info.good_name
                    obj['spec'] = ""
                    obj['unit'] = infos.project.good_info.packing_unit
                    obj['is_total']= 0

                  }

                }
                if (infos.advice && infos.advice.id > 0 && infos.project && infos.project.id == 0) {
                  obj['p_time'] = this.getTimes(infos.advice.prescription.ctime)
                  obj['name'] = infos.advice.drug.drug_name
                  obj['unit'] = infos.advice.drug.min_unit
                  obj['feedetl_sn'] =  infos.feedetl_sn
                  obj['spec'] =  infos.advice.drug.dose +  infos.advice.drug.dose_unit+"*" + infos.advice.drug.min_number +  infos.advice.drug.min_unit+"/"+infos.advice.drug.max_unit
                  obj['is_total']= 0

                  // v-if="scope.row.min_unit != scope.row.dose_unit">{{ scope.row.dose }}{{ scope.row.dose_unit }}&nbsp;* &nbsp;</span>{{ scope.row.min_number }}{{ scope.row.min_unit }}/{{ scope.row.max_unit }}


                }
                this.list.push(obj)
                this.list.sort(function(a, b) {
                  return b.p_time < a.p_time ? 1 : -1
                })
              }

              let newObj = {}
              newObj['name'] = "合计"
              newObj['price']= this.order.medfee_sumamt
              newObj['is_total']= 1
              this.list.push(newObj)

            }
          }
        })
      },getJiaTotal(items){
        console.log(items)
        let total = 0
        for(let i = 0; i < items.length; i++){
          if(items[i].chrgitm_lv == "01") {
            total = parseFloat(total) + (parseFloat(items[i].cnt) * parseFloat(items[i].pric))

          }
        }
        return total.toFixed(2)
      },getYiTotal(items){
        let total = 0
        for(let i = 0; i < items.length; i++){
          if(items[i].chrgitm_lv == "02") {
            total = parseFloat(total) + (parseFloat(items[i].cnt) * parseFloat(items[i].pric))

            // total = Number(total) + Number((parseFloat(items[i].count) * parseFloat(items[i].price)).toFixed(2))
          }
        }
        return total.toFixed(2)
      },getNoMedicineTotal(items){
        let total = 0
        for(let i = 0; i < items.length; i++){
          total = parseFloat(total) + (parseFloat(items[i].cnt) * parseFloat(items[i].pric))
        }
        return total.toFixed(2)

      },getBingTotal(items){

        let total = 0
        for(let i = 0; i < items.length; i++){
          if(items[i].chrgitm_lv == "03") {
            total = parseFloat(total) + (parseFloat(items[i].cnt) * parseFloat(items[i].pric))

            // total = Number(total) + Number((parseFloat(items[i].count) * parseFloat(items[i].price)).toFixed(2))
          }
        }
        return total.toFixed(2)
      },getOtherTotal(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)
      },getTotal:function(items){
        let total = 0
        for(let i = 0; i < items.length; i++){
          total = Number(total) + Number(parseFloat(items[i].cnt) * parseFloat(items[i].pric))
        }
        return total.toFixed(2)
      },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
            }
            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

            }
            project_ids.push(obj)

          }

        }
        //
        // console.log('old ids')
        // console.log(drug_ids)
        // console.log(project_ids)
        // console.log('old ids')

        let new_drug_ids = this.unique(drug_ids)
        let new_project_ids = this.unique(project_ids)
        let list = []

        // console.log('ids')
        // console.log(new_drug_ids)
        // console.log(new_project_ids)
        // console.log('ids')

        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.drug_spec
                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
                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'] = ''
                  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

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

        return list

      },
      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

        }

      },
      getMedicineInsuranceType(type) {
        switch (type) {
          case "01":
            return '甲类'
            break
          case "02":
            return '乙类'

            break
          case "03":
            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
      },
      printThisPage() {
        if(this.org_id != 10106){
          if(this.org_id == 10215 || this.org_id == 0){
            const style =
                '@media print {.allListTitle{font-size: 24px;text-align: center;font-weight: bold;margin-bottom: 10px;}.allListInfo{display: flex;font-size: 16px;justify-content: space-between;margin: 10px 0;} .allListTable{width: 100%;text-align: center;border-collapse: collapse;line-height: 20px;font-size: 14px;border-color: #000;text-align:left;}.allListTable tr td {padding: 0 5px;}.tableBottom{font-size: 16px;display: flex;margin-top: 20px;}.tableBottomOne{margin-right: 40px;}}'
            printJS({
              printable: 'allList-print',
              type: 'html',
              style: style,
              scanStyles: false
            })


          }else {
            const style =
                '@media print {.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: 20px;font-size: 14px;border-color: #000;text-align:left;}.listTable tr td {padding: 0 5px;}.tableBottom{font-size: 16px;display: flex;margin-top: 10px;}.tableBottomOne{margin-right: 40px;}}'
            printJS({
              printable: 'list-print',
              type: 'html',
              style: style,
              scanStyles: false
            })
          }
        }else{
          const style =
          '@media print {#statement-print{font-size:12px;width:800px;margin:0 auto;}.statementTitle{font-size: 22px;text-align: center;margin-bottom: 10px;}.statementTable{width: 100%;text-align: center;border-collapse: collapse;line-height: 40px;font-size: 16px;border-color: #000;}}';
          printJS({
            printable: "statement-print",
            type: "html",
            style: style,
            scanStyles: false
          });
        }


        // if (this.org_template_info.template_id == 1) {
        //   printJS({
        //     printable: "dialysis-print-box",
        //     type: "html",
        //     style: style,
        //     scanStyles: false
        //   });
        // }
      },getInfoPrivateExpenses10106(order_id){
        let params = {
          order_id: order_id,
          admin_user_id:this.$store.getters.xt_user.user.id
        }

        getPrivateExpensesOrder(params).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {
            this.info = response.data.data.info
            this.info['org_name'] = response.data.data.org_name
            this.info['date'] = response.data.data.date
            this.info['number'] = response.data.data.number
            this.info['name'] = response.data.data.name
            this.p_admin = response.data.data.printor_admin
            this.charge_admin = response.data.data.charge_admin
            this.order_infos = response.data.data
          }
        })

      }, getGoodUnit(id) {
        var goodUnit = this.$store.getters.good_unit
        for (let i = 0; i < goodUnit.length; i++) {
          if (goodUnit[i].id == id) {
            return goodUnit[i].name
          }
        }
      },
      getInfo10106(order_id) {
        if (this.$store.getters.xt_user.org_id == 3877 || this.$store.getters.xt_user.org_id == 9919 || this.$store.getters.xt_user.org_id == 4  || this.$store.getters.xt_user.org_id == 9538 || this.$store.getters.xt_user.org_id == 9671 ||  this.$store.getters.xt_user.org_id == 9674 || this.$store.getters.xt_user.org_id == 10106 || this.$store.getters.xt_user.org_id == 9990 ) {

          var that = this

          axios.get('http://127.0.0.1:9532/api/settle/query', {
            params: {
              order_id: order_id,
              admin_user_id:that.$store.getters.xt_user.user.id
            }
          })
            .then(function (response) {
              if (response.data.state == 0) {
                this.$message.error(response.data.msg)
                return false
              } else {
                console.log("logloglog")

                console.log(response.data.data.info)
                that.info = response.data.data.info
                that.p_admin = response.data.data.printor_admin
                that.charge_admin = response.data.data.charge_admin
                that.order_infos = response.data.data


                console.log(that.info)

                that.info['bed_cost_total'] = response.data.data.bedCostTotal
                that.info['bed_cost_self_total'] = response.data.data.bedCostSelfTotal
                that.info['bed_cost_part_self_total'] = response.data.data.bedCostPartSelfTotal

                that.info['operation_cost_total'] = response.data.data.operationCostTotal
                that.info['operation_cost_self_total'] = response.data.data.operationCostSelfTotal
                that.info['operation_cost_part_self_total'] = response.data.data.operationCostPartSelfTotal

                that.info['other_cost_total'] = response.data.data.otherCostTotal
                that.info['other_cost_self_total'] = response.data.data.otherCostSelfTotal
                that.info['other_cost_part_self_total'] = response.data.data.otherCostPartSelfTotal

                that.info['material_cost_total'] = response.data.data.materialCostTotal
                that.info['material_cost_self_total'] = response.data.data.materialCostSelfTotal
                that.info['material_cost_part_self_total'] = response.data.data.materialCostPartSelfTotal

                that.info['western_medicine_cost_total'] = response.data.data.westernMedicineCostTotal
                that.info['western_medicine_cost_self_total'] = response.data.data.westernMedicineCostSelfTotal
                that.info['western_medicine_cost_part_self_total'] = response.data.data.westernMedicineCostPartSelfTotal

                that.info['chinese_traditional_medicine_cost_total'] = response.data.data.chineseTraditionalMedicineCostTotal
                that.info['chinese_traditional_medicine_cost_self_total'] = response.data.data.chineseTraditionalMedicineCostSelfTotal
                that.info['chinese_traditional_medicine_cost_part_self_total'] = response.data.data.chineseTraditionalMedicineCostPartSelfTotal


                that.info['check_cost_total'] = response.data.data.checkCostTotal
                that.info['check_cost_self_total'] = response.data.data.checkCostSelfTotal
                that.info['check_cost_part_self_total'] = response.data.data.checkCostPartSelfTotal


                that.info['laboratory_cost_total'] = response.data.data.laboratoryCostTotal
                that.info['laboratory_cost_self_total'] = response.data.data.laboratoryCostSelfTotal
                that.info['laboratory_cost_part_self_total'] = response.data.data.laboratoryCostPartSelfTotal

                that.info['treat_cost_total'] = response.data.data.treatCostTotal
                that.info['treat_cost_self_total'] = response.data.data.treatCostSelfTotal
                that.info['treat_cost_part_self_total'] = response.data.data.treatCostPartSelfTotal
                console.log(this.info)
              }
            })
            .catch(function (error) {

            });
        }
      }
    }

  }
</script>

<style>
  .dialysis-print-order {
    width: 960px;
    margin: 0 auto;
  }

  .dialysis-print-order .order-yy-name {
    margin: auto;
    text-align: center;
    font-size: 20px;
    letter-spacing: 5px;
  }

  .dialysis-print-order .order-title {
    margin: auto;
    font-weight: 600;
    text-align: center;
    font-size: 22px;
    padding: 10px;
  }

  .dialysis-print-order .table-box {
    width: 100%;
    line-height: 23px;
    font-size: 14px;
  }

  .dialysis-print-order .print-table {
    width: 100%;
    text-align: center;
    border-collapse: collapse;
    line-height: 40px;
    font-size: 14px;
    border-color: #000;
  }

  .dialysis-print-order .print-table-no {
    width: 100%;
    text-align: center;
    border-collapse: collapse;
    font-size: 14px;
  }

  .dialysis-print-order .under-line {
    border-bottom: 1px solid #999;
    width: 95%;
    text-align: center;
    margin-left: 2px;
  }

  .dialysis-print-order .title-box {
    text-align: center;
    font-size: 16px;
  }

  .dialysis-print-order .radio-lebel-box {
    font-weight: 400;
    cursor: pointer;
  }

  .dialysis-print-order .radio-no {
    opacity: 0;
    outline: none;
    position: absolute;
    margin: 0;
    width: 0;
    height: 0;
    z-index: -1;
  }

  .dialysis-print-order .radio-inner {
    white-space: nowrap;
    cursor: pointer;
    outline: none;
    display: inline-block;
    line-height: 1;
    position: relative;
    vertical-align: middle;
  }

  .dialysis-print-order .radio-fang {
    display: inline-block;
    position: relative;
    border: 1px solid #000;
    box-sizing: border-box;
    width: 14px;
    height: 14px;
    background-color: #fff;
    z-index: 1;
    transition: border-color 0.25s cubic-bezier(0.71, -0.46, 0.29, 1.46),
    background-color 0.25s cubic-bezier(0.71, -0.46, 0.29, 1.46);
  }

  .dialysis-print-order .is-checked-radio::after {
    content: "√";
    font-size: 15px;
  }

  .dialysis-print-order .print-table-no tr td {
    padding: 8px 5px;
    line-height: 25px;
  }

  .dialysis-print-order .print-table tr td {
    padding: 1px 1px;
    /*line-height: 25px;*/
  }

  .es-img {
    height: 30px;
  }

  .advice-name {
    text-align: left;
  }

  .advice-children {
    display: flex;
  }

  .title-box-pro {
    border: 0 #fff;
    line-height: 25px;
    height: 25px;
    text-align: left;
    padding-left: 10px !important;
  }

  .title-box-pro-tr {
    border: 0 #fff;
  }

  .text-align-left {
    text-align: left !important;
    padding-left: 10px !important;
    font-size: 14px !important;
    line-height: 25px;
  }

  .print-table-tr-new td {
    line-height: 20px !important;
  }

  .border-top-solid {
    border: solid 1px #000;
  }

  .print-template-two tr {
    line-height: 30px;
  }

  .table-box1 {
    border: 1px solid #000;
    width: 100%;
    line-height: 30px;
    font-size: 14px;
    border-collapse: collapse;
  }

  .table-box1 tr {
    border-bottom: 1px solid #000;
  }
</style>

<style lang="scss">

  .newContainer {

  .dialysisPage::-webkit-scrollbar {
    height: 15px;
  }

  .el-date-editor {

  .el-input__inner {
    padding-right: 0px;
  }

  }
  .el-table td, .el-table th {
    text-align: center;
  }

  }
  .newContainer::-webkit-scrollbar {
    height: 15px !important;
  }
</style>
