<template>
  <div>
    <el-form ref="formRef" :model="form" :rules="rules" label-width="110px">
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="上级部门">
            <el-tag>{{ parentName }}</el-tag>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" placeholder="这里输入名称..." />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门级别" prop="level">
            <el-select v-model="form.level">
              <el-option
                v-for="item in departmentLevelList"
                :key="item.dictionaryId"
                :label="item.name"
                :value="item.dictionaryId"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门负责人" prop="headman">
            <el-input v-model="form.headman" splicing-label />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="form.phone" splicing-label />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序" prop="orderBy">
            <el-input
              v-model.number="form.orderBy"
              placeholder="这里输入排序..."
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="mt-10 tc">
      <el-button type="primary" @click="fnSubmit">保存</el-button>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import useForm from "@/hooks/useForm.js";
import { useRoute, useRouter } from "vue-router";
import { appFnGetDepartmentLevelList } from "@/data_dictionary/data_dictionary.js";
import useDataDictionary from "@/hooks/useDataDictionary.js";
import { debounce } from "throttle-debounce";
import {
  getDepartmentInfo,
  setDepartmentAdd,
  setDepartmentUpdate,
} from "@/request/user_management.js";
import { ElMessage } from "element-plus";
import { MOBILE_PHONE } from "@/assets/js/regular.js";

const route = useRoute();
const router = useRouter();
const { parentId, parentName, departmentId } = route.query;
const { formRef, validate } = useForm();
const form = ref({
  name: "",
  level: "",
  headman: "",
  phone: "",
  orderBy: "",
});
const rules = {
  name: [{ required: true, message: "请输入部门名称", trigger: "blur" }],
  level: [{ required: true, message: "请选择部门级别", trigger: "change" }],
  phone: [
    { required: false, message: "手机号", trigger: "blur" },
    { pattern: MOBILE_PHONE, message: "请输入正确的手机号码", trigger: "blur" },
  ],
  orderBy: [{ required: true, message: "请输入排序", trigger: "blur" }],
};
const { data: departmentLevelList } = useDataDictionary(
  appFnGetDepartmentLevelList
);
const fnGetData = async () => {
  if (!departmentId) return;
  const { department } = await getDepartmentInfo({
    departmentId: departmentId,
  });
  form.value = department;
};
fnGetData();

const fnSubmit = debounce(
  1000,
  async () => {
    await validate();
    const params = {
      ...form.value,
      parentId,
    };
    !departmentId
      ? await setDepartmentAdd(params)
      : await setDepartmentUpdate(params);
    ElMessage.success("操作成功");
    router.back();
  },
  { atBegin: true }
);
</script>

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