<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">
              <div>个人信息</div>
            </div>
          </template>
          <div class="userDetail">
            <div class="userInfo_item">
              <div style="display: flex;align-items: center">
                <el-icon size="18" style="margin-right: 5px">
                  <User/>
                </el-icon>
                用户名:
              </div>
              <div>{{ userParams.userName }}</div>
            </div>
            <div class="userInfo_item">
              <div style="display: flex;align-items: center">
                <el-icon size="18" style="margin-right: 5px">
                  <User/>
                </el-icon>
                用户名称:
              </div>
              <div>{{ userParams.nickName }}</div>
            </div>
            <div class="userInfo_item">
              <div style="display: flex;align-items: center">
                <el-icon size="18" style="margin-right: 5px">
                  <Notification/>
                </el-icon>
                工号:
              </div>
              <div>{{ userParams.workCode }}</div>
            </div>
            <div class="userInfo_item">
              <div style="display: flex;align-items: center">
                <el-icon size="18" style="margin-right: 5px">
                  <Phone/>
                </el-icon>
                电话号码:
              </div>
              <div>{{ userParams.mobile }}</div>
            </div>
            <div class="userInfo_item">
              <div style="display: flex;align-items: center">
                <el-icon size="18" style="margin-right: 5px">
                  <House/>
                </el-icon>
                所属公司:
              </div>
              <div>{{ userParams.subCompanyName }}</div>
            </div>
            <div class="userInfo_item">
              <div style="display: flex;align-items: center">
                <el-icon size="18" style="margin-right: 5px">
                  <FolderOpened/>
                </el-icon>
                所属部门:
              </div>
              <div>{{ userParams.departmentName }}</div>
            </div>
            <div class="userInfo_item">
              <div style="display: flex;align-items: center">
                <el-icon size="18" style="margin-right: 5px">
                  <Folder/>
                </el-icon>
                岗位:
              </div>
              <div>{{ userParams.jobActivityDesc }}</div>
            </div>
            <div class="userInfo_item">
              <div style="display: flex;align-items: center">
                <el-icon size="18" style="margin-right: 5px">
                  <Wallet/>
                </el-icon>
                账号类型:
              </div>
              <div>{{ userParams.accountType === '0' ? '主账号' : '次账号' }}</div>
            </div>
            <div class="userInfo_item">
              <div style="display: flex;align-items: center">
                <el-icon size="18" style="margin-right: 5px">
                  <User/>
                </el-icon>
                所属角色:
              </div>
              <div>{{ belongToRole(userParams.roles) }}</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="14" :xs="24">
        <el-card class="box-card">
          <!-- 标题 -->
          <template #header>
            <div class="card-header">
              <div>{{ isInitPassword ? '修改密码' : '初始化密码' }}</div>
            </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 {editPassword} 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({
  oldPassword: [
    {required: true, message: "旧密码不能为空", trigger: ["change", "blur"]},
  ],
  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 () => {
    editPassword({
      ...userPassword.value
    }).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 {
    display: flex;align-items: center;
    justify-content: space-between;
    height: 40px;
    border-bottom-style: solid;
    border-color: #daddd2;
    border-width: 1px;
    //text-align: left;
    //line-height: 40px;

  }

  //
  //.userInfo_item > div {
  //  float: right;
  //}
}

</style>
