<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <div class="search-form mb-4">
      <el-form
        :model="queryParams"
        inline
        @submit.prevent
      >
        <el-form-item label="搜索关键词">
          <el-input
            v-model="queryParams.searchKey"
            placeholder="请输入药品名称/编码等关键词"
            clearable
            style="width: 200px"
            @keyup.enter="getList"
          />
        </el-form-item>
        <el-form-item label="开始时间">
          <el-date-picker
            v-model="queryParams.startTime"
            type="datetime"
            placeholder="选择开始时间"
            clearable
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            style="width: 180px"
          />
        </el-form-item>
        <el-form-item label="结束时间">
          <el-date-picker
            v-model="queryParams.endTime"
            type="datetime"
            placeholder="选择结束日期"
            clearable
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            style="width: 180px"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="getList"
          >
            查询
          </el-button>
          <el-button
            icon="el-icon-refresh"
            @click="resetQuery"
          >
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 数据表格 -->
    <el-table
      v-loading="loading"
      :data="medicationUsageList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="50"
        align="center"
      />
      <el-table-column
        key="orgId"
        label="医疗机构代码"
        align="center"
        prop="orgId"
        width="200"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="orgName"
        label="组织机构名称"
        align="center"
        prop="orgName"
        width="200"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="nationalDrugCode"
        label="国家药品编码(YPID)"
        align="center"
        prop="nationalDrugCode"
        width="200"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="busNo"
        label="院内药品唯一码"
        align="center"
        prop="busNo"
        width="200"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="provincialDrugCode"
        label="省级药品集中采购平台药品编码"
        align="center"
        prop="provincialDrugCode"
        width="220"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="itemName"
        label="产品名称"
        align="center"
        prop="itemName"
        width="200"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="totalSalesPrice"
        label="销售总金额(元)"
        align="center"
        prop="totalSalesPrice"
        width="200"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="packageSalesQuantity"
        label="销售数量(最小销售包装单位)"
        align="center"
        prop="packageSalesQuantity"
        width="200"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="unitCode_dictText"
        label="销售数量(最小制剂单位)"
        align="center"
        prop="unitCode_dictText"
        width="200"
        :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="medicationUsageDetails">
import {onMounted, ref} from 'vue';
import {getReportMedicationUsage} from './statisticalManagent';

// 表格数据
const medicationUsageList = ref([]);
// 加载状态
const loading = ref(true);
// 选中的ID列表
const ids = ref([]);
// 总条数
const total = ref(0);
// 单选/多选状态（原代码遗漏，补充）
const single = ref(true);
const multiple = ref(true);

// 查询参数（新增searchKey、startTime、endTime）
const queryParams = ref({
  pageNo: 1,
  pageSize: 10,
  searchKey: '', // 搜索关键词
  startTime: '', // 开始时间
  endTime: '', // 结束时间
});

/** 查询调拨管理项目列表 */
function getList() {
  loading.value = true;
  getReportMedicationUsage(queryParams.value).then((res) => {
    medicationUsageList.value = res.data.records;
    total.value = res.data.total;
    loading.value = false;
  });
}

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

/** 重置查询条件 */
function resetQuery() {
  queryParams.value = {
    pageNo: 1,
    pageSize: 10,
    searchKey: '',
    startTime: '',
    endTime: '',
  };
  getList(); // 重置后重新查询
}

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

<style scoped>
/* 搜索表单间距 */
.search-form {
  padding: 10px;
  background: #f5f5f5;
  border-radius: 4px;
}
/* 表格与搜索框间距 */
.mb-4 {
  margin-bottom: 16px;
}
</style>