<template>
  <a-modal :open="visible" title="新增" cancelText="取消" okText="提交" @ok="submit" @cancel="cancel">
    <a-form ref="formRef" :model="formState" :rules="formRules" :label-col="labelCol" :wrapper-col="wrapperCol">
      <a-form-item ref="skuCode" label="物料编号" name="skuCode">
        <a-input v-model:value="formState.skuCode" placeholder="请输入物料编号" />
      </a-form-item>
      <a-form-item ref="planCode" label="计划编号" name="planCode">
        <a-input v-model:value="formState.planCode" placeholder="请输入计划编号" />
      </a-form-item>
      <a-form-item ref="channelCode" label="通道编号" name="channelCode">
        <a-input-number v-model:value="formState.channelCode" placeholder="请输入通道编号" style="width: 100%;"/>
      </a-form-item>
      <a-form-item ref="target" label="目标站台" name="target">
        <a-input-number v-model:value="formState.target" placeholder="请输入目标站台" style="width: 100%;"/>
      </a-form-item>
      <a-form-item ref="fullQty" label="满盘数量" name="fullQty">
        <a-input-number v-model:value="formState.fullQty" placeholder="请输满盘数量" style="width: 100%;"/>
      </a-form-item>
      <a-form-item ref="totalQty" label="总分配数量" name="totalQty">
        <a-input-number v-model:value="formState.totalQty" placeholder="请输计划数量" style="width: 100%;"/>
      </a-form-item>
      <a-form-item ref="currentQty" label="当前数量" name="currentQty">
        <a-input-number v-model:value="formState.currentQty" placeholder="请输当前数量" style="width: 100%;"/>
      </a-form-item>
      <a-form-item ref="mode" label="码垛模式" name="mode">
        <a-input-number v-model:value="formState.mode" placeholder="请输码垛模式" style="width: 100%;"/>
      </a-form-item>
      <a-form-item ref="channelGroup" label="通道分组" name="channelGroup">
        <a-input-number v-model:value="formState.channelGroup" placeholder="请输通道分组" style="width: 100%;"/>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
    import {
    message
  } from '@hwork/ant-design-vue';
  import {
    save
  } from "@/api/wms/plan";
  import {
    defineComponent,
    reactive,
    ref,
    toRaw
  } from "vue";
  export default defineComponent({
    props: {
      visible: {
        type: Boolean,
      },
    },
    emit: ["close"],
    setup(props, context) {

      const formRef = ref();

      const state = reactive({});

      const formState = reactive({});

      const seqDeleted = ref([{
        value: 'false',
        label: '否',
      }, {
        value: 'true',
        label: '是',
      }]);

      const formRules = {
        skuCode: [{
          required: true,
          message: '请输入物料编号(必填)',
          trigger: 'blur'
        }],
        channelCode: [{
          required: true,
          message: '请输入通道编号(必填)',
          trigger: 'blur'
        }],
        fullQty: [{
          required: true,
          message: '请输入满盘数量(必填)',
          trigger: 'blur',
          type:'number'
        }],
        totalQty: [{
          required: true,
          message: '请输入总分配数量(必填)',
          trigger: 'blur',
          type:'number'
        }],
        currentQty: [{
          required: true,
          message: '请输入当前数量(必填)',
          trigger: 'blur',
          type:'number'
        }],
        mode: [{
          required: true,
          message: '请输入码垛模式(必填)',
          trigger: 'blur',
          type:'number'
        }],
      };

      const saveKey = "save";


      const submit = (e) => {
        formRef.value
        .validate()
        .then(() => {
            message.loading({
              content: '提交中...',
              key: saveKey
            });
            save(toRaw(formState)).then((response) => {
              if (response.success) {
                message.success({
                  content: '保存成功',
                  key: saveKey,
                  duration: 1
                }).then(() => {
                  cancel();
                });
              } else {
                message.error({
                  content: response.msg,
                  key: saveKey,
                  duration: 1
                }).then(() => {
                });
              }
            });
          })
          .catch(error => {
            console.log('error', error);
          });
      };

      const cancel = (e) => {
        formRef.value.resetFields();
        context.emit("close", false);
      };


      return {
        state,
        submit,
        cancel,
        formRef,
        formState,
        formRules,
        seqDeleted,

        labelCol: {
          span: 8
        },
        wrapperCol: {
          span: 12
        },
      };
    },
  });
</script>
