<template>
  <BasicDialog ref="basicDialogRef" title="发起盘点任务" @refresh="refreshFromData">
    <template #form>
      <BasicForm
        ref="basicFormRef"
        :formData="ruleFormData"
        :formRules="formRules"
        :formConfig="ruleFormConfig"
        style="margin-bottom: 15px"
      />
    </template>
    <template #footer>
      <el-button type="primary" @click="handleSumbit">提交</el-button>
      <el-button @click="basicDialogRef?.closeDialog">取消</el-button>
    </template>
  </BasicDialog>
</template>

<script setup lang="ts">
  import { ref } from 'vue';
  import { ElMessage } from 'element-plus';
  import BasicDialog from '@/components/BasicDialog.vue';
  import BasicForm from '@/components/BasicForm.vue';
  import { useFormConfigHook } from '@/hooks/useFormConfigHook';
  import { InventoryTaskForm } from '@/types/emergency-supplier';
  import { saveInventoryTask } from '@/api/emergency-supplier';
  import { INVENTORY_TASK_FROM_CONFIG, INVENTORY_TASK_FROM_DATA, INVENTORY_TASK_FROM_RULES } from '../config';

  const basicDialogRef = ref<InstanceType<typeof BasicDialog>>();
  const basicFormRef = ref<InstanceType<typeof BasicForm>>();
  const { ruleFormConfig, ruleFormData, formRules } = useFormConfigHook<InventoryTaskForm>(
    INVENTORY_TASK_FROM_CONFIG,
    INVENTORY_TASK_FROM_DATA,
    INVENTORY_TASK_FROM_RULES,
  );
  const openDialog = () => {
    basicDialogRef.value?.openDialog();
  };
  const handleSumbit = async () => {
    const validate = await basicFormRef.value?.validateForm();
    if (!validate) return;
    await saveInventoryTask(ruleFormData.taskName, ruleFormData.endTime);
    ElMessage.success('发起盘点任务成功');
    basicDialogRef.value?.closeDialog();
  };
  const refreshFromData = () => {
    basicFormRef.value?.clearValidate();
  };
  defineExpose({
    openDialog,
  });
</script>

<style scoped lang="scss"></style>
