<script setup lang="ts">
import { ref } from 'vue';
import { useNaiveForm } from '@/hooks/common/form';
import { $t } from '@/locales';
import { useDict } from '@/hooks/business/dict';
defineOptions({
  name: 'BatchSearch'
});

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

const emit = defineEmits<Emits>();

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


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

const { options: preBatchStatusOptions } = useDict('pre_batch_status');
const { options: afterBatchStatusOptions } = useDict('after_batch_status');
const { options: mpsCheckStatusOptions } = useDict('mps_check_status');

async function reset() {
  Object.assign(model.value.params!, {});
  await restoreValidation();
  emit('reset');
}

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

function disablePreviousDate(ts) {
  return ts > Date.now()
}
</script>

<template>
  <NCard :bordered="false" size="small" class="card-wrapper">
    <NCollapse>
      <NCollapseItem :title="$t('common.search')" name="user-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="批次月份" path="batchMonth" class="pr-24px">
              <NDatePicker
                v-model:formatted-value="model.batchMonth"
                type="month"
                format="y年 M月"
                year-format="y年"
                month-format="M月"
                value-format="yyyyMM"
                clearable
                :is-date-disabled="disablePreviousDate"
                :update-value-on-close="true"
              />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="核对备注" path="checkRemark" class="pr-24px">
              <NInput v-model:value="model.checkRemark" placeholder="请输入核对备注" />
            </NFormItemGi>
            <NFormItemGi span="24" class="pr-24px">
              <NSpace class="w-full" justify="end">
                <NButton @click="reset">
                  <template #icon>
                    <SvgIcon local-icon="round-refresh" class="text-icon" />
                  </template>
                  {{ $t('common.reset') }}
                </NButton>
                <NButton type="primary" ghost @click="search">
                  <template #icon>
                    <SvgIcon local-icon="round-search" class="text-icon" />
                  </template>
                  {{ $t('common.search') }}
                </NButton>
              </NSpace>
            </NFormItemGi>
          </NGrid>
        </NForm>
      </NCollapseItem>
    </NCollapse>
  </NCard>
</template>

<style scoped></style>
