<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="categoryCode" label="种类编号" name="categoryCode">
            <a-input
              v-model:value="formState.categoryCode"
              placeholder="请输入种类编号"
            />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item ref="categoryName" label="种类名称" name="categoryName">
            <a-input
              v-model:value="formState.categoryName"
              placeholder="请输入种类名称"
            />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item ref="categoryDesc" label="种类描述" name="categoryDesc">
            <a-input
              v-model:value="formState.categoryDesc"
              placeholder="请输入种类描述"
            />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item ref="treeLevel" label="树层级" name="treeLevel">
            <a-input
              v-model:value="formState.treeLevel"
              placeholder="请输入树层级"
            />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item ref="treeCode" label="树编号" name="treeCode">
            <a-input
              v-model:value="formState.treeCode"
              placeholder="请输入树编号"
            />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item ref="isActive" label="是否有效" name="isActive">
            <a-select
              v-model:value="formState.isActive"
              :options="skuCategoryIsActive"
              :disabled="false"
              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>
  </a-modal>
</template>
<script>
import { message } from "@hwork/ant-design-vue";
import { save } from "@/api/wms/skuCategory";

import { defineComponent, reactive, ref, toRaw } from "vue";
export default defineComponent({
  props: {
    visible: {
      type: Boolean,
    },
  },
  emit: ["close"],
  setup(props, context) {
    const formRef = ref();

    const formState = reactive({
      isActive: "1",
    });

    const skuCategoryIsActive = ref([
      {
        value: "1",
        label: "是",
      },
    ]);

    const formRules = {
      categoryCode: [
        {
          required: true,
          message: "请输入种类编号",
          trigger: "blur",
        },
      ],
      categoryName: [
        {
          required: true,
          message: "请输入种类名称",
          trigger: "blur",
        },
      ],
      categoryDesc: [
        {
          required: true,
          message: "请输入种类描述",
          trigger: "blur",
        },
      ],
      treeLevel: [
        {
          required: true,
          message: "请输入树层级",
          trigger: "blur",
        },
      ],
      treeCode: [
        {
          required: true,
          message: "请输入树编号",
          trigger: "blur",
        },
      ],
      fullTreeCode: [
        {
          required: true,
          message: "请输入全树编号",
          trigger: "blur",
        },
      ],
      isActive: [
        {
          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
                .success({
                  content: "保存失败",
                  key: saveKey,
                  duration: 1,
                })
                .then(() => {
                  cancel();
                });
            }
          });
        })
        .catch((error) => {
          console.log("error", error);
        });
    };

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

    return {
      submit,
      cancel,
      formRef,
      formState,
      formRules,
      skuCategoryIsActive,

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