<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 {
    edit
  } from "@/api/wms/channel";
  import {
    defineComponent,
    reactive,
    ref,
    toRaw,
    watch
  } from "vue";
  export default defineComponent({
    props: {
      visible: {
        type: Boolean,
      },
      record: {
        type: Object,
      }
    },
    emit: ["close"],
    setup(props, context) {

      const formRef = ref();

      const formState = reactive({});

      const seqDeleted = ref([{
        value: 'false',
        label: '否',
      }, {
        value: 'true',
        label: '是',
      }]);
      watch(props, (props) => {
        formState.id = props.record.id
        formState.skuCode = props.record.skuCode;
        formState.planCode = props.record.planCode;
        formState.channelCode = props.record.channelCode;
        formState.target = props.record.target;
        formState.fullQty = props.record.fullQty;
        formState.totalQty = props.record.totalQty;
        formState.currentQty = props.record.currentQty;
        formState.mode = props.record.mode;
        formState.channelGroup = props.record.channelGroup;
      })

      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 editKey = "add";

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

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

      return {

        submit,
        cancel,
        formRef,
        formState,
        formRules,
        seqDeleted,

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