<template>
  <div>
    <div class="right-modifyPsw">
      <div class="right-modifyPsw-title">
        <span style="font-size: 18px;font-weight: bold">修改密码</span>
      </div>
      <div style="background-color: rgba(233,239,249,1);padding: 10px">
        <i class="el-icon-warning" style="color: rgba(0, 117, 203, 1);font-size: 18px"></i>
        <span style="font-size: 16px">安全提醒：请妥善保管密码，众测服务平台人员不会以任何理由向您索取密码。</span>
      </div>
      <el-form ref="modifyForm" :rules="rules" :model="modifyForm" label-width="80px" style="margin-top: 20px; width: 40%;">
        <el-form-item label="当前密码" prop="oldPassword">
          <el-input v-model="modifyForm.oldPassword" type="password"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="password">
          <el-input v-model="modifyForm.password" type="password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="password2">
          <el-input v-model="modifyForm.password2" type="password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="ModifyPassword">确认修改</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  import Http from '@/js/http.js'
  import {storageGet,logout} from '@/js/index'
  import {notify} from '@/constants/index'
  export default {
    name: "ModifyPsw",
    data(){
      return {
        user:{},
        modifyForm: {
          oldPassword:'',
          password:'',
          password2:'',
        },
        rules: {
            oldPassword: [
                {required: true, message: '当前密码不可为空', trigger: 'blur'},
                {min: 6, max: 16, message: '密码长度在 6 到 16 个字符', trigger: 'blur'}
            ],
            password: [
                {required: true, message: '新密码不可为空', trigger: 'blur'},
                {min: 6, max: 16, message: '密码长度在 6 到 16 个字符', trigger: 'blur'}
            ],
            password2: [
                {required: true, message: '确认密码不可为空', trigger: 'blur'},
                {min: 6, max: 16, message: '密码长度在 6 到 16 个字符', trigger: 'blur'}
            ]
        }
      }
    },
    methods:{
      setUserInfo() {
        this.user = storageGet('user') && storageGet('user').userVO;
      },
      ModifyPassword(){
          this.$refs['modifyForm'].validate(valid => {
              if (valid) {
                  let params = {
                      id:this.user.id.toString(),
                      ...this.modifyForm
                  }
                  if(this.modifyForm.password != this.modifyForm.password2){
                      notify('error', '两次密码不一致，请重新输入！')
                      return ;
                  }

                  Http.put(`/api/password/reset`,params).then(res=>{
                      // notify('success', '密码修改成功，请重新登录！')
                      window.alert('密码修改成功，请重新登录');
                      logout().then((res) => {
                          window.location.href = process.env.LOGIN_URL;
                          // this.$router.push('/home')
                      })

                  }).catch(err=>{
                      notify('error', '修改密码失败:' + err.data)
                  })
              }
          })
      }
    },
    mounted() {
      this.setUserInfo();
    }
  }
</script>

<style scoped lang="less">
  .right-modifyPsw {
    padding: 20px;
    background: rgba(255, 255, 255, 1);
    box-shadow: 0px 1px 6px 0px rgba(8, 6, 6, 0.13);

    .right-modifyPsw-title {
      padding: 10px;
      border-bottom: 1px solid #ccc;
      margin-bottom: 20px;
    }
  }
</style>
