<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="trackCode" label="通道号" name="trackCode">
        <a-input v-model:value="formState.trackCode" placeholder="请输入通道号" />
      </a-form-item>
      <a-form-item ref="sourceLoc" label="WMS源地址" name="sourceLoc">
        <a-input v-model:value="formState.sourceLoc" placeholder="请输入WMS源地址" />
      </a-form-item>
      <a-form-item ref="targetLoc" label="WMS目标地址" name="targetLoc">
        <a-input v-model:value="formState.targetLoc" placeholder="请输入WMS目标地址" />
      </a-form-item>
      <a-form-item ref="equipCode" label="关键设备" name="equipCode">
        <a-input v-model:value="formState.equipCode" placeholder="请输入关键设备" />
      </a-form-item>
      <a-form-item label="WCS状态" name="equipStatus">
        <a-select v-model:value="formState.equipStatus" :options="equipStatus" placeholder="请选择启用状态">
        </a-select>
      </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 {
    save
  } from "@/api/wms/track";
  import {
    defineComponent,
    reactive,
    ref,
    toRaw
  } from "vue";
  export default defineComponent({
    props: {
      visible: {
        type: Boolean,
      },
    },
    emit: ["close"],
    setup(props, context) {

      const formRef = ref();

      const state = reactive({ });

      const formState = reactive({
        equipStatus:"true",
        status:"true"
      });

      const formRules = {
        trackCode: [{
          required: true,
          message: '请输入通道号(必填)',
          trigger: 'blur'
        }],
        sourceLoc: [{
          required: true,
          message: '请输入WMS源位置(必填)',
          trigger: 'blur'
        }],
        targetLoc: [{
          required: true,
          message: '请输入WMS目标位置(必填)',
          trigger: 'blur'
        }],
        equipCode: [{
          required: true,
          message: '请输入关键设备(必填)',
          trigger: 'blur'
                }],
        equipStatus: [{
          required: true,
          message: '请输入WCS状态(必填)',
          trigger: 'blur'
        }],
      };

      const saveKey = "save";

      const equipStatus = ref([{
        value: 'true',
        label: '正常',
      }]);

      const submit = (e) => {
        formRef.value
        .validate()
        .then(() => {
            message.loading({
              content: '提交中...',
              key: saveKey
            });
            save(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,
        equipStatus,
        formRef,
        formState,
        formRules,

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