<template>
    <div class="home_wrapper">
    <van-nav-bar
      title="修改密码"
      left-arrow
      placeholder
      safe-area-inset-top
      @click-left="onClickLeft"
    />

      <van-form @submit="onSubmit">
      <div class="main">
        <p class="title"><i></i>修改密码</p>
        <van-field
          required
          :rules="[{ required: true }]"
          v-model="user.oldPassword"
          label="旧密码"
          placeholder="请输入"
          input-align="right"
          label-width="auto"
        />
        <van-field
          required
          v-model="user.newPassword"
          :rules="[{ validator, message: '包含非法字符，重新输入' }]"
          label="新密码"
          placeholder="请输入"
          input-align="right"
          label-width="auto"
        />
        <van-field
          required
          :rules="[{ required: true }]"
          v-model="user.confirmPassword"
          label="确认密码"
          placeholder="请输入"
          input-align="right"
          label-width="auto"
        />
      </div>

      <van-button round block type="primary" native-type="submit" class="subClass">提交</van-button>
      </van-form>

    </div>
</template>

<script>
  // import { getMenuApp } from "@/api/app/index";
  import Cookies from "js-cookie";
  import { updateUserPwd } from "@/api/system/user"
  export default {
    name: "appUserInfo",
    data() {
      return {
        form: {},
        user: {
          oldPassword: undefined,
          newPassword: undefined,
          confirmPassword: undefined
        }
      };
    },
    created() {

    },
    methods: {
      onClickLeft(){
        history.back(-1);
      },
      validator(val) {
        return /^[^<>"'|\\]+$/.test(val);
      },
      onSubmit() {
        if(this.user.newPassword.length < 6 || this.user.newPassword.length > 20){
          this.$modal.msgWarning("长度在 6 到 20 个字符");
          return;
        }
        if (this.user.newPassword != this.user.confirmPassword){
          this.$modal.msgWarning("两次输入的密码不一致");
          return;
        }
        updateUserPwd(this.user.oldPassword, this.user.newPassword).then(response => {
          this.$modal.msgSuccess("修改成功")
          setTimeout(function(){
            history.back(-1);
          },2000)
        })
      },

    },
  };
</script>

<style scoped lang="scss">
  p{margin: 0;}
  .home_wrapper{
    width: 100vw;
    min-height: 100vh;
    background: #F6F9FB;
    padding-bottom: 5vh;
  }
  .van-nav-bar{
    background: linear-gradient( 173deg, #91E2D3 0%, #CDFCF0 100%);
    ::v-deep.van-icon{
      color: #000000;
    }
  }
  .main{
    width: 94%;
    margin: 3vw auto;
    padding: 3vw;
    background-color: #ffffff;
    border-radius: 10px;
    overflow: hidden;

  }
  .title{
    display: flex;
    align-items: center;
    font-size: 20px;
    font-weight: bold;
    margin-bottom: 10px;
    i{
      width: 5px;
      height: 20px;
      display: block;
      background-color: #29D2AF;
      margin-right: 10px;
    }
  }
  .subClass{
    background: linear-gradient( 270deg, #53E4A5 0%, #24DBDB 100%);
    border-radius: 50px 50px 50px 50px;
    border: none;
    width: 90%;
    margin: 3vw auto;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #ffffff;
    font-size: 18px;
  }

</style>
