<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="earlyWarningRef"
      :inline="true"
      :rules="rules"
      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="remainingDays" label-width="150px">
        <el-input
          v-model="queryParams.remainingDays"
          placeholder="请输入效期小于（天）"
          clearable
          style="width: 200px"
          @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="categoryCode in item_category_code"
            :key="categoryCode.value"
            :label="categoryCode.label"
            :value="categoryCode.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="有效期介于：">
        <el-date-picker
          v-model="occurrenceTime"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 300px"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item label="库存件数检查">
        <el-radio-group v-model="queryParams.flg" @change="handleQuery">
          <el-radio label="1" :value="1">是</el-radio>
          <el-radio label="0" :value="0">否</el-radio>
        </el-radio-group>
      </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="categoryCode_dictText"
        prop="categoryCode_dictText"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="药品编码"
        align="center"
        key="busNo"
        prop="busNo"
        width="140"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="药品名称"
        align="center"
        key="medicineName"
        prop="medicineName"
        width="240"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="规格"
        align="center"
        key="totalVolume"
        prop="totalVolume"
        width="270"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="厂家/产地"
        align="center"
        key="manufacturerText"
        prop="manufacturerText"
        width="320"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="生产批号"
        align="center"
        key="lotNumber"
        prop="lotNumber"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="有效期至"
        align="center"
        key="expirationDate"
        prop="expirationDate"
        width="165"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.expirationDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="效期（天）"
        align="center"
        key="remainingDays"
        prop="remainingDays"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="效期（月）"
        align="center"
        key="remainingMonth"
        prop="remainingMonth"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="库存单位"
        align="center"
        key="minUnitCode_dictText"
        prop="minUnitCode_dictText"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="库存量（最小单位）"
        align="center"
        key="quantity"
        prop="quantity"
        width="150"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="该药总库存（最小单位）"
        align="center"
        key="specificationQuantity"
        prop="specificationQuantity"
        width="170"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="最小库存警戒量"
        align="center"
        key="itemMinQuantity"
        prop="itemMinQuantity"
        width="150"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="最大库存警戒量"
        align="center"
        key="itemMaxQuantity"
        prop="itemMaxQuantity"
        width="150"
        :show-overflow-tooltip="true"
      />
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="earlyWarning">
import { getExpirationWarning } from './statisticalManagent';

const { proxy } = getCurrentInstance();

const { item_category_code } = proxy.useDict('item_category_code');

const purchaseinventoryList = ref([]);
const loading = ref(true);
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,
    remainingDays: 180,
    expirationDateSTime: undefined,
    expirationDateETime: undefined,
    flg: undefined,
  },
  rules: {
    remainingMonth: [
      // { required: true, message: '单次最大剂量不能为空', trigger: 'blur' },
      { validator: validateMaxUnit, trigger: 'blur' },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

function validateMaxUnit(rule, value, callback) {
  const numberRegex = /^[0-9]+$/; // 正则表达式，确保输入为数字
  if (!numberRegex.test(value)) {
    callback(new Error('请输入有效的数字'));
  } else {
    callback();
  }
}
/** 查询调拨管理项目列表 */
function getList() {
  loading.value = true;
  const params = {
    ...queryParams.value,
    flg: queryParams.value.flg == 1 ? '1' : null,
  };
  // getExpirationWarning(params).then((res) => {
  //   loading.value = false;
  //   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>