<template>
  <page-layer>
    <template #header>
      <el-date-picker v-model="dateRange" type="daterange" range-separator="至" start-placeholder="开始日期"
        end-placeholder="结束日期" :shortcuts="shortcuts"> </el-date-picker>
      <el-select v-model="insurId" style="width: 200px" clearable>
        <el-option label="长沙市城职普通住院" value="1"></el-option>
        <el-option label="长沙市城居普通住院" value="2"></el-option>
      </el-select>
      <el-button type="primary" icon="Search" @click="query" style="margin-left: 5px">查询</el-button>
      <el-button type="primary" icon="Download" @click="exportKsDetailExcel" style="margin-left: 5px">导出Excel
      </el-button>
      <el-button type="primary" icon="Upload" @click="cssybImort" style="margin-left: 5px">决算数据导入</el-button>
    </template>
    <template #main>
      <el-table :data="returnData" :height="tableHeight" stripe highlight-current-row show-summary
        :summary-method="getSummaries" @row-dblclick="ysJsdetails">
        <el-table-column fixed prop="deptName" label="科室" width="120"></el-table-column>
        <el-table-column prop="zfy" label="总费用" width="100" align="right" sortable></el-table-column>
        <el-table-column prop="yjzf" label="预计支付" width="100" align="right" sortable></el-table-column>
        <el-table-column prop="tczf" label="统筹支付" width="100" align="right" sortable></el-table-column>
        <el-table-column prop="bal" label="盈亏额" width="100" align="right" sortable>
          <template #default="scope">
            <span v-if="scope.row.bal < 0" style="color: red">{{ scope.row.bal }}</span>
            <span v-else>{{ scope.row.bal }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="bxbl" label="报销比例(%)" width="100" align="right" sortable></el-table-column>
        <el-table-column prop="zrs" label="总人数" width="80" align="right" sortable></el-table-column>
        <el-table-column prop="zfz" label="总分值" width="100" align="right" sortable></el-table-column>
        <el-table-column label="有对照" header-align="center">
          <el-table-column prop="ydzrs" label="人数" width="80" align="right" sortable />
          <el-table-column prop="ydzbal" label="盈亏额" width="100" align="right" sortable>
            <template #default="scope">
              <span v-if="scope.row.ydzbal < 0" style="color: red">{{ scope.row.ydzbal }}</span>
              <span v-else>{{ scope.row.ydzbal }}</span>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="无对照" header-align="center">
          <el-table-column prop="wdzrs" label="人数" width="80" align="right" sortable />
          <el-table-column prop="wdzbal" label="盈亏额" width="100" align="right" sortable>
            <template #default="scope">
              <span v-if="scope.row.wdzbal < 0" style="color: red">{{ scope.row.wdzbal }}</span>
              <span v-else>{{ scope.row.wdzbal }}</span>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="难度系数" header-align="center">
          <el-table-column prop="ndxsrs" label="人数" width="80" align="right" sortable />
          <el-table-column prop="ndxsbal" label="盈亏额" width="100" align="right" sortable>
            <template #default="scope">
              <span v-if="scope.row.ndxsbal < 0" style="color: red">{{ scope.row.ndxsbal }}</span>
              <span v-else>{{ scope.row.ndxsbal }}</span>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="治疗未完成" header-align="center">
          <el-table-column prop="wwcrs" label="人数" width="80" align="right" sortable />
          <el-table-column prop="wwcbal" label="盈亏额" width="100" align="right" sortable>
            <template #default="scope">
              <span v-if="scope.row.wwcbal < 0" style="color: red">{{ scope.row.wwcbal }}</span>
              <span v-else>{{ scope.row.wwcbal }}</span>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="转科" header-align="center">
          <el-table-column prop="zkrs" label="人数" width="80" align="right" sortable />
          <el-table-column prop="zkbal" label="盈亏额" width="100" align="right" sortable>
            <template #default="scope">
              <span v-if="scope.row.zkbal < 0" style="color: red">{{ scope.row.zkbal }}</span>
              <span v-else>{{ scope.row.zkbal }}</span>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column prop="dlrs" label="单列" width="80" align="right" sortable></el-table-column>
      </el-table>
    </template>
  </page-layer>
  <el-dialog v-model="showYsSetldetails" :close-on-click-modal="false" :close-on-press-escape="false"
    :title="filterYsDialogTitle" width="90%" top="40px">
    <div style="width: 130px; height: 36px; position: absolute; top: 16px; right: 60px">
      <el-button icon="Download" type="primary" @click="exportYsDetailExcel">导出Excel</el-button>
    </div>
    <el-table :data="setlYsdetails" :height="tableHeight - 160" show-summary :summary-method="getSummaries"
      highlight-current-row stripe @row-dblclick="brJsdetails">
      <el-table-column fixed prop="deptName" label="科室" width="120"></el-table-column>
      <el-table-column fixed prop="doctor" label="医生" width="80" align="center"></el-table-column>
      <el-table-column prop="zfy" label="总费用" width="100" align="right" sortable></el-table-column>
      <el-table-column prop="yjzf" label="预计支付" width="100" align="right" sortable></el-table-column>
      <el-table-column prop="tczf" label="统筹支付" width="100" align="right" sortable></el-table-column>
      <el-table-column prop="bal" label="盈亏额" width="100" align="right" sortable>
        <template #default="scope">
          <span v-if="scope.row.bal < 0" style="color: red">{{ scope.row.bal }}</span>
          <span v-else>{{ scope.row.bal }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="bxbl" label="报销比例(%)" width="100" align="right" sortable></el-table-column>
      <el-table-column prop="zrs" label="总人数" width="80" align="right" sortable></el-table-column>
      <el-table-column prop="zfz" label="总分值" width="100" align="right" sortable></el-table-column>
      <el-table-column label="有对照" header-align="center">
        <el-table-column prop="ydzrs" label="人数" width="80" align="right" sortable />
        <el-table-column prop="ydzbal" label="盈亏额" width="100" align="right" sortable>
          <template #default="scope">
            <span v-if="scope.row.ydzbal < 0" style="color: red">{{ scope.row.ydzbal }}</span>
            <span v-else>{{ scope.row.ydzbal }}</span>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="无对照" header-align="center">
        <el-table-column prop="wdzrs" label="人数" width="80" align="right" sortable />
        <el-table-column prop="wdzbal" label="盈亏额" width="100" align="right" sortable>
          <template #default="scope">
            <span v-if="scope.row.wdzbal < 0" style="color: red">{{ scope.row.wdzbal }}</span>
            <span v-else>{{ scope.row.wdzbal }}</span>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="难度系数" header-align="center">
        <el-table-column prop="ndxsrs" label="人数" width="80" align="right" sortable />
        <el-table-column prop="ndxsbal" label="盈亏额" width="100" align="right" sortable>
          <template #default="scope">
            <span v-if="scope.row.ndxsbal < 0" style="color: red">{{ scope.row.ndxsbal }}</span>
            <span v-else>{{ scope.row.ndxsbal }}</span>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="治疗未完成" header-align="center">
        <el-table-column prop="wwcrs" label="人数" width="80" align="right" sortable />
        <el-table-column prop="wwcbal" label="盈亏额" width="100" align="right" sortable>
          <template #default="scope">
            <span v-if="scope.row.wwcbal < 0" style="color: red">{{ scope.row.wwcbal }}</span>
            <span v-else>{{ scope.row.wwcbal }}</span>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="转科" header-align="center">
        <el-table-column prop="zkrs" label="人数" width="80" align="right" sortable />
        <el-table-column prop="zkbal" label="盈亏额" width="100" align="right" sortable>
          <template #default="scope">
            <span v-if="scope.row.zkbal < 0" style="color: red">{{ scope.row.zkbal }}</span>
            <span v-else>{{ scope.row.zkbal }}</span>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column prop="dlrs" label="单列" width="100" align="right" sortable></el-table-column>
    </el-table>
  </el-dialog>
  <el-dialog v-model="showBrSetldetails" :close-on-click-modal="false" :close-on-press-escape="false"
    :title="filterBrDialogTitle" width="90%" top="40px">
    <div style="width: 130px; height: 36px; position: absolute; top: 16px; right: 60px">
      <el-button icon="Download" type="primary" @click="exportBrDetailExcel">导出Excel</el-button>
    </div>
    <el-table :data="setlBrdetails.slice((currentPage - 1) * pageSize, currentPage * pageSize)"
      :height="tableHeight - 160" show-summary :summary-method="getSummaries1" highlight-current-row stripe>
      <el-table-column fixed prop="deptName" label="科室" width="100"></el-table-column>
      <el-table-column fixed prop="doctor" label="医生" width="80" align="center"></el-table-column>
      <el-table-column prop="zyh" label="住院号" width="80" align="center"></el-table-column>
      <el-table-column prop="xm" label="姓名" width="80" align="center"></el-table-column>
      <el-table-column prop="yblx" label="险种" width="100" align="center"></el-table-column>
      <el-table-column prop="zfy" label="总费用" width="100" align="right" sortable></el-table-column>
      <el-table-column prop="yjzf" label="预计支付" width="100" align="right" sortable></el-table-column>
      <el-table-column prop="tczf" label="统筹支付" width="100" align="right" sortable></el-table-column>
      <el-table-column prop="bal" label="盈亏额" width="100" align="right" sortable>
        <template #default="scope">
          <span v-if="scope.row.bal < 0" style="color: red">{{ scope.row.bal }}</span>
          <span v-else>{{ scope.row.bal }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="bxbl" label="报销比例(%)" width="100" align="right" sortable></el-table-column>
      <el-table-column prop="zfz" label="总分值" width="100" align="right" sortable></el-table-column>
      <el-table-column prop="zlfs" label="治疗方式" width="100" align="left"></el-table-column>
      <el-table-column prop="sblx" label="申报类型" width="100" align="left"></el-table-column>
      <el-table-column prop="zdmc" label="主要诊断" width="200" align="left" show-overflow-tooltip>
        <template #default="scope"> {{ scope.row.zdmc }}<span>(</span>{{ scope.row.zdbm }}<span>)</span> </template>
      </el-table-column>
      <el-table-column prop="ssmc" label="主要手术" width="200" align="left" show-overflow-tooltip>
        <template #default="scope">
          <span v-if="scope.row.ssmc">{{ scope.row.ssmc }}<span>(</span>{{ scope.row.ssbm }}<span>)</span></span>
        </template>
      </el-table-column>
      <el-table-column prop="jssj" label="结算时间" width="140" align="center"></el-table-column>
    </el-table>
    <el-footer style="height: 40px">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
        :page-sizes="[15, 30, 50, 100]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="setlBrdetails.length" style="margin-top: 5px">
      </el-pagination>
    </el-footer>
  </el-dialog>
  <el-dialog v-model="showImportYb" :close-on-click-modal="false" title="市医保数据导入" width="50%" top="40px">
    <YbImport :ybkf="ybImportData" />
  </el-dialog>
</template>
<script>
import { reactive, ref } from '@vue/reactivity'
import { shortcuts, seltYearAndNowLast } from '@/data/shortcuts'
import { onMounted } from 'vue'
import { clone } from '@/utils/clone'
import { formatDate, getDateRangeFormatDate } from '@/utils/date'
import YbImport from '../../../components/medical-insurance/ybkf/YbImport.vue'
import { selectCssYbJsTj, selectYsJsTj, selectBrJsTj } from '@/api/ybkf/yb-cssybtj'
import { ElMessage, ElMessageBox } from 'element-plus'
import { downloadExcel } from '@/utils/excel'
import store from '@/store'
import PageLayer from '@/layout/PageLayer.vue'

export default {
  components: {
    YbImport,
    PageLayer
  },
  setup() {
    const windowSize = store.state.app.windowSize
    const tableHeight = windowSize.h / 1.03

    const start = formatDate(seltYearAndNowLast[0].value[0])
    const end = formatDate(seltYearAndNowLast[1].value[1])

    const insurId = ref('')

    const queryTerm = reactive({
      startTime: '',
      endTime: '',
      insurName: '',
      deptId: '',
      doctorId: '',
    })
    onMounted(() => {
      queryTerm.startTime = start
      queryTerm.endTime = end + ' 23:59:59'
      dateRange.value = [start, end]
    })

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

    const handleCurrentChange = (val) => {
      currentPage.value = val
    }

    const currentPage = ref(1)
    const pageSize = ref(30)

    const dateRange = ref([])
    const returnData = ref([])

    const query = () => {
      if (dateRange.value) {
        let dateS = getDateRangeFormatDate(dateRange.value)
        queryTerm.startTime = dateS.startTime
        queryTerm.endTime = dateS.endTime
      } else {
        queryTerm.startTime = start
        queryTerm.endTime = end
        ElMessage({
          type: 'info',
          message: '默认查询本月的数据',
          duration: 2500,
          showClose: true,
        })
      }

      queryTerm.insurName = filterInsurName()

      selectCssYbJsTj(queryTerm)
        .then((res) => {
          returnData.value = res
        })
        .catch(() => {
          returnData.value = []
        })
      returnData.value = []
    }

    const exportKsDetailExcel = () => {
      if (returnData.value.length === 0) {
        ElMessage({
          message: '没有可以导出的数据！',
          type: 'warning',
          duration: 2500,
          showClose: true,
        })
      } else {
        // 导出实现
        let selectData = clone(queryTerm)
        let url = '/cssYbTj/exportKsYbSetlExcel'
        exportExcelData(selectData, url)
      }
    }

    // -------------- 医生详情统计 -----------------
    const setlYsdetails = ref([])
    const showYsSetldetails = ref(false)
    // 科室钻取到医生查询
    const ysJsdetails = (row) => {
      queryTerm.deptId = row.deptId
      let selectInfo = clone(queryTerm)
      selectYsJsTj(selectInfo)
        .then((res) => {
          showYsSetldetails.value = true
          setlYsdetails.value = res
        })
        .catch(() => {
          setlYsdetails.value = []
        })
    }

    const filterYsDialogTitle = computed(() => {
      return '市医保结算统计(医生)'
    })

    const exportYsDetailExcel = () => {
      if (setlYsdetails.value.length === 0) {
        ElMessage({
          message: '没有可以导出的数据！',
          type: 'warning',
          duration: 2500,
          showClose: true,
        })
      } else {
        // 导出实现
        let selectData = clone(queryTerm)
        let url = '/cssYbTj/exportYsYbSetlExcel'
        exportExcelData(selectData, url)
      }
    }

    // -------------- 病人详情统计 -----------------
    const setlBrdetails = ref([])
    const showBrSetldetails = ref(false)
    let heji = {}
    // 科室钻取到医生查询
    const brJsdetails = (row) => {
      queryTerm.deptId = row.deptId
      queryTerm.doctorId = row.doctorId == null ? '' : row.doctorId
      let selectInfo = clone(queryTerm)
      selectBrJsTj(selectInfo)
        .then((res) => {
          showBrSetldetails.value = true
          setlBrdetails.value = res.data
          heji = res.total
        }).catch(() => {
          setlBrdetails.value = []
        })
    }

    const filterBrDialogTitle = computed(() => {
      return '市医保结算统计(病人)'
    })

    const exportBrDetailExcel = () => {
      if (setlBrdetails.value.length === 0) {
        ElMessage({
          message: '没有可以导出的数据！',
          type: 'warning',
          duration: 2500,
          showClose: true,
        })
      } else {
        // 导出实现
        let selectData = clone(queryTerm)
        let url = '/cssYbTj/exportBrYbSetlExcel'
        exportExcelData(selectData, url)
      }
    }

    // -------------- 合计 -----------------
    const getSummaries = (param) => {
      const { columns, data } = param
      const sums = []
      let bxblIndex, zfyIndex, tczfIndex

      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        }

        if (column.property === 'doctor' || column.property === 'zyh' || column.property === 'zlfs' || column.property === 'ssmc') {
          sums[index] = ''
          return
        }

        if (column.property === 'bxbl') {
          bxblIndex = index
        }
        if (column.property === 'zfy') {
          zfyIndex = index
        }
        if (column.property === 'tczf') {
          tczfIndex = index
        }

        const values = data.map((item) => Number(item[column.property]))
        if (!values.every((value) => isNaN(value))) {
          let hj = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)

          let reg = /rs$/
          if (!reg.test(column.property)) {
            sums[index] = hj.toFixed(2)
          } else {
            sums[index] = hj
          }
        } else {
          sums[index] = ''
        }
      })
      sums[bxblIndex] = ((sums[tczfIndex] / sums[zfyIndex]) * 100).toFixed(2)
      return sums
    }

    // -------------- 分页合计行(需要统计全部数据的合计，而不是当前页数据的合计) -----------------
    const getSummaries1 = (param) => {
      const { columns } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        }
        if (column.property === 'zfy') {
          sums[index] = heji.zfy
        } else if (column.property === 'tczf') {
          sums[index] = heji.tczf
        } else if (column.property === 'yjzf') {
          sums[index] = heji.yjzf
        } else if (column.property === 'zfz') {
          sums[index] = heji.zfz
        } else if (column.property === 'bal') {
          sums[index] = heji.bal
        } else if (column.property === 'bxbl') {
          sums[index] = heji.bxbl
        }
      })
      return sums
    }

    const filterInsurName = () => {
      if (insurId.value == '1') {
        return '长沙市城职普通住院'
      } else if (insurId.value == '2') {
        return '长沙市城居普通住院'
      } else {
        return ''
      }
    }

    // ------------------ 页面医保数据导出 ------------------
    const exportExcelData = (selectData, t_url) => {
      ElMessageBox.prompt('导出文件名字', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /\S/,
        inputErrorMessage: '文件名不能为空 (∩•̀ω•́)⊃-*⋆',
      })
        .then(({ value }) => {
          const data = {
            param: selectData,
            url: t_url,
            fileName: value + '.xlsx',
          }
          setTimeout(() => {
            downloadExcel(data)
          }, 500)
        })
        .catch(() => { })
    }

    // ------------------ 医保数据导入 ------------------
    const showImportYb = ref(false)
    const ybImportData = ref({})
    const cssybImort = () => {
      showImportYb.value = true
      ybImportData.value = { type: '1' }
    }

    return {
      dateRange,
      shortcuts,
      tableHeight,
      queryTerm,
      insurId,
      returnData,
      pageSize,
      currentPage,
      setlYsdetails,
      showYsSetldetails,
      filterYsDialogTitle,
      setlBrdetails,
      showBrSetldetails,
      filterBrDialogTitle,
      showImportYb,
      ybImportData,
      query,
      cssybImort,
      ysJsdetails,
      brJsdetails,
      exportKsDetailExcel,
      exportYsDetailExcel,
      exportBrDetailExcel,
      getSummaries,
      getSummaries1,
      handleSizeChange,
      handleCurrentChange,
    }
  },
}
</script>
<style scoped>
:deep(.el-dialog__header) {
  margin-right: 2px;
}
</style>