<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="palletCode" label="托盘号" name="palletCode">
        <a-input v-model:value="formState.palletCode" placeholder="请输入托盘号" />
      </a-form-item>
      <a-form-item ref="palletType" label="托盘类型" name="palletType">
        <a-input v-model:value="formState.palletType" placeholder="请输入托盘类型" />
      </a-form-item>
      <a-form-item ref="sizeType" label="托盘大小" name="sizeType">
        <a-input v-model:value="formState.sizeType" placeholder="请输入托盘大小" />
      </a-form-item>
      <a-form-item ref="fullFlag" label="满盘标志" name="fullFlag">
        <a-input v-model:value="formState.fullFlag" placeholder="请输入满盘标志" />
      </a-form-item>
      <a-form-item ref="locCode" label="托盘位置" name="locCode">
        <a-input v-model:value="formState.locCode" placeholder="请输入托盘位置" />
      </a-form-item>
      <a-form-item ref="status" label="托盘状态" name="status">
        <a-input v-model:value="formState.status" placeholder="请输入托盘状态" />
      </a-form-item>
      <a-form-item ref="jobFlag" label="任务标志" name="jobFlag">
        <a-input v-model:value="formState.jobFlag" placeholder="请输入任务标志" />
      </a-form-item>
      <a-form-item ref="note" label="备注" name="note">
        <a-input v-model:value="formState.note" placeholder="请输入备注" />
      </a-form-item>
      <a-form-item label="备注" name="remark">
        <a-textarea v-model:value="formState.remark" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
    import {
    message
  } from '@hwork/ant-design-vue';
  import {
    edit,
  } from "@/api/wms/pallet";
  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 state = reactive({});

      const formState = reactive({});

      const formRules = {
        palletCode: [{
          required: true,
          message: '请输入托盘号',
          trigger: 'blur'
        }],
        locCode: [{
          required: true,
          message: '请输入托盘位置',
          trigger: 'blur'
        }],
        status: [{
          required: true,
          message: '请输入托盘状态',
          trigger: 'blur',
        }],
      }

      watch(props, (props) => {
        console.log("state.record", props.record)
        formState.id = props.record.id
        formState.palletCode = props.record.palletCode
        formState.locCode = props.record.locCode;
        formState.status = props.record.status;
        // formState.regionName = props.record.regionName;
        // formState.status = props.record.status;
        // formState.pbgnDate = props.record.pbgnDate;
        // formState.bgnDate = props.record.bgnDate;
        // formState.pendDate = props.record.pendDate;
        // formState.endDate = props.record.endDate;
        formState.note = props.record.note;
        formState.remark = props.record.remark;
      })

      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(() => {
                  // cancel();
                });
              }
            });
          })
          .catch(error => {
            console.log('error', error);
          });
      };

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

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

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