<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"
              laceholder="请选择是否有效"
            />
          </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 { edit } from "@/api/wms/skuCategory";
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({});
    const skuCategoryIsActive = ref([
      {
        value: "1",
        label: "是",
      },
      {
        value: "0",
        label: "否",
      },
    ]);

    watch(props, (props) => {
      formState.id = props.record.id;
      formState.categoryName = props.record.categoryName;
      formState.categoryDesc = props.record.categoryDesc;
      formState.categoryCode = props.record.categoryCode;
      formState.treeLevel = props.record.treeLevel;
      formState.treeCode = props.record.treeCode;
      formState.fullTreeCode = props.record.fullTreeCode;
      formState.isActive = props.record.isActive;
      formState.remark = props.record.remark;
    });

    const formRules = {};

    const editKey = "add";

    const submit = (e) => {
      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
                .success({
                  content: "保存失败",
                  key: editKey,
                  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>
