<template>
  <div class="app-container">
    <el-form
      ref="queryRef"
      :model="queryParams"
      :inline="true"
      label-width="100px"
    >
      <el-form-item
        label="搜索条件"
        prop="searchKey"
      >
        <el-input
          v-model="queryParams.searchKey"
          placeholder="门诊号/药品名/生产企业"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="药品医保编码"
        prop="medYbNo"
      >
        <el-input
          v-model="queryParams.medYbNo"
          placeholder=""
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="药品类型："
        prop="categoryCode"
      >
        <el-select
          v-model="queryParams.categoryCode"
          placeholder=""
          clearable
          style="width: 150px"
        >
          <el-option
            v-for="dict in med_category_code"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
    </el-form>

    <el-row
      :gutter="10"
      class="mb8"
    >
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Search"
          @click="handleQuery"
        >
          查询
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="CircleClose"
          @click="handleClear"
        >
          重置
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
        >
          导出
        </el-button>
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="patientDetailsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="50"
        align="center"
      />
      <el-table-column
        key="encounterBusNo"
        label="门诊住院号"
        align="center"
        prop="encounterBusNo"
        width="200"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="medName"
        label="药品通用名"
        align="center"
        prop="medName"
        width="140"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="medYbNo"
        label="药品医保编码"
        align="center"
        prop="medYbNo"
        width="200"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="categoryCode_dictText"
        label="药品类别"
        align="center"
        prop="categoryCode_dictText"
        width="200"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="doseFormCode_dictText"
        label="剂型"
        align="center"
        prop="doseFormCode_dictText"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="totalVolume"
        label="规格"
        align="center"
        prop="totalVolume"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="packageUnit_dictText"
        label="包装"
        align="center"
        prop="packageUnit_dictText"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="unitCode_dictText"
        label="计价单位"
        align="center"
        prop="unitCode_dictText"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="isSelected"
        label="是否中选产品"
        align="center"
        prop="isSelected"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="manufacturerText"
        label="生产企业"
        align="center"
        prop="manufacturerText"
        width="240"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="price"
        label="单价（元）"
        align="center"
        prop="price"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="quantity"
        label="使用数量（片袋支）"
        align="center"
        prop="quantity"
        width="160"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="totalPrice"
        label="使用总金额（元）"
        align="center"
        prop="totalPrice"
        width="140"
        :show-overflow-tooltip="true"
      />
    </el-table>
    <pagination
      v-show="total > 0"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="patientDetails">
import {getCurrentInstance, onMounted, ref} from 'vue';
import {getReportPatientDetails} from './statisticalManagent';

// 获取当前实例的proxy
const { proxy } = getCurrentInstance();

const patientDetailsList = ref([]);
const loading = ref(true);
const ids = ref([]);
const total = ref(0);
const single = ref(true); 
const multiple = ref(true); 
const queryRef = ref(); 

// 字典数据
const { med_category_code } = proxy.useDict("med_category_code");

const queryParams = ref({
  pageNo: 1,
  pageSize: 10,
  searchKey: undefined,
  categoryCode: undefined, // 添加缺失的categoryCode属性
  encounterBusNo: undefined,
  medName: undefined,
  medYbNo: undefined,
  manufacturerText: undefined,
});

/** 查询患者明细列表 */
function getList() {
  loading.value = true;
  getReportPatientDetails(queryParams.value).then((res) => {
    loading.value = false;
    patientDetailsList.value = res.data.records;
    total.value = res.data.total;
  }).catch(() => {
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNo = 1;
  getList();
}

/** 清空条件按钮操作 */
function handleClear() {
  proxy.resetForm('queryRef');
  queryParams.value.pageNo = 1;
  getList();
}

/** 选择条数  */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 导出按钮操作 */
function handleExport() {
  proxy.$download.downloadGet(
    'report-manage/inbound/excel-out',
    {
      ...queryParams.value,
    },
    `patient_details_${new Date().getTime()}.xlsx`
  );
}

// 页面加载时获取数据
onMounted(() => {
  getList();
});
</script>

<style scoped>
.custom-tree-node {
  display: flex;
  align-items: center;
}

.title {
  font-weight: bold;
  font-size: large;
  margin-bottom: 10px;
}

.mb8 {
  margin-bottom: 8px;
}
</style>