<template>
  <div>请选择要导出的数据：</div>
  <div class="input_area">
    <el-select v-model="exportRequest.type" style="width: 220px">
      <el-option label="复印" value="DUPLICATE"></el-option>
      <el-option label="封存" value="CLOSEDOWN"></el-option>
      <el-option label="借阅" value="LENDOUT"></el-option>
    </el-select>
  </div>

  <div style="margin-top: 24px">请选择数据时间范围：</div>
  <div class="input_area mg-btm">
    <CyDateRange />
  </div>
</template>

<script setup lang="ts">
import useDateRange from "@/utils/cy-use/useDateRange";
import { Export } from "@/utils/ExportExcel";
import { getExportableDclData } from "@/api/case-front-sheet";
import { UseDialogType } from "@/components/cy/CyDialog/index.js";

const exportRequest = reactive({
  type: "DUPLICATE",
  begin: "",
  end: "",
});

const { CyDateRange, dateRange } = useDateRange({
  shortcutsIndex: 5,
  clearable: false,
});

function executeExport() {
  exportRequest.begin = dateRange.value.start;
  exportRequest.end = dateRange.value.end;
  let label = getLabel();
  getExportableDclData(exportRequest).then(res => {
    Export(res, generateField(label), label + "记录");
  });
}

function generateField(label) {
  const field = {
    bah: "住院号",
    times: "住院次数",
    patName: "患者姓名",
    admissDate: "入院时间",
    disDate: "出院时间",
    disDept: "出院科室",
    doctor: "管床医生",
    visitStaff: label + "人员",
    opTime: label + "时间",
    remark: "备注",
    opStaff: "病案管理员",
  };
  if (exportRequest.type === "LENDOUT") {
    field.lendStaffPhone = "借阅人员电话";
    field.state = "状态";
    field.returnTime = "归还时间";
  }
  return field;
}

function getLabel() {
  switch (exportRequest.type) {
    case "DUPLICATE":
      return "复印";
    case "CLOSEDOWN":
      return "封存";
    case "LENDOUT":
      return "借阅";
    default:
      return "";
  }
}

defineExpose<UseDialogType.Expose>({
  confirm: executeExport,
});
</script>

<style scoped>
.input_area {
  padding: 8px 24px 0 24px;
}

.mg-btm {
  margin-bottom: 16px;
}
</style>
