<template>
  <view class="page">
    <l-title>修改密码</l-title>
    <l-input v-model="oldPwd" title="旧密码" placeholder="请输入旧密码" password></l-input>
    <l-input v-model="newPwd" title="新的密码" placeholder="请输入新密码" password></l-input>
    <l-input v-model="confirmPwd" title="确认输入" placeholder="请再次输入新密码" password></l-input>

    <view class="padding">
      <l-button @click="submit" size="lg" color="blue" class="block" block>确认修改</l-button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      oldPwd: '',
      newPwd: '',
      confirmPwd: ''
    }
  },

  methods: {
    // 提交修改
    async submit() {
      const { auth, oldPwd, newPwd, confirmPwd } = this
      if (oldPwd.length < 6) {
        this.CONFIRM('操作失败', '旧密码输入不正确，请重新确认')
        return
      }
      if (newPwd.length < 6 || newPwd.length > 16) {
        this.CONFIRM('操作失败', '新密码不符合要求，请修改后重试')
        return
      }
      if (newPwd !== confirmPwd) {
        this.CONFIRM('操作失败', '新密码和确认密码输入不一致，请修改')
        return
      }

      const success = await this.HTTP_POST(
        'learun/adms/user/modifypw',
        {
          newpassword: this.MD5(newPwd),
          oldpassword: this.MD5(oldPwd)
        },
        '未能成功修改密码'
      )

      if (!success) {
        return
      }

      this.NAV_BACK()
      this.TOAST('密码修改成功')
    }
  }
}
</script>
