<!-- 
 * @Description: 表单
 * @Author: syy 
 * @Date: 2023-12-15 15:45:59
-->
<template>
  <div>
    <form-container v-model="visibleChamber" :title="`${SysDormitoryProps.opt}寝室`" form-size="400px" @close="onClose">
      <el-form
        ref="SysDormitoryFormRef"
        :rules="rules"
        :disabled="SysDormitoryProps.disabled"
        :model="SysDormitoryProps.record"
        :hide-required-asterisk="SysDormitoryProps.disabled"
        label-width="auto"
        label-suffix=" :"
      >
        <div>
          <el-row :gutter="16">
            <el-col :span="22">
              <s-form-item label="寝室名称" prop="name">
                <s-input v-model="SysDormitoryProps.record.name"></s-input>
              </s-form-item>
            </el-col>
            <!-- <el-col :span="22">
              <s-form-item label="性别" prop="gender">
                <s-radio-group v-model="SysDormitoryProps.record.gender" :options="genderOptions" />
              </s-form-item>
            </el-col> -->
          </el-row>
        </div>
      </el-form>
      <template #footer>
        <el-button @click="onClose"> 取消 </el-button>
        <el-button v-show="!SysDormitoryProps.disabled" type="primary" @click="handleSubmit"> 确定 </el-button>
      </template>
    </form-container>
  </div>
</template>

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

const visibleChamber = ref(false); //是否显示表单
const genderOptions = [
  {
    label: "男",
    value: true
  },
  {
    label: "女",
    value: false
  }
];
// 表单参数
const SysDormitoryProps = reactive<FormProps.Base<SysDormitory.ChamberInfo>>({
  opt: FormOptEnum.ADD,
  record: {},
  disabled: false
});
// 表单验证规则
const rules = reactive({
  name: [required("请输入寝室名称")]
});

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

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

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

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

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