<template>
  <a-modal
    :open="visible"
    title="编辑搬运类型"
    cancelText="取消"
    okText="提交"
    @ok="submit"
    @cancel="cancel"
  >
    <a-form
      ref="formRef"
      :model="formState"
      :rules="formRules"
      layout="vertical"
      :popupScroll="popupScroll"
    >
      <!-- <a-form-item ref="deliverDesc" label="搬运类型描述" name="deliverDesc">
        <a-input v-model:value="formState.deliverDesc" placeholder="请输入搬运类型描述" />
      </a-form-item> -->
      <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 label="产生任务标志">
            <a-select
              v-model:value="formState.jobFlag"
              placeholder="请选择产生任务标志"
              :options="jobFlag"
            >
            </a-select>
          </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 label="源策略">
            <a-select
              v-model:value="formState.sourceStactics"
              :options="deliverSourceStactics"
              mode="tags"
              :size="size"
              placeholder="请选择源策略"
              autoClearSearchValue="ture"
            >
            </a-select>
          </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
            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="pltFlag" label="删除托盘标记" name="pltFlag">
            <a-input
              v-model:value="formState.pltFlag"
              placeholder="请输入删除托盘标记"
            />
          </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-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-row>

      <!-- <a-form-item ref="sourceStactics" label="源策略" name="sourceStactics">
        <a-radio-group v-model:value="formState.sourceStactics" placeholder="请输入源策略" />
      </a-form-item> -->
    </a-form>
  </a-modal>
</template>
<script>
import { message } from "@hwork/ant-design-vue";
import { edit } from "@/api/wms/deliverType";
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({});

    watch(props, (props) => {
      formState.id = props.record.id;
      formState.deliver = props.record.deliver;
      formState.priority = props.record.priority;
      formState.directFlag = props.record.directFlag;
      formState.deliverDesc = props.record.deliverDesc;
      formState.jobFlag = props.record.jobFlag;
      formState.sourceLoc = props.record.sourceLoc;
      formState.sourceStactics = props.record.sourceStactics;
      formState.targetLoc = props.record.targetLoc;
      formState.targetStactics = props.record.targetStactics;
      formState.pltFlag = props.record.pltFlag;
      formState.sourceLoc = props.record.sourceLoc;
      // formState.beginDate = props.record.beginDate;
      // formState.finishDate = props.record.finishDate;
    });

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

    const deliverSourceStactics = ref([
      {
        value: "散盘优先",
        label: "散盘优先",
      },
      {
        value: "先进先出",
        label: "先进先出",
      },
      {
        value: "3",
        label: "先进后出",
      },
      {
        value: "4",
        label: "后进后出",
      },
      {
        value: "5",
        label: "后进先出",
      },
      {
        value: "6",
        label: "整盘优先",
      },
      {
        value: "7",
        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 jobFlag = ref([
      {
        value: "1",
        label: "是",
      },
      {
        value: "0",
        label: "否",
      },
    ]);

    const formRules = {
      deliver: [
        {
          required: true,
          message: "请输入搬运类型(必填)",
          trigger: "blur",
        },
      ],
      priority: [
        {
          required: true,
          message: "请选择优先级(必填)",
          trigger: "blur",
        },
      ],
      sourceStactics: [
        {
          required: true,
          message: "请输入源策略(必填)",
          trigger: "blur",
        },
      ],
      targetStactics: [
        {
          required: true,
          message: "请输入目标策略(必选)",
          trigger: "blur",
        },
      ],
      directFlag: [
        {
          required: true,
          message: "请选择方向标志(必选)",
          trigger: "blur",
        },
      ],
    };

    const editKey = "add";

    const submit = (e) => {
      console.log("formRef.value", formRef.value);
      console.log("formState", formState);
      console.log("formState sourceStactics", formState.sourceStactics);
      var a = JSON.stringify(formState.sourceStactics);
      formState.sourceStactics = JSON.stringify(formState.sourceStactics);
      console.log("formState sourceStactics", a);
      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 popupScroll = () => {
      console.log("popupScroll");
    };
    const cancel = (e) => {
      formRef.value.resetFields();
      context.emit("close", false);
    };
    return {
      submit,
      cancel,
      formRef,
      formState,

      directFlag,
      formRules,
      priority,
      jobFlag,
      deliverSourceStactics,
      popupScroll,

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