<!-- 
 * @Description: 表单
 * @Author: syy 
 * @Date: 2023-12-18 15:45:59
-->
<template>
  <div>
    <form-container v-model="visibleClass" :title="`${sysUserProps.opt}服装库`" form-size="400px" @close="onClose">
      <el-form
        ref="sysUserFormRef"
        :rules="rules"
        :disabled="sysUserProps.disabled"
        :model="sysUserProps.record"
        :hide-required-asterisk="sysUserProps.disabled"
        label-width="auto"
        label-suffix=" :"
      >
        <div>
          <el-row :gutter="16">
            <el-col :span="22">
              <s-form-item label="服装库名称" prop="clothSetName">
                <s-input v-model="sysUserProps.record.clothSetName"></s-input>
              </s-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>
      <template #footer>
        <el-button @click="onClose"> 取消 </el-button>
        <el-button v-show="!sysUserProps.disabled" type="primary" @click="handleSubmit"> 确定 </el-button>
      </template>
    </form-container>
  </div>
</template>

<script setup lang="ts" name="SysUserClothDk">
import { ref } from "vue";
import { userManageClothApi, SysUserCloth } from "@/api";
import { FormOptEnum } from "@/enums";
import { required } from "@/utils/formRules";
import { FormInstance } from "element-plus";

const visibleClass = ref(false); //是否显示表单
// 表单参数
const sysUserProps = reactive<FormProps.Base<SysUserCloth.Page>>({
  opt: FormOptEnum.ADD,
  record: {},
  disabled: false
});
// 表单验证规则
const rules = reactive({
  clothSetName: [required("请输入服装库名称")]
});

/**
 * 打开表单
 * @param props 表单参数
 */
function onOpen(props: FormProps.Base<SysUserCloth.Page>) {
  Object.assign(sysUserProps, props); //合并参数
  visibleClass.value = true; //显示表单
  sysUserProps.record = props.record;
}

// 提交数据（新增/编辑）
const sysUserFormRef = ref<FormInstance>();
/** 提交表单 */
async function handleSubmit() {
  sysUserFormRef.value?.validate(async valid => {
    if (!valid) return; //表单验证失败
    //提交表单
    if (sysUserProps.record.clothSetId) {
      await userManageClothApi
        .update(sysUserProps.record)
        .then(() => {
          sysUserProps.successful!(); //调用父组件的successful方法
        })
        .finally(() => {
          onClose();
        });
    } else {
      await userManageClothApi
        .addClothDataBaseA(sysUserProps.record)
        .then(() => {
          sysUserProps.successful!(); //调用父组件的successful方法
        })
        .finally(() => {
          onClose();
        });
    }
  });
}

/** 关闭表单*/
function onClose() {
  visibleClass.value = false;
}

// 暴露给父组件的方法
defineExpose({
  onOpen
});
</script>

<style lang="scss" scoped></style>
