<template>
  <page-layer>
    <template #header>
      <el-select size="small" v-model="cataType" placeholder="请选择目录类别" filterable clearable
                 @change="handleCataChanged">
        <el-option v-for="item in cataTypes" :key="item.code" :value="item.code" :label="item.name"></el-option>
      </el-select>
      <el-divider direction="vertical"></el-divider>
      <span v-show="showUpdtTime"> <el-date-picker v-model="updtTime" size="small" placeholder="请选择更新日期"
                                                   style="width: 160px"></el-date-picker>&nbsp; </span>
      <el-button size="small" type="primary" icon="Download" :disabled="!cataType" @click="beforeDownload">从中心获取
      </el-button>
      <span v-show="cataType === '1319'" style="margin-left: 18px">
        从第
        <el-input-number v-model="startPageNum"></el-input-number>
        页开始，每页
        <el-input-number v-model="downloadPageSize"></el-input-number>
        条数据
        <el-button size="small" type="primary" icon="Collection" @click="saveSelfpayCatlg">下载到HIS</el-button>
      </span>
      <span v-show="!showUpdtTime && cataType !== '1901'">
        &nbsp;<el-button size="small" type="primary" icon="Download" :disabled="!cataType"
                         @click="fetchData">从医院获取</el-button>
      </span>
    </template>
    <template #main>
      <el-table size="small" :height="tableHeight" :data="data.list" stripe>
        <el-table-column v-if="!showUpdtTime" prop="code" label="医疗目录编码"></el-table-column>
        <!-- 西药中成药目录 -->
        <el-table-column v-if="cataType === '1301'" prop="drugTradeName" label="药品商品名"></el-table-column>
        <el-table-column v-if="cataType === '1301'" prop="generalDrugName" label="药品通用名"></el-table-column>
        <el-table-column v-if="cataType === '1301'" prop="registrationName" label="注册名称"></el-table-column>
        <el-table-column v-if="cataType === '1301'" prop="remarks" label="备注"></el-table-column>
        <!-- 中药饮片目录 -->
        <el-table-column v-if="cataType === '1302'" prop="monooName" label="单味药名称"></el-table-column>
        <el-table-column v-if="cataType === '1302'" prop="medicalMaterialsName" label="药材名称"></el-table-column>
        <!-- 医用耗材目录 -->
        <el-table-column v-if="cataType === '1306'" prop="suppliesName" label="耗材名称"></el-table-column>
        <el-table-column v-if="cataType === '1306'" prop="siUniversalName" label="医保通用名"></el-table-column>
        <!-- 疾病与诊断目录 -->
        <el-table-column v-if="cataType === '1307'" prop="chapter" label="章"></el-table-column>
        <el-table-column v-if="cataType === '1307'" prop="chapterCodeRange" label="章代码范围"></el-table-column>
        <el-table-column v-if="cataType === '1307'" prop="chapterName" label="章名称"></el-table-column>
        <el-table-column v-if="cataType === '1307'" prop="sectionCodeRange" label="节代码范围"></el-table-column>
        <el-table-column v-if="cataType === '1307'" prop="sectionName" label="节名称"></el-table-column>
        <el-table-column v-if="cataType === '1307'" prop="classOrderName" label="类目名称"></el-table-column>
        <el-table-column v-if="cataType === '1307'" prop="diagnosisCode" label="诊断代码"></el-table-column>
        <el-table-column v-if="cataType === '1307'" prop="diagnosisName" label="诊断名称"></el-table-column>
        <el-table-column v-if="cataType === '1307'" prop="useTheTag" label="使用标记"></el-table-column>
        <el-table-column v-if="cataType === '1307'" prop="uniqueRecordNumber" label="唯一记录号"></el-table-column>
        <!-- 手术操作目录 -->
        <el-table-column v-if="cataType === '1308'" prop="chapter" label="章"></el-table-column>
        <el-table-column v-if="cataType === '1308'" prop="chapterName" label="章名称"></el-table-column>
        <el-table-column v-if="cataType === '1308'" prop="classOrderName" label="类目名称"></el-table-column>
        <el-table-column v-if="cataType === '1308'" prop="subname" label="亚目名称"></el-table-column>
        <el-table-column v-if="cataType === '1308'" prop="detailsName" label="细目名称"></el-table-column>
        <el-table-column v-if="cataType === '1308'" prop="surgicalOperationCode" label="手术操作代码"></el-table-column>
        <el-table-column v-if="cataType === '1308'" prop="operationOperationName"
                         label="手术操作名称"></el-table-column>
        <el-table-column v-if="cataType === '1308'" prop="useTheTag" label="使用标记"></el-table-column>
        <!-- 门诊慢特病种目录 -->
        <el-table-column v-if="cataType === '1309'" prop="categoryName" label="门慢门特病种大类名称"></el-table-column>
        <el-table-column v-if="cataType === '1309'" prop="detailDiseasesName"
                         label="门慢门特病种细分类名称"></el-table-column>
        <el-table-column v-if="cataType === '1309'" prop="medicalInsuranceDivision" label="医保区划"></el-table-column>
        <el-table-column v-if="cataType === '1309'" prop="connotation" label="病种内涵"></el-table-column>
        <el-table-column v-if="cataType === '1309'" prop="diseaseName" label="门慢门特病种名称"></el-table-column>
        <el-table-column v-if="cataType === '1309'" prop="categoryCode" label="门慢门特病种大类代码"></el-table-column>
        <!-- 按病种付费病种目录 -->
        <el-table-column v-if="cataType === '1310'" prop="settlementCode"
                         label="按病种结算病种目录代码"></el-table-column>
        <el-table-column v-if="cataType === '1310'" prop="settlementName" label="按病种结算病种名称"></el-table-column>
        <el-table-column v-if="cataType === '1310'" prop="limitedOperationCode"
                         label="限定手术操作代码"></el-table-column>
        <el-table-column v-if="cataType === '1310'" prop="limitedOperationName"
                         label="限定手术操作名称"></el-table-column>
        <!-- 日间手术治疗病种目录 -->
        <el-table-column v-if="cataType === '1311'" prop="daytimeSurgeryCode"
                         label="日间手术病种目录代码"></el-table-column>
        <el-table-column v-if="cataType === '1311'" prop="daytimeSurgeryName"
                         label="日间手术病种名称"></el-table-column>
        <el-table-column v-if="cataType === '1311'" prop="operationCode" label="手术操作代码"></el-table-column>
        <el-table-column v-if="cataType === '1311'" prop="operationName" label="手术操作名称"></el-table-column>
        <!-- 医保目录信息查询 -->
        <el-table-column v-if="cataType === '1312'" prop="hilist_code" label="医保目录编码"></el-table-column>
        <el-table-column v-if="cataType === '1312'" prop="hilist_name" label="医保目录名称"></el-table-column>
        <el-table-column v-if="cataType === '1312'" prop="insu_admdvs" label="参保机构医保区划"></el-table-column>
        <el-table-column v-if="cataType === '1312'" prop="begndate" label="开始日期"></el-table-column>
        <el-table-column v-if="cataType === '1312'" prop="enddate" label="结束日期"></el-table-column>
        <el-table-column v-if="cataType === '1312'" prop="med_chrgitm_type" label="医疗收费项目类别"></el-table-column>
        <el-table-column v-if="cataType === '1312'" prop="chrgitm_lv" label="收费项目等级"></el-table-column>
        <el-table-column v-if="cataType === '1312'" prop="lmt_used_flag" label="限制使用标志"></el-table-column>
        <el-table-column v-if="cataType === '1312'" prop="list_type" label="目录类别"></el-table-column>
        <el-table-column v-if="cataType === '1312'" prop="med_use_flag" label="医疗使用标志"></el-table-column>
        <el-table-column v-if="cataType === '1312'" prop="matn_used_flag" label="生育使用标志"></el-table-column>
        <el-table-column v-if="cataType === '1312'" prop="hilist_use_type" label="医保目录使用类别"></el-table-column>
        <el-table-column v-if="cataType === '1312'" prop="lmt_cpnd_type" label="限复方使用类型"></el-table-column>
        <!-- 肿瘤形态学目录 -->
        <el-table-column v-if="cataType === '1313'" prop="tumorCellTypeCode"
                         label="肿瘤/细胞类型代码"></el-table-column>
        <el-table-column v-if="cataType === '1313'" prop="tumorCellType" label="肿瘤/细胞类型"></el-table-column>
        <el-table-column v-if="cataType === '1313'" prop="morphologyClassificationCode"
                         label="形态学分类代码"></el-table-column>
        <el-table-column v-if="cataType === '1313'" prop="morphologicalClassification"
                         label="形态学分类"></el-table-column>
        <!-- 中医疾病目录 -->
        <el-table-column v-if="cataType === '1314'" prop="categoryCode" label="科别类目代码"></el-table-column>
        <el-table-column v-if="cataType === '1314'" prop="categoryName" label="科别类目名称"></el-table-column>
        <el-table-column v-if="cataType === '1314'" prop="classificationListCode"
                         label="专科系统分类目代码"></el-table-column>
        <el-table-column v-if="cataType === '1314'" prop="classificationListName"
                         label="专科系统分类目名称"></el-table-column>
        <el-table-column v-if="cataType === '1314'" prop="diseaseClassificationCode"
                         label="疾病分类代码"></el-table-column>
        <el-table-column v-if="cataType === '1314'" prop="diseaseClassificationName"
                         label="疾病分类名称"></el-table-column>
        <el-table-column v-if="cataType === '1314'" prop="remarks" label="备注"></el-table-column>
        <!-- 中医证候目录 -->
        <el-table-column v-if="cataType === '1315'" prop="catalogueCode" label="证候类目代码"></el-table-column>
        <el-table-column v-if="cataType === '1315'" prop="catalogueName" label="证候类目名称"></el-table-column>
        <el-table-column v-if="cataType === '1315'" prop="propertyCode" label="证候属性代码"></el-table-column>
        <el-table-column v-if="cataType === '1315'" prop="properties" label="证候属性"></el-table-column>
        <el-table-column v-if="cataType === '1315'" prop="classificationCode" label="证候分类代码"></el-table-column>
        <el-table-column v-if="cataType === '1315'" prop="classificationName" label="证候分类名称"></el-table-column>
        <el-table-column v-if="cataType === '1315'" prop="remarks" label="备注"></el-table-column>
        <!-- 医疗目录与医保目录匹配信息 -->
        <el-table-column v-if="cataType === '1316'" prop="med_list_codg" label="医疗目录编码"></el-table-column>
        <el-table-column v-if="cataType === '1316'" prop="hilist_code" label="医保目录编码"></el-table-column>
        <el-table-column v-if="cataType === '1316'" prop="list_type" label="目录类别"></el-table-column>
        <el-table-column v-if="cataType === '1316'" prop="insu_admdvs" label="参保机构医保区划"></el-table-column>
        <!-- 医药机构目录匹配信息 -->
        <el-table-column v-if="cataType === '1317'" prop="fixmedins_code" label="定点医药机构编号"></el-table-column>
        <el-table-column v-if="cataType === '1317'" prop="medins_list_codg"
                         label="定点医药机构目录编号"></el-table-column>
        <el-table-column v-if="cataType === '1317'" prop="medins_list_name"
                         label="定点医药机构目录名称"></el-table-column>
        <el-table-column v-if="cataType === '1317'" prop="insu_admdvs" label="参保机构医保区划"></el-table-column>
        <el-table-column v-if="cataType === '1317'" prop="list_type" label="目录类别"></el-table-column>
        <el-table-column v-if="cataType === '1317'" prop="med_list_codg" label="医疗目录编码"></el-table-column>
        <el-table-column v-if="cataType === '1317'" prop="aprvno" label="批准文号"></el-table-column>
        <el-table-column v-if="cataType === '1317'" prop="dosform" label="剂型"></el-table-column>
        <el-table-column v-if="cataType === '1317'" prop="exct_cont" label="除外内容"></el-table-column>
        <el-table-column v-if="cataType === '1317'" prop="item_cont" label="项目内涵"></el-table-column>
        <el-table-column v-if="cataType === '1317'" prop="prcunt" label="计价单位"></el-table-column>
        <el-table-column v-if="cataType === '1317'" prop="spec" label="规格"></el-table-column>
        <el-table-column v-if="cataType === '1317'" prop="pacspec" label="包装规格"></el-table-column>
        <!-- 医保目录限价信息 -->
        <el-table-column v-if="cataType === '1318'" prop="hilist_code" label="医保目录编码"></el-table-column>
        <el-table-column v-if="cataType === '1318'" prop="hilist_lmtpric_type"
                         label="医保目录限价类型"></el-table-column>
        <el-table-column v-if="cataType === '1318'" prop="overlmt_dspo_way"
                         label="医保目录超限处理方式"></el-table-column>
        <el-table-column v-if="cataType === '1318'" prop="insu_admdvs" label="参保机构医保区划"></el-table-column>
        <el-table-column v-if="cataType === '1318'" prop="hilist_pric_uplmt_amt"
                         label="医保目录定价上限金额"></el-table-column>
        <el-table-column v-if="cataType === '1318'" prop="tabname" label="表名"></el-table-column>
        <!-- 医保目录先自付比例信息 -->
        <el-table-column v-if="cataType === '1319'" prop="hilist_code" label="医保目录编码"></el-table-column>
        <el-table-column v-if="cataType === '1319'" prop="selfpay_prop_psn_type"
                         label="医保目录自付比例人员类别"></el-table-column>
        <el-table-column v-if="cataType === '1319'" prop="selfpay_prop_type" label="目录自付比例类别"></el-table-column>
        <el-table-column v-if="cataType === '1319'" prop="insu_admdvs" label="参保机构医保区划"></el-table-column>
        <el-table-column v-if="cataType === '1319'" prop="selfpay_prop" label="自付比例"></el-table-column>
        <el-table-column v-if="cataType === '1319'" prop="tabname" label="表名"></el-table-column>
        <!-- 下载公有部分 -->
        <el-table-column v-if="!showUpdtTime" prop="startTime" label="开始日期"></el-table-column>
        <el-table-column v-if="!showUpdtTime" prop="endTime" label="结束日期"></el-table-column>
        <el-table-column v-if="!showUpdtTime" prop="validFlag" label="有效标志"></el-table-column>
        <el-table-column v-if="!showUpdtTime" prop="uniqueRecordNumber" label="唯一记录号"></el-table-column>
        <el-table-column v-if="!showUpdtTime" prop="dataCreationTime" label="数据创建时间"></el-table-column>
        <el-table-column v-if="!showUpdtTime" prop="dataUpdateTime" label="数据更新时间"></el-table-column>
        <el-table-column v-if="!showUpdtTime" prop="versionNumber" label="版本号"></el-table-column>
        <el-table-column v-if="!showUpdtTime" prop="versionName" label="版本名称"></el-table-column>
        <!-- 查询公有部分 -->
        <el-table-column v-if="showUpdtTime" prop="begndate" label="开始日期"></el-table-column>
        <el-table-column v-if="showUpdtTime" prop="enddate" label="结束日期"></el-table-column>
        <el-table-column v-if="showUpdtTime" prop="vali_flag" label="有效标志"></el-table-column>
        <el-table-column v-if="showUpdtTime" prop="rid" label="唯一记录号"></el-table-column>
        <el-table-column v-if="showUpdtTime" prop="crte_time" label="数据创建时间"></el-table-column>
        <el-table-column v-if="showUpdtTime" prop="updt_time" label="数据更新时间"></el-table-column>
        <el-table-column v-if="showUpdtTime" prop="memo" label="备注"></el-table-column>
        <el-table-column v-if="showUpdtTime" prop="crter_id" label="创建人"></el-table-column>
        <el-table-column v-if="showUpdtTime" prop="crter_name" label="创建人姓名"></el-table-column>
        <el-table-column v-if="showUpdtTime" prop="crte_optins_no" label="创建机构"></el-table-column>
        <el-table-column v-if="showUpdtTime" prop="opter_id" label="经办人"></el-table-column>
        <el-table-column v-if="showUpdtTime" prop="opter_name" label="经办人姓名"></el-table-column>
        <el-table-column v-if="showUpdtTime" prop="opt_time" label="经办时间"></el-table-column>
        <el-table-column v-if="showUpdtTime" prop="optins_no" label="经办机构"></el-table-column>
        <el-table-column v-if="showUpdtTime" prop="poolarea_no" label="统筹区"></el-table-column>
      </el-table>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handlePageChange"
          :current-page="currentPage"
          :page-sizes="[15, 30, 45, 70, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="data.totalSize"
          style="margin-top: 5px"
      ></el-pagination>
      <el-dialog title="字典表查询" v-model="showDldDict" width="40%">
        <el-select v-model="dictType" size="small" placeholder="请选择字典类型" filterable>
          <el-option v-for="item in siDictTypes" :key="item.code" :value="item.code" :label="item.name"></el-option>
        </el-select>
        &nbsp;
        <el-select v-model="valiFlag" size="small" style="width: 100px">
          <el-option label="有效" value="1"></el-option>
          <el-option label="已失效" value="0"></el-option>
        </el-select>
        <el-divider direction="vertical"></el-divider>
        <el-button type="primary" size="small" icon="Search" @click="qrySiDicts">查询</el-button>
        <el-table :data="dictVals" stripe size="small" height="300">
          <el-table-column prop="sort" label="序号" width="60"></el-table-column>
          <el-table-column prop="version" label="版本号" width="80"></el-table-column>
          <el-table-column prop="value" label="字典键值" width="120"></el-table-column>
          <el-table-column prop="label" label="字典标签"></el-table-column>
        </el-table>
      </el-dialog>
    </template>
  </page-layer>
