<template>
  <view>
    <input class="input" :password="true" style="margin-top: 48rpx;" :value="oldPassword" maxlength="20"
           placeholder="请输入原密码" placeholder-class="input-placeholder" :focus="true" @input="updateOldPassword"></input>
    <input class="input" :password="true" :value="newPassword" maxlength="20" placeholder="请输入新密码"
           placeholder-class="input-placeholder" @input="updateNewPassword"></input>
    <input class="input" :password="true" :value="confirmPassword" maxlength="20" placeholder="请确认密码"
           placeholder-class="input-placeholder" @input="updateConfirmPassword"></input>
    <view class="text-tips">密码必须是8-20位，至少包括数字/字母/符号中的2种</view>
    <view class="button" @click="updatePassword">保存</view>
  </view>
</template>

<script>
import { mapActions, mapState } from 'pinia'
import { useUserStore } from '@/stores/user'
import { updatePasswordRequest } from '@/api/user'

export default {
  data () {
    return {
      oldPassword: '',
      newPassword: '',
      confirmPassword: '',
      pending: false
    }
  },
  computed: {
    ...mapState(useUserStore, ['userInfo'])
  },
  methods: {
    ...mapActions(useUserStore, ['updateUserInfo']),
    updateOldPassword (data) {
      this.oldPassword = data.detail.value
    },
    updateNewPassword (data) {
      this.newPassword = data.detail.value
    },
    updateConfirmPassword (data) {
      this.confirmPassword = data.detail.value
    },
    async updatePassword () {
      if (this.oldPassword === '' || this.newPassword === '' || this.confirmPassword === '') {
        uni.showToast({
          title: '密码不能为空',
          icon: 'none'
        })
        return
      }
      if (this.newPassword.length < 8 || this.newPassword.length > 20 || this.confirmPassword.length < 8 ||
          this.confirmPassword.length > 20 || this.oldPassword.length < 8 || this.oldPassword.length > 20) {
        uni.showToast({
          title: '密码长度必须是8-20位',
          icon: 'none'
        })
        return
      }
      if (this.newPassword !== this.confirmPassword) {
        uni.showToast({
          title: '两次密码输入不一致',
          icon: 'none'
        })
        return
      }
      if(this.pending) {
        return
      }
      this.pending = true
      const { code, message } = await updatePasswordRequest({
        oldPassword: this.oldPassword,
        newPassword: this.newPassword,
        date: new Date().getTime()
      })
      if (code === 0) {
        this.updateUserInfo({
          ...this.userInfo,
          nickname: this.nickname
        })
        uni.navigateBack({
          complete () {
            uni.showToast({
              title: '密码重置成功',
              icon: 'none'
            })
          }
        })
      } else {
        uni.showToast({
          title: message,
          icon: 'none'
        })
      }
      this.pending = false
    }
  }
}
</script>

<style lang="scss">
page {
  background-color: $uni-bg-color-grey;
}
</style>

<style scoped lang="scss">
.input {
  border-radius: 16rpx;
  background: #FFFFFF;
  margin-left: 35rpx;
  margin-top: 24rpx;
  height: 108rpx;
  width: 616rpx;
  padding-left: 32rpx;
  padding-right: 32rpx;
}

.input-placeholder {
  height: 108rpx;
  font-size: 36rpx;
  font-weight: bold;
  line-height: 108rpx;
}

.button {
  margin-top: 32rpx;
  margin-left: 35rpx;
  width: 680rpx;
  height: 96rpx;
  background: #63b8af;
  border-radius: 16rpx;
  line-height: 96rpx;
  text-align: center;
  font-size: 32rpx;
  color: #FFFFFF;
}

.text-tips {
  text-align: center;
  margin-top: 32rpx;
  font-size: 28rpx;
  color: #9B9B9B;
}
</style>
