<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="totalQty" label="总分配数量" name="totalQty">
        <a-input-number v-model:value="formState.totalQty" placeholder="请输计划数量" style="width: 100%;"/>
      </a-form-item>
      <a-form-item ref="pingkuQty" label="平库数量" name="pingkuQty">
        <a-input-number v-model:value="formState.pingkuQty" placeholder="请输平库数量" style="width: 100%;"/>
      </a-form-item>
      <a-form-item ref="likuQty" label="立库数量" name="likuQty">
        <a-input-number v-model:value="formState.likuQty" placeholder="请输立库数量" style="width: 100%;"/>
      </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="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/plan";
  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.totalQty = props.record.totalQty;
        formState.target = props.record.target;
        formState.pingkuQty = props.record.pingkuQty;
        formState.likuQty = props.record.likuQty;
        formState.planNum = props.record.planNum;
        formState.channelCode = props.record.channelCode;
        formState.channelGroup = props.record.channelGroup;
      })

      const formRules = {
        skuCode: [{
          required: true,
          message: '请输入物料编号(必填)',
          trigger: 'blur'
        }],
        channelCode: [{
          required: true,
          message: '请输入通道编号(必填)',
          trigger: 'blur'
        }],
        totalQty: [{
          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>
