<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="seqCode" label="值编号" name="seqCode">
            <a-input
              v-model:value="formState.seqCode"
              placeholder="请输入值编号"
            />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item ref="seqName" label="名称" name="seqName">
            <a-input
              v-model:value="formState.seqName"
              placeholder="请输入名称"
            />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item ref="minValue" label="起始值" name="minValue">
            <a-input
              v-model:value="formState.minValue"
              placeholder="请输入起始值"
            />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item ref="incValue" label="自增值" name="incValue">
            <a-input
              v-model:value="formState.incValue"
              placeholder="请输入自增值"
            />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item ref="maxValue" label="最大值" name="maxValue">
            <a-input
              v-model:value="formState.maxValue"
              placeholder="请输入最大值"
            />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item ref="currValue" label="当前值" name="currValue">
            <a-input
              v-model:value="formState.currValue"
              placeholder="请输入当前值"
            />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="备注" name="remark">
            <a-textarea v-model:value="formState.remark" />
          </a-form-item>
        </a-col>
      </a-row>

      <!-- <a-form-item ref="deleted" label="删除" name="deleted">
        <a-select v-model:value="formState.deleted" :options="seqDeleted" placeholder="请选择是或否" />
      </a-form-item>
      <a-form-item ref="createBy" label="录入人" name="createBy">
        <a-input v-model:value="formState.createBy" placeholder="请输入录入人" />
      </a-form-item>
      <a-form-item ref="createTime" label="录入时间" name="createTime">
        <a-input v-model:value="formState.createTime" placeholder="请输入录入时间" />
      </a-form-item>
      <a-form-item ref="updateBy" label="修改人" name="updateBy">
        <a-input v-model:value="formState.updateBy" placeholder="请输入修改人" />
      </a-form-item>
      <a-form-item ref="updateTime" label="修改时间" name="updateTime">
        <a-input v-model:value="formState.updateTime" placeholder="请输入修改时间" />
      </a-form-item> -->
    </a-form>
  </a-modal>
</template>
<script>
import { message } from "@hwork/ant-design-vue";
import { save } from "@/api/wms/seqNum";
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 seqDeleted = ref([
      {
        value: "false",
        label: "否",
      },
      {
        value: "true",
        label: "是",
      },
    ]);

    const formRules = {
      seqCode: [
        {
          required: true,
          message: "请输入值编号(必填)",
          trigger: "blur",
        },
      ],
      sqeName: [
        {
          required: true,
          message: "请输入名称(必填)",
          trigger: "blur",
        },
      ],
      minValue: [
        {
          required: true,
          message: "请输入起始值(必填)",
          trigger: "blur",
        },
      ],
      incValue: [
        {
          required: true,
          message: "请输入自增值(必填)",
          trigger: "blur",
        },
      ],
      maxValue: [
        {
          required: true,
          message: "请输入最大值(必填)",
          trigger: "blur",
        },
      ],
      currValue: [
        {
          required: true,
          message: "请输入当前值(必填)",
          trigger: "blur",
        },
      ],
    };

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

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

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

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