<template>
  <div class="right_con">
    <Form ref="updateData" :model="updateData" :rules="update_rule" :label-width="90" class="mar_T_15">
      <FormItem label="旧密码" prop="old_password">
        <Input type="password" class="wid_275" v-model="updateData.old_password"></Input>
      </FormItem>
      <FormItem label="新密码" prop="password">
        <Input type="password" class="wid_275" v-model="updateData.password"></Input>
      </FormItem>
      <FormItem label="确认密码" prop="repassword">
        <Input type="password" class="wid_275" v-model="updateData.repassword"></Input>
      </FormItem>
      <FormItem class="form_btn">
        <Button type="primary" @click="updata_pwd('updateData')">确认</Button>
      </FormItem>
    </Form>
  </div>
</template>
<script>
import { mapState } from "vuex";
export default {
  name: "update_pwd",
  computed:{
    ...mapState({
      adminInfo: state => state.adminInfo,
    }),
  },
  data() {
    const confirmnewPwd = (rule, value, cb) => {
      // let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,30}$/;
      let reg = /^[a-zA-Z0-9]{6,30}$/;
      if (!reg.test(value)) {
        cb(new Error("密码为6-30位字母或数字"));
      } else if (value.length > 30 || value.length < 6) {
        cb(new Error("密码为6-30位字母或数字"));
      } else {
        cb();
      }
    };
    // const confirmoldPwd = (rule, value, cb) => {
    //   if(this.updateData.old_pwd.toString()!==this.adminInfo.password){
    //     cb(new Error("原密码错误"))
    //   }else{
    //     cb()
    //   }
    // };
    const confirmagnPwd = (rule, value, cb) => {
      // let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,30}$/;
      let reg = /^[a-zA-Z0-9]{6,30}$/;
      if (!reg.test(value)) {
        cb(new Error("密码为6-30位字母或数字"));
      } else if (value !== this.updateData.password) {
        cb(new Error("请输入相同的密码"));
      } else {
        cb();
      }
    };
    return {
      updateData: {
        old_password:'',
        password:'',
        repassword:'',
      },
      update_rule: {
        old_password: [
          { required: true, message: "原密码不能为空", trigger: "blur" },
          // { required: true, validator: confirmoldPwd, trigger: "blur" }
        ],
        password: [
          { required: true, message: "新密码不能为空", trigger: "blur" },
          { required: true, validator: confirmnewPwd, trigger: "blur" }
        ],
        repassword: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          { required: true, validator: confirmagnPwd, trigger: "blur" }
        ]
      }
    };
  },
  watch: {},
  methods: {
    updata_pwd(formname) {
      this.$refs[formname].validate(valid => {
        if (valid) {
          this.$api.updataPassword({...this.updateData}).then(data =>{
            if(data.code==200){
              this.$Message.info(data.msg)
              // location.reload()
            }else{
              this.$Message.info(data.msg)
            }
          })
        }
      })
    }
  },
  created() {}
};
</script>


