<template>
  <a-modal
    :open="visible"
    title="新增搬运类型"
    cancelText="取消"
    okText="提交"
    @ok="submit"
    @cancel="cancel"
  >
    <a-form
      ref="formRef"
      :model="formState"
      :rules="formRules"
      layout="vertical"
    >
      <a-row :gutter="[16, 0]">
        <a-col :span="12">
          <a-form-item ref="deliver" label="搬运类型" name="deliver">
            <a-input
              v-model:value="formState.deliver"
              placeholder="请输入搬运类型描述"
            />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item
            ref="deliverDesc"
            label="搬运类型描述"
            name="deliverDesc"
          >
            <a-input
              v-model:value="formState.deliverDesc"
              placeholder="请输入搬运类型描述"
            />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item
            ref="sourceStactics"
            label="源策略"
            name="sourceStactics"
          >
            <a-input
              v-model:value="formState.sourceStactics"
              placeholder="请输入源策略"
            />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item
            ref="targetStactics"
            label="目标策略"
            name="targetStactics"
          >
            <a-input
              v-model:value="formState.targetStactics"
              placeholder="请输入目标策略"
            />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item ref="sourceLoc" label="源位置" name="sourceLoc">
            <a-input
              v-model:value="formState.sourceLoc"
              placeholder="请输入源位置"
            />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item ref="targetLoc" label="目标位置" name="targetLoc">
            <a-input
              v-model:value="formState.targetLoc"
              placeholder="请输入目标位置"
            />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="方向标志">
            <a-select
              v-model:value="formState.directFlag"
              placeholder="请选择方向标志"
              :options="directFlag"
            >
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="优先级">
            <a-select
              v-model:value="formState.priority"
              placeholder="请选择优先级"
              :options="priority"
            >
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-modal>
</template>
<script>
import { message } from "@hwork/ant-design-vue";
import { save } from "@/api/wms/deliverType";
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({});

    /* const deliver = ref([{
      value: '人工',
      label: '人工',
    }, {
      value: '机械手',
      label: '机械手',
    }]); */

    const directFlag = ref([
      {
        value: "In",
        label: "入库",
      },
      {
        value: "Out",
        label: "出库",
      },
      {
        value: "Move",
        label: "移库",
      },
    ]);

    const priority = ref([
      {
        value: "1",
        label: "正常",
      },
      {
        value: "0",
        label: "低",
      },
      {
        value: "2",
        label: "高",
      },
    ]);

    const formRules = {
      deliver: [
        {
          required: true,
          message: "请输入搬运类型(必填)",
          trigger: "blur",
        },
      ],
      priority: [
        {
          required: true,
          message: "请选择优先级(必填)",
          trigger: "change",
        },
      ],
      sourceLoc: [
        {
          required: true,
          message: "请输入原位置(必填)",
          trigger: "blur",
        },
      ],
      targetLoc: [
        {
          required: true,
          message: "请输入目标位置(必选)",
          trigger: "blur",
        },
      ],
      directFlag: [
        {
          required: true,
          message: "请选择方向标志(必选)",
          trigger: "change",
        },
      ],
    };

    const saveKey = "save";

    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,
      formRef,
      formState,
      directFlag,
      formRules,
      priority,

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