<template>
  <div class="layout_container">
    <header>
      <el-button  type="success" icon="Plus"  @click="openShouFee" >收费</el-button>
      <el-button  type="primary" icon="Delete"  @click="clearData" >清空</el-button>
      <el-button  type="success" icon="Plus"  @click="openSjh" >设置收据号</el-button>
      <el-button  type="danger" icon="Money"  @click="openPos" >智能POS工具包</el-button>
    </header>
    <div class="layout_main">
      <div class="demo-collapse">
        <el-form ref="ruleFormRef"  label-width="127px" :model="formData"   class="demo-ruleForm"
                 :size="formSize">
          <el-collapse v-model="activeName">
            <el-collapse-item title="基本信息" name="1" disabled>
              <el-row>
                <el-col :span="6">
                  <el-form-item label="住院号" prop="inpatientNo">
                    <el-input v-model="formData.inpatientNo" clearable  @keyup.enter="queryPatientList(formData.inpatientNo)">
                      <template #append>
                        <el-button  icon="Search"  @click="queryPatientList(formData.inpatientNo)"/>
                      </template>
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="门诊号" prop="patientId">
                    <el-input v-model="formData.patientId" clearable  @keyup.enter="queryPatientList(formData.patientId)">
                      <template #append>
                        <el-button  icon="Search" @click="queryPatientList(formData.patientId)"/>
                      </template>
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="姓名" prop="name">
                    <el-input v-model="formData.name" disabled />
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="性别" prop="sexName">
                    <el-input v-model="formData.sexName" disabled />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="6">
                  <el-form-item label="床号" prop="bedNo">
                    <el-input v-model="formData.bedNo" disabled />
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="病房" prop="wardName">
                    <el-input v-model="formData.wardName" disabled />
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="入院日期" prop="admissDate">
                    <el-input v-model="formData.admissDate" disabled />
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="身份" prop="responceTypeName">
                    <el-input v-model="formData.responceTypeName" disabled />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="6">
                  <el-form-item label="总费用" prop="totalAmount">
                    <el-input v-model="formData.totalCharge" disabled />
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="余额" prop="wardName">
                    <el-input v-model="formData.balance" disabled />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="" prop="inpatientNoType">
                    <el-radio-group v-model="formData.inOutFlag" >
                      <el-radio-button label="在院病人" value="0" />
                      <el-radio-button label="出院病人" value="1" />
                    </el-radio-group>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-collapse-item>
          </el-collapse>
        </el-form>
      </div>
      <div class="layout_el-table">
        <el-table :data="tableData" stripe style="width: 100%">
          <el-table-column align="center"  property="admissTimes" label="住院次数" />
          <el-table-column align="center"  property="ledgerSn" label="账页号" />
          <el-table-column align="center" width="150" property="depoDate" label="时间" />
          <el-table-column align="center"  property="statusName" label="事务" />
          <el-table-column align="center"  property="depoAmount"  label="金额" />
          <el-table-column align="center"  property="receiptNo" label="收据号" />
          <el-table-column align="center"  property="depoTypeName" label="收费方式" >
          </el-table-column>
          <el-table-column align="center" property="chequeNo" label="凭证号" />
          <el-table-column align="center"  property="opIdName" label="操作员" />
          <el-table-column label="操作" width="200">
            <template #default="scope" >
              <el-button v-if="formData.inOutFlag ===0 && scope.row.settleType === '0' && scope.row.status === '1' && scope.row.reportFlag !== '1'" size="small" type="danger" icon="Back"  @click="openRefund(scope.row)" >退费</el-button>
              <el-button v-if="formData.inOutFlag ===0 && scope.row.printed !==1 && scope.row.status ==='1' && scope.row.reportFlag !=='1' " size="small" type="primary" icon="Printer" @click="printShouJu(scope.row.inpatientNo,scope.row.receiptNo,false)">打印收据</el-button>
              <el-button v-if="formData.inOutFlag ===0 && scope.row.status ==='1' && scope.row.printed === 1 && scope.row.reportFlag !=='1' " size="small" type="primary" icon="Printer" @click="printShouJu(scope.row.inpatientNo,scope.row.receiptNo,true)" >重打收据</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div style="width: 100%;height: 50px;margin-top: 10px;margin-left: 20px;font-size: 18px">
           <el-row>
             <el-col :span="12">预交金合计: {{formData.yjjHj}}</el-col>
             <el-col :span="12">直退押金合计: {{formData.ztYjjHj}}</el-col>
           </el-row>
        </div>
      </div>
    </div>
  </div>
  <el-dialog v-model="patientVisible" title="病人信息" width="500"  draggable>
    <el-table :data="patientTableData" style="width: 100%" height="300" stripe  border @row-click="fetchPatient" >
      <el-table-column align="center" prop="name" label="姓名"  />
      <el-table-column align="center"  prop="inpatientNo" label="住院号"  />
      <el-table-column align="center"  prop="admissTimes" label="住院次数"  />
      <el-table-column align="center"  prop="sexName" label="性别"  />
    </el-table>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="patientVisible = false">关闭</el-button>
      </div>
    </template>
  </el-dialog>
  <el-dialog v-model="shouFeeVisible" title="缴费" width="400"  @closed="clearShouFeeForm"  draggable>
    <el-form   class="demo-ruleForm" label-width="127px"
              :size="formSize" >
      <el-row>
        <el-col :span="24">
          <el-form-item label="收费方式*">
            <el-select v-model="shouFeeForm.depoType">
              <el-option v-for="item in zdChequeTypes" :key="item.code" :label="item.name" :value="item.code"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="金额">
            <el-input-number v-model="shouFeeForm.depoAmount"  :controls="false" :precision="2" :min="0"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="凭证号">
            <el-input v-model="shouFeeForm.chequeNo" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="shuaKa">刷卡/扫码</el-button>
        <el-button type="success" icon="Check" @click="saveShouFee">保存</el-button>
        <el-button @click="shouFeeVisible = false">关闭</el-button>
      </div>
    </template>
  </el-dialog>
  <el-dialog v-model="refundVisible" title="退费" width="400"   draggable>
    <el-form   class="demo-ruleForm" label-width="127px"
                >
      <el-row>
        <el-col :span="24">
          <el-form-item label="支付方式*">
            <el-select v-model="refundForm.depoType">
              <el-option v-for="item in zdChequeTypes" :key="item.code" :label="item.name" :value="item.code"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="金额">
            <el-input v-model="refundForm.depoAmount"  disabled/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="收据号">
            <el-input v-model="refundForm.receiptNo"  disabled/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="refundYjj(0)">普通退费</el-button>
        <el-button type="success" @click="refundYjj(1)">原路退费</el-button>
        <el-button @click="refundVisible = false">关闭</el-button>
      </div>
    </template>
  </el-dialog>
  <el-dialog v-model="sjhVisible" title="设置收据号" width="400"   draggable>
    <el-form   class="demo-ruleForm" label-width="127px"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="当前收据号:">
            <el-input-number v-model="currentNo" :min="1"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="saveSjh">保存</el-button>
        <el-button @click="sjhVisible = false">关闭</el-button>
      </div>
    </template>
  </el-dialog>
    <div id="report_table" v-show="false"
         style="width:192mm;height:97mm;margin:0 auto;border: 1px solid #337ab7;font-size: 10px;padding-top: 5mm;overflow-y: auto; overflow-x:hidden;">
      <div id="report_table_1">
        <div style="text-align:center;font-weight: 700;padding-top: -8mm">{{hospitalName}}</div>
        <div style="text-align:center;font-weight: 700;margin-top: 5px;">预交金收据</div>
        <div>
          <span>收据日期：</span><span style="margin-left: 7mm;" id="printDate">{{getFormatDatetime(new Date(),'YYYY-MM-DD')}}</span>
          <span style="margin-left: 54mm;">收据号：</span><span style="margin-left: 4mm;">{{printData.receiptNo}}</span><span
            style="margin-left: 2mm;">{{printData.cdFlag}}</span>
        </div>
        <table class="pageTable" style="width: 170mm;">
          <tbody>
          <tr>
            <td style="width: 15mm;">病人ID</td>
            <td style="width: 20mm;" >{{printData.patientId}}</td>
            <td>住院号</td>
            <td style="width: 20mm;" >{{printData.inpatientNo}}</td>
            <td colspan="3">姓名</td>
            <td colspan="2">{{printData.name}}</td>
          </tr>
          <tr>
            <td>住院科室</td>
            <td colspan="2">{{printData.admissDeptName}}</td>
            <td>支付方式</td>
            <td colspan="2" >{{printData.depoTypeName}}</td>
            <td colspan="2">操作类型</td>
            <td>交款</td>
          </tr>
          <tr>
            <td>金额</td>
            <td >{{printData.depoAmount}}</td>
            <td>支票号</td>
            <td colspan="6">{{printData.chequeNo}}</td>
          </tr>
          <tr>
            <td>大写金额</td>
            <td colspan="5">{{printData.depoAmountDx}}</td>
            <td colspan="2">收费员</td>
            <td >{{printData.opIdCodeName}}</td>
          </tr>
          <tr>
            <td colspan="9" rowspan="3" style="height: 26mm">
              1、此系临时收据，请妥善保管。出院时凭此收据按实用医疗费用换取住院发票。<br/>
              2、此收据遗失，请及时携带病人身份证和挂失人身份证到出院结算处办理挂失手续。如存在第三方肇事者预缴医疗费用，挂失时另需公安机关出示证明。<br/>
              3、如果您的缴费方式为银行卡支付，请您妥善保管好缴费的刷卡单，并于办理出院退费时带上缴费时所用银行卡。
            </td>
          </tr>
          </tbody>
        </table>
      </div>
    </div>
  <PosTransaction ref="posTransactionRef"></PosTransaction>
