<template>
  <view class="reset-password">
    <u--form ref="formRef" :rules="rules" :model="formData">
      <u-form-item label="原密码" labelWidth="140rpx" borderBottom prop="old_password">
        <u--input v-model="formData.old_password" :password="!pwdShow.old_password" placeholder="请输入原密码" maxlength="18">
          <!-- #ifdef APP-PLUS -->
          <!-- <template slot="suffix">
            <uni-icons :type="pwdShow.old_password?'eye-filled':'eye-slash-filled'" @click="pwdShow.old_password=!pwdShow.old_password" color="#999"></uni-icons>
          </template> -->
          <!-- #endif -->
        </u--input>
      </u-form-item>
      <u-form-item label="新密码" labelWidth="140rpx" borderBottom prop="password">
        <u--input v-model="formData.password" :password="!pwdShow.password" placeholder="请输入新密码" maxlength="18">
          <!-- #ifdef APP-PLUS -->
          <!-- <template slot="suffix">
            <u--icon :name="pwdShow.password?'eye-off':'eye-fill'" @click="pwdShow.old_password=!pwdShow.old_password" color="#999"></u--icon>
          </template> -->
          <!-- #endif -->
        </u--input>
      </u-form-item>
      <u-form-item label="确认密码" labelWidth="140rpx" borderBottom prop="password_confirm">
        <u--input v-model="formData.password_confirm"  :password="!pwdShow.password_confirm" placeholder="请确认新密码" maxlength="18">
          <!-- #ifdef APP-PLUS -->
          <!-- <template slot="suffix">
            <u--icon :name="pwdShow.password_confirm?'eye-off':'eye-fill'" @click="pwdShow.old_password=!pwdShow.old_password" color="#999"></u--icon>
          </template> -->
          <!-- #endif -->
        </u--input>
      </u-form-item>
      <u-button style="margin-top: 28rpx;background-color: #0122c7;color: #fff;" @click="submit">提交</u-button>
    </u--form>
  </view>
</template>

<script>
  import { Toast } from '../../libs/uniApi';
  import { changePassword } from "@/api/oaUser.js"
  export default {
    data() {
      return {
        pwdShow: {
          old_password: false,
          password: false,
          password_confirm: false,
        },
        formData: {
          old_password: '', // 原密码
          password: '', // 新密码
          password_confirm: '', // 确认密码
        },
        rules: {
          old_password: {
            type: 'string',
            required: true,
            min: 6,
            max: 18,
            message: '请输入6-18位密码',
            trigger: ['change', 'blur']
          },
          password: {
            type: 'string',
            required: true,
            min: 6,
            max: 18,
            message: '请输入6-18位密码',
            trigger: ['change', 'blur']
          },
          password_confirm: {
            type: 'string',
            required: true,
            min: 6,
            max: 18,
            message: '请输入6-18位密码',
            trigger: ['change', 'blur']
          },
        }
      }
    },
    methods: {
      submit() {
        this.$refs.formRef.validate().then(async (e) => {
          if (e) {
            if (this.formData.password == this.formData.old_password) return Toast('新密码不能与原密码一致');
            if (this.formData.password !== this.formData.password_confirm) return Toast('两次新密码不一致');
            try {
              await changePassword({ ...this.formData });
              Toast('修改成功');
              this.$u.sleep(500).then(() => {
                uni.showLoading({
                  mask: true,
                  title: '加载中'
                })
                uni.switchTab({
                  url: '/pages/oaHome/oaHome',
                  success: () => {
                    uni.hideLoading()
                  }
                })
              })
            } catch (e) {
              // console.log(e);
              Toast(e.msg || '修改失败')
            }
          }
        })
      }
    }
  }
</script>

<style>
  .reset-password {
    margin: 28rpx;
    padding: 28rpx;
    background-color: #fff;
    border-radius: 14rpx;
  }
</style>