<template>
  <div class="my">
    <el-row :gutter="20">
      <el-col :span="10" :xs="24">
        <el-card class="box-card1">
          <!-- 标题 -->
          <template #header>
            <div class="card-header">
              <span>个人信息</span>
            </div>
          </template>
          <div class="userDetail">
            <div class="userInfo_item">
              用户名: <span>{{ userParams.userName }}</span>
            </div>
            <div class="userInfo_item">
              用户名称: <span>{{ userParams.nickName }}</span>
            </div>
            <div class="userInfo_item">
              工号: <span>{{ userParams.workCode }}</span>
            </div>
            <div class="userInfo_item">
              <el-icon>
                <Iphone/>
              </el-icon>
              电话号码: <span>{{ userParams.mobile }}</span></div>
            <div class="userInfo_item">
              <el-icon>
                <HomeFilled/>
              </el-icon>
              所属公司: <span>{{ userParams.subCompanyName }}</span>
            </div>
            <div class="userInfo_item">
              <el-icon>
                <HomeFilled/>
              </el-icon>
              所属部门: <span>{{ userParams.departmentName }}</span>
            </div>
            <div class="userInfo_item">
              <el-icon>
                <HomeFilled/>
              </el-icon>
              岗位: <span>{{ userParams.jobActivityDesc }}</span>
            </div>
            <div class="userInfo_item">
              <el-icon>
                <HomeFilled/>
              </el-icon>
              账号类型: <span>{{ userParams.accountType === '0' ? '主账号' : '次账号' }}</span>
            </div>
            <div class="userInfo_item">
              <el-icon>
                <OfficeBuilding/>
              </el-icon>
              所属角色: <span>{{ belongToRole(userParams.roles) }}</span></div>
          </div>
          <div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="14" :xs="24">
        <el-card class="box-card">
          <!-- 标题 -->
          <template #header>
            <div class="card-header">
              <span>{{ isInitPassword ? '修改密码' : '初始化密码' }}</span>
            </div>
          </template>
          <el-form label-width="120px" ref="passwordRef" :model="userPassword" :rules="rules">
            <el-form-item label="旧密码" prop="oldPassword" :required="true" style="text-align:left" v-if="isInitPassword">
              <el-input placeholder="请输入旧密码" v-model="userPassword.oldPassword" type="password"/>
            </el-form-item>
            <el-form-item label="新密码" prop="newPassword" :required="true" style="text-align:left">
              <el-input placeholder="请输入新密码" v-model="userPassword.newPassword" type="password"/>
            </el-form-item>
            <el-form-item label="确认密码" prop="confirmPassword" :required="true" style="text-align:left">
              <el-input placeholder="请确认新密码" v-model="userPassword.confirmPassword" type="password"/>
            </el-form-item>
            <el-form-item>
              <el-button color="#DED0B2" @click="handleSubmit">保存</el-button>
              <el-button @click="close">关闭</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>
<script setup>
import {ElNotification} from 'element-plus'
import {useTagsView} from '@/stores/tagsview.js'
import {getAuthInfo} from "@/api/login";
import {editPassword, initPassword} from "@/api/auth/auth";

const isInitPassword = ref(false);
const userParams = ref({});
const passwordRef = ref();
const router = useRouter()
const userPassword = ref({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
});
const tagsViewStore = useTagsView()
const validatePasswordComplexity = (rule, value, callback) => {
  const reg = /^(?=.*[a-zA-Z])(?=.*\d).{1,9}$/; //密码必须是至少包含字母、数字，1-9位
  if (!reg.test(value)) {
    callback(new Error("密码必须是至少包含字母、数字，1-9位"));
  } else {
    callback();
  }
};


const validatePasswordEquality = (rule, value, callback) => {
  if (userPassword.value.newPassword != value) {
    callback(new Error("两次输入密码不一致!"));
  } else {
    callback();
  }
}
const rules = reactive({
  newPassword: [
    {required: true, message: "密码不能为空", trigger: ["change", "blur"]},
    {
      required: true,
      trigger: ["change", "blur"],
      validator: validatePasswordComplexity
    }
  ],
  confirmPassword: [
    {required: true, message: "密码不能为空", trigger: ["change", "blur"]},
    {
      required: true,
      trigger: ["change", "blur"],
      validator: validatePasswordEquality
    }
  ],
})

const getInfo = async () => {
  await getAuthInfo().then(res => {
    userParams.value = res.data.user
    isInitPassword.value = res.data.initPassword
  })
}

const belongToRole = (roles) => {
  if (!roles) return;
  return roles.map(item => item.roleName).join('，')
}

// 修改密码
const handleSubmit = async () => {
  if (isInitPassword.value) {
    editPassword({
      ...userPassword.value
    }).then(res => {
      ElNotification({
        title: '提示',
        message: res.msg,
        type: res.code === 1000 ? 'success' : 'error'
      })
      if (res.code === 1000) {
        passwordRef.value.resetFields()
      }
    })
  } else {
    initPassword({
      newPassword: userPassword.value.newPassword,
      confirmPassword: userPassword.value.confirmPassword
    }).then(res => {
      ElNotification({
        title: '提示',
        message: res.msg,
        type: res.code === 1000 ? 'success' : 'error'
      })
      if (res.code === 1000) {
        passwordRef.value.resetFields()
      }
    })
  }
}
// 关闭
const close = () => {
  tagsViewStore.delVisitedViews(router.currentRoute.value.path)
  router.push('/')
}
onMounted(() => {
  getInfo()
})
</script>
<style lang="scss">
body, div {
  margin: 0;
  padding: 0;
}

.my {
  margin: 0 auto;
  margin-top: 20px;

  .userDetail {
    .userImg {
      height: 170px;
      text-align: center;
      border-bottom-style: solid;
      border-color: #daddd2;
      border-width: 1px;

      .avatar {
        width: 150px;
        height: 150px;
        border-radius: 50%;
      }
    }
  }

  .userInfo_item {
    height: 40px;
    border-bottom-style: solid;
    border-color: #daddd2;
    border-width: 1px;
    text-align: left;
    line-height: 40px;
  }

  .userInfo_item > span {
    float: right;
  }
}

</style>
