<template>
  <el-form
    :label-position="'right'"
    label-width="120px"
    :model="systemForm"
    :rules="formRules"
    ref="systemRef"
    style="max-width: 66%">
    <el-form-item label="所属组织" prop="organization_id">
      <el-tree-select
        v-model="systemForm.organization_id"
        :data="orgData"
        :props="treeProps"
        :render-after-expand="false"
        :disabled="formType"
        style="width: 100%" />
    </el-form-item>
    <el-form-item label="姓名" prop="name">
      <el-input v-model="systemForm.name" />
    </el-form-item>
    <el-form-item label="头像" prop="logo">
      <bg-upload-image
        v-model="systemForm.logo"
        :showTips="true"
        :limit="1"
        :fileSize="500"
        :fileSizeUnit="'KB'"
        listType="picture-card"
        :accept="['.jpg', '.jpeg', '.png']"
        customTips="请选择图片上传：大小120 * 120像素支持jpg、png等格式，图片需小于500KB"></bg-upload-image>
    </el-form-item>
    <el-form-item label="角色" prop="select_role">
      <el-select v-model="systemForm.select_role" multiple placeholder="请选择角色" style="width: 100%">
        <el-option v-for="item in roleList" :key="item.role_id" :label="item.role_name" :value="item.role_id" />
      </el-select>
    </el-form-item>
    <el-form-item label="账号" prop="system_account">
      <el-input v-model="systemForm.system_account" />
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input v-model="systemForm.phone" />
    </el-form-item>
    <el-form-item v-if="!formType" label="密码" prop="password">
      <el-input :type="password_eye ? 'text' : 'password'" v-model="systemForm.password">
        <template #suffix>
          <bg-icon @click="password_eye = !password_eye" class="icon_eye" icon="#bg-ic-eye"></bg-icon>
        </template>
      </el-input>
    </el-form-item>
    <el-form-item v-if="!formType" label="确认密码" prop="confirm_password">
      <el-input :type="confirm_eye ? 'text' : 'password'" v-model="systemForm.confirm_password">
        <template #suffix>
          <bg-icon @click="confirm_eye = !confirm_eye" class="icon_eye" icon="#bg-ic-eye"></bg-icon>
        </template>
      </el-input>
    </el-form-item>
    <el-form-item label="是否启用" prop="state">
      <bg-switch :labels="['否', '是']" :values="[0, 1]" v-model="systemForm.state"></bg-switch>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { reactive, ref, onBeforeMount, onMounted, watch } from "vue";
import axios from "@/request/http.js";
import { ElMessage } from "element-plus";
import { validatePhone } from "@/services/rules.js";
const props = defineProps({
  formType: {
    type: Boolean,
    default: false, //false 新增 true 编辑
  },
});
const systemForm = reactive({
  organization_id: "",
  name: "",
  logo: [],
  select_role: [],
  system_account: "",
  phone: "",
  password: "",
  confirm_password: "",
  state: 1,
});
const password_eye = ref(false);
const confirm_eye = ref(false);

const validatePass = (rule, value, callback) => {
  if (value !== systemForm.password) {
    callback(new Error("密码输入不一致"));
  } else {
    callback();
  }
};

const formRules = reactive({
  organization_id: [{ required: true, message: "请选择组织", trigger: "change" }],
  name: [
    { required: true, message: "请输入姓名", trigger: "blur" },
    { max: 50, message: "姓名最大长度为50个字符", trigger: "blur" },
  ],
  select_role: [{ required: true, type: "array", message: "请选择角色", trigger: "change" }],
  system_account: [
    { required: true, message: "请输入账号", trigger: "blur" },
    { min: 4, message: "帐号长度不得低于4个字符", trigger: "blur" },
    { max: 20, message: "帐号最大长度为20个字符", trigger: "blur" },
  ],
  phone: [
    { required: true, message: "请输入账号", trigger: "blur" },
    { validator: validatePhone, trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 8, message: "密码长度不得低于8位", trigger: "blur" },
  ],
  confirm_password: [
    { required: true, message: "请确认密码", trigger: "blur" },
    { validator: validatePass, trigger: "blur" },
  ],
});

const systemRef = ref(null);
const roleList = ref([]);

watch(
  () => systemForm.organization_id,
  (val) => {
    getRoleList(val);
  }
);

const setForm = (data) => {
  Object.assign(systemForm, data);
  console.log("systemForm", systemForm);
};

const orgData = ref([]);
const treeProps = {
  label: "name",
  children: "Child",
  value: "organization_id",
};
const getOrgTree = () => {
  axios.get(`/v1/api/org/tree`).then((res) => {
    if (res.data.code == 200) {
      const orgDataTemp = res.data.data || [];
      orgData.value = orgDataTemp;
    } else {
      ElMessage.error(res.data.data);
    }
  });
};
const getRoleList = (oid) => {
  axios.get(`/v1/api/user/select/role`, { params: { oid } }).then((res) => {
    if (res.data.code == 200) {
      roleList.value = res.data.data;
    } else {
      ElMessage.error(res.data.data);
    }
  });
};

const validateForm = () => {
  return new Promise((resolve, reject) => {
    systemRef.value.validate((res) => resolve(res));
  });
};

onBeforeMount(() => {
  getOrgTree();
});

onMounted(() => {});

defineExpose({ systemForm, setForm, validateForm });
</script>
