<!-- 
 * @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="userId">
                <s-select v-model="sysUserProps.record.userId" :options="teacherData" label="name" value="userId"></s-select>
              </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, userManageTeacherApi } from "@/api";
import { required } from "@/utils/formRules";
import { FormInstance } from "element-plus";
const teacherData = ref<any>([]);
const visibleClass = ref(false); //是否显示表单
// 表单参数
enum FormOptEnum {
  /** 新增 */
  AddTeacher = "绑定班主任",
  /** 编辑 */
  UpdateTeacher = "修改班主任"
}
const sysUserProps = reactive<FormProps.Base<SysUserPersonnel.ClassPage>>({
  opt: FormOptEnum.AddTeacher,
  record: {},
  disabled: false
});
// 表单验证规则
const rules = reactive({
  userId: [required("请选择班主任名称")]
});

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

const getRequestData = async (callback: Function | null = null) => {
  const { data } = await userManageTeacherApi.page();
  teacherData.value = data;
  if (callback) callback();
};

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

/** 关闭表单*/
function onClose() {
  visibleClass.value = false;
}
// 暴露给父组件的方法
defineExpose({
  onOpen
});
</script>

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