<script setup lang="ts">
import { reactive, ref } from "vue";
import ReCol from "@/components/ReCol";
import { usePublicHooks } from "../../hooks";
import { isEmail, isPhone } from "@pureadmin/utils";
import type { FormRules } from "element-plus";
interface FormItemProps {
  id?: string;
  /** 用于判断是`新增`还是`修改` */
  way: string;
  higherOptions: Record<string, unknown>[];
  username: string;
  nickname: string;
  password?: string;
  phone: string | number;
  email: string;
  gender: string | number;
  status: number;
  department_id: string;
}
interface FormProps {
  formInline: FormItemProps;
}
/** 自定义表单规则校验 */
const formRules = reactive<FormRules>({
  username: [{ required: true, message: "用户账号为必填项", trigger: "blur" }],
  nickname: [{ required: true, message: "用户名称为必填项", trigger: "blur" }],
  gender: [{ required: true, message: "用户性别为必选项", trigger: "blur" }],
  status: [{ required: true, message: "用户状态为必选项", trigger: "blur" }],
  department_id: [
    { required: true, message: "用户部门必选项", trigger: "blur" }
  ],
  password: [{ required: true, message: "用户密码为必填项", trigger: "blur" }],
  phone: [
    {
      validator: (rule, value, callback) => {
        if (value === "") {
          callback();
        } else if (!isPhone(value)) {
          callback(new Error("请输入正确的手机号码格式"));
        } else {
          callback();
        }
      },
      trigger: "blur"
      // trigger: "click" // 如果想在点击确定按钮时触发这个校验，trigger 设置成 click 即可
    }
  ],
  email: [
    {
      validator: (rule, value, callback) => {
        if (value === "") {
          callback();
        } else if (!isEmail(value)) {
          callback(new Error("请输入正确的邮箱格式"));
        } else {
          callback();
        }
      },
      trigger: "blur"
    }
  ]
});

const props = withDefaults(defineProps<FormProps>(), {
  formInline: () => ({
    higherOptions: [],
    way: "新增",
    username: "",
    nickname: "",
    password: "",
    phone: "",
    email: "",
    gender: "",
    status: 1,
    department_id: ""
  })
});

const sexOptions = [
  {
    value: 1,
    label: "男"
  },
  {
    value: 0,
    label: "女"
  }
];
const ruleFormRef = ref();
const { switchStyle } = usePublicHooks();
const newFormInline = ref(props.formInline);

function getRef() {
  return ruleFormRef.value;
}

defineExpose({ getRef });
</script>

<template>
  <el-form
    ref="ruleFormRef"
    :model="newFormInline"
    :rules="formRules"
    label-width="82px"
  >
    <el-row :gutter="30">
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="用户账号" prop="username">
          <el-input
            v-model="newFormInline.username"
            clearable
            placeholder="请输入用户账号~"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="用户姓名" prop="nickname">
          <el-input
            v-model="newFormInline.nickname"
            clearable
            placeholder="请输入用户名称~"
          />
        </el-form-item>
      </re-col>

      <re-col v-if="newFormInline.way === '新增'" :value="12" :xs="24" :sm="24">
        <el-form-item label="用户密码" prop="password">
          <el-input
            v-model="newFormInline.password"
            clearable
            placeholder="请输入用户密码~"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="手机号" prop="phone">
          <el-input
            v-model="newFormInline.phone"
            clearable
            placeholder="请输入手机号~"
          />
        </el-form-item>
      </re-col>

      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="邮箱" prop="email">
          <el-input
            v-model="newFormInline.email"
            clearable
            placeholder="请输入邮箱~"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="用户性别" prop="gender">
          <el-select
            v-model="newFormInline.gender"
            placeholder="请选择用户性别~"
            class="w-full"
            clearable
          >
            <el-option
              v-for="(item, index) in sexOptions"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="归属部门" prop="department_id">
          <el-cascader
            v-model="newFormInline.department_id"
            class="w-full"
            :options="newFormInline.higherOptions"
            :props="{
              value: 'id',
              label: 'name',
              emitPath: false,
              checkStrictly: true
            }"
            clearable
            filterable
            placeholder="请选择归属部门~"
          >
            <template #default="{ node, data }">
              <span>{{ data.name }}</span>
              <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
            </template>
          </el-cascader>
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="用户状态" prop="status">
          <el-switch
            v-model="newFormInline.status"
            inline-prompt
            :active-value="1"
            :inactive-value="0"
            active-text="启用"
            inactive-text="停用"
            :style="switchStyle"
          />
        </el-form-item>
      </re-col>
    </el-row>
  </el-form>
</template>