</template>
<script setup>
import {onMounted, ref} from "vue";
import router from "@/router";
import sleep from "@/utils/sleep";
import {
  queryAdvancePayment,
  saveAdvancePayment,
  printZyDepositFile,
  queryPrintAdvancePayment,
  queryAdvancePaymentPatientList
} from '@/api/zy-gl/advance-paymentDeal'
import {
  queryHospitalName
} from '@/api/zy-gl/zy-daily'
import {getLodop,initLodop} from "@/utils/c-lodop.js";
import {ElMessage, ElMessageBox} from "element-plus";
import PosTransaction from "@/components/zfsf/PosTransaction.vue"
import {queryZdChequeType} from "@/api/zfsf/cashier-process.js";
import {stringIsBlank, stringNotBlank} from "@/utils/blank-utils.js";
import {consume} from "@/api/zfsf/pos-transaction.js";
import {getFormatDatetime} from "../../../utils/date.js";
import {saveYjjSjh,queryYjjSjh} from '@/api/zfsf/sfbillno'
const sjhVisible = ref(false)
const currentNo = ref(null)
const openSjh = ()=>{
  sjhVisible.value = true
  queryYjjSjh({}).then(res=>{
    currentNo.value = res.currentNo
  })
}

const saveSjh =()=>{
  saveYjjSjh({
    currentNo :currentNo.value
  }).then(res=>{
    sjhVisible.value = false
  })
}

