<template>
    <page-layer>
    <template #header>
        <el-select v-model="queryuploadTerm.zhuYuanFlag" placeholder="住院类型" style="width: 110px">
            <el-option label="住院" value="0" />
            <el-option label="门诊" value="1" />
        </el-select>
          <el-input v-model="queryuploadTerm.patNo" clearable placeholder="住院号"
                    @blur="patNo = $event.target.value.trim()" @keyup.enter="queryPage" style="width: 110px"></el-input>
        <el-input v-model="queryuploadTerm.times" clearable placeholder="住院次数"
                  @blur="times = $event.target.value.trim()"  style="width: 70px"></el-input>
        <el-input v-model="queryuploadTerm.ledgerSn" clearable placeholder="账页号"
                  @blur="times = $event.target.value.trim()"  style="width: 60px"></el-input>
          <el-select v-model="queryuploadTerm.typeFlag" placeholder="上传类型" style="width: 110px">
            <el-option label="全部" value="0" />
            <el-option label="自动上传" value="1" />
            <el-option label="手动上传" value="2" />
          </el-select>
          <el-select v-model="queryuploadTerm.flag" placeholder="上传结果标识" style="width: 110px" >
            <el-option label="全部" value="0" />
            <el-option label="上传成功" value="1" />
            <el-option label="上传失败" value="2" />
          </el-select>
        <el-select v-model="queryuploadTerm.outDeptList" :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>
            <span>上传时间</span>
          <el-date-picker
                  v-model="queryuploadTerm.uploadDateRange"
                  type="daterange"
                  format="YYYY-MM-DD"
                  placeholder="上传开始日期"
                  range-separator="至"
                  start-placeholder="上传开始日期"
                  end-placeholder="上传结束日期"
                  :shortcuts="clockinShortcuts"
                  style="width: 180px"
                  value-format="YYYY-MM-DD"
          />
        <span>结算时间</span>
          <el-date-picker
                  v-model="queryuploadTerm.setlDateRange"
                  type="daterange"
                  format="YYYY-MM-DD"
                  placeholder="结算开始日期"
                  range-separator="至"
                  start-placeholder="结算开始日期"
                  end-placeholder="结算结束日期"
                  :shortcuts="clockinShortcuts"
                  style="width: 180px"
                  value-format="YYYY-MM-DD"
          />
        <el-select v-model="queryuploadTerm.distinctFlag" placeholder="是否去重" style="width: 110px">
            <el-option label="全部" value="0" />
            <el-option label="去重" value="1" />
        </el-select>
          <el-button icon="Search"  type="primary" @click="queryPage">查询</el-button>
          <el-button type="primary" @click="ExportToExcel">导出Excel</el-button>
    </template>
    <template #main>
      <el-table ref="uploadTableRef" :data="queryuploadTerm.list" :height="tableHeight"   border highlight-current-row >
        <el-table-column prop="patNo" label="住院号" width="80"></el-table-column>
        <el-table-column prop="times" label="住院次数" width="80"></el-table-column>
        <el-table-column prop="ledgerSn" label="账页号" width="80"></el-table-column>
        <el-table-column prop="outDeptName" label="出院科室" width="80"></el-table-column>
        <el-table-column prop="typeFlagName" label="上传类型" width="80"></el-table-column>
        <el-table-column prop="uploadName" label="上传人" width="80"></el-table-column>
        <el-table-column prop="flagName" label="上传结果标识" width="90"></el-table-column>
        <el-table-column prop="createdDate" label="上传时间" width="130"></el-table-column>
        <el-table-column prop="setlTime" label="患者结算时间" width="130"></el-table-column>
        <el-table-column prop="logText" label="日志" ></el-table-column>
      </el-table>
      <el-pagination
              :current-page="queryuploadTerm.currentPage"
              :page-size="queryuploadTerm.pageSize"
              :page-sizes="[10, 20, 30, 40, 50, 100]"
              :total="queryuploadTerm.total"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
      ></el-pagination>
    </template>
    </page-layer>
</template>

<script setup name='ExportAutoUploadInfo'>
import {clockinShortcuts} from '@/data/shortcuts'
import {onMounted,ref,onActivated} from "vue";
import {getServerDateApi,getZhuYuanDept} from "@/api/public-api";
import moment from "moment";
import {getDateRangeFormatDate} from "@/utils/date";
import {downloadExcel} from "@/utils/excel";
import PageLayer from "@/layout/PageLayer";
import store from '@/store';
import router from "@/router";
import sleep from "@/utils/sleep";
import {
  queryUploadInfo
} from '@/api/medical-insurance/si-setl-upload';
const windowSize = store.state.app.windowSize;
const tableHeight = ref(windowSize.h - 70)
const emit = defineEmits(['close'])

const dialog = ref(true)
const uploadInfoData = ref([])
const  queryuploadTerm = ref({
  zhuYuanFlag:"",
  patNo:"",
  times:null,
  ledgerSn:null,
  typeFlag:'0',
  flag:'0',
  updateStartTime:"",
  updateEndTime:"",
  setlStartTime:"",
  setlEndTime:"",
  uploadDateRange:[clockinShortcuts[0].value[0], clockinShortcuts[0].value[1]],
  setlDateRange:[],
  currentPage:1,
  pageSize:40,
  list:[],
  outDeptList:[],
  distinctFlag: '0',
  total: 0
})

const ExportToExcel = async () => {
    createQuery()
  const data = {
    param: queryuploadTerm.value,
    url: '/setlListUpld/exportAutoUploadInfo',
    fileName: '上传日志.xlsx',
  }
  setTimeout(() => {
    downloadExcel(data)
  }, 500)
}

function createQuery() {
    let uploadDates = getDateRangeFormatDate(queryuploadTerm.value.uploadDateRange)
    let setlDates = getDateRangeFormatDate(queryuploadTerm.value.setlDateRange)
    queryuploadTerm.value.updateStartTime =uploadDates.startTime
    queryuploadTerm.value.updateEndTime =uploadDates.endTime

    queryuploadTerm.value.setlStartTime =setlDates.startTime
    queryuploadTerm.value.setlEndTime =setlDates.endTime
}
const queryPage=async () =>{
    createQuery()
    queryUploadInfo(queryuploadTerm.value).then((res) => {
      queryuploadTerm.value.list = res.records?res.records:[]
      queryuploadTerm.value.total =res.total
    })

}
onActivated(async () => {
    const params = router.currentRoute.value.query
    if (params && params.patNo && params.times && params.ledgerSn) {
        queryuploadTerm.value.times = params.times
        queryuploadTerm.value.patNo = params.patNo
        queryuploadTerm.value.ledgerSn = params.ledgerSn
        queryuploadTerm.value.uploadDateRange = []
        queryuploadTerm.value.setlDateRange = []
        await sleep(300);
    }
    await queryPage()
})

// 出院科室
const disDepts = ref([])

onMounted( () => {
    getZhuYuanDept().then((res) => {
        disDepts.value = res
    })
})

const handleSizeChange = (val) => {
  queryuploadTerm.value.pageSize = val
  queryPage()

}
const handleCurrentChange = (val) => {
  queryuploadTerm.value.currentPage = val
  queryPage()
}
</script>

<style scoped>
  .maxhight{
    height: 950px;
    overflow: auto;
  }
</style>
