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

defineOptions({
  name: 'TodoWorkSearch'
});

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

const emit = defineEmits<Emits>();

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

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

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

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

async function search() {
  await validate();
  emit('search');
}

// 监听 range 变化并同步到 model
watch(range, newRange => {
  if (newRange !== null) {
    model.value.CreateTimeStart = newRange[0];
    model.value.CreateTimeEnd = newRange[1];
  }
});
</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 :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="OrderLine" class="pr-24px">
              <NInput v-model:value="model.OrderLine" placeholder="请输入订单+行号" />
            </NFormItemGi>

            <NFormItemGi :label-width="100" span="24 s:12 m:6" label="任务数量" path="TaskQty" class="pr-24px">
              <NInputNumber v-model:value="model.TaskQty" 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="TaskStatus" class="pr-24px">
              <NSelect
                v-model:value="model.TaskStatus"
                :options="[
                  { label: '待执行', value: 0 },
                  { label: '执行中', value: 1 },
                  { label: '已完成', value: 2 },
                  { label: '异常', value: 3 }
                ]"
                placeholder="请选择任务状态"
              />
            </NFormItemGi>

            <NFormItemGi :label-width="100" span="24 s:12 m:6" label="任务类型" path="TaskType" class="pr-24px">
              <NSelect
                v-model:value="model.TaskType"
                :options="[
                  { label: '入库', value: 1 },
                  { label: '出库', value: 2 },
                  { label: '盘库', value: 3 }
                ]"
                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="CreateTime" class="pr-24px">
              <!-- <NInput v-model:value="model.CreateTime" placeholder="请输入创建时间" /> -->
              <NDatePicker
                v-model:formatted-value="range"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetimerange"
                clearable
              />
              <!-- <NDatePicker v-model:value="range" type="datetimerange" clearable /> -->
            </NFormItemGi>

            <!-- 操作按钮 -->
            <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>
              </NSpace>
            </NFormItemGi>
          </NGrid>
        </NForm>
      </NCollapseItem>
    </NCollapse>
  </NCard>
</template>

<style scoped></style>