const hospitalName = ref('')
const posTransactionRef = ref(null)
const printData = ref({
  patientId:'',
  receiptNo:'',
  inpatientNo:'',
  name:'',
  admissDeptName:'',
  depoTypeName:'',
  depoAmount:'',
  depoAmountDx:'',
  opIdCodeName:'',
  cdFlag:'',
  depoDate:'',
})

onActivated(async ()=>{
  const params = router.currentRoute.value.query;
  if(params && params.patNo){
    await sleep(300);
    queryPatientList(params.patNo)
  }
})
const clearPrintData=()=>{
  printData.value = {
    patientId:'',
    inpatientNo:'',
    name:'',
    admissDeptName:'',
    depoTypeName:'',
    depoAmount:'',
    depoAmountDx:'',
    opIdCodeName:'',
    cdFlag:'',
  }
}

const formData = ref({
  inpatientNo:'',
  patientId:'',
  name:'',
  sexName:'',
  bedNo:'',
  wardName:'',
  admissDate:'',
  responceTypeName:'',
  totalCharge:null,
  balance:null,
  inOutFlag:0,
  yjjHj:0,
  ztYjjHj:0,
})
const shouFeeVisible = ref(false)
const shouFeeForm = ref({
  depoType : 'O',
  status : '',
  autoFlag : 0,
  inpatientNo : '',
  depoAmount : null,
  chequeNo : '',
  psordnum : '',
  agtordnum : '',
  transDate : '',
  parChannel : '',
  traceNo : '',
  receiptNo : '',
})
// 保存收费
const saveShouFee =()=>{
    shouFeeForm.value.inpatientNo = formData.value.inpatientNo
    shouFeeForm.value.status = "1"
    saveAdvancePayment(shouFeeForm.value).then(res=>{
       shouFeeVisible.value = false
      ElMessage.success('收费成功')
      ElMessageBox.confirm('请确认是否打印收据?', {
        cancelButtonText: '取消',
        confirmButtonText: '确定',
      })
          .then(() => {
            printShouJu(res.inpatientNo,res.receiptNo,false)
          })
    })

}

const refundVisible = ref(false)

