<!-- 
 * @Description: 表单
 * @Author: syy 
 * @Date: 2023-12-15 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="majorId">
                <s-select v-model="sysUserProps.record.majorId" :options="majorIdData" label="name" value="id"></s-select>
              </s-form-item>
              <s-form-item label="班级名称" prop="personSetName">
                <s-input v-model="sysUserProps.record.personSetName"></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="SysUserPerformClass">
import { ref } from "vue";
import { SysUserPersonnel, userManageClassManageApi, userManageMajorApi } from "@/api";
import { FormOptEnum } from "@/enums";
import { required } from "@/utils/formRules";
import { FormInstance } from "element-plus";

const visibleClass = ref(false); //是否显示表单
const majorIdData = ref<any>([]); //专业数据
// 表单参数
const sysUserProps = reactive<FormProps.Base<SysUserPersonnel.ClassPage>>({
  opt: FormOptEnum.ADD,
  record: {},
  disabled: false
});
// 表单验证规则
const rules = reactive({
  majorId: [required("请选择专业")],
  personSetName: [required("请输入班级名称")]
});

/**
 * 打开表单
 * @param props 表单参数
 */
function onOpen(props: FormProps.Base<SysUserPersonnel.ClassPage>) {
  Object.assign(sysUserProps, props); //合并参数
  visibleClass.value = true; //显示表单
  getRequestData(() => {
    sysUserProps.record = props.record;
  }); //获取专业数据
}

/* 获取专业数据 */
const getRequestData = async (callback: Function | null = null) => {
  const { data } = await userManageMajorApi.list({});
  majorIdData.value = data;
  if (callback) callback;
};
// 提交数据（新增/编辑）
const sysUserFormRef = ref<FormInstance>();
/** 提交表单 */
async function handleSubmit() {
  sysUserFormRef.value?.validate(async valid => {
    if (!valid) return; //表单验证失败
    sysUserProps.record.name = sysUserProps.record.personSetName;
    //提交表单
    if (sysUserProps.record.personSetId) {
      sysUserProps.record.id = sysUserProps.record.personSetId;
      await userManageClassManageApi
        .update(sysUserProps.record)
        .then(() => {
          sysUserProps.successful!(); //调用父组件的successful方法
        })
        .finally(() => {
          onClose();
        });
    } else {
      await userManageClassManageApi
        .add(sysUserProps.record)
        .then(() => {
          sysUserProps.successful!(); //调用父组件的successful方法
        })
        .finally(() => {
          onClose();
        });
    }
  });
}

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

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

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