<template>
  <page-layer>
    <template #header>
      <el-button icon="Search" type="primary" @click="queryTermDialog = true">检索条件</el-button>
      <el-divider direction="vertical"></el-divider>
      <el-button v-if="auditQuanXian()" :disabled="queryTerm.list.length === 0" icon="Upload" type="success"
                 @click="upldAllList"> 选中上传
      </el-button>
      <el-button v-if="auditQuanXian()" :disabled="queryTerm.list.length === 0" icon="Upload" type="warning"
                 @click="upldSelections"> 全部上传
      </el-button>
      <el-button v-if="auditQuanXian()" :disabled="queryTerm.list.length === 0" icon="Download" type="success"
                 @click="exportExcel">导出 Execl
      </el-button>
      <el-button icon="RefreshLeft" type="primary" @click="queryCurrentPage">刷新数据</el-button>
      <el-button v-if="auditQuanXian()" type="success" @click="xianZhiShiJianClick">设置限制时间</el-button>
      <el-button  type="success" @click="toUploadInfo">上传日志</el-button>
      <el-button  type="success" @click="toUploadDrgGroupInfo">分组信息</el-button>
      <el-button  type="success" v-if="bmyQuaXian()" @click="openSetlDeptConfGialog">科室配置</el-button>
      <xian-zhi-shi-jian ref="xianZhiShiJian"></xian-zhi-shi-jian>
    </template>
    <template #main>
      <el-table ref="tableRef" :data="queryTerm.list" :height="store.state.app.windowSize.h - 55" border
                highlight-current-row stripe @selection-change="handleSelectionChange">
        <el-table-column fixed type="selection" width="35"></el-table-column>
        <el-table-column fixed label="姓名" prop="psnName">
          <template #default="scope">
            <el-button text @click="jieSuanDanXinXi(scope.row)" type="primary">
              <span v-html="auditName(scope.row)"></span>
            </el-button>
          </template>
        </el-table-column>
        <el-table-column fixed label="审核状态" prop="auditFlagName">
        </el-table-column>
        <el-table-column fixed label="质控提醒" prop="qualityReminder"  width="100">
          <template #default="scope">
            <span style="color: red">{{scope.row.qualityReminder}}</span>
          </template>
        </el-table-column>
        <el-table-column fixed label="已上传能修改提醒" prop="uploadUpdateReminder"  width="110">
          <template #default="scope">
            <span style="color: red">{{scope.row.uploadUpdateReminder}}</span>
          </template>
        </el-table-column>
        <el-table-column fixed label="上传截止日期" prop="uploadCutoffDate"  width="100">
          <template #default="scope">
            <span style="color: red">{{scope.row.uploadCutoffDate}}</span>
          </template>
        </el-table-column>
        <el-table-column fixed label="上传状态" prop="uploadStatus"></el-table-column>
        <el-table-column label="出院科室" prop="outDeptName">
          <template #default="scope">
            <el-button
                type="primary"
                v-if="scope.row.outDept === dept || auditQuanXian"
                text
                @click="huoQuXiuGaiYiShen(scope.row.referPhysician, scope.row.referPhysicianName, scope.row.patNo, scope.row.times)"
            >
              {{ scope.row.outDeptName }}
            </el-button>
            <span v-else>{{ scope.row.outDeptName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="住院号/门诊号" prop="patNo" width="100"></el-table-column>
        <el-table-column label="结算时间" prop="setlTime"></el-table-column>
        <el-table-column label="出院时间" prop="disDate"></el-table-column>
        <el-table-column label="管床医生" prop="referPhysicianName"></el-table-column>
        <el-table-column v-if="bmyQuaXian()" label="编码员备注" prop="bmyBz" width="150">
          <template #default="scope">
            <el-button
                    type="primary"
                    text
                    @click="bianJiBmyBz(scope.row)"
            >
              {{ scope.row.bmyBz?scope.row.bmyBz: '无'}}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="医疗类别" prop="iptMedType">
          <template #default="scope">
            <el-button text type="primary" @click="clickToModifyMedicalCategory(scope.row)">
              {{ getIptMedName(scope.row.iptMedType) }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="治疗方式" prop="operationName"></el-table-column>
        <el-table-column label="总费用" prop="medfeeSumamt"></el-table-column>
        <el-table-column label="报销金额" prop="fundPaySumamt"></el-table-column>
        <el-table-column label="险种类型" prop="insutypeName"></el-table-column>
        <el-table-column label="人员类别" prop="psnTypeName"></el-table-column>
        <el-table-column label="性别" prop="gendName" width="40"></el-table-column>
        <el-table-column label="住院天数" prop="actIptDays"></el-table-column>
        <el-table-column label="医疗类别" prop="medTypeName"></el-table-column>
        <el-table-column label="清算机构" prop="clrOptinsName"></el-table-column>
        <el-table-column label="参保地" prop="insuplcAdmdvsName"></el-table-column>
        <el-table-column label="转科室" prop="zhuanKeName"></el-table-column>
        <el-table-column label="申请备注" prop="reqRemark"></el-table-column>
        <el-table-column v-if="auditQuanXian()" label="结算单" fixed="right" align="center" width="120">
          <template #default="scope">
            <el-button  @click="jieSuanDanClick(scope.row)">查看结算单</el-button>
            <el-button  @click="getUploadInfo(scope.row)">查看上传信息</el-button>
            <el-button  @click="getdrgGroupResultInfo(scope.row)">查看分组信息</el-button>
          </template>
        </el-table-column>
      </el-table>
      <jie-suan-dan-da-ying v-if="daYingJieSuanDanRef.dialog" :data="daYingJieSuanDanRef"
                            @close="daYingJieSuanDanRef.dialog = false"></jie-suan-dan-da-ying>
      <el-pagination
          :current-page="queryTerm.currentPage"
          :page-size="queryTerm.pageSize"
          :page-sizes="[10, 20, 30, 40, 50, 100]"
          :total="queryTerm.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      >
      </el-pagination>
      <el-dialog v-model="queryTermDialog" title="数据筛选" width="60%">
        <el-form ref="queryTermRef" :model="queryTerm" label-width="120px" size="small">
          <el-row>
            <el-col :span="spanWidth">
              <el-form-item label="日期范围" prop="dateRange">
                <el-date-picker
                    v-model="queryTerm.dateRange"
                    :shortcuts="clockinShortcuts"
                    end-placeholder="结束日期"
                    placeholder="选择日期"
                    range-separator="至"
                    size="small"
                    start-placeholder="开始日期"
                    type="daterange"
                ></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="spanWidth">
              <el-form-item label="结算类型" prop="clrType">
                <el-select v-model="queryTerm.clrType" placeholder="结算类别" style="width: 100px">
                  <el-option v-for="item in clrTypes" :key="item.code" :label="item.name" :value="item.code">
                    <span style="color: #8492a6; font-size: 12px">{{ item.code }}</span>
                    <el-divider direction="vertical"></el-divider>
                    <span>{{ item.name }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="spanWidth">
              <el-form-item label="住院号" prop="patNo">
                <el-input v-model="queryTerm.patNo" clearable placeholder="住院号" style="width: 120px"
                          @blur="patNo = $event.target.value.trim()" @keyup.enter="query"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="spanWidth">
              <el-form-item label="住院次数" prop="times">
                <el-input v-model="queryTerm.times" clearable placeholder="住院次数" style="width: 120px"
                          @blur="times = $event.target.value.trim()" @keyup.enter="query"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="spanWidth">
              <el-form-item label="出院科室" prop="disDept">
                <el-select v-model="queryTerm.outDept" :collapse-tags="true" clearable filterable multiple
                           placeholder="出院科室">
                  <el-option v-for="item in disDepts" :key="item.code" :label="item.name" :value="item.code">
                    <span style="color: #8492a6; font-size: 12px">{{ item.code }}</span>
                    <el-divider direction="vertical"></el-divider>
                    <span>{{ item.name }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="spanWidth">
              <el-form-item label="医疗类别" prop="medType">
                <el-select v-model="queryTerm.medType" :collapse-tags="true" clearable filterable multiple
                           placeholder="医疗类别">
                  <el-option v-for="item in medTypes" :key="item.code" :label="item.name" :value="item.code">
                    <span style="color: #8492a6; font-size: 12px">{{ item.code }}</span>
                    <el-divider direction="vertical"></el-divider>
                    <span>{{ item.name }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="spanWidth">
              <el-form-item label="上传类型" prop="psnType">
                <el-select v-model="queryTerm.flag">
                  <el-option v-for="item in flags" :key="item.code" :label="item.name" :value="item.code">
                    <span style="color: #8492a6; font-size: 12px">{{ item.code }}</span>
                    <el-divider direction="vertical"></el-divider>
                    <span>{{ item.name }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="spanWidth">
              <el-form-item label="管床医生" prop="referPhysician">
                <el-select v-model="queryTerm.referPhysician" :remote-method="remoteMethod" clearable filterable
                           placeholder="管床医生" remote reserve-keyword>
                  <el-option v-for="item in referPhysicians" :key="item.code" :label="item.name" :value="item.code">
                    <span style="color: #8492a6; font-size: 12px">{{ item.code }}</span>
                    <el-divider direction="vertical"></el-divider>
                    <span>{{ item.name }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="spanWidth">
              <el-form-item label="险种类型" prop="insutype">
                <el-select v-model="queryTerm.insutype" :collapse-tags="true" clearable filterable multiple
                           placeholder="险种类型">
                  <el-option v-for="item in insutypes" :key="item.code" :label="item.name" :value="item.code">
                    <span style="color: #8492a6; font-size: 12px">{{ item.code }}</span>
                    <el-divider direction="vertical"></el-divider>
                    <span>{{ item.name }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="spanWidth">
              <el-form-item label="人员类别" prop="psnType">
                <el-select v-model="queryTerm.psnType" :collapse-tags="true" clearable filterable multiple
                           placeholder="人员类别">
                  <el-option v-for="item in getPsnType" :key="item.code" :label="item.name" :value="item.code">
                    <span style="color: #8492a6; font-size: 12px">{{ item.code }}</span>
                    <el-divider direction="vertical"></el-divider>
                    <span>{{ item.name }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="spanWidth">
              <el-form-item label="结算机构" prop="clrOptins">
                <el-select v-model="queryTerm.clrOptins" :collapse-tags="true" clearable filterable multiple
                           placeholder="结算机构">
                  <el-option v-for="item in clrOptins" :key="item.code" :label="item.name" :value="item.code">
                    <span style="color: #8492a6; font-size: 12px">{{ item.code }}</span>
                    <el-divider direction="vertical"></el-divider>
                    <span>{{ item.name }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="spanWidth">
              <el-form-item label="参保机构" prop="insuplcAdmdvs">
                <el-select v-model="queryTerm.insuplcAdmdvs" :collapse-tags="true" clearable filterable multiple
                           placeholder="参保机构">
                  <el-option v-for="item in clrOptins" :key="item.code" :label="item.name" :value="item.code">
                    <span style="color: #8492a6; font-size: 12px">{{ item.code }}</span>
                    <el-divider direction="vertical"></el-divider>
                    <span>{{ item.name }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="spanWidth">
              <el-form-item label="审核类型" prop="auditFlag">
                <el-select v-model="queryTerm.auditFlag">
                  <el-option v-for="item in auditFlags" :key="item.code" :label="item.name" :value="item.code">
                    <span style="color: #8492a6; font-size: 12px">{{ item.code }}</span>
                    <el-divider direction="vertical"></el-divider>
                    <span>{{ item.name }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="spanWidth">
              <el-form-item label="申报类型" prop="auditFlag">
                <el-select v-model="queryTerm.decTypes" :collapse-tags="true" clearable filterable multiple>
                  <el-option v-for="item in decTypes" :key="item.code" :label="item.name" :value="item.code">
                    <span style="color: #8492a6; font-size: 12px">{{ item.code }}</span>
                    <el-divider direction="vertical"></el-divider>
                    <span>{{ item.name }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="spanWidth">
              <el-form-item label="支付类型" prop="hiPaymtd">
                <el-select v-model="queryTerm.hiPaymtd" :collapse-tags="true" clearable>
                  <el-option v-for="item in hiPaymtdData" :key="item.code" :label="item.name" :value="item.code">
                    <span style="color: #8492a6; font-size: 12px">{{ item.code }}</span>
                    <el-divider direction="vertical"></el-divider>
                    <span>{{ item.name }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="spanWidth">
              <el-form-item label="上传截止倒数天数" prop="hiPaymtd">
                <el-select v-model="queryTerm.uploadJieZhiDay" :collapse-tags="true" clearable filterable multiple>
                  <el-option v-for="item in uploadJieZhiData" :key="item.code" :label="item.name" :value="item.code">
                    <span style="color: #8492a6; font-size: 12px">{{ item.code }}</span>
                    <el-divider direction="vertical"></el-divider>
                    <span>{{ item.name }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-button icon="Search" size="small" type="primary" @click="query">查询</el-button>
        <el-button icon="Search" size="small" type="primary" @click="zhongKongTiaoJian">总控数据条件</el-button>

        <el-button icon="RefreshRight" size="small" type="warning" @click="chongZhi">重置</el-button>
        <el-divider direction="vertical"></el-divider>
      </el-dialog>
      <el-dialog v-model="xiuGaiYiShen" title="修改管床医生">
        <el-select v-model="guanChuangYiShen.yiShenCode" :remote-method="remoteMethod" clearable filterable remote
                   reserve-keyword>
          <el-option v-for="item in referPhysicians" :key="item.code" :label="item.name" :value="item.code">
            <span style="color: #8492a6; font-size: 12px">{{ item.code }}</span>
            <el-divider direction="vertical"></el-divider>
            <span>{{ item.name }}</span>
          </el-option>
        </el-select>
        <el-divider direction="vertical"></el-divider>
        <el-button @click="xiuGaiGuanChuangYiShenClick">修改</el-button>
      </el-dialog>
      <el-dialog v-model="xiuGaibmyBzdata.dialog" title="修改编码员备注" center>
        <el-input v-model="xiuGaibmyBzdata.bmyBz" clearable placeholder="编码员备注" type="textarea"
                   ></el-input>
        <template #footer>
          <span >
            <el-button type="primary" @click="xiuGaibmyBzClick">保存</el-button>
          </span>
        </template>
      </el-dialog>
      <jie-suan-dan-xiang-qing v-if="jieSuanDanDataDialog" :data="jieSuanDanData" @close="jieSuanDanDataDialog = false"
                               @queryCurrentPage="queryCurrentPage"></jie-suan-dan-xiang-qing>
      <Progress/>
      <el-dialog v-model="medType.dialog" title="修改住院医疗类别">
        <el-radio-group v-model="medType.iptMedType">
          <el-radio :label="1">住院</el-radio>
          <el-radio :label="2">日间手术</el-radio>
        </el-radio-group>
        <el-divider direction="vertical"></el-divider>
        <el-button @click="clickToSaveMedicalCategory">保存</el-button>
      </el-dialog>
      <el-dialog v-model="setlDeptConf.dialog" title="修改科室配置信息">
        <el-select v-model="setlDeptConf.outDept" :collapse-tags="true" clearable filterable multiple
                   placeholder="出院科室">
          <el-option v-for="item in disDepts" :key="item.code" :label="item.name" :value="item.code">
            <span style="color: #8492a6; font-size: 12px">{{ item.code }}</span>
            <el-divider direction="vertical"></el-divider>
            <span>{{ item.name }}</span>
          </el-option>
        </el-select>
        <el-divider direction="vertical"></el-divider>
        <el-button @click="clicksaveSetlDeptConfInfo">保存</el-button>
      </el-dialog>
    </template>
  </page-layer>
</template>

<script>
import {clockinShortcuts} from '@/data/shortcuts'
import {onMounted, onActivated,ref, unref, watch} from 'vue'
import store from '../../../store'
import {getClrTypes, getInsutypes} from '@/api/medical-insurance/si-dict'
import {getDateRangeFormatDate} from '@/utils/date'
import {
  getMedType,
  huoQuJieSuanRenYuan,
  jieSuanDanKeXiuGaiXinXi,
  modifyMedicalCategory,
  upldSetlList,
  upldSetlListTimes,
  xiuGaiGuanChuangYiShen,
  querySetlDeptConfInfo,
  saveSetlDeptConfInfo,
  getUploadMaxDay,
  updateBmyBz
} from '@/api/medical-insurance/si-setl-upload'
import {clrOptinsData, decTypes, getPsnType} from '@/data'
import {getDept} from '@/api/inpatient/xiang-mu-lu-ru'
import JieSuanDanXiangQing from '../../../components/si-sheet-upload/jie-suan-dan-xiu-gai/JieSuanDanXiangQing.vue'
import {ElMessage, ElMessageBox} from 'element-plus'
import Progress from '../../../components/progress/Index.vue'
import {downloadExcel} from '@/utils/excel'
import {getRenYuan,getZhuYuanDept} from '@/api/public-api'
import {
  auditFlags,
  auditName,
  huoQuZhongKongChaXunTiaoJian,
  shenHeQuanXian,
  auditQuanXian,
  bmyQuaXian
} from '@/components/si-sheet-upload/jieSuanDanXiuGai'
import XianZhiShiJian from '../../../components/si-sheet-upload/XianZhiShiJian.vue'
import {stringIsBlank, stringNotBlank} from '@/utils/blank-utils'
import JieSuanDanDaYing from '../../../components/si-sheet-upload/jieSuanDanDaYing.vue'
import ExportAutoUploadInfo from "@/components/si-sheet-upload/ExportAutoUploadInfo.vue";
import PageLayer from "@/layout/PageLayer";
import router from "@/router";
import sleep from "@/utils/sleep";

export default {
  name: 'SetSheetUpload',
  components: {PageLayer, ExportAutoUploadInfo, JieSuanDanDaYing, XianZhiShiJian, JieSuanDanXiangQing, Progress},
  setup: function () {
    const queryTerm = ref({
      startTime: '',
      endTime: '',
      patNo: '', // 0406567
      referPhysician:  '',
      insutype: [],
      dateRange: [clockinShortcuts[0].value[0], clockinShortcuts[0].value[1]],
      outDept: [],
      insuplcAdmdvs: [],
      clrType: '21',
      medType: [],
      clrOptins: [],
      decTypes: [],
      flag: 0,
      hiPaymtd: 0,
      auditFlag: 4,
      psnType: [],
      currentPage: 1,
      pageSize: 40,
      total: 0,
      list: [],
      times:null,
      uploadJieZhiDay:[]
    })
    const dept = store.state.user.info.deptCode
    // 表单
    const queryTermRef = ref(null)
    // 表格的宽度
    const spanWidth = ref(8)
    // 查询的 对话框
    const queryTermDialog = ref(true)
    // 结算单信息
    const jieSuanDanData = ref({})
    // 险种类型
    const insutypes = ref([])
    // 出院科室
    const disDepts = ref([])
    // 结算类别
    const clrTypes = ref([])
    // 医疗类别
    const medTypes = ref([])
    // 结算机构
    const clrOptins = ref([])
    // 管床医生
    const referPhysicians = ref([])

    const selections = ref([])
    // 表格 ref
    const tableRef = ref(null)

    const jieSuanDanDataDialog = ref(false)
    //跳转上传日志
    const toUploadInfo =() =>{
      router.push({
        name: 'ExportAutoUploadInfo'
      })
    }
    //跳转分组信息
    const toUploadDrgGroupInfo =()=>{
      router.push({
        name: 'DrgGroupResult'
      })
    }
    //单个查询上传信息
    const getUploadInfo = (row)=>{
      router.push({
        name: 'ExportAutoUploadInfo',
        query:{
          patNo:row.patNo,
          times:row.times,
          ledgerSn:row.ledgerSn
        }
      })
    }
    //查询分组信息
    const getdrgGroupResultInfo = (row)=>{
      router.push({
        name: 'DrgGroupResult',
        query:{
          patNo:row.patNo,
          times:row.times,
          ledgerSn:row.ledgerSn
        }
      })
    }

    const handleSelectionChange = (val) => {
      selections.value = val
    }

    const query = async () => {
      let form = unref(queryTermRef)
      if (!form) return
      try {
        await form.validate()
        let dates = getDateRangeFormatDate(queryTerm.value.dateRange)
        queryTerm.value.startTime = dates.startTime
        queryTerm.value.endTime = dates.endTime
        queryTerm.value.total = 0
        queryTerm.value.currentPage = 1
        huoQuJieSuanRenYuan(queryTerm.value).then((res) => {
          queryTerm.value.list = res === null ? [] : res.records
          queryTerm.value.total = res.total
          queryTermDialog.value = false
        })
      } catch (error) {
      }
    }

    onActivated(async () => {
      const params = router.currentRoute.value.query
      if (params && params.patNo && params.times) {
        queryTerm.value.times = params.times
        queryTerm.value.patNo = params.patNo
        queryTerm.value.dateRange = []
        queryTerm.value.referPhysician=''
        queryTermDialog.value=false
        await sleep(300);
        await queryCurrentPage()
      }
    })

    const queryCurrentPage = async () => {
      let form = unref(queryTermRef)
      if (!form) return
      try {
        await form.validate()
        let dates = getDateRangeFormatDate(queryTerm.value.dateRange)
          queryTerm.value.startTime = dates.startTime
          queryTerm.value.endTime = dates.endTime
        huoQuJieSuanRenYuan(queryTerm.value).then((res) => {
          queryTerm.value.list = res.records
          queryTerm.value.total = res.total
        })
      } catch (error) {
      }
    }

    watch(
        () => store.state.app.windowSize.w,
        () => {
          const w = store.state.app.windowSize.w
          if (w >= 1500) {
            spanWidth.value = 8
          } else if (w >= 1300) {
            spanWidth.value = 12
          } else {
            spanWidth.value = 24
          }
        }
    )

    const upldAllList = () => {
      upldSetlList(selections.value).catch(() => {
        store.commit('app/setJdt', {title: '', isOpen: false, closeButton: true})
      })
    }

    const upldSelections = () => {
      if (queryTerm.value.list.length === 0) {
        ElMessage({
          message: '请先查询出数据',
          type: 'warning',
          showClose: true,
        })
        return
      }
      upldSetlListTimes(queryTerm.value)
    }

    const handleSizeChange = (val) => {
      queryTerm.value.pageSize = val
      query()
    }

    const handleCurrentChange = (val) => {
      queryTerm.value.currentPage = val
      queryCurrentPage()
      tableRef.value.$refs.bodyWrapper.scrollTop = 0
    }

    const chongZhi = () => {
      unref(queryTermRef).resetFields()
    }
    //科室配置
    const setlDeptConf = ref({
      dialog:false,
      outDept:[],
    })

    const openSetlDeptConfGialog = () =>{
      let query = { code:store.state.user.info.code,
        typeFlag:1
      }
      querySetlDeptConfInfo(query).then((res)=>{
        setlDeptConf.value.outDept=res
        setlDeptConf.value.dialog = true
      })
    }

    const clicksaveSetlDeptConfInfo = () =>{
      if(setlDeptConf.value.outDept.length <=0){
        ElMessage({
          message: '请先选择要配置的科室数据',
          type: 'warning',
          showClose: true,
        })
        return
      }
      let list = [];
      for(let item of setlDeptConf.value.outDept){
        let deptConf = {}
        deptConf['code'] = store.state.user.info.code
        deptConf['name'] =  store.state.user.info.name
        deptConf['typeFlag'] = 1
        deptConf['deptCode'] = item
        list.push(deptConf)
      }
      saveSetlDeptConfInfo(list).then((res)=>{
        setlDeptConf.value.dialog = false
      })
    }


    const jieSuanDanXinXi = (val) => {
      if (queryTerm.value.clrType !== '21') {
        ElMessage({
          message: '结算类别请选择为【住院】！',
          type: 'warning',
          showClose: true,
        })
        return
      }
      jieSuanDanKeXiuGaiXinXi(val.patNo, val.times, val.ledgerSn).then((res) => {
        jieSuanDanData.value = res
        jieSuanDanData.value.medType = val.medType
        jieSuanDanData.value.actIptDays = val.actIptDays
        jieSuanDanData.value.outDeptName = val.outDeptName
        jieSuanDanData.value.medTypeName = val.medTypeName
        jieSuanDanData.value.insutypeName = val.insutypeName
        jieSuanDanData.value.disDiag = val.disDiag
        jieSuanDanData.value.disDiagComment = val.disDiagComment
        jieSuanDanData.value.medfeeSumamt = val.medfeeSumamt
        jieSuanDanData.value.fundPaySumamt = val.fundPaySumamt
        jieSuanDanData.value.decType = val.decType
        jieSuanDanData.value.upload = val
        jieSuanDanData.value.deptList = disDepts.value
        jieSuanDanData.value.ledgerSn= val.ledgerSn
        jieSuanDanData.value.disDept= val.outDept
        jieSuanDanData.value.shenHeXinXi = {
          id: val.id,
          reqOpId: val.reqOpId,
          reqOpName: val.reqOpName,
          reqOpDate: val.reqOpDate,
          reqRemark: val.reqRemark,
          auditStaff: val.auditStaff,
          auditName: val.auditName,
          auditDate: val.auditDate,
          auditRemark: val.auditRemark,
          auditFlag: val.auditFlag,
        }
        jieSuanDanDataDialog.value = true
      })
    }

    const exportExcel = () => {
      if(selections.value.length>0){
        ElMessageBox.prompt('导出文件名字', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPattern: /\S/,
          inputErrorMessage: '文件名不能为空 (∩•̀ω•́)⊃-*⋆',
        })
                .then(({value}) => {
                  const data = {
                    param: {siSetlinfoTemps:selections.value},
                    url: '/setlListUpld/exportExcel',
                    fileName: `${value}.xlsx`,
                    jdt: true,
                  }
                  setTimeout(() => {
                    downloadExcel(data)
                  }, 500)
                })
                .catch((e) => {
                })
      }else {
        if (queryTerm.value.total > 0) {
          ElMessageBox.prompt('导出文件名字', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            inputPattern: /\S/,
            inputErrorMessage: '文件名不能为空 (∩•̀ω•́)⊃-*⋆',
          })
                  .then(({value}) => {
                    const data = {
                      param: queryTerm.value,
                      url: '/setlListUpld/exportExcel',
                      fileName: `${value}.xlsx`,
                      jdt: true,
                    }
                    setTimeout(() => {
                      downloadExcel(data)
                    }, 500)
                  })
                  .catch((e) => {
                  })
        } else {
          ElMessage.error('请先查询出数据')
        }
      }
    }

    const remoteMethod = (val) => {
      if (val.length > 1) {
        getRenYuan(val).then((res) => {
          referPhysicians.value = res
        })
      }
    }

    /* 限制时间 */
    const xianZhiShiJian = ref(null)
    const xianZhiShiJianClick = () => {
      xianZhiShiJian.value.xianZhiShiJianDialog = true
    }

    /* 修改管床医生 */
    const guanChuangYiShen = ref({
      yiShenCode: '',
      patNo: '',
      times: 0,
    })
    const xiuGaiYiShen = ref(false)
    const huoQuXiuGaiYiShen = (code, name, patNo, times) => {
      if (stringNotBlank(code)) {
        referPhysicians.value = [{code: code, name: name}]
        guanChuangYiShen.value.yiShenCode = code
      }
      xiuGaiYiShen.value = true
      guanChuangYiShen.value.patNo = patNo
      guanChuangYiShen.value.times = times
    }


    const xiuGaibmyBzdata = ref({
      dialog:false,
      bmyBz:'',
      patNo:'',
      times:null,
      ledgerSn:null,
    })

    //编辑编码员备注
    const bianJiBmyBz = (row) =>{
      xiuGaibmyBzdata.value.dialog = true
      xiuGaibmyBzdata.value.bmyBz = row.bmyBz
      xiuGaibmyBzdata.value.patNo = row.patNo
      xiuGaibmyBzdata.value.times = row.times
      xiuGaibmyBzdata.value.ledgerSn = row.ledgerSn
    }
    //保存编码员备注
    const xiuGaibmyBzClick = () =>{
      let updateBmyBzdata = {
        bmyBz:xiuGaibmyBzdata.value.bmyBz,
        patNo:xiuGaibmyBzdata.value.patNo,
        times:xiuGaibmyBzdata.value.times,
        ledgerSn:xiuGaibmyBzdata.value.ledgerSn
      }
      updateBmyBz(updateBmyBzdata).then((res)=>{
        xiuGaibmyBzdata.value.dialog = false
        queryCurrentPage()
      })
    }

    const xiuGaiGuanChuangYiShenClick = () => {
      xiuGaiGuanChuangYiShen(guanChuangYiShen.value.patNo, guanChuangYiShen.value.times, guanChuangYiShen.value.yiShenCode).then((res) => {
        xiuGaiYiShen.value = false
        queryCurrentPage()
      })
    }

    const zhongKongTiaoJian = () => {
      if (!auditQuanXian()) {
        queryTerm.value.outDept = [store.state.user.info.deptCode]
        referPhysicians.value = [{code: store.state.user.info.code, name: store.state.user.info.name}]
      }
      queryTerm.value.medType = ['22', '2101']
      queryTerm.value.insutype = huoQuZhongKongChaXunTiaoJian(insutypes.value, ['340', '350', '360', '510', '410'])
      queryTerm.value.psnType = huoQuZhongKongChaXunTiaoJian(getPsnType, ['13'])
      queryTerm.value.clrOptins = huoQuZhongKongChaXunTiaoJian(clrOptins.value, ['439900', '430121'])
    }

    const daYingJieSuanDanRef = ref({
      dialog: false,
      patNo: '',
      times: 0,
      ledgerSn: 0,
    })
    const jieSuanDanClick = (val) => {
      daYingJieSuanDanRef.value.dialog = true
      daYingJieSuanDanRef.value.patNo = val.patNo
      daYingJieSuanDanRef.value.times = val.times
      daYingJieSuanDanRef.value.ledgerSn = val.ledgerSn
    }

    let medType = $ref({
      dialog: false,
      setlId: '',
      iptMedType: 1,
    })
    const clickToModifyMedicalCategory = ({setlId, iptMedType}) => {
      if (stringIsBlank(setlId)) {
        return ElMessage.error('该患者没有结算ID')
      }
      medType.dialog = true
      medType.setlId = setlId
      medType.iptMedType = iptMedType
    }

    const clickToSaveMedicalCategory = () => {
      modifyMedicalCategory(medType.iptMedType, medType.setlId)
      query()
    }

    function getIptMedName(val) {
      switch (val) {
        case 1:
          return '住院'
        case 2:
          return '日间手术'
        default:
          return ''
      }
    }

    // 查询自动上传
    const autoUploadDialog = ref(false)
   const uploadJieZhiData =  ref([])

    onMounted(() => {
      getInsutypes().then((res) => {
        insutypes.value = res
        getClrTypes().then((res2) => {
          clrTypes.value = res2
        })
      })
      getMedType().then((res) => {
        medTypes.value = res
      })
      getZhuYuanDept().then((res) => {
        disDepts.value = res
      })
      getUploadMaxDay().then((res=>{
        for (let i = 1; i <= res; i++) {
          let temp = {
            code : i,
            name : i+'天'
          }
          uploadJieZhiData.value.push(temp)
        }
      }))
      clrOptins.value = clrOptinsData
    })
    return {
      queryTerm,
      clockinShortcuts,
      handleSizeChange,
      handleCurrentChange,
      insutypes,
      query,
      upldAllList,
      upldSelections,
      disDepts,
      clrTypes,
      medTypes,
      getPsnType,
      queryTermDialog,
      chongZhi,
      queryTermRef,
      store,
      spanWidth,
      jieSuanDanXinXi,
      jieSuanDanData,
      handleSelectionChange,
      exportExcel,
      referPhysicians,
      clrOptins,
      jieSuanDanDataDialog,
      remoteMethod,
      auditFlags,
      queryCurrentPage,
      clicksaveSetlDeptConfInfo,
      openSetlDeptConfGialog,
      setlDeptConf,
      xianZhiShiJian,
      xianZhiShiJianClick,
      shenHeQuanXian,
      auditQuanXian,
      bmyQuaXian,
      guanChuangYiShen,
      huoQuXiuGaiYiShen,
      bianJiBmyBz,
      xiuGaibmyBzClick,
      xiuGaibmyBzdata,
      xiuGaiGuanChuangYiShenClick,
      xiuGaiYiShen,
      auditName,
      dept,
      decTypes,
      zhongKongTiaoJian,
      daYingJieSuanDanRef,
      toUploadInfo,
      toUploadDrgGroupInfo,
      getUploadInfo,
      getdrgGroupResultInfo,
      jieSuanDanClick,
      uploadJieZhiData,
      flags: ref([
        {code: 0, name: '全部'},
        {code: 1, name: '未传'},
        {code: 2, name: '已传'},
      ]),
      hiPaymtdData: [
        {code: 1, name: '按项目'},
        {code: 2, name: '单病种'},
        {code: 3, name: '按病种分值'},
        {code: 4, name: '基本诊断相关分组（DRG）'},
        {code: 5, name: '按床日'},
        {code: 6, name: '按人次'},
        {code: 9, name: '其他'},
        {code: 0, name: '全部'},
      ],
      tableRef,
      getIptMedName,
      medType,
      clickToModifyMedicalCategory,
      clickToSaveMedicalCategory,
      medTypeData: [
        {code: 1, name: '住院'},
        {code: 1, name: '日间手术'},
      ],
      autoUploadDialog
    }
  },
}
</script>