const openRefund = (row)=>{
  refundForm.value.inpatientNo = row.inpatientNo
  refundForm.value.receiptNo = row.receiptNo
  refundForm.value.depoType = row.depoType
  refundForm.value.status = 2
  refundForm.value.depoAmount = row.depoAmount
  refundVisible.value = true
}

const refundForm =ref({
  depoType:'',
  inpatientNo:'',
  depoAmount:'',
  receiptNo:'',
  status:2,
  refundType:0,
})

// 退费
const refundYjj = (refundType)=>{
  refundForm.value.refundType = refundType
  saveAdvancePayment(refundForm.value).then(res=>{
    ElMessage.success('退费成功')
    if(stringNotBlank(res.warMessage)){
      ElMessage.warning(warMessage)
    }
    refundVisible.value = false
    queryPatientList(res.inpatientNo)
  })
}

// 打印收据 flag true 重打  false 首次打印
const printShouJu = (inpatientNo,receiptNo,flag)=>{
    // 重打
    if(flag){
      printReal(inpatientNo,receiptNo,flag)
    }else {
      printZyDepositFile(receiptNo).then(res=>{
        printReal(inpatientNo,receiptNo,flag)
        queryPatientList(inpatientNo)
      })
    }
}

const printReal =(inpatientNo,receiptNo,flag)=>{
  queryPrintAdvancePayment({
    inpatientNo:inpatientNo,
    receiptNo:receiptNo,
  }).then(res=>{
    printData.value.inpatientNo =res.inpatientNo
    printData.value.patientId = res.patientId
    printData.value.name = res.name
    printData.value.admissDeptName = res.admissDeptName
    printData.value.depoAmount = res.depoAmount
    printData.value.depoTypeName = res.depoTypeName
    printData.value.depoAmountDx = res.depoAmountDx
    printData.value.opIdCodeName = res.opIdCodeName
    printData.value.receiptNo = res.receiptNo
    printData.value.depoDate = getFormatDatetime(res.depoDate,'YYYY年MM月DD日')
    if(flag){
      printData.value.cdFlag = '(重打)'
    }else {
      printData.value.cdFlag = ''
    }
    setTimeout(printSjNew,1000)
  })
}

