<script setup lang="ts">
import { ref, watch } from 'vue';
import { $t } from '@/locales';
import { useNaiveForm } from '@/hooks/common/form';

defineOptions({
  name: 'LybSearch'
});

interface Emits {
  (e: 'reset'): void;
  (e: 'search'): void;
}

const emit = defineEmits<Emits>();

const { formRef, validate, restoreValidation } = useNaiveForm();

const model = defineModel<Api.Exploitation.InventorySearchParams>('model', { required: true });

const range = ref<[string, string] | null>(null);

async function reset() {
  range.value = null;
  await restoreValidation();
  emit('reset');
}

// 监听 range 变化并同步到 model
watch(range, newRange => {
  if (newRange !== null) {
    model.value.CreateTimeStart = newRange[0];
    model.value.CreateTimeEnd = newRange[1];
  }
});
async function search() {
  await validate();
  emit('search');
}
// 定义额外条件显示状态
const showExtra = ref(false);

// 切换额外条件显示/隐藏
const toggleExtra = () => {
  showExtra.value = !showExtra.value;
};
</script>

<template>
  <NCard :bordered="false" size="small" class="card-wrapper">
    <NCollapse>
      <NCollapseItem :title="$t('common.search')" name="code-search">
        <NForm ref="formRef" :model="model" label-placement="left" :label-width="80">
          <NGrid responsive="screen" item-responsive>
            <!-- 常用查询条件 -->
            <NFormItemGi
              span="24 s:12 m:6"
              label="订单号加行号"
              :label-width="100"
              path="OrderLineNumber"
              class="pr-24px"
            >
              <NInput v-model:value="model.OrderLineNumber" placeholder="请输入订单号加行号" />
            </NFormItemGi>

            <NFormItemGi
              :label-width="100"
              span="24 s:12 m:6"
              label="采购订单号"
              path="PurchaseOrderNumber"
              class="pr-24px"
            >
              <NInput v-model:value="model.PurchaseOrderNumber" placeholder="请输入采购订单号" />
            </NFormItemGi>

            <NFormItemGi :label-width="100" span="24 s:12 m:6" label="库存单位" path="StockUnit" class="pr-24px">
              <NInput v-model:value="model.StockUnit" placeholder="请输入库存单位" />
            </NFormItemGi>

            <NFormItemGi :label-width="100" span="24 s:12 m:6" label="物资类型" path="MaterialType" class="pr-24px">
              <NInput v-model:value="model.MaterialType" placeholder="请输入物资类型" />
            </NFormItemGi>

            <NFormItemGi :label-width="100" span="24 s:12 m:6" label="项目名称" path="ProjectName" class="pr-24px">
              <NInput v-model:value="model.ProjectName" placeholder="请输入项目名称" />
            </NFormItemGi>

            <NFormItemGi :label-width="100" span="24 s:12 m:6" label="物资名称" path="MaterialName" class="pr-24px">
              <NInput v-model:value="model.MaterialName" placeholder="请输入物资名称" />
            </NFormItemGi>

            <NFormItemGi :label-width="100" span="24 s:12 m:6" label="项目类型" path="ProjectType" class="pr-24px">
              <NInput v-model:value="model.ProjectType" placeholder="请输入项目类型" />
            </NFormItemGi>
            <NFormItemGi :label-width="100" span="24 s:12 m:6" label="查询方式" path="SearchType" class="pr-24px">
              <NSelect
                v-model:value="model.SearchType"
                :options="[
                  { label: '按数量统计', value: 0 },
                  { label: '按记录查询', value: 1 }
                ]"
                placeholder="请选择查询方式"
              />
            </NFormItemGi>
            <!-- 可隐藏的查询条件 -->
            <template v-if="showExtra">
              <NFormItemGi :label-width="100" span="24 s:12 m:6" label="库位号" path="CellNo" class="pr-24px">
                <NInput v-model:value="model.CellNo" placeholder="请输入库位号" />
              </NFormItemGi>
              <NFormItemGi :label-width="100" span="24 s:12 m:6" label="RFID编码" path="RFID" class="pr-24px">
                <NInput v-model:value="model.RFID" placeholder="请输入RFID编码" />
              </NFormItemGi>
              <NFormItemGi :label-width="100" span="24 s:12 m:6" label="托盘编码" path="PalletNo" class="pr-24px">
                <NInput v-model:value="model.PalletNo" placeholder="请输入托盘编码" />
              </NFormItemGi>
              <NFormItemGi :label-width="100" span="24 s:12 m:6" label="创建人" path="CreateUser" class="pr-24px">
                <NInput v-model:value="model.CreateUser" placeholder="请输入创建人名称" />
              </NFormItemGi>
              <NFormItemGi
                :label-width="100"
                span="24 s:12 m:6"
                label="采购订单行项目号"
                path="PurchaseOrderLineItem"
                class="pr-24px"
              >
                <NInput v-model:value="model.PurchaseOrderLineItem" placeholder="请输入采购订单行项目号" />
              </NFormItemGi>

              <NFormItemGi
                :label-width="100"
                span="24 s:12 m:6"
                label="采购申请号"
                path="PurchaseRequestNumber"
                class="pr-24px"
              >
                <NInput v-model:value="model.PurchaseRequestNumber" placeholder="请输入采购申请号" />
              </NFormItemGi>

              <NFormItemGi :label-width="100" span="24 s:12 m:6" label="订单数量" path="OrderQuantity" class="pr-24px">
                <NInputNumber v-model:value="model.OrderQuantity" placeholder="请输入订单数量" />
              </NFormItemGi>

              <NFormItemGi :label-width="100" span="24 s:12 m:6" label="物料单位" path="MaterialUnit" class="pr-24px">
                <NInput v-model:value="model.MaterialUnit" placeholder="请输入物料单位" />
              </NFormItemGi>

              <NFormItemGi :label-width="100" span="24 s:12 m:6" label="供应商名称" path="SupplierName" class="pr-24px">
                <NInput v-model:value="model.SupplierName" placeholder="请输入供应商名称" />
              </NFormItemGi>

              <NFormItemGi
                :label-width="100"
                span="24 s:12 m:6"
                label="采购订单计划交货日期"
                path="PlannedDeliveryDate"
                class="pr-24px"
              >
                <NInput v-model:value="model.PlannedDeliveryDate" placeholder="请输入采购订单计划交货日期" />
              </NFormItemGi>

              <NFormItemGi
                :label-width="100"
                span="24 s:12 m:6"
                label="ERP系统工厂"
                path="ERPSystemFactory"
                class="pr-24px"
              >
                <NInput v-model:value="model.ERPSystemFactory" placeholder="请输入ERP系统工厂" />
              </NFormItemGi>

              <NFormItemGi :label-width="100" span="24 s:12 m:6" label="项目编码" path="ProjectCode" class="pr-24px">
                <NInput v-model:value="model.ProjectCode" placeholder="请输入项目编码" />
              </NFormItemGi>

              <NFormItemGi
                :label-width="100"
                span="24 s:12 m:6"
                label="签约单价(不含税)"
                path="ContractUnitPrice"
                class="pr-24px"
              >
                <NInputNumber
                  v-model:value="model.ContractUnitPrice"
                  placeholder="请输入签约单价(不含税)"
                  :precision="2"
                />
              </NFormItemGi>

              <NFormItemGi
                :label-width="100"
                span="24 s:12 m:6"
                label="签约总价(不含税)"
                path="ContractTotalPrice"
                class="pr-24px"
              >
                <NInputNumber
                  v-model:value="model.ContractTotalPrice"
                  placeholder="请输入签约总价(不含税)"
                  :precision="2"
                />
              </NFormItemGi>

              <NFormItemGi :label-width="100" span="24 s:12 m:6" label="大类" path="MajorCategory" class="pr-24px">
                <NInput v-model:value="model.MajorCategory" placeholder="请输入大类" />
              </NFormItemGi>

              <NFormItemGi :label-width="100" span="24 s:12 m:6" label="中类" path="MiddleCategory" class="pr-24px">
                <NInput v-model:value="model.MiddleCategory" placeholder="请输入中类" />
              </NFormItemGi>

              <NFormItemGi :label-width="100" span="24 s:12 m:6" label="小类" path="SubCategory" class="pr-24px">
                <NInput v-model:value="model.SubCategory" placeholder="请输入小类" />
              </NFormItemGi>

              <NFormItemGi :label-width="100" span="24 s:12 m:6" label="日期" path="status" class="pr-24px">
                <NDatePicker
                  v-model:formatted-value="range"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  type="datetimerange"
                  clearable
                />
              </NFormItemGi>

              <NFormItemGi
                :label-width="100"
                span="24 s:12 m:6"
                label="采购方式"
                path="ProcurementMethod"
                class="pr-24px"
              >
                <NInput v-model:value="model.ProcurementMethod" placeholder="请输入采购方式" />
              </NFormItemGi>
            </template>

            <!-- 操作按钮 -->
            <NFormItemGi span="24 m:24" class="pr-24px">
              <NSpace class="w-full" justify="end">
                <NButton @click="reset">
                  <template #icon>
                    <icon-ic-round-refresh class="text-icon" />
                  </template>
                  {{ $t('common.reset') }}
                </NButton>
                <NButton type="primary" ghost @click="search">
                  <template #icon>
                    <icon-ic-round-search class="text-icon" />
                  </template>
                  {{ $t('common.search') }}
                </NButton>
                <!-- 展开/隐藏按钮 -->
                <NButton type="info" ghost @click="toggleExtra">{{ showExtra ? '收起' : '展开' }}详细条件</NButton>
              </NSpace>
            </NFormItemGi>
          </NGrid>
        </NForm>
      </NCollapseItem>
    </NCollapse>
  </NCard>
</template>

<style scoped></style>