</template>

<script>
import {computed, onMounted, reactive, ref} from 'vue'
import {getCatalogues} from '@/api/medical-insurance/si-dict'
import {executeDownload, queryFromHis} from '@/api/medical-insurance/si-download'
import {ElMessage} from 'element-plus'
import store from '@/store'
import {siDictTypes} from '@/data'
import {formatDatetime} from '@/utils/date'
import PageLayer from "@/layout/PageLayer";

export default {
  components: {PageLayer},
  setup() {
    const windowSize = store.state.app.windowSize
    const tableHeight = windowSize.h - 85
    const cataType = ref(null)
    const cataTypes = ref([])
    const updtTime = ref(null)
    const needUpdtTime = ['1304', '1312', '1316', '1317', '1318', '1319']
    const showUpdtTime = computed(() => {
      return needUpdtTime.indexOf(cataType.value) !== -1
    })
    const showDldDict = ref(false)
    const dictVals = ref([])
    const dictType = ref('')
    const valiFlag = ref('1')

    const handleCataChanged = () => {
      data.totalSize = 0
      data.list = []
    }

    const data = reactive({
      totalSize: 0,
      list: [],
    })

    const pageSize = ref(30)
    const currentPage = ref(1)
    const handleSizeChange = (val) => {
      pageSize.value = val
      fetchData()
    }
    const handlePageChange = (val) => {
      currentPage.value = val
      fetchData()
    }

    const fetchData = () => {
      const param = {
        code: cataType.value,
        updtTime: formatDatetime(updtTime.value),
        pageNum: currentPage.value,
        pageSize: pageSize.value,
      }
      if (showUpdtTime.value) {
        download()
      } else {
        queryFromHis(param).then((res) => {
          data.totalSize = res.totalSize
          data.list = res.list
        })
      }
    }

    const beforeDownload = () => {
      if (cataType.value === '1901') {
        showDldDict.value = true
      } else {
        download()
      }
    }

    const startPageNum = ref(1)
    const downloadPageSize = ref(3000)
    const saveSelfpayCatlg = () => {
      if (!updtTime.value) {
        ElMessage({
          message: '请选择更新日期！',
          type: 'warning',
          duration: 2000,
          showClose: true,
        })
        return
      }
      const param = {
        code: '1319_save',
        pageNum: startPageNum.value,
        pageSize: downloadPageSize.value,
        updtTime: formatDatetime(updtTime.value),
      }
      executeDownload(param).then((res) => {
        if (showUpdtTime.value) {
          data.totalSize = res.totalSize
          data.list = res.list
        }
      })
    }

    const qrySiDicts = () => {
      if (!dictType.value) {
        ElMessage({
          message: '请选择字典类型！',
          type: 'warning',
          duration: 2000,
          showClose: true,
        })
        return
      }
      const param = {
        code: cataType.value,
        type: dictType.value,
        valiFlag: valiFlag.value,
      }
      executeDownload(param).then((res) => {
        dictVals.value = res.list
      })
    }

    const download = () => {
      if (showUpdtTime.value && !updtTime.value) {
        ElMessage({
          message: '请选择更新日期！',
          type: 'warning',
          duration: 2000,
          showClose: true,
        })
        return
      }
      const param = {
        code: cataType.value,
        version: 'V00',
        updtTime: formatDatetime(updtTime.value),
        pageNum: currentPage.value,
        pageSize: pageSize.value,
      }
      executeDownload(param).then((res) => {
        if (showUpdtTime.value) {
          data.totalSize = res.totalSize
          data.list = res.list
        } else {
          queryFromHis(param).then((res) => {
            data.totalSize = res.totalSize
            data.list = res.list
          })
        }
      })
    }

    onMounted(() => {
      getCatalogues().then((res) => {
        // res.push({ code: '1303', name: '医院制剂' })
        cataTypes.value = res
      })
    })
    return {
      tableHeight,
      cataType,
      cataTypes,
      showDldDict,
      dictVals,
      dictType,
      siDictTypes,
      valiFlag,
      startPageNum,
      downloadPageSize,
      qrySiDicts,
      handleCataChanged,
      updtTime,
      showUpdtTime,
      beforeDownload,
      saveSelfpayCatlg,
      data,
      pageSize,
      currentPage,
      handlePageChange,
      handleSizeChange,
      fetchData,
    }
  },
}
</script>