function printSjNew(){
  let LODOP = getLodop();
  //设置默认打印机
  LODOP.SET_PRINTER_INDEX(-1);
  LODOP.SET_PRINT_STYLE("FontSize", 9); //字体大小
  LODOP.SET_SHOW_MODE("BKIMG_WIDTH", "80mm");
  LODOP.SET_SHOW_MODE("BKIMG_HEIGHT", "108mm");
  LODOP.SET_PRINT_PAGESIZE(1, "80mm", "108mm", "CreateCustomPage");
  LODOP.SET_PRINT_MODE("CREATE_CUSTOM_PAGE_NAME", "住院预交金");//对新建的纸张重命名
  LODOP.SET_PRINT_STYLE("FontSize", 11); //字体大小
  LODOP.ADD_PRINT_TEXT("1mm", "6mm", "75mm", "3mm", hospitalName.value+"预交金缴费凭证"+printData.value.cdFlag);
  LODOP.ADD_PRINT_TEXT("6mm", "2mm", "30mm", "3mm", "姓名:"+printData.value.name);
  LODOP.ADD_PRINT_TEXT("6mm", "32mm", "40mm", "3mm", "住院号:"+printData.value.inpatientNo);
  LODOP.ADD_PRINT_TEXT("11mm", "2mm", "75mm", "3mm", "住院科室:"+printData.value.admissDeptName);
  LODOP.ADD_PRINT_TEXT("16mm", "2mm", "75mm", "3mm", "收据号:"+printData.value.receiptNo);
  LODOP.ADD_PRINT_TEXT("21mm", "2mm", "75mm", "3mm", "支付方式:"+printData.value.depoTypeName);
  LODOP.ADD_PRINT_TEXT("27mm", "2mm", "80mm", "3mm", "-------------------------------------");
  LODOP.ADD_PRINT_TEXT("32mm", "2mm", "35mm", "3mm", "金额:"+printData.value.depoAmount);
  LODOP.ADD_PRINT_TEXT("32mm", "37mm", "45mm", "3mm", "大写金额:"+printData.value.depoAmountDx);
  LODOP.ADD_PRINT_TEXT("38mm", "2mm", "75mm", "3mm", "收费员:"+printData.value.opIdCodeName);
  LODOP.ADD_PRINT_TEXT("44mm", "2mm", "80mm", "3mm", "收费日期:"+printData.value.depoDate);
  LODOP.ADD_PRINT_TEXT("50mm", "2mm", "80mm", "3mm", "-------------------------------------");
  LODOP.ADD_PRINT_TEXT("56mm", "2mm", "80mm", "3mm", "1、此系临时收据，请妥善保管。出院时凭此");
  LODOP.ADD_PRINT_TEXT("62mm", "2mm", "80mm", "3mm", "收据按实用医疗费用换取住院发票。");
  LODOP.ADD_PRINT_TEXT("68mm", "2mm", "80mm", "3mm", "2、此收据遗失，请及时携带病人身份证和挂");
  LODOP.ADD_PRINT_TEXT("72mm", "2mm", "80mm", "3mm", "失人身份证到出院结算处办理挂失手续。");
  LODOP.ADD_PRINT_TEXT("78mm", "2mm", "80mm", "3mm", "如存在第三方肇事者预缴医疗费用，挂失时");
  LODOP.ADD_PRINT_TEXT("84mm", "2mm", "80mm", "3mm", "另需公安机关出示证明。");
  LODOP.ADD_PRINT_TEXT("90mm", "2mm", "80mm", "3mm", "3、如果您的缴费方式为银行卡支付，请您妥");
  LODOP.ADD_PRINT_TEXT("96mm", "2mm", "80mm", "3mm", "善保管好缴费的刷卡单，并于办理出院退费");
  LODOP.ADD_PRINT_TEXT("102mm", "2mm", "80mm", "3mm", "时带上缴费时所用银行卡。");
  if(printData.value.cdFlag ===''){
    LODOP.NewPageA();
    LODOP.ADD_PRINT_TEXT("1mm", "6mm", "75mm", "3mm", hospitalName.value+"预交金缴费凭证"+printData.value.cdFlag);
    LODOP.ADD_PRINT_TEXT("6mm", "2mm", "30mm", "3mm", "姓名:"+printData.value.name);
    LODOP.ADD_PRINT_TEXT("6mm", "32mm", "40mm", "3mm", "住院号:"+printData.value.inpatientNo);
    LODOP.ADD_PRINT_TEXT("11mm", "2mm", "75mm", "3mm", "住院科室:"+printData.value.admissDeptName);
    LODOP.ADD_PRINT_TEXT("16mm", "2mm", "75mm", "3mm", "收据号:"+printData.value.receiptNo);
    LODOP.ADD_PRINT_TEXT("21mm", "2mm", "75mm", "3mm", "支付方式:"+printData.value.depoTypeName);
    LODOP.ADD_PRINT_TEXT("27mm", "2mm", "80mm", "3mm", "-------------------------------------");
    LODOP.ADD_PRINT_TEXT("32mm", "2mm", "35mm", "3mm", "金额:"+printData.value.depoAmount);
    LODOP.ADD_PRINT_TEXT("32mm", "37mm", "45mm", "3mm", "大写金额:"+printData.value.depoAmountDx);
    LODOP.ADD_PRINT_TEXT("38mm", "2mm", "75mm", "3mm", "收费员:"+printData.value.opIdCodeName);
    LODOP.ADD_PRINT_TEXT("44mm", "2mm", "80mm", "3mm", "收费日期:"+printData.value.depoDate);
    LODOP.ADD_PRINT_TEXT("50mm", "2mm", "80mm", "3mm", "-------------------------------------");
    LODOP.ADD_PRINT_TEXT("56mm", "2mm", "80mm", "3mm", "1、此系临时收据，请妥善保管。出院时凭此");
    LODOP.ADD_PRINT_TEXT("62mm", "2mm", "80mm", "3mm", "收据按实用医疗费用换取住院发票。");
    LODOP.ADD_PRINT_TEXT("68mm", "2mm", "80mm", "3mm", "2、此收据遗失，请及时携带病人身份证和挂");
    LODOP.ADD_PRINT_TEXT("72mm", "2mm", "80mm", "3mm", "失人身份证到出院结算处办理挂失手续。");
    LODOP.ADD_PRINT_TEXT("78mm", "2mm", "80mm", "3mm", "如存在第三方肇事者预缴医疗费用，挂失时");
    LODOP.ADD_PRINT_TEXT("84mm", "2mm", "80mm", "3mm", "另需公安机关出示证明。");
    LODOP.ADD_PRINT_TEXT("90mm", "2mm", "80mm", "3mm", "3、如果您的缴费方式为银行卡支付，请您妥");
    LODOP.ADD_PRINT_TEXT("96mm", "2mm", "80mm", "3mm", "善保管好缴费的刷卡单，并于办理出院退费");
    LODOP.ADD_PRINT_TEXT("102mm", "2mm", "80mm", "3mm", "时带上缴费时所用银行卡。");
  }
  LODOP.PREVIEW();
  // LODOP.PRINT();;
  clearPrintData()
}

