<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="snCode" label="物料条码" name="snCode">
        <a-input v-model:value="formState.snCode" 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="skuCode" label="skuCode" name="skuCode">
        <a-input-number v-model:value="formState.skuCode" placeholder="请输物料编号 "/>
      </a-form-item>
      <a-form-item ref="channelCode" label="通道编号" name="channelCode">
        <a-input-number v-model:value="formState.channelCode" placeholder="请输入通道编号" />
      </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="mode" label="码垛模式" name="mode">
        <a-input-number v-model:value="formState.mode" placeholder="请输码垛模式" style="width: 100%;"/>
      </a-form-item>
      <a-form-item ref="boxType" label="跺型" name="boxType">
        <a-input-number v-model:value="formState.boxType" placeholder="请输跺型" style="width: 100%;"/>
      </a-form-item>
      <a-form-item ref="assigned" label="分配情况" name="assigned">
        <a-input-number v-model:value="formState.assigned" 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/snNoPlan";
  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.snCode = props.record.snCode;
        formState.target = props.record.target;
        formState.mode = props.record.mode;
        formState.boxType = props.record.boxType;
        formState.assigned = props.record.assigned;
      })

      const formRules = {

      };

      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>
