<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="请输入托盘号" :disabled="true" />
      </a-form-item>
      <a-form-item ref="locCode" label="托盘位置" name="sourceLoc">
        <a-input v-model:value="formState.sourceLoc" placeholder="请输入托盘位置" :disabled="true" />
      </a-form-item>
      <a-form-item label="目标站台" name="targetLoc">
        <a-select v-model:value="formState.targetLoc" placeholder="请选择任务标志" :options="checkOutStations">
        </a-select>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
    import {
    message
  } from '@hwork/ant-design-vue';
  import {
    palletCheck
  } from "@/api/wms/pallet";
  import {
    list
  } from "@/api/module/dictData";
  import {
    defineComponent,
    reactive,
    ref,
    toRaw,
    watch,
  } from "vue";
  export default defineComponent({
    props: {
      visible: {
        type: Boolean,
      },
      palletInfo: {
        type: Object,
      },
    },
    emit: ["close"],
    setup(props, context) {

      const formRef = ref();

      const state = reactive({});

      const formState = reactive({});

      //抽检出库的目标站台集合
      const checkOutStations = ref();
      //获取入库单业务类型
      list({
        code: "checkOutStations"
      }).then((response) => {
        checkOutStations.value = response.data;
      });

      const formRules = {
        targetLoc: [{
          required: true,
          message: '目标地址不能为空',
          trigger: 'blur'
        }]
      }

      watch(props, (props) => {
        console.log("state.record", props.palletInfo)
        formState.palletCode = props.palletInfo.palletCode
        formState.sourceLoc = props.palletInfo.locCode;
        formState.targetLoc = "";
        state.palletInfo = props.palletInfo;
      })
      const saveKey = "palletCheck";

      const submit = (e) => {
        formRef.value
        .validate()
        .then(() => {
            message.loading({
              content: '提交中...',
              key: saveKey
            });
            palletCheck(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(() => {
                  // cancel();
                });
              }
            });
          })
          .catch(error => {
            console.log('error', error);
          });
      };



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

      return {
        state,
        submit,
        cancel,
        formRef,
        formState,
        checkOutStations,
        formRules,
        labelCol: {
          span: 8
        },
        wrapperCol: {
          span: 12
        },
      };
    },
  });
</script>
