<script lang="ts">
export default {
  name: 'personalcenter'
};
</script>

<script setup lang="ts">
import { ref, onMounted} from 'vue';
import { useRouter } from 'vue-router';
import { getUserInfo } from '@/api/user';
import { FormInstance, ElMessage } from 'element-plus'
import { gettableData } from '@/api/dept';
import { updatePersonalInfo, updatePassword, updateAvatar } from '@/api/user';
import { encrypt } from '@/utils/rsaEncrypt';
import { useTagsViewStore } from '@/store/modules/tagsView';
import { useUserStore } from '@/store/modules/user';
const userStore = useUserStore();
const tagsViewStore = useTagsViewStore();
const router = useRouter();
const url = import.meta.env.VITE_APP_BASE_API;

// 初始加载
function init() {
  getUserInfo().then((res: any) => {
    info.value = res.data.userInfo
    roleids.value = res.data.roles
    imageSrc.value = url + '/avatar/' + res.data.userInfo.avatar
    gettableData({id: res.data.userInfo.poid}).then((reset: any) => {
      orgData.value = reset.data
    })
  })

}
const roleids = ref();
const info = ref({
  id: '',
  oid: '',
  username: '',
  porgname: '',
  nickname: '',
  phone: '',
  email: '',
});
const pass = ref({
  password: '',
  newPassword: '',
  confirmPassword: '',
})
const imageSrc = ref()
const orgData = ref()
const ruleFormRef = ref<FormInstance>()
const rulePass = ref()
// 用户弹窗规则定义
const moderules = ref(
  {
    password: [
      { required: true, message: '请输入原密码', trigger: 'blur' }
    ],
    newPassword: [
      { required: true, message: '请输入新密码', trigger: 'blur' }
    ],
    oid: [
      { required: true, message: '请选择所属部门', trigger: 'select' }
    ],
    confirmPassword: [
      { required: true, validator: confirmPassword, trigger: 'blur' }
    ],
  },
)
function confirmPassword(rule: any, value: any, callback: any) {
  if(value == null) {
    callback('请输入新密码');
  } else {
    if (value.length == 0 ) {
      callback('请输入新密码');
    } else if (value.length < 6 ) {
      callback('密码不能小于6位');
    } else {
      if (pass.value.newPassword != value) {
        callback([new Error('两次输入密码不一致')]);
      } else {
        callback();
      }
    }
  }
}
function validMail(value: any) {
  const reg = /^(([^<>()\]\\.,;:\s@"]+(\.[^<>()\]\\.,;:\s@"]+)*)|(".+"))@(([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  if (!reg.test(value)) {
    return '邮箱格式错误'
  } else {
    return true
  }
}
function phoneRules(value: any) {
  const phone = value.replace(/\s/g, "");
  let regs = /^((13[0-9])|(17[0-1,6-8])|(15[^4,\\D])|(18[0-9]))\d{8}$/;
  if (!regs.test(phone)) {
    return '手机号输入不合法';
  } else {
    return true;
  }
}
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  init()
}
const resetPass = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  init()
}

function save(){
  if (info.value.email != '') {
    if(validMail(info.value.email) != true) {
      ElMessage({
        type: 'warning',
        message: String(validMail(info.value.email)),
      })
      return
    }
  }
  if (info.value.phone != '') {
    if(phoneRules(info.value.phone) != true) {
      ElMessage({
        type: 'warning',
        message: String(phoneRules(info.value.email)),
      })
      return
    }
  }
  
  const params = {
    id: info.value.id,
    email: info.value.email,
    phone: info.value.phone,
    orgid: info.value.oid,
  }
  updatePersonalInfo(params).then(() => {
    ElMessage({
      type: 'success',
      message: '修改成功',
    })
  })
}
function savePass(formEl: any){
  formEl.validate((valid: any) => {
    if (valid) {
      const params = {
        id: info.value.id,
        password: encrypt(pass.value.newPassword)
      }
      updatePassword(params).then((res: any) => {
        if(res.code == 0) {
          userStore.logout().then(() => {
            tagsViewStore.delAllViews();
          }).then(() => {
            router.push(`/login`);
          });
          ElMessage({
            type: 'success',
            message: '修改成功，请重新登录',
          })
        }
      })
    }
  })
}
function fileClick() {
  const avatar = document.getElementById('avatar');
  avatar?.click();
}
function changeFile(e: any){
  const files = new FormData()
  
  files.append('id', info.value.id)
  files.append('multipartFile', e.target.files[0])
  updateAvatar(files).then(() => {
    ElMessage({
      type: 'success',
      message: '上传成功',
    })
    location.reload()
    var file : any = document.getElementById('avatar');
    file.value = '';
  }).catch(() => {
    ElMessage({
      type: 'error',
      message: '上传失败',
    })
    var file : any = document.getElementById('avatar');
    file.value = '';
  });
}
onMounted(() => {
  init()
})
</script>

