<template>
  <el-form
    :label-position="'right'"
    label-width="120px"
    :model="platformAccountForm"
    :rules="platformAccountFormRules"
    ref="platformAccountRef"
    style="max-width: 66%">
    <el-form-item label="头像" prop="logo">
      <bg-upload-image
        v-model="platformAccountForm.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="system_account">
      <el-input v-model="platformAccountForm.system_account" />
    </el-form-item>
    <el-form-item label="手机号" prop="contact_phone">
      <el-input v-model="platformAccountForm.contact_phone" />
    </el-form-item>
    <el-form-item v-if="!formType" label="密码" prop="password">
      <el-input type="password" v-model="platformAccountForm.password" />
    </el-form-item>
    <el-form-item v-if="!formType" label="确认密码" prop="confirm_password">
      <el-input type="password" v-model="platformAccountForm.confirm_password" />
    </el-form-item>
    <el-form-item label="角色授权" prop="select_role">
      <el-select v-model="platformAccountForm.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 v-if="formType" label="姓名" prop="contact_name">
      <el-input v-model="platformAccountForm.contact_name" />
    </el-form-item>
    <el-form-item v-if="formType" label="邮箱" prop="contact_email">
      <el-input v-model="platformAccountForm.contact_email" />
    </el-form-item>
    <el-form-item v-if="formType" label="备注" prop="remark">
      <el-input type="textarea" :rows="3" v-model="platformAccountForm.remark" />
    </el-form-item>
    <el-form-item label="是否启用" prop="state">
      <bg-switch :labels="['否', '是']" :values="[0, 1]" v-model="platformAccountForm.state"></bg-switch>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { reactive, ref, onMounted, onBeforeMount } from "vue";
import axios from "@/request/http.js";
const props = defineProps({
  formType: {
    type: Boolean,
    default: false, //false 新增 true 编辑
  },
  id: {
    type: String,
    default: "",
  },
});
const platformAccountForm = reactive({
  logo: [],
  system_account: "",
  contact_phone: "",
  password: "",
  confirm_password: "",
  select_role: "",
  state: 1,

  contact_name: "",
  contact_email: "",
  remark: "",
});

const validatePass = (rule, value, callback) => {
  if (value !== platformAccountForm.password) {
    callback(new Error("密码输入不一致"));
  } else {
    callback();
  }
};
const checkPhone = (rule, value, callback) => {
  var phone_ruler = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/;
  setTimeout(() => {
    if (!phone_ruler.test(value) && value.length !== 0) {
      callback(new Error("请输入正确的手机号码"));
    } else {
      callback();
    }
  });
};

const validateSystemAccount = (rule, value, callback) => {
  let reg = /^[a-zA-Z0-9]+$/;
  if (!reg.test(value)) {
    callback(new Error("只能输入字母和数字"));
  } else {
    let params = null;
    if (props.id) {
      params = { id: parseInt(props.id), system_account: value };
    } else {
      params = { system_account: value };
    }

    axios.post(`/apaas/system/v5/user/check/account`, params).then((res) => {
      if (res.data.code == 200) {
        callback();
      } else {
        callback(new Error(res.data.data));
      }
    });
  }
};

const checkPhoneRepet = (rule, value, callback) => {
  let params = null;
  if (props.id) {
    params = { id: parseInt(props.id), contact_phone: value };
  } else {
    params = { id: 0, contact_phone: value };
  }

  axios.post(`/apaas/system/v5/org/check`, params).then((res) => {
    if (res.data.code == 200) {
      callback();
    } else {
      callback(new Error(res.data.data));
    }
  });
};

const platformAccountFormRules = reactive({
  system_account: [
    { required: true, message: "请输入账号", trigger: "blur" },
    { min: 4, message: "帐号长度不得低于4个字符", trigger: "blur" },
    { max: 20, message: "帐号最大长度为20个字符", trigger: "blur" },
    { validator: validateSystemAccount, trigger: "blur" },
  ],
  contact_phone: [
    { required: true, message: "请输入手机号", trigger: "blur" },
    { validator: checkPhone, trigger: "blur" },
    { validator: checkPhoneRepet, trigger: "blur" },
  ],
  select_role: [{ required: true, message: "请选择角色", 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" },
  ],
  contact_email: [{ type: "email", message: "请输入正确的邮箱", trigger: "blur" }],
});

const roleList = ref([]);

const platformAccountRef = ref(null);
const emit = defineEmits(["action"]);

const submitForm = async () => {
  if (!platformAccountRef) return;
  await platformAccountRef.value.validate((valid, fields) => {
    if (valid) {
      emit("action", platformAccountForm);
    } else {
      emit("action", null);
    }
  });
};

const clearForm = () => {
  if (!platformAccountRef) return;
  platformAccountRef.value.resetFields();
};

const setForm = (data) => {
  Object.assign(platformAccountForm, data);
};

const getRoleList = () => {
  axios.get(`/apaas/system/v5/org/select/role?is_admin=3`).then((res) => {
    if (res.data.code == 200) {
      roleList.value = res.data.data;
    } else {
    }
  });
};

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

onMounted(() => {});

defineExpose({ submitForm, clearForm, setForm });
</script>
