<template>
    <div class="main-contain summaryDetail">
        <div class="position">
            <bread-crumb :crumbs='crumbs'></bread-crumb>
        </div>
      <el-popover
        placement="bottom"
        width="210"
        trigger="click">
        <el-button size="small" ref="button_two" @click="open(1)">打印清单</el-button>
        <el-button size="small" ref="button_six" @click="open(2)">打印汇总</el-button>
        <el-button  slot="reference" style="margin:10px 10px;float: right" type="primary" size="small" >打印</el-button>

      </el-popover>
        <div class="app-container" style="display:flex;flex: 1;padding: 20px 20px 0px 20px;">
            <div class="mainCenter">
                <div class="centerLeft">
                    <div class="tabsBox">
                      <el-table  :data="tableData" border style="width: 100%;"
                                :row-style="{ color: '#303133' }"
                                :header-cell-style="{backgroundColor: 'rgb(245, 247, 250)',color: '#606266'}" highlight-current-row>

                        <el-table-column align="center" prop="project_name" label="名称">
                          <template slot-scope="scope">{{ scope.row.name }}</template>
                        </el-table-column>

                        <el-table-column align="center" prop="project_name" label="分类" width="100">
                          <template slot-scope="scope">{{ scope.row.type == 1?'药品': scope.row.project_type == 2? '项目':'耗材'}}</template>
                        </el-table-column>


                        <el-table-column align="center" prop="single_dose" width="80" label="单次用量">
                          <template slot-scope="scope">{{scope.row.single_dose}}&nbsp;&nbsp;{{scope.row.single_dose_unit}}</template>

                        </el-table-column>
                        <el-table-column align="center" prop="delivery_way" width="90" label="用法">
                          <template slot-scope="scope">
                            <!--<el-input v-model="scope.row.delivery_way" placeholder="" readonly></el-input>-->
                            {{scope.row.delivery_way}}

                          </template>
                        </el-table-column>
                        <el-table-column align="center" prop="execution_frequency" width="90" label="频率">
                          <template slot-scope="scope">{{scope.row.execution_frequency}}</template>
                        </el-table-column>
                        <el-table-column align="center" prop="number_days" width="60" label="天数">
                          <template slot-scope="scope">
                            <!--<el-input v-model="scope.row.number_days" placeholder="" readonly></el-input>-->
                            {{scope.row.day}}天

                          </template>
                        </el-table-column>
                        <el-table-column align="center" prop="total" width="60" label="总量">
                          <template slot-scope="scope">{{scope.row.count}}&nbsp;&nbsp;{{scope.row.prescribing_number_unit}}</template>
                        </el-table-column>
                        <el-table-column align="center" prop="name" width="60" label="单价">
                          <template slot-scope="scope">
                            <!--<el-input v-model="scope.row.price" placeholder="" readonly></el-input>-->
                            {{scope.row.price}}元

                          </template>
                        </el-table-column>

                          <el-table-column align="center" prop="name" width="60" label="总价">
                              <template slot-scope="scope">
                                  <!--<el-input v-model="scope.row.price" placeholder="" readonly></el-input>-->
                                  {{scope.row.price}}元

                              </template>
                          </el-table-column>


                        <el-table-column align="center" prop="name" width="80" label="备注">
                          <template slot-scope="scope">
                            <!--<el-input v-model="scope.row.remark" readonly></el-input>-->
                            {{scope.row.remark}}

                          </template>
                        </el-table-column>
                      </el-table>

                      <div class="additionalBox">
                        <div class="additionalOne" v-for="(item,index) in additions" :key="index">
                          <span :title="item.item_name">{{item.item_name}}</span>
                          <el-input v-model="item.price" placeholder="" style="width:50px;" readonly></el-input>
                          共
                          <el-input v-model="item.count" placeholder="" style="width:50px;" readonly></el-input>
                          次
                          <!--<i class="el-icon-delete deleteIcon" @click="delAddition(index,item)"></i>-->
                        </div>
                      </div>

                    </div>
                    <div class="costBox">
                      <span>当前处方总费用：<span style="color:red;">{{ order.medfee_sumamt}}元</span></span>
                      <span v-if="order.order_status == 1">待结算</span>
                      <span v-if="order.order_status == 2">已结算</span>
                      <span v-if="order.order_status == 3">已退费</span>


                    </div>
                </div>
                <div class="centerRight">
                    <p class="centerRightTitle">人员信息</p>
                    <ul class="basicUl">
                        <li style="width:50%;">姓名：{{patientInfo.name}}</li>
                        <li style="width:50%;">性别：{{patientInfo.gender == 1 ? '男' : '女' }}</li>
                        <li style="width:50%;">年龄：{{patientInfo.age }}</li>
                        <li style="width:50%;">身高：{{patientInfo.height?patientInfo.height:''}}</li>
                        <li style="width:50%;">体重：{{patientInfo.weight?patientInfo.weight:''}}</li>
                        <li style="width:50%;">电话：{{patientInfo.phone}}</li>
                    </ul>
                    <p class="centerRightTitle" style="margin-top:10px;">收费信息</p>
                    <ul class="basicUl">
                        <li style="width:50%;">应收金额：{{order.medfee_sumamt }}元</li>
                        <li style="width:50%;">实收金额：{{order.reality_price}}元</li>
                        <li style="width:50%;">舍入金额：{{order.medfee_sumamt - order.reality_price}}元</li>
                        <li style="width:50%;">优惠金额：{{order.preferential_price}}元</li>
                        <li style="width:50%;">总金额</li>
                        <li style="width:50%;">{{order.medfee_sumamt}}元</li>
                    </ul>
                    <!--<p class="centerRightTitle">发票费用</p>-->
                    <!--<ul class="basicUl">-->
                        <!--<li style="width:100%;">西药费：0.00元</li>-->
                    <!--</ul>-->
                </div>
            </div>
        </div>

      <div v-if="listVisible">
      <el-dialog
        class="centerDialog"
        width="900px"
        title="打印"
        :visible.sync="listVisible">
        <list-print  :paramsObj='paramsObj'></list-print>
      </el-dialog>
      </div>

      <div v-if="allListVisible">
      <el-dialog
        class="centerDialog"
        width="900px"
        title="打印"
        :visible.sync="allListVisible">
        <all-list-print :paramsObj='paramsObj'></all-list-print>
      </el-dialog>
      </div>


    </div>
</template>


<script>
import BreadCrumb from '@/xt_pages/components/bread-crumb'
import { getHisOrder } from '@/api/his/his'
import prescriptionTable from './components/prescriptionTable'
import { getDictionaryDataConfig} from "@/utils/data";
import ListPrint from './listPrint'
import AllListPrint from './allListPrint'

export default {
    components:{
      AllListPrint,
      ListPrint,
        BreadCrumb,
      prescriptionTable,

    },
    data(){
        return{
          listVisible:false,
          allListVisible:false,
          paramsObj:{
            id:this.$route.query.id,
            balance_accounts_type:0
          },
            crumbs: [
                { path: false, name: '门诊收费' },
                { path: false, name: '项目消费明细汇总详情' }
            ],
          info: {},
          curPrescriptions: {},
          prescriptions: [],
          record_date: '',
          tableData: [],
          additions:[],
          editableTabsValue: '1',
          loadingone: false,
          editableTabs: [{
            title: '处方1',
            name: '1'
          }],
          tabIndex: 1,
          hisPatientInfo: {},
          loadingtwo: false,
          patientTableData: [{
            name: '杨美英',
            mdtrt_id: '1709946'
          }],
          patientInfo: { id: 0 },
          doctor: {},
          total: 0,
          state: '未收费',
          radio: 1,
          radioStatus: 1,
          search_input: '',
          order:{},
        }
    },created(){
      this.GetOrderDetail()
  },methods:{
      open(val){
        if(val == 1){
          this.paramsObj.id == this.$route.query.id
          this.paramsObj.balance_accounts_type = this.hisPatientInfo.balance_accounts_type
          this.listVisible = true

        }else if(val == 2){
          this.paramsObj.id == this.$route.query.id
          this.paramsObj.balance_accounts_type = this.hisPatientInfo.balance_accounts_type

          this.allListVisible = true
        }
      },
    // getTotal() {
    //   var total = 0
    //   for (let i = 0; i < this.prescriptions.length; i++) {
    //     if (this.prescriptions[i].advices != null) {
    //       for (let a = 0; a < this.prescriptions[i].advices.length; a++) {
    //         console.log(this.prescriptions[i].advices[a].price )
    //         console.log(this.prescriptions[i].advices[a].prescribing_number)
    //
    //         total = parseFloat(total) + parseFloat(this.prescriptions[i].advices[a].price * this.prescriptions[i].advices[a].prescribing_number)
    //       }
    //     }
    //     if (this.prescriptions[i].project != null) {
    //       for (let a = 0; a < this.prescriptions[i].project.length; a++) {
    //
    //         total = parseFloat(total) + parseFloat(this.prescriptions[i].project[a].price * this.prescriptions[i].project[a].count)
    //       }
    //     }
    //
    //     if (this.prescriptions[i].addition != null) {
    //       for (let b = 0; b < this.prescriptions[i].addition.length; b++) {
    //         total = parseFloat(total) + parseFloat(this.prescriptions[i].addition[a].price * this.prescriptions[i].addition[b].count)
    //       }
    //     }
    //   }
    //   return total.toFixed(2)
    //
    // },
    getDictionaryDataConfig(module, filed_name) {
      return getDictionaryDataConfig(module, filed_name)
    },
    tabclickEvent(val) {
      for (let i = 0; i < this.prescriptions.length; i++) {
        if (this.prescriptions[i].name == val.name) {
          this.curPrescriptions = {}
          var temp = this.deepClone(this.prescriptions[i])
          this.curPrescriptions = temp
        }
      }
    },
    deepClone(source) {
      if (!source && typeof source !== 'object') {
        throw new Error('error arguments', 'shallowClone')
      }
      const targetObj = source.constructor === Array ? [] : {}
      Object.keys(source).forEach((keys) => {
        if (source[keys] && typeof source[keys] === 'object') {
          targetObj[keys] = this.deepClone(source[keys])
        } else {
          targetObj[keys] = source[keys]
        }
      })
      return targetObj
    },  getGroup(id){
      var name = ""
      var statistics_category =  getDictionaryDataConfig('system','statistics_category')
      for(let i=0;i<statistics_category.length;i++){
        if(id == statistics_category[i].id){
          name = statistics_category[i].name
        }
      }
      return name
    },
    getTotal() {
      var total = 0
      for (let i = 0; i < this.prescriptions.length; i++) {
        if (this.prescriptions[i].advices != null) {
          for (let a = 0; a < this.prescriptions[i].advices.length; a++) {
            total = total + this.prescriptions[i].advices[a].retail_price * this.prescriptions[i].advices[a].prescribing_number
          }
        }
        if (this.prescriptions[i].project != null) {
          for (let b = 0; b < this.prescriptions[i].project.length; b++) {
            total = total + this.prescriptions[i].project[b].price * this.prescriptions[i].project[b].total
          }
        }
      }
      return total
    },
    moreState(tab, event) {
      if (tab == 'more') {
        return false
      }
    },  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
    },
    setMonthPrescription(month_prescriptions) {
      console.log(month_prescriptions)
      let drug_month_prescriptions = {
        advices: []
      }
      let drug_ids = []
      let project_month_prescriptions = {
        project: []

      }
      let project_ids = []

      let addition_month_prescriptions = {
        addition: []

      }
      let additions_ids = []

      for (let i = 0; i < month_prescriptions.length; i++) {
        if (month_prescriptions[i].type == 1) { //药品
          for (let a = 0; a < month_prescriptions[i].advices.length; a++) {
            let obj = {
              id: month_prescriptions[i].advices[a].drug_id,
              price: month_prescriptions[i].advices[a].price

            }
            drug_ids.push(obj)
            drug_month_prescriptions.advices.push(month_prescriptions[i].advices[a])
          }

        } else if (month_prescriptions[i].type == 2) { //项目
          for (let a = 0; a < month_prescriptions[i].project.length; a++) {
            let obj = {
              id: month_prescriptions[i].project[a].project_id,
              price: month_prescriptions[i].project[a].price

            }
            project_ids.push(obj)
            project_month_prescriptions.project.push(month_prescriptions[i].project[a])
          }
        }
        //附加收费
        for (let a = 0; a < month_prescriptions[i].addition.length; a++) {
          let obj = {
            id: month_prescriptions[i].addition[a].item_id,
            price: month_prescriptions[i].addition[a].price

          }
          additions_ids.push(obj)
          addition_month_prescriptions.addition.push(month_prescriptions[i].addition[a])

        }

      }

      drug_ids = this.unique(drug_ids)
      project_ids= this.unique(project_ids)
      additions_ids= this.unique(additions_ids)





      for (let i = 0; i < drug_ids.length; i++) {
        let obj = {}
        let count = 0
        for (let a = 0; a < drug_month_prescriptions.advices.length; a++) {
          if (drug_ids[i].price == drug_month_prescriptions.advices[a].price && drug_ids[i].id ==  drug_month_prescriptions.advices[a].drug_id) {
            obj['name'] = drug_month_prescriptions.advices[a].advice_name
            obj['statistical_classification'] = ''
            obj['type'] = 1
            obj['single_dose'] = drug_month_prescriptions.advices[a].single_dose
            obj['delivery_way'] = drug_month_prescriptions.advices[a].delivery_way
            obj['execution_frequency'] = drug_month_prescriptions.advices[a].execution_frequency
            obj['day'] = drug_month_prescriptions.advices[a].day
            obj['prescribing_number_unit'] = drug_month_prescriptions.advices[a].prescribing_number_unit
            obj['remark'] = drug_month_prescriptions.advices[a].remark
            obj['price'] = parseFloat(drug_month_prescriptions.advices[a].price)
            count = count + drug_month_prescriptions.advices[a].prescribing_number

          }
        }
        obj['count'] = count
        this.tableData.push(obj)
      }

      for (let i = 0; i < project_ids.length; i++) {
        let obj = {}
        let count = 0
        for (let a = 0; a < project_month_prescriptions.project.length; a++) {
          if (project_ids[i].price == project_month_prescriptions.project[a].price &&project_ids[i].id == project_month_prescriptions.project[a].project_id ) {

            if(project_month_prescriptions.project[a].type == 2){
              obj['name'] = project_month_prescriptions.project[a].project.project_name
              obj['statistical_classification'] = ''
              obj['project_type'] = 2


            }else if (project_month_prescriptions.project[a].type == 3){
              obj['name'] = project_month_prescriptions.project[a].good_info.good_name
              obj['statistical_classification'] = ''
              obj['project_type'] = 3
            }
            obj['single_dose'] = project_month_prescriptions.project[a].single_dose
            obj['delivery_way'] = project_month_prescriptions.project[a].delivery_way
            obj['execution_frequency'] = project_month_prescriptions.project[a].execution_frequency
            obj['day'] = project_month_prescriptions.project[a].day
            obj['single_dose_unit'] = project_month_prescriptions.project[a].unit
            obj['prescribing_number_unit'] = project_month_prescriptions.project[a].unit
            count = count + project_month_prescriptions.project[a].count
            obj['price'] = parseFloat(project_month_prescriptions.project[a].price)
            obj['remark'] = project_month_prescriptions.project[a].remark
            obj['type'] = 2


          }
        }
        obj['count'] = count
        this.tableData.push(obj)

      }

      for (let i = 0; i < additions_ids.length; i++) {
        let obj = {}
        let count = 0
        console.log(addition_month_prescriptions.addition)
        if (addition_month_prescriptions.addition) {
          for (let a = 0; a < addition_month_prescriptions.addition.length; a++) {
            if (additions_ids[i].price == addition_month_prescriptions.addition[a].price &&additions_ids[i].id == addition_month_prescriptions.addition[a].item_id) {
              obj['item_name'] = addition_month_prescriptions.addition[a].item_name
              count = count + addition_month_prescriptions.addition[a].count
              obj['price'] = parseFloat(addition_month_prescriptions.addition[a].price)
            }
          }
          obj['count'] = count
          this.additions.push(obj)

        }
      }

    },

    GetOrderDetail(){
      let params={
        number:this.$route.query.number,
        patient_id:this.$route.query.patient_id,
      }
      getHisOrder(params).then(response => {
        if (response.data.state == 0) {
          this.$message.error(response.data.msg)
          return false
        } else {
          this.patientInfo = response.data.data.order.patient
          this.hisPatientInfo = response.data.data.order.his_patient
          this.info = response.data.data.p_info
          this.order = response.data.data.order

          this.prescriptions = []
          this.tableData = []


          var prescription = response.data.data.prescription
          this.prescriptions = prescription
          this.setMonthPrescription(prescription)


          // for (let i = 0; i < response.data.data.prescription.length; i++) {
          //   var prescription = response.data.data.prescription[i]


            // for (let b = 0; b < prescription.advices.length; b++) {
            //   let obj = {
            //     name: prescription.advices[b].advice_name,
            //     statistical_classification:"",
            //     type:1,
            //     single_dose: prescription.advices[b].single_dose,
            //     delivery_way: prescription.advices[b].delivery_way,
            //     execution_frequency: prescription.advices[b].execution_frequency,
            //     price: prescription.advices[b].price.toString(),
            //     day: prescription.advices[b].day,
            //     count:prescription.advices[b].prescribing_number.toString(),
            //     remark: prescription.advices[b].remark,
            //     single_dose_unit: prescription.advices[b].single_dose_unit,
            //     prescribing_number_unit: prescription.advices[b].prescribing_number_unit,
            //
            //   }
            //   this.tableData.push(obj)
            // }
            //
            // for (let b = 0; b < prescription.project.length; b++) {
            //   let obj = {
            //     name: prescription.project[b].project.project_name,
            //     statistical_classification: prescription.project[b].project.statistical_classification,
            //     single_dose: prescription.project[b].single_dose,
            //     delivery_way: prescription.project[b].delivery_way,
            //     execution_frequency: prescription.project[b].execution_frequency,
            //     price: prescription.project[b].price,
            //     day: prescription.project[b].day,
            //     count: prescription.project[b].count.toString(),
            //     remark: prescription.project[b].remark,
            //     single_dose_unit:prescription.project[b].project.unit,
            //     prescribing_number_unit:prescription.project[b].project.unit,
            //     type:2,
            //   }
            //   this.tableData.push(obj)
            // }
            //
            // for (let b = 0; b < prescription.addition.length; b++) {
            //     this.additions.push( prescription.addition[b])
            // }



            // let index = i + 1
            // let obj = {
            //   id: prescription.id,
            //   name: '处方' + index,
            //   advices: tempAdvice,
            //   project: tempProject,
            //   type: response.data.data.prescription[i].type
            // }
            // this.prescriptions.push(obj)
            // this.curPrescriptions = this.prescriptions[0]

          // }
          // this.tableData = response.data.data.order

        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.summaryDetail{
    height: 100%;
    display: flex;
    flex-direction: column;
    .tabsBox{
        position: relative;
        height: 90%;
        overflow-y: auto;
        margin-bottom: 60px;
        .el-tabs__item{
            padding: 0 10px;
        }
    }
    .addTab{
        position: absolute;
        right: 0;
        top: 6px;
        z-index: 20;
    }
    .mainCenter{
        display: flex;
        flex:1;
    }
    .costBox{
        width:100%;
        height:60px;
        background:#fff;
        position: absolute;
        bottom:0;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .centerLeft{
        flex: 1;
        display: flex;
        flex-direction: column;
        position: relative;
    }
    .centerRight{
        width: 300px;
        margin-left: 5px;
        display: flex;
        flex-direction: column;
    }
    .RP{
        color:#409EFF;
        font-size: 20px;
        margin-bottom: 5px;
    }
    .centerRightTitle{
        color:#409EFF;
        padding:0 0 10px 0;
    }
    .basicUl{
        border-top: 1px solid #e5e5e5;
        border-left: 1px solid #e5e5e5;
        display: flex;
        flex-wrap: wrap;
        li{
            border-bottom: 1px solid #e5e5e5;
            border-right: 1px solid #e5e5e5;
            width: 100%;
            height: 38px;
            line-height: 38px;
            text-indent: 5px;
            font-size: 14px;
        }
    }
    .additionalBox{
      margin-top: 20px;
      display: flex;
      flex-wrap: wrap;
      .additionalOne{
        margin-right:20px;
        margin-bottom:10px;
        display: flex;
        align-items: center;
        >span{
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
          width:80px;
          display: inline-block;
          font-size: 14px;
        }
      }
      .deleteIcon{
        color:red;
        margin-left:5px;
      }
    }
}
</style>

<style lang="scss">
.summaryDetail{
    .el-table th .cell, .el-table td .cell{
        padding: 0 2px;
    }
    .el-icon-delete{
        color:red;
    }
}

</style>