<template>
  <div class="bg-white p-5 rounded-md h-[calc(100vh-130px)]">
    <div class="main">
      <el-form ref="ruleFormRef" :model="info" label-width="100px" style="margin-top:10px" :rules="moderules">
        <div class="title font-black text-lg  pl-2 mb-5">个人信息</div>
        <el-form-item label="登录账号">
          <el-input v-model="info.username" style="width:100%" disabled></el-input>
        </el-form-item>
        <el-form-item label="用户姓名">
          <el-input v-model="info.nickname" style="width:100%" disabled></el-input>
        </el-form-item>
        <el-form-item label="所属企业">
          <el-input v-model="info.porgname" style="width:100%" disabled></el-input>
        </el-form-item>
        <el-form-item label="所属部门" prop="orgid">
          <el-select v-model="info.oid" placeholder="请选择" style="width:100%;" clearable>
            <el-option v-for="item in orgData" :key="item.id" :label="item.orgname" :value="item.id"  />
          </el-select>
        </el-form-item>
        <el-form-item label="联系邮箱" prop="email">
          <el-input v-model="info.email" style="width:100%"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="info.phone" style="width:100%"></el-input>
        </el-form-item>
        <div class="footer">
          <el-button style="padding: 10px 15px;" @click="resetForm(ruleFormRef)">取 消</el-button>
          <el-button type="primary" style="padding: 10px 15px;" @click="save">保存</el-button>
        </div>
      </el-form>
      <el-form ref="rulePass" :model="pass" label-width="100px" style="margin-top:10px" :rules="moderules">
        <div class="title font-black text-lg  pl-2 mb-5">修改密码</div>
        <!-- <el-form-item label="原密码" prop="password">
          <el-input v-model="pass.password" style="width:100%"></el-input>
        </el-form-item> -->
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="pass.newPassword" style="width:100%"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="confirmPassword">
          <el-input v-model="pass.confirmPassword" style="width:100%"></el-input>
        </el-form-item>
      </el-form>
      <div class="avatar" @click="fileClick">
        <img v-if="imageSrc !== ''" :src="imageSrc" alt="">
        <img v-else src="@/assets/MenuIcon/top_tx.png" alt="">
      </div>
      <input type="file" id="avatar" style="display:none" @change="changeFile">
      <div class="footer">
        <!-- <el-button style="padding: 10px 15px;" @click="resetPass(rulePass)">取 消</el-button> -->
        <el-button type="primary" style="padding: 10px 15px;" @click="savePass(rulePass)">保存</el-button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.main{
  max-width: 600px;
  position: relative;
}
.avatar{
  position: absolute;
  right: -120px;
  top: 50px;
  width: 100px;
  height: 100px;
  border-radius: 3px;
  background: #ddd;
  cursor: pointer;
  border: 1px solid #ddd;
}

.avatar img {
  width: 100%;
  height: 100%;
  border-radius: 3px;
}
.footer{
  margin-left: 100px;
}
.title{
  height: 30px;
  border-left: 4px solid #266fff;
  font-size: 16px;
  /* pa */
}
.personalCenter-box{
  width: 100%;
}
</style>
