<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;" v-if="this.$store.getters.xt_user.org_id == 10215">
      <private-charge-print :info="info" v-if="this.paramsObj.balance_accounts_type == 2"></private-charge-print>
      <printOne :info="info" v-else></printOne>
    </div>
    <div class='dialysisPage' style="padding-top:40px;" v-if="this.$store.getters.xt_user.org_id == 10265">
<!--      <private-charge-print :info="info" v-if="this.paramsObj.balance_accounts_type == 2"></private-charge-print>-->
<!--      <printOne :info="info" v-else></printOne>-->
<!--      <print-one :info="info" ></print-one>-->
      <hospital-settle-print :info="info" :list="list"></hospital-settle-print>
    </div>

<!--    <div class='dialysisPage' style="padding-top:40px;" v-else>-->
<!--      <private-charge-print :info="info" v-if="this.paramsObj.balance_accounts_type == 2"></private-charge-print>-->
<!--      <printOne :info="info" v-else></printOne>-->
<!--    </div>-->

  </div>
</template>

<script>


import { uParseTime } from '@/utils/tools'
import axios from 'axios'
import { getPrivateExpensesOrder } from '@/api/his/his'

import BreadCrumb from '@/xt_pages/components/bread-crumb'
import printOne from './statementTemplate/printOne'
// import printOne from './settlementTemplate/hospitalPrintOne'
import printTwo from './statementTemplate/printTwo'
import { getAllDoctorList, getAllHisPatientList } from '@/api/project/project'
import PrivateChargePrint from '../outpatientCharges/statementTemplate/privateChargePrint'
import HospitalSettlePrint from './settlementTemplate/hospitalSettlePrint'

export default {
  name: 'dialysisPrintOrder',
  components: {
    HospitalSettlePrint,
    PrivateChargePrint,
    BreadCrumb,
    printOne,
    printTwo
  },
  props: {
    paramsObj: Object
  },
  data() {
    return {
      crumbs: [
        { path: false, name: '门诊医生站' },
        { path: false, name: '打印' }
      ],
      record_date: '',
      patientTableData: [],
      advicePrint: [],
      hisPatient: {},
      patient: {},
      search_input: '',
      loading: false,
      patient_id: 0,
      prescription_id: 0,
      ids: '',
      info: null,
      org_id: '',
      list:[],
    }
  },
  methods: {
    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

      }


    },

    printThisPage() {
      var ptime = Math.round(new Date().getTime() / 1000)
      this.print_time = uParseTime(ptime, '{y}-{m}-{d} {h}:{i}')

      if(this.org_id == 10265){

        const style =
            '@media print {.printTitle{font-size: 22px;text-align: center;font-weight: bold;}.infoMain{display: flex;flex-wrap: wrap;padding:0 10px;margin-top:10px;}.infoMain .infoP{width: 33%;}.chargeBox{border: 1px solid #000;}.chargeUl{display:flex;justify-content: space-between;text-align: center;}.chargeUl p{line-height: 1px;}.chargeP{line-height: 1px;padding-bottom:16px;}.moneyBox{display: flex;justify-content: space-between;padding: 0 10px;background: #eeeeee;-webkit-print-color-adjust:exact;-moz-print-color-adjust:exact;-ms-print-color-adjust:exact;print-color-adjust:exact;height: 40px;align-items: center;border:1px solid #000; border-top:none}.actionBar{display: flex;justify-content: space-between; line-height: 24px;padding:0 10px;}.actionBar div{width:150px;}}';
        const style1 = "@media print{#prescription-print{font-size:14px;}.title{text-align: center;font-size: 22px;font-weight: bold;}.row{display: flex;justify-content: space-between;}.under_line{border-bottom: 1px solid #999;width: 95%;text-align: center;margin-left: 2px;display: inline-block;}.inline_block{display: inline-block;}.print_page_main_content .proj_table {width: 100%;border: 1px solid;border-collapse: collapse;padding: 2px;}.print_page_main_content .proj_table tbody tr td {border: 1px solid;font-size: 14px;padding: 5px 8px;line-height: 30px;}.print_page_main_content .proj_table .inside_table {width: 100%;border: hidden; /* 解决边框冲突 */border-collapse: collapse;}.print_page_main_content .proj_table .inside_table tr td {border: 1px solid;text-align: center;font-size: 14px;padding: 6px 5px;line-height: 16px;}}";
        printJS({
          printable: "prescription-print",
          type: "html",
          style: style1,
          scanStyles: false
        });

      }else{

        const style =
            '@media print {.statementTitle{font-size: 28px;text-align: center;font-weight: bold;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
      //   });
      // }
    },
    getAllDoctorList() {
      getAllDoctorList().then(response => {
        if (response.data.state == 1) {
          var doctor = response.data.data.doctor

          this.doctorList = doctor
        }
      })
    },
    getAllHisPatientList() {
      const params = {
        record_date: this.record_date
      }
      getAllHisPatientList(params).then(response => {
        if (response.data.state == 1) {
          this.patientTableData = response.data.data.list
          this.patientTableDataTwo = response.data.data.list

          console.log('222', this.patientTableData)
          let cal_one = 0
          let cal_two = 0
          for (let i = 0; i < response.data.data.list.length; i++) {
            if (response.data.data.list[i].prescription == null || response.data.data.list[i].prescription.length == 0) {
              cal_one = cal_one + 1
            }
            if (response.data.data.list[i].prescription != null && response.data.data.list[i].prescription.length > 0) {
              cal_two = cal_two + 1
            }
          }
          // this.$refs.tab.setCurrentRow(this.patientTableData[0])
          // console.log(this.patientTableData[0])
          // this.choosePatient(this.patientTableData[0])
          this.cal_one = cal_one
          this.cal_two = cal_two
        }
      })
    },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)
    },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)
    },

    changePatient(row) {
      console.log('row', row)
      this.patient_id = row.patient_id
      this.record_date = this.record_date
      this.prescription_id = this.prescription_id
      //   var params = {
      //     patient_id:row.patient_id,
      //     record_date:this.record_date,
      //     prescription_id:this.prescription_id,
      //   }
      // getPrescriptionPrint(params).then(response=>{
      //     if(response.data.state == 1){
      //       var advicePrint =  response.data.data.advicePrint
      //       console.log("adviceprint",advicePrint)
      //       this.advicePrint = advicePrint
      //       // var hisPatient =  response.data.data.hisPatient
      //       // console.log("hispatient",hisPatient)
      //       // this.hisPatient = hisPatient
      //       var patient =  response.data.data.patient
      //       console.log("patient",patient)
      //       this.patient = patient
      //       var doctorPorject = response.data.data.doctorPorject
      //       console.log("doctorporject",doctorPorject)
      //     }
      // })
    },
    searchAction() {

    },
    getInfo(order_id, balance_accounts_type) {
      console.log(balance_accounts_type)

      var that = this
      if (balance_accounts_type == 2) {
        let params = {
          id: order_id,
          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 {
            var that = this
            that.info = response.data.data.info
            that.p_admin = response.data.data.printor_admin
            that.charge_admin = response.data.data.charge_admin

            that.info['p_admin'] = that.p_admin
            that.info['charge_admin'] = that.charge_admin
            that.info['order_number'] = response.data.data.order_number
            that.info['patient'] = response.data.data.patient
            that.info['date'] = response.data.data.date
            that.info['number'] = response.data.data.number
            that.info['order_infos'] = response.data.data.order_infos
            that.info['diagnosis'] = response.data.data.diagnosis
            that.info['fixmedins_name'] = response.data.data.org_name
            that.info['fixmedins_code'] = response.data.data.org_code
            that.info['his'] = response.data.data.his_hospital

            // that.info["doctor_code"] = response.data.data.doctor_code;
            // that.info["department"] = response.data.data.department;
            // that.info["health_card_no"] = response.data.data.health_card_no;

            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
            that.info['date'] = response.data.data.date
            // that.info["number"] = response.data.data.number;
            that.info['order_infos'] = response.data.data.order_infos
            that.info['diagnosis'] = response.data.data.diagnosis

            //获取所有项目类型进行去重
            let med_chrgitm_types = []
            for (let i = 0; i < response.data.data.order_infos.length; i++) {
              med_chrgitm_types.push(response.data.data.order_infos[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_infos.length; b++) {
                if (med_chrgitm_types[i] == response.data.data.order_infos[b].med_chrgitm_type) {
                  tempDetails.push(response.data.data.order_info[b])
                }
              }
              obj.jiaTotal = that.getJiaTotal(tempDetails)
              obj.yiTotal = that.getYiTotal(tempDetails)
              obj.bingTotal = that.getBingTotal(tempDetails)
              obj.details = that.setNewData(tempDetails)
              that.list.push(obj)

            }
            console.log(that.list)
          }
        })

      } else {
        axios.get('http://127.0.0.1:9532/api/settle/query', {
          params: {
            order_id: order_id,
            admin_user_id: this.$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
                console.log(that.info)

                that.p_admin = response.data.data.printor_admin
                that.charge_admin = response.data.data.charge_admin
                that.order_infos = response.data.data
                that.info['patient'] = response.data.data.patient
                that.info['diagnosis'] = response.data.data.diagnosis
                that.info['p_admin'] = that.p_admin
                that.info['charge_admin'] = that.charge_admin
                that.info['fixmedins_name'] = response.data.data.org_name
                that.info['fixmedins_code'] = response.data.data.org_code
                that.info['order'] = response.data.data.order

                that.info['date'] = response.data.data.date
                that.info['number'] = response.data.data.number
                that.info['order_infos'] = response.data.data.order_infos
                that.info['diagnosis'] = response.data.data.diagnosis
                that.info['dia_config'] = response.data.data.dia_config
                that.info['doctor_info'] = response.data.data.doctor_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
                that.info['date'] = response.data.data.date
                that.info['number'] = response.data.data.number
                that.info['order_infos'] = response.data.data.order_infos
                that.info['diagnosis'] = response.data.data.diagnosis
                that.info['his'] = response.data.data.his_hospital

                console.log("22222222")
                that.list = []
                //获取所有项目类型进行去重
                let med_chrgitm_types = []
                for (let i = 0; i < response.data.data.order_infos.length; i++) {
                  med_chrgitm_types.push(response.data.data.order_infos[i].med_chrgitm_type)
                }
                console.log(med_chrgitm_types)

                const obj = {}
                med_chrgitm_types = med_chrgitm_types.reduce((cur, next) => {
                  obj[next] ? '' : obj[next] = true && cur.push(next)
                  return cur
                }, []) // 设置cur默认类型为数组，并且初始值为空的数组
                console.log(med_chrgitm_types)
                console.log("1111111")
                for (let i = 0; i < med_chrgitm_types.length; i++) {
                  let obj = {
                    details: [],
                    med_chrgitm_name: that.getType(med_chrgitm_types[i]),
                    jiaTotal: 0,
                    total: 0,
                    yiTotal: 0,
                    noMedicineTotal: 0,
                    bingTotal: 0,
                    otherTotal: 0,
                  }
                  console.log("222233333333211111222")

                  let tempDetails = []
                  for (let b = 0; b < response.data.data.order_infos.length; b++) {
                    if (med_chrgitm_types[i] == response.data.data.order_infos[b].med_chrgitm_type) {
                      tempDetails.push(response.data.data.order_infos[b])
                    }
                  }
                  console.log("2222211111222")
                  obj.jiaTotal = that.getJiaTotal(tempDetails)
                  obj.yiTotal = that.getYiTotal(tempDetails)
                  obj.bingTotal = that.getBingTotal(tempDetails)
                  that.list.push(obj)
                  console.log(that.list)
                }
                console.log(that.list)

              }
            })
            .catch(function(error) {
            })
      }
    }
  },

  created() {
    this.getInfo(this.paramsObj.order_id, this.paramsObj.balance_accounts_type)
    this.org_id = this.$store.getters.xt_user.org_id
  },
  watch: {
    paramsObj: {//深度监听，可监听到对象、数组的变化
      handler(val, oldVal) {
        this.paramsObj = val
        this.getInfo(this.paramsObj.order_id, this.paramsObj.balance_accounts_type)

      },
      deep: true
    }
  }

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