<template>
  <div class="app-container" v-loading="loadingcontainer" v-if="typeDetail == '1'">
    <el-form
      :model="queryParams"
      ref="earlyWarningRef"
      :inline="true"
      :rules="rules"
      label-width="200px"
    >
      <el-form-item label="处方号/病人名称/开方医生：" prop="searchKey">
        <el-input
          v-model="queryParams.searchKey"
          placeholder="可按处方号/病人名称/开方医生查询"
          clearable
          style="width: 260px"
        />
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <!-- v-hasPermi="['system:user:import']" -->
        <el-button type="primary" plain icon="Search" @click="handleQuery">查询</el-button>
      </el-col>
      <el-col :span="1.5">
        <!-- v-hasPermi="['system:user:export']" -->
        <el-button type="warning" plain icon="CircleClose" @click="handleClear">重置</el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="purchaseinventoryList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column
        label="处方号"
        align="center"
        key="prescriptionNo"
        prop="prescriptionNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="患者"
        align="center"
        key="patientName"
        prop="patientName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="疾病诊断"
        align="center"
        key="conditionDefinitionName"
        prop="conditionDefinitionName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="修改时间"
        align="center"
        key="requestTime"
        prop="requestTime"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.requestTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="开方医生"
        align="center"
        key="practitionerName"
        prop="practitionerName"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="viewDetails(scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
  <!-- <historicalPrescriptionDetail
    v-loading="loadingcontainer"
    v-else
    @handleBack="handleBack"
    :typeDetail="typeDetail"
    :prescriptionNo="prescriptionNo"
  /> -->
  <Prescription v-else />
</template>

<script setup name="historicalPrescription">
import { ref } from 'vue';
import { getPrescriptionPageInfo } from './component/api';
import historicalPrescriptionDetail from './component/details.vue';
import Prescription from './component/prescription.vue';
const { proxy } = getCurrentInstance();

const { item_category_code } = proxy.useDict('item_category_code');
const prescriptionNo = ref('');
const typeDetail = ref('1');
const purchaseinventoryList = ref([]);
const loading = ref(false);
const loadingcontainer = ref(false);
const occurrenceTime = ref([]);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const supplyTypeOptions = ref(undefined);
const supplyStatusOptions = ref(undefined);

const data = reactive({
  form: {},
  queryParams: {
    pageNo: 1,
    pageSize: 10,
    searchKey: undefined,
    categoryCode: undefined,
    remainingMonth: undefined,
    expirationDateSTime: undefined,
    expirationDateETime: undefined,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);
function handleBack(val) {
  loadingcontainer.value = true;
  typeDetail.value = val;
  setTimeout(() => {
    loadingcontainer.value = false;
  }, 800);
}
function viewDetails(row) {
  loadingcontainer.value = true;
  prescriptionNo.value = row.prescriptionNo;
  console.log(row.prescriptionNo, prescriptionNo.value, '详情');
  typeDetail.value = '2';
  setTimeout(() => {
    loadingcontainer.value = false;
  }, 700);
}

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

    //     res = {
    // 	"code": 200,
    // 	"msg": "操作成功",
    // 	"data": {
    // 		"records": [
    // 			{
    // 				"prescriptionNo": "PCN00000029",
    // 				"encounterId": "1937393559205539842",
    // 				"patientId": "1937393514477481986",
    // 				"patientName": "龙傲天",
    // 				"conditionDefinitionName": "感冒",
    // 				"requestTime": "2025-06-24 17:05:36",
    // 				"practitionerName": "李辉"
    // 			},
    // 			{
    // 				"prescriptionNo": "PCN00000028",
    // 				"encounterId": "1937393559205539842",
    // 				"patientId": "1937393514477481986",
    // 				"patientName": "龙傲天",
    // 				"conditionDefinitionName": "感冒",
    // 				"requestTime": "2025-06-24 17:02:18",
    // 				"practitionerName": "李辉"
    // 			},
    // 			{
    // 				"prescriptionNo": "PCN00000027",
    // 				"encounterId": "1937393559205539842",
    // 				"patientId": "1937393514477481986",
    // 				"patientName": "龙傲天",
    // 				"conditionDefinitionName": "感冒",
    // 				"requestTime": "2025-06-24 14:25:21",
    // 				"practitionerName": "李辉"
    // 			},
    // 			{
    // 				"prescriptionNo": "PCN00000023",
    // 				"encounterId": "1937393559205539842",
    // 				"patientId": "1937393514477481986",
    // 				"patientName": "龙傲天",
    // 				"conditionDefinitionName": "感冒",
    // 				"requestTime": "2025-06-24 14:14:26",
    // 				"practitionerName": "李辉"
    // 			},
    // 			{
    // 				"prescriptionNo": "PCN00000022",
    // 				"encounterId": "1937113899091587074",
    // 				"patientId": "1937066626118815745",
    // 				"patientName": "就诊卡",
    // 				"conditionDefinitionName": "肾痨",
    // 				"requestTime": "2025-06-24 14:05:18",
    // 				"practitionerName": "李辉"
    // 			},
    // 			{
    // 				"prescriptionNo": "PCN00000004",
    // 				"encounterId": "1935570184300519426",
    // 				"patientId": "1935541163105837058",
    // 				"patientName": "孙嘉琦",
    // 				"conditionDefinitionName": "眼科杂病",
    // 				"requestTime": "2025-06-19 16:24:47",
    // 				"practitionerName": "李辉"
    // 			},
    // 			{
    // 				"prescriptionNo": "PCN00000003",
    // 				"encounterId": "1935570184300519426",
    // 				"patientId": "1935541163105837058",
    // 				"patientName": "孙嘉琦",
    // 				"conditionDefinitionName": "眼科杂病",
    // 				"requestTime": "2025-06-19 16:24:47",
    // 				"practitionerName": "李辉"
    // 			},
    // 			{
    // 				"prescriptionNo": "PCN00000002",
    // 				"encounterId": "1935570184300519426",
    // 				"patientId": "1935541163105837058",
    // 				"patientName": "孙嘉琦",
    // 				"conditionDefinitionName": "眼科杂病",
    // 				"requestTime": "2025-06-19 14:40:05",
    // 				"practitionerName": "李辉"
    // 			}
    // 		],
    // 		"total": 8,
    // 		"size": 10,
    // 		"current": 1,
    // 		"orders": [],
    // 		"optimizeCountSql": true,
    // 		"searchCount": true,
    // 		"maxLimit": null,
    // 		"countId": null,
    // 		"pages": 1
    // 	}
    // }

    purchaseinventoryList.value = res.data.records;
    total.value = res.data.total;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  //   proxy.$refs['earlyWarningRef'].validate((valid) => {
  // if (valid) {
  queryParams.value.expirationDateSTime =
    occurrenceTime.value && occurrenceTime.value.length == 2
      ? occurrenceTime.value[0] + ' 00:00:00'
      : '';
  queryParams.value.expirationDateETime =
    occurrenceTime.value && occurrenceTime.value.length == 2
      ? occurrenceTime.value[1] + ' 23:59:59'
      : '';
  queryParams.value.pageNo = 1;
  getList();
  // }
  //   })
}

/** 清空条件按钮操作 */
function handleClear() {
  // 清空查询条件
  queryParams.value.expirationDateSTime = '';
  queryParams.value.expirationDateETime = '';
  occurrenceTime.value = '';
  proxy.resetForm('earlyWarningRef');
  getList();
}

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

getList();
</script>
<style scoped>
.custom-tree-node {
  display: flex;
  align-items: center;
}
</style>