const clearShouFeeForm =()=>{
  shouFeeForm.value = {
    depoType : 'O',
    status : '',
    autoFlag : 0,
    inpatientNo : '',
    depoAmount : '',
    chequeNo : '',
    psordnum : '',
    agtordnum : '',
    transDate : '',
    parChannel : '',
    traceNo : '',
    receiptNo : '',
  }
}

// 刷卡
const shuaKa =()=>{
  if(stringIsBlank(shouFeeForm.value.depoAmount)){
    ElMessage.error('输入收费金额')
    return
  }
  let obj = {
    chequeType : shouFeeForm.value.depoType,
    amt : shouFeeForm.value.depoAmount,
    type : 1
  }
  consume(obj).then(res=>{
    // 1 联机刷卡 0 不联机 直接输凭证号
    shouFeeForm.value.autoFlag = 1
    shouFeeForm.value.psordnum = res.refNo
    shouFeeForm.value.agtordnum = res.wxAliPayOrderNo
    shouFeeForm.value.transDate = res.transDate
    shouFeeForm.value.parChannel = res.payChannel
    shouFeeForm.value.traceNo = res.traceNo
    shouFeeForm.value.receiptNo = res.lsOrderNo
  })
}

const openShouFee = ()=>{
  if(stringIsBlank(formData.value.name)){
    ElMessage.error('请先查询出要缴费的病人信息')
    return
  }
  shouFeeVisible.value = true

}
const zdChequeTypes = ref([])

const openPos = ()=>{
  posTransactionRef.value.openOuter()
}
const patientVisible = ref(false)
const patientTableData = ref([])
const clearData = ()=>{
  formData.value = {
    inpatientNo:'',
    patientId:'',
    name:'',
    sexName:'',
    bedNo:'',
    wardName:'',
    admissDate:'',
    responceTypeName:'',
    totalCharge:null,
    balance:null,
    inOutFlag:0,
    yjjHj:0,
    ztYjjHj:0,
  }
  tableData.value = []
}

function setformData(data){
  formData.value.inpatientNo = data.inpatientNo
  formData.value.patientId = data.patientId
  formData.value.name = data.name
  formData.value.sexName = data.sexName
  formData.value.bedNo = data.bedNo
  formData.value.wardName = data.wardName
  formData.value.admissDate = data.admissDate
  formData.value.responceTypeName = data.responceTypeName
  formData.value.totalCharge = data.totalCharge
  formData.value.balance = data.balance
  formData.value.yjjHj = data.yjjHj
  formData.value.ztYjjHj = data.ztYjjHj
}

onMounted(()=>{
  initLodop()
  queryZdChequeType().then(res=>{
    zdChequeTypes.value = res
  })
  queryHospitalName().then(res=>hospitalName.value = res)
})


const fetchPatient =(row)=>{
    queryAdvancePayment({
      inpatientNo:row.inpatientNo,
      inOutFlag:formData.value.inOutFlag,
      admissTimes:row.admissTimes,
    }).then(res=>{
      patientVisible.value = false
      if(res == null || res.length === 0){
        ElMessage.error('没有查询到数据')
        clearData()
      }else {
        setformData(res)
        tableData.value = res.zyDepositFiles
      }
    })
}
const queryPatientList = (code)=>{
    queryAdvancePaymentPatientList({
      inpatientNo : code,
      inOutFlag : formData.value.inOutFlag
    }).then(res=>{
       if(res == null || res.length === 0){
         ElMessage.error('没有查询到数据')
         clearData()
       }
       else if(res.length ===1){
          let row = {
            inpatientNo:res[0].inpatientNo,
            admissTimes:res[0].admissTimes,
          }
          fetchPatient(row)
        }
        else if(res.length > 1){
          patientVisible.value = true
          patientTableData.value = res
        }

    })
}
const tableData = ref([])
const activeName = ref(['1', '2'])
const formSize = ref('default')
</script>
<style scoped lang="scss">
.pageTable tr td, .pageTable tr th {
  border: 1px solid black;
  text-align: center;
  font-size: 12px;
  padding: 0 0;

}
.pageTable {
  border-collapse: collapse;
}
</style>