<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="earlyWarningRef" :inline="true" :rules="rules" label-width="140px">
      <el-form-item label="追溯码：" prop="searchKey" label-width="120px">
        <el-input v-model="queryParams.searchKey" placeholder="请输入药品名称/追溯码" clearable @keyup.enter="handleQuery" style="width: 200px" />
      </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="追溯码状态：" prop="statusEnum" label-width="120px">
        <el-select v-model="queryParams.statusEnum" placeholder="请选择追溯码状态" clearable style="width: 200px">
          <el-option v-for="traceNoStatus in traceNoStatusOptions" :key="traceNoStatus.value"
            :label="traceNoStatus.label" :value="traceNoStatus.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="操作类型：" prop="operationType" label-width="120px">
        <el-select v-model="queryParams.operationType" placeholder="请选择操作类型" clearable style="width: 200px">
          <el-option v-for="operationType in operationTypeOptions" :key="operationType.value"
            :label="operationType.label" :value="operationType.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <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-form-item>
    </el-form>



    <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="name" prop="name"
        :show-overflow-tooltip="true" />
      <el-table-column label="药房药库" align="center" key="locationName" prop="locationName" width="140"
        :show-overflow-tooltip="true" />
      <el-table-column label="产品批号" align="center" key="lotNumber" prop="lotNumber" width="120"
        :show-overflow-tooltip="true" />
      <el-table-column label="追溯码" align="center" key="traceNo" prop="traceNo" width="200"
        :show-overflow-tooltip="true" />
      <el-table-column label="追溯码状态" align="center" key="statusEnum_enumText" prop="statusEnum_enumText" width="140"
        :show-overflow-tooltip="true" />
      <el-table-column label="追溯码单位" align="center" key="unitCode_dictText" prop="unitCode_dictText" width="140"
        :show-overflow-tooltip="true" />
      <el-table-column label="操作类型" align="center" key="operationType_enumText" prop="operationType_enumText" width="165"
        :show-overflow-tooltip="true" />
      <el-table-column label="项目类型" align="center" key="itemType_enumText" prop="itemType_enumText" :show-overflow-tooltip="true" />
      <el-table-column label="进出时间" align="center" key="createTime" prop="createTime" :show-overflow-tooltip="true" width="180" />
    </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 {
  inventoryReceiptPage,
  traceabilityCodeManagementInit
} from "./statisticalManagent";
import { onMounted } from 'vue';
import { formatDateStr } from '@/utils';


const { proxy } = getCurrentInstance();

const {
  item_type,
} = proxy.useDict(
  "item_type",
);

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 locationIdList = ref(undefined);
const traceNoStatusOptions = ref(undefined);
const operationTypeOptions = ref(undefined);
const itemTypeOptions = ref(undefined);

const props = defineProps({
  recordInfo: {
    type: Object,
    required: true,
    default: () => ({})
  }
});

const data = reactive({
  form: {},
  queryParams: {
    pageNo: 1,
    pageSize: 10,
    searchKey: undefined,
    categoryCode: undefined,
    remainingMonth: undefined,
    createTimeSTime: undefined,
    createTimeETime: undefined,
  },
  rules: {
    remainingMonth: [
      // { required: true, message: '单次最大剂量不能为空', trigger: 'blur' },
      { validator: validateMaxUnit, trigger: 'blur' },
    ],
  },
});

onMounted(() => {
  setDefaultDateRange();
  getList();
});

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 setDefaultDateRange() {
  const today = new Date();
  const oneMonthAgo = new Date(today);
  oneMonthAgo.setMonth(today.getMonth() - 1);
  occurrenceTime.value = [formatDateStr(oneMonthAgo, 'YYYY-MM-DD'), formatDateStr(today, 'YYYY-MM-DD')];
  queryParams.value.createTimeSTime =
    occurrenceTime.value && occurrenceTime.value.length == 2
      ? occurrenceTime.value[0] + " 00:00:00"
      : "";
  queryParams.value.createTimeETime =
    occurrenceTime.value && occurrenceTime.value.length == 2
      ? occurrenceTime.value[1] + " 23:59:59"
      : "";
}
function getInit() {
  traceabilityCodeManagementInit().then((res) => {
    locationIdList.value = res.data.locationOptions;
    traceNoStatusOptions.value = res.data.traceNoStatusOptions;
    operationTypeOptions.value = res.data.operationTypeOptions;
    itemTypeOptions.value = res.data.itemTypeOptions;
  });
}
/** 查询调拨管理项目列表 */
function getList() {
  loading.value = true;
  queryParams.value.locationId = props.recordInfo.locationId;
  queryParams.value.lotNumber = props.recordInfo.lotNumber;
  queryParams.value.itemTable = props.recordInfo.itemTable;
  queryParams.value.itemId = props.recordInfo.itemId;
  inventoryReceiptPage(queryParams.value).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.createTimeSTime =
        occurrenceTime.value && occurrenceTime.value.length == 2
          ? occurrenceTime.value[0] + " 00:00:00"
          : "";
      queryParams.value.createTimeETime =
        occurrenceTime.value && occurrenceTime.value.length == 2
          ? occurrenceTime.value[1] + " 23:59:59"
          : "";
      queryParams.value.locationId = props.recordInfo.locationId;
      queryParams.value.lotNumber = props.recordInfo.lotNumber;
      queryParams.value.itemTable = props.recordInfo.itemTable;
      queryParams.value.itemId = props.recordInfo.itemId;
      queryParams.value.pageNo = 1;
      getList();
    }
  })
}

/** 清空条件按钮操作 */
function handleClear() {
  // 清空查询条件
  queryParams.value.createTimeSTime = ""
  queryParams.value.createTimeETime = ""
  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;
}
getInit()

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