<template>
  <div class="main-contain outpatientChargesManagement">
    <div class="position">
      <bread-crumb :crumbs='crumbs'></bread-crumb>
    </div>
    <div class="app-container" style="display:flex;flex: 1;padding: 10px 20px 0px 20px;">
      <div class="mainLeft">

        <!-- <div class="mainCell" style="justify-content: space-between;font-size:14px;">
         <p >未收费：<span style="color: red">{{cal_one}}</span>人</p>
         <p>已收费：<span  style="color: red">{{cal_two}}</span>人</p>
         <p>已退费：<span  style="color: red">{{cal_three}}</span>人</p>
       </div> -->
        <div class="mainCell">
          <el-radio-group v-model="radio" @change="changeRadio">
            <!-- <el-radio :label=1>未收费</el-radio>
            <el-radio :label=2>已收费</el-radio>
            <el-radio :label=3>已退费</el-radio>
            <el-radio :label=4>全部</el-radio> -->
            <el-radio :label=1>未收费<span style="color: red;margin-bottom:10px;display: inline-block;">{{cal_one}}</span>人
            </el-radio>
            <el-radio :label=2>已收费<span style="color: red;margin-bottom:10px;display: inline-block;">{{cal_two}}</span>人
            </el-radio>
            <el-radio :label=3>已退费<span
              style="color: red;margin-bottom:10px;display: inline-block;">{{cal_three}}</span>人
            </el-radio>
            <el-radio :label=4>全部</el-radio>

          </el-radio-group>
        </div>
        <div class="mainCell" style="margin-bottom:10px;">
          <el-input size="small" @keyup.enter.native='searchAction' v-model.trim="search_input" class="filter-item"/>
          <el-button size="small" style="margin-left:10px;" class="filter-item" type="primary" @click="searchAction">
            搜索
          </el-button>
        </div>
        <div>
          <el-table :data="patientTableData" :height="tableHeight" border style="width: 100%;"
                    :row-style="{ color: '#303133' }"
                    :header-cell-style="{backgroundColor: 'rgb(245, 247, 250)',color: '#606266'}"
                    highlight-current-row ref="tab"
                    @current-change="handleCurrentChange">
            <el-table-column align="center" prop="name" label="姓名" wdith='89'>
              <template slot-scope="scope">{{scope.row.patients?scope.row.patients.name:''}}</template>
            </el-table-column>
            <el-table-column align="center" prop="name" label="就诊号" width="110">
              <template slot-scope="scope">{{ scope.row.his_patient ?scope.row.his_patient.number:''}}</template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <div class="mainRight">

        <div class="mainCell fixedCell" style="margin-bottom:10px;">
          <!-- <el-button size="small" @click="open(1)" type="primary" style="margin-left:10px;">打印处置单</el-button>
          <el-button size="small" @click="open(2)" type="primary">打印治疗单</el-button> -->
          <el-button size="small" @click="open(3)" type="primary">打印</el-button>
          <el-button v-loading="loadingone" size="small"
                     @click="open(6)"
                     v-if="this.hisPatientInfo && this.hisPatientInfo.id == 0"
                     type="primary">挂号
          </el-button>
          <el-button v-loading="loadingtwo" v-if="this.info.prescription_status == 1" size="small"
                     @click="open(4)"
                     type="primary">收费
          </el-button>
          <el-button size="small" @click="open(5)" type="primary"
                     v-if="this.info && this.info.prescription_status == 3">退费
          </el-button>


          <el-button size="small" @click="open(7)" type="primary"
          >对账
          </el-button>


          <el-button size="small" @click="open(8)" type="primary"
          >明细对账
          </el-button>
          <el-button size="small" @click="open(9)" type="primary"
          >结算单
          </el-button>
        </div>

        <div class="mainCenter">
          <el-tabs class="settlementTabs" v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="日结" name="first">
              <div style="display:flex;height:100%;">
                <div class="centerLeft">
                  <div class="tabsBox">
                    <!--<el-button type="text" class="addTab" @click="addCharges" icon="el-icon-circle-plus">附加收费</el-button>-->
                    <el-tabs class="preTabs" v-model="editableTabsValue" type="border-card" @tab-click="tabclickEvent">
                      <el-tab-pane
                        v-for="(item, index) in prescriptions"
                        :key="index"
                        :label="item.name"
                        :name="item.name"
                      >
                      </el-tab-pane>

                      <div class="RP">
                        Rp
                        <el-date-picker
                          v-model="record_date"
                          type="date"
                          :clearable="false"
                          @change="changeDate"
                          format="yyyy-MM-dd"
                          value-format="yyyy-MM-dd"
                          placeholder="选择日期">
                        </el-date-picker>
                      </div>
                      <prescription-table ref="prescription_tables"
                                          :prescription="curPrescriptions"></prescription-table>

                    </el-tabs>

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

                  </div>
                </div>
                <div class="centerRight">
                  <p class="centerRightTitle">人员信息</p>
                  <div style="display:flex;justify-content: space-between;line-height:30px;">
                    <span style="font-size:14px;">人员编码:{{patientInfo.dialysis_no}}</span>
                    <span style="font-size:14px;">日期:{{record_date}}</span>
                  </div>
                  <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>
                    <li style="width:100%;">地址：{{patientInfo.home_address}}</li>
                    <li style="width:100%;">过敏史及其他病史：{{info.sick_history}}</li>
                  </ul>
                  <p class="centerRightTitle" style="padding-top: 10px;">诊断信息</p>
                  <div style="display:flex;justify-content: space-between;line-height:30px;">
                    <span style="font-size:14px;">门诊编号:{{ hisPatientInfo.number}}</span>
                  </div>
                  <ul class="basicUl">
                    <li style="width:50%;">医生：{{ info.doctor }}</li>
                    <li style="width:50%;">科室：{{ info.departments }}</li>
                    <li style="width:100%;">总费用：{{ getTotal() }}</li>

                    <li style="width:100%;">判断结果：{{info.diagnosis}}</li>
                    <li style="width:100%;">是否有传染病：</li>
                    <li style="width:100%;">血压：</li>
                  </ul>
                </div>
              </div>
            </el-tab-pane>
            <!--<el-tab-pane label="月结" name="second">-->
            <!--<div style="display:flex;height:100%;">-->
            <!--<div class="centerLeft">-->
            <!--<div class="tabsBox">-->
            <!--&lt;!&ndash;<el-button type="text" class="addTab" @click="addCharges" icon="el-icon-circle-plus">附加收费</el-button>&ndash;&gt;-->
            <!--<el-tabs class="preTabs" v-model="editableTabsValue" type="card" closable>-->
            <!--<el-tab-pane-->
            <!--v-for="(item, index) in editableTabs"-->
            <!--:key="index"-->
            <!--:label="item.title"-->
            <!--:name="item.name"-->
            <!--&gt;-->
            <!--<div class="RP">-->
            <!--Rp-->
            <!--<el-date-picker-->
            <!--style="width:420px;"-->
            <!--v-model="value1"-->
            <!--type="daterange"-->
            <!--range-separator="至"-->
            <!--start-placeholder="开始日期"-->
            <!--end-placeholder="结束日期">-->
            <!--</el-date-picker>-->
            <!--</div>-->
            <!--<prescription-table :preTableData='preTableData'></prescription-table>-->
            <!--</el-tab-pane>-->
            <!--&lt;!&ndash;<el-tab-pane name="more" closable><span slot="label"><i class="el-icon-plus" @click="addTab"></i></span></el-tab-pane>&ndash;&gt;-->
            <!--</el-tabs>-->

            <!--</div>-->
            <!--<div class="costBox">-->
            <!--<span>处方编号：2134348971237883</span>-->
            <!--<span>当前处方总费用：<span style="color:red;">{{ total }}元</span></span>-->
            <!--<span>{{ state }}</span>-->
            <!--</div>-->
            <!--</div>-->
            <!--<div class="centerRight">-->
            <!--<p class="centerRightTitle">人员信息</p>-->
            <!--<div style="display:flex;justify-content: space-between;line-height:30px;">-->
            <!--<span style="font-size:14px;">人员编码:</span>-->
            <!--<span style="font-size:14px;">日期:{{start_time}}</span>-->
            <!--</div>-->
            <!--<ul class="basicUl">-->
            <!--<li style="width:50%;">姓名：{{ patientInfo.name }}</li>-->
            <!--<li style="width:50%;">性别：{{ patientInfo && patientInfo.gend == 1 ? '男' : "女" }}</li>-->
            <!--<li style="width:50%;">年龄：{{ patientInfo.age }}</li>-->
            <!--<li style="width:50%;">身高：</li>-->
            <!--<li style="width:50%;">体重：</li>-->
            <!--<li style="width:50%;">电话：</li>-->
            <!--<li style="width:100%;">地址：</li>-->
            <!--<li style="width:100%;">过敏史：</li>-->
            <!--</ul>-->
            <!--<p class="centerRightTitle">诊断信息</p>-->
            <!--<div style="display:flex;justify-content: space-between;line-height:30px;">-->
            <!--<span style="font-size:14px;">门诊编号:676273816287361</span>-->
            <!--</div>-->
            <!--<ul class="basicUl">-->
            <!--<li style="width:50%;">医生：{{ doctor.name }}</li>-->
            <!--<li style="width:50%;">科室：{{ doctor.department }}</li>-->
            <!--<li style="width:100%;">费用：{{ doctor.total }}元</li>-->
            <!--<li style="width:100%;">判断结果：</li>-->
            <!--<li style="width:100%;">是否有传染病：</li>-->
            <!--<li style="width:100%;">血压：</li>-->
            <!--</ul>-->
            <!--</div>-->
            <!--</div>-->
            <!--</el-tab-pane>-->
          </el-tabs>


        </div>
      </div>
    </div>
    <additionalCharges ref='additionalCharges' @click="addCharges"
                       :addtions_charge="addtions_charge"></additionalCharges>

    <register-dialog ref='register' v-on:confirm="confirm" v-on:cancel="cancel"></register-dialog>
    <charge-dialog ref='charge' v-on:confirm="chargeConfirm" v-on:cancel="chargeCancel"></charge-dialog>
    <el-dialog
      class="centerDialog"
      width="900px"
      title="打印"
      :visible.sync="treatVisible">
      <treatPrint :paramsObj='paramsObj'></treatPrint>
    </el-dialog>
    <el-dialog
      class="centerDialog"
      width="1200px"
      title="打印"
      :visible.sync="statementVisible">
      <statementPrint ref="print" :paramsObj='orderObj'></statementPrint>
    </el-dialog>
  </div>
</template>


<script>
  import axios from 'axios'

  import BreadCrumb from '@/xt_pages/components/bread-crumb'
  import noCharge from './components/noCharge'
  import charged from './components/charged'
  import medicalInsuranceRefund from './components/medicalInsuranceRefund'
  import prescriptionTable from './components/prescriptionTable'
  import additionalCharges from './components/additionalCharges'
  import {getPatientInformation, getPatientList} from '@/api/project/project'
  import {getPatientInfo, getSchedulePatientList, Refund, register, upload} from '@/api/his/his'
  import RegisterDialog from "./components/registerDialog";
  import treatPrint from './treatPrint'
  import statementPrint from './statementPrint'
  import ChargeDialog from "./components/chargeDialog";

  const moment = require('moment')
  export default {
    components: {
      ChargeDialog,
      RegisterDialog,
      BreadCrumb,
      noCharge,
      charged,
      medicalInsuranceRefund,
      prescriptionTable,
      additionalCharges,
      treatPrint,
      statementPrint

    },
    data() {
      return {
        crumbs: [
          {path: false, name: '门诊收费'},
          {path: false, name: '门诊收费管理'}
        ],
        current_index: 0,
        cal_one: '',
        cal_two: '',
        cal_three: '',
        tableHeight: '',
        fullHeight: document.documentElement.clientHeight,
        activeName: 'first',
        info: {},
        curPrescriptions: {},
        prescriptions: [],
        record_date: '',
        tableData: [],
        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: '',
        start_time: moment().locale('zh-cn').format('YYYY-MM-DD'),
        patient_id: 0,
        prescription_id: 0,
        all_table_data: [],
        order: {},
        addtions_charge: [],
        treatVisible: false,
        orderObj:{
        },
        paramsObj: {
        },
        statementVisible:false
      }
    },
    created() {
      let tableHeight = document.body.clientHeight - 243
      this.tableHeight = tableHeight
      var nowDate = new Date()
      var nowYear = nowDate.getFullYear()
      var nowMonth = nowDate.getMonth() + 1
      var nowDay = nowDate.getDate()
      this.record_date =
        nowYear +
        '-' +
        (nowMonth < 10 ? '0' + nowMonth : nowMonth) +
        '-' +
        (nowDay < 10 ? '0' + nowDay : nowDay)

      //获取患者信息
      this.getPatientList()
    },
    methods: {
      chargeConfirm(form) {
        form['id'] = this.patientInfo.id
        form['record_time'] = this.record_date
        this.loadingtwo = true
        if (this.$store.getters.xt_user.org_id == 4) {
          axios.get('http://127.0.0.1:9532/api/upload/get', {
            params: form
          })
            .then(function (response) {
              if (response.data.state == 0) {
                this.$message.error(response.data.msg)
                this.loadingtwo = false
                this.$refs.charge.hide()
                return false
              } else {
                this.$refs.charge.hide()
                this.loadingtwo = false
                this.state = '已收费'
                this.$message({message: '收费成功', type: 'success'})
                this.loadingtwo = false
                this.cal_one = this.cal_one - 1
                this.cal_two = this.cal_two + 1
                this.patientTableData.splice(this.current_index, 1)
              }
            })
            .catch(function (error) {

            });
        }
        else {
          upload(form).then(response => {
            if (response.data.state == 0) {
              this.$message.error(response.data.msg)
              this.loadingtwo = false
              this.$refs.charge.hide()
              return false
            } else {
              this.$refs.charge.hide()

              this.loadingtwo = false
              this.state = '已收费'
              this.$message({message: '收费成功', type: 'success'})
              this.$refs.charge.hide()
              this.loadingtwo = false
              this.cal_one = this.cal_one - 1
              this.cal_two = this.cal_two + 1
              this.patientTableData.splice(this.current_index, 1)
            }
          })
        }
      }, chargeCancel() {
        this.$refs.charge.hide()
      },
      confirm(forms) {
        forms['id'] = this.patientInfo.id,
          forms['record_time'] = this.record_date,
          this.loadingone = true

        if (this.$store.getters.xt_user.org_id == 4) {
          axios.get('http://127.0.0.1:9532/api/register/get', {
            params: forms
          })
            .then(function (response) {
              if (response.data.state == 0) {
                this.$message.error(response.data.msg)
                this.loadingone = false
                this.$refs.register.hide()
                return false
              } else {
                this.$refs.register.hide()
                this.getPatientList()
                this.$message({message: '挂号成功', type: 'success'})
                this.loadingone = false
                var his_info = response.data.data.his_info
                this.hisPatientInfo = his_info
              }
            })
            .catch(function (error) {

            });
        } else {
          register(forms).then(response => {
            if (response.data.state == 0) {
              this.$message.error(response.data.msg)
              this.loadingone = false
              this.$refs.register.hide()
              return false
            } else {
              this.getPatientList()
              this.$message({message: '挂号成功', type: 'success'})
              this.$refs.register.hide()
              this.loadingone = false
              var his_info = response.data.data.his_info
              this.hisPatientInfo = his_info
            }
          })
        }

      }, cancel() {
        this.$refs.register.hide()
      },
      addCharges() {
        this.$refs.additionalCharges.show()
      },
      changeDate(val) {
        if (this.patientInfo.id == 0) {
          this.$message.error('请先选择患者')
          return
        }
        let params = {
          'record_date': val,
          'patient_id': this.patientInfo.id
        }
        getPatientInfo(params).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {
            this.patientInfo = response.data.data.xt_info
            this.hisPatientInfo = response.data.data.his_info
            this.info = response.data.data.info
            this.addtions_charge = response.data.data.addtions_charge

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

              let tempAdvice = []
              let tempProject = []

              for (let b = 0; b < prescription.advices.length; b++) {
                let obj = {
                  advice_id: prescription.advices[b].id,
                  drug_name: prescription.advices[b].advice_name,
                  single_dose: prescription.advices[b].single_dose,
                  delivery_way: prescription.advices[b].delivery_way,
                  execution_frequency: prescription.advices[b].execution_frequency,
                  retail_price: prescription.advices[b].price.toString(),
                  remark: prescription.advices[b].remark,
                  day: prescription.advices[b].day,
                  prescribing_number: prescription.advices[b].prescribing_number.toString(),
                  single_dose_unit: prescription.advices[b].single_dose_unit,
                  prescribing_number_unit: prescription.advices[b].prescribing_number_unit,
                  medical_insurance_number: prescription.advices[b].med_list_codg

                }
                tempAdvice.push(obj)
              }

              for (let b = 0; b < prescription.project.length; b++) {
                console.log(prescription.project[b].project.project_name)
                let obj = {
                  id: prescription.project[b].id,
                  project_id: prescription.project[b].project.id,
                  project_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,
                  number_days: prescription.project[b].day,
                  total: prescription.project[b].count.toString(),
                  price: prescription.project[b].price,
                  remark: prescription.project[b].remark,
                  medical_code: prescription.project[b].project.medical_code,
                  unit: prescription.project[b].unit,
                }

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

      },
      searchAction() {

      },
      tabclickEvent(val) {
        console.log('val', val.name)
        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
          }
        }
        console.log('9999', this.curPrescriptions)
        this.prescription_id = this.curPrescriptions.id
        this.total = 0
        this.total = this.getTotalOne()

      }, 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
      }, getTotalOne() {
        var total = 0
        if (this.curPrescriptions.type == 1) {
          if (this.curPrescriptions.advices != null) {
            for (let a = 0; a < this.curPrescriptions.advices.length; a++) {
              total = total + Math.floor((this.curPrescriptions.advices[a].retail_price * this.curPrescriptions.advices[a].prescribing_number) * 100) / 100

            }
          }
        } else {
          if (this.curPrescriptions.project != null) {
            for (let b = 0; b < this.curPrescriptions.project.length; b++) {
              total = total + Math.floor((this.curPrescriptions.project[b].price * this.curPrescriptions.project[b].total) * 100) / 100

            }
          }
        }

        if (this.curPrescriptions.addition != null) {
          for (let b = 0; b < this.curPrescriptions.addition.length; b++) {
            total = total + Math.floor((this.curPrescriptions.addition[b].price * this.curPrescriptions.addition[b].count) * 100) / 100
          }
        }
        return Math.floor(total * 100) / 100

      },
      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 + Math.floor((this.prescriptions[i].advices[a].retail_price * this.prescriptions[i].advices[a].prescribing_number) * 100) / 100
            }
          }
          if (this.prescriptions[i].project != null) {
            for (let b = 0; b < this.prescriptions[i].project.length; b++) {

              total = total + Math.floor((this.prescriptions[i].project[b].price * this.prescriptions[i].project[b].total) * 100) / 100
            }
          }
          if (this.prescriptions[i].addition != null) {
            for (let b = 0; b < this.prescriptions[i].addition.length; b++) {
              total = total + Math.floor((this.prescriptions[i].addition[b].price * this.prescriptions[i].addition[b].count) * 100) / 100
            }
          }
        }
        return Math.floor(total * 100) / 100

      },
      moreState(tab, event) {
        if (tab == 'more') {
          return false
        }
      },
      open(index) {

        if (index == 1) {


        } else if (index == 2) {

          this.$router.push('/outpatientDoctorStation/printtwo?record=' + this.record_date)

        } else if (index == 3) {
          let obj = {
            record_date: this.record_date,
            patient_id: this.patient_id,
            prescription_id: this.prescription_id
          }
          this.paramsObj = obj
          this.treatVisible = true
          // this.$router.push('/outpatientCharges/treatPrint?record_date=' + this.record_date + '&patient_id=' + this.patient_id + '&prescription_id=' + this.prescription_id)
        } else if (index == 4) {

          if (this.hisPatientInfo.id == 0) {
            this.$message({message: '该患者尚未挂号,请先挂号', type: 'error'})
            return
          }

          this.$refs.charge.show(this.getTotal())


        } else if (index == 5) {
          if (this.$store.getters.xt_user.org_id == 4) {
            this.$confirm('是否退费', '退费', {
              confirmButtonText: '确 定',
              cancelButtonText: '取 消',
              type: 'warning'
            }).then(() => {
              let params = {
                'order_id': this.order.id,
                'number': this.order.number,
                'patient_id': this.patientInfo.id,
                'record_time': this.record_date,

              }
              axios.get('http://127.0.0.1:9532/api/refund/post', {
                params: params
              })
                .then(function (response) {
                  if (response.data.state == 0) {
                    this.$message.error(response.data.msg)
                    this.loadingtwo = false
                    return false
                  } else {
                    this.$message({message: '退费成功', type: 'success'})
                    this.loadingtwo = false

                  }
                })
                .catch(function (error) {

                });

            }).catch(() => {

            })

          } else {

            this.$confirm('是否退费', '退费', {
              confirmButtonText: '确 定',
              cancelButtonText: '取 消',
              type: 'warning'
            }).then(() => {
              let params = {
                'order_id': this.order.id,
                'number': this.order.number,
                'patient_id': this.patientInfo.id,
                'record_time': this.record_date,

              }
              Refund(params).then(response => {
                if (response.data.state == 0) {
                  this.$message.error(response.data.msg)
                  this.loadingtwo = false
                  return false
                } else {
                  this.$message({message: '退费成功', type: 'success'})
                  this.loadingtwo = false

                }
              })

            }).catch(() => {

            })


          }


        } else if (index == 6) {

          if (this.patientInfo.id == 0) {
            this.$message.error('请先选择要挂号的病人')
            return
          }

          this.$refs.register.show(this.patientInfo)


        } else if (index == 7) {
          let params={
            start_time:"2020-12-28",
            end_time:"2020-12-30",
            insutype:"390",
            clr_type:"11",
          }

          if (this.$store.getters.xt_user.org_id == 4) {
            axios.get('http://127.0.0.1:9532/api/checkaccount/get', {
              params: params
            })
              .then(function (response) {
                if (response.data.state == 0) {
                  this.$message.error(response.data.msg)
                  this.loadingtwo = false
                  return false
                } else {


                }
              })
              .catch(function (error) {

              });


          }

        } else if (index == 8) {
          let params={
            start_time:"2020-12-28",
            end_time:"2020-12-30",
          }
          axios.get('http://127.0.0.1:9532/api/checkdetailaccount/get', {
            params: params
          })
            .then(function (response) {
              if (response.data.state == 0) {
                this.$message.error(response.data.msg)
                this.loadingtwo = false
                return false
              } else {

              }
            })
            .catch(function (error) {

            });



        } else if (index == 9){
          this.statementVisible = true
          let obj = {
            order_id:this.order.id
          }
          this.orderObj = obj
          // this.$refs.print.getInfo(this.order.id)


        }
      },

      choosePatient() {
        console.log(id)
        this.radioStatus = id
        this.getPatientList()
      },
      changeRadio(id) {
        // console.log(id)
        // this.radioStatus = id
        // this.getPatientList()
        let temp_id = id

        let params = {
          'record_date': this.record_date
        }
        getSchedulePatientList(params).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {
            this.patientTableData = []
            this.all_table_data = []
            let one_count = 0
            let two_count = 0
            let three_count = 0
            for (let i = 0; i < response.data.data.list.length; i++) {
              console.log('99999999', response.data.data.list)

              if (response.data.data.list[i].info.prescription_status == 0 || response.data.data.list[i].info.prescription_status == 1 || response.data.data.list[i].info.prescription_status == 2) {
                if (response.data.data.list[i].prescription != null && response.data.data.list[i].prescription.length > 0) {
                  one_count = one_count + 1
                  this.patientTableData.push(response.data.data.list[i])
                }
              }

              if (response.data.data.list[i].info.prescription_status == 3) {
                if (response.data.data.list[i].prescription != null && response.data.data.list[i].prescription.length > 0) {

                  two_count = two_count + 1
                }
              }

              if (response.data.data.list[i].info.prescription_status == 4) {
                if (response.data.data.list[i].prescription != null && response.data.data.list[i].prescription.length > 0) {
                  three_count = three_count + 1
                }
              }

              if (response.data.data.list[i].prescription != null && response.data.data.list[i].prescription.length > 0) {
                this.all_table_data.push(response.data.data.list[i])
              }
            }

            this.cal_one = one_count
            this.cal_two = two_count
            this.cal_three = three_count

            switch (temp_id) {

              case 1:
                this.patientTableData = []

                for (let i = 0; i < this.all_table_data.length; i++) {
                  if (this.all_table_data[i].info.prescription_status == 0 || this.all_table_data[i].info.prescription_status == 1 || this.all_table_data[i].info.prescription_status == 2) {
                    this.patientTableData.push(this.all_table_data[i])
                  }

                }
                break
              case 2:
                this.patientTableData = []
                for (let i = 0; i < this.all_table_data.length; i++) {
                  if (this.all_table_data[i].info.prescription_status == 3) {
                    this.patientTableData.push(this.all_table_data[i])
                  }


                }
                break
              case 3:
                this.patientTableData = []

                for (let i = 0; i < this.all_table_data.length; i++) {

                  if (this.all_table_data[i].info.prescription_status == 4) {
                    this.patientTableData.push(this.all_table_data[i])
                  }

                }
                break
              case 4:
                this.patientTableData = []
                this.patientTableData = this.all_table_data
                break

            }

            this.current_index = 0
            this.$refs.tab.setCurrentRow(this.patientTableData[0])
            this.getPatientInformation(this.patientTableData[0].patients.id, "")


          }
        })


      },

      //患者列表
      getPatientList() {
        let params = {
          'record_date': this.record_date
        }
        getSchedulePatientList(params).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {
            this.patientTableData = []
            this.all_table_data = []
            let one_count = 0
            let two_count = 0
            let three_count = 0
            for (let i = 0; i < response.data.data.list.length; i++) {
              // console.log('99999999', response.data.data.list)

              if (response.data.data.list[i].info.prescription_status == 0 || response.data.data.list[i].info.prescription_status == 1 || response.data.data.list[i].info.prescription_status == 2) {
                if (response.data.data.list[i].prescription != null && response.data.data.list[i].prescription.length > 0) {
                  one_count = one_count + 1
                  this.patientTableData.push(response.data.data.list[i])
                }
              }

              if (response.data.data.list[i].info.prescription_status == 3) {
                if (response.data.data.list[i].prescription != null && response.data.data.list[i].prescription.length > 0) {

                  two_count = two_count + 1
                }
              }

              if (response.data.data.list[i].info.prescription_status == 4) {
                if (response.data.data.list[i].prescription != null && response.data.data.list[i].prescription.length > 0) {
                  three_count = three_count + 1
                }
              }

              if (response.data.data.list[i].prescription != null && response.data.data.list[i].prescription.length > 0) {
                this.all_table_data.push(response.data.data.list[i])
              }
            }

            this.cal_one = one_count
            this.cal_two = two_count
            this.cal_three = three_count

            this.current_index = 0
            this.$refs.tab.setCurrentRow(this.patientTableData[0])
            this.getPatientInformation(this.patientTableData[0].patients.id, "")

          }
        })
      },

      unique(arr) {
        const res = new Map()
        return arr.filter((arr) => !res.has(arr.id_card_no) && res.set(arr.id_card_no, 1))
      },
      handleCurrentChange(val) {
        console.log('val', val)
        this.getPatientInformation(val.patients.id, val.info.batch_number)
        this.patient_id = val.patients.id

        for (let i = 0; i < this.patientTableData.length; i++) {
          if (this.patientTableData[i].patients.id == val.patients.id) {
            this.current_index = i
          }

        }

      },

      //获取患者的基本信息
      getPatientInformation(id, batch_number) {
        let params = {
          'record_date': this.record_date,
          'patient_id': id,
          'number': batch_number,
        }
        getPatientInfo(params).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {
            this.patientInfo = response.data.data.xt_info
            this.hisPatientInfo = response.data.data.his_info
            this.info = response.data.data.info
            this.order = response.data.data.order
            this.addtions_charge = response.data.data.addtions_charge

            this.prescriptions = []
            console.log("9991323242r5253535", response.data.data.prescription)
            if( response.data.data.prescription[0].advices.length > 0 ) {
              this.prescription_id = response.data.data.prescription[0].advices[0].prescription_id
            }else{
              this.prescription_id = 0
            }
            for (let i = 0; i < response.data.data.prescription.length; i++) {
              var prescription = response.data.data.prescription[i]

              let tempAdvice = []
              let tempProject = []
              let tempAddition = []

              for (let b = 0; b < prescription.advices.length; b++) {
                let obj = {
                  advice_id: prescription.advices[b].id,
                  drug_name: prescription.advices[b].advice_name,
                  single_dose: prescription.advices[b].single_dose,
                  delivery_way: prescription.advices[b].delivery_way,
                  execution_frequency: prescription.advices[b].execution_frequency,
                  retail_price: prescription.advices[b].price.toString(),
                  remark: prescription.advices[b].remark,
                  day: prescription.advices[b].day,
                  prescribing_number: prescription.advices[b].prescribing_number.toString(),
                  single_dose_unit: prescription.advices[b].single_dose_unit,
                  prescribing_number_unit: prescription.advices[b].prescribing_number_unit,
                  medical_insurance_number: prescription.advices[b].med_list_codg

                }
                tempAdvice.push(obj)
              }

              for (let b = 0; b < prescription.project.length; b++) {
                console.log(prescription.project[b].project.project_name)
                let obj = {
                  id: prescription.project[b].id,
                  project_id: prescription.project[b].project.id,
                  project_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,
                  number_days: prescription.project[b].day,
                  total: prescription.project[b].count.toString(),
                  price: prescription.project[b].price,
                  remark: prescription.project[b].remark,
                  medical_code: prescription.project[b].project.medical_code
                }
                tempProject.push(obj)
              }

              for (let b = 0; b < prescription.addition.length; b++) {
                let obj = {
                  id: prescription.addition[b].id,
                  item_name: prescription.addition[b].item_name,
                  price: prescription.addition[b].price,
                  count: prescription.addition[b].count,
                  item_id: prescription.addition[b].item_id,
                }
                tempAddition.push(obj)
              }


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

            }
          }
        })
      }
    },
    mounted() {
      console.log(this.$store.getters.xt_user.org_id)
      const that = this
      window.onresize = () => {
        return (() => {
          window.fullHeight = document.documentElement.clientHeight
          that.fullHeight = window.fullHeight
        })()
      }
    },
    watch: {
      fullHeight(val) {
        if (!this.timer) {
          this.fullHeight = val
          let tableHeight = val - 243
          this.tableHeight = tableHeight
          this.timer = true
          let that = this
          setTimeout(function () {
            that.timer = false
          }, 400)
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .app-container {
    height: 100%;
  }

  .outpatientChargesManagement {
    height: 100%;
    display: flex;
    flex-direction: column;

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

  .el-radio {
    margin-right: 5px;
  }

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

  .fixedCell {
    position: fixed;
    z-index: 99;
    right: 42px;
    background: #fff;
    display: flex;
    justify-content: space-between;
  }

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

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

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

  .el-tabs__item {
    padding: 0 10px;
  }

  }
  .addTab {
    position: absolute;
    right: 6px;
    top: 3px;
    z-index: 20;
  }

  .mainCenter {
    display: flex;
    flex: 1;
    height: 100%;
  }

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

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

  .preTabs {
    height: 100%;
    display: flex;
    flex-direction: column;

  .el-tab-pane {
    height: auto !important;
  }

  }

  .settlementTabs {
    flex: 1;

  .el-tab-pane {
    height: 100%;
  }

  .el-tabs__content {
    height: 100%;
  }

  }
  .centerRight {
    width: 300px;
    margin-left: 10px;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
  }

  .RP {
    color: #409EFF;
    font-size: 20px;
    margin-bottom: 5px;
  }

  .centerRightTitle {
    color: #409EFF;
  }

  .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;
  }

  }
  }
</style>

<style lang="scss">
  #tab-more {

  .el-icon-close {
    display: none;
  }

  }
  .settlementTabs {

  .el-tabs__content {
    height: 90%;
  }

  }
  .preTabs {

  .el-tabs__content {
    flex: 1;
    overflow-y: auto;
    padding: 5px 0 5px 5px;
  }

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

