<template>
  <div class="app-container common-list-page">
    <el-form
      ref="temp"
      :model="temp"
      :rules="rules"
      status-icon
      label-width="100px"
    >
      <el-form-item label="旧密码：" prop="oldpassword">
        <el-input v-model="temp.oldpassword" type="password" auto-complete="off" />
      </el-form-item>
      <el-form-item label="新密码：" prop="password">
        <el-input v-model="temp.password" type="password" auto-complete="off" />
      </el-form-item>
      <el-form-item label="确认密码：" prop="password1">
        <el-input v-model="temp.password1" type="password" auto-complete="off" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click.native.prevent="toAmend">确认修改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { ExecDataByConfig } from '@/api/common'
import Cookies from 'js-cookie'
export default {
  data() {
    var oldPass = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入旧密码'))
      } else if (value.toString().length < 6 || value.toString().length > 18) {
        callback(new Error('密码长度为6-18位'))
      } else {
        callback()
      }
    }
    var validatePass = (rule, value, callback) => {
      console.log(value)
      if (!value) {
        callback(new Error('请输入新密码'))
      } else if (value.toString().length < 6 || value.toString().length > 18) {
        callback(new Error('密码长度为6-18位'))
      } else {
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.temp.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      resetForm: {
        // name: 'updatePwd',
        // returntype: 'Map',
        // parammaps: {
        //   password: '',
        //   username: this.$store.state.user.name
        // }
      },
      temp: {},
      rules: {
        oldpassword: [
          { required: true, validator: oldPass, trigger: 'blur' }
        ],
        password: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        password1: [
          { required: true, validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    toAmend() {
      console.log(111)
      this.$refs['temp'].validate(valid => {
        if (valid) {
          this.resetForm.common = { 'returnmap': '0' }
          this.resetForm.data = []
          this.resetForm.data[0] = { 'name': 'checkPassword', 'type': 'v', 'parammaps': {
            'id': Cookies.get('employeid'),
            'password': this.temp.oldpassword
          }}

          this.resetForm.data[1] = { 'name': 'updatePwd', 'type': 'e', 'parammaps': {
            'password': this.temp.password,
            'username':  Cookies.get('name')
          }}

          ExecDataByConfig(this.resetForm).then(response => {
            console.log('保存发送参数', this.resetForm)
            if (response.msg === 'fail') {
              this.$notify({
                title: '保存失败',
                message: response.data,
                type: 'warning',
                duration: 2000
              })
            } else {
              setTimeout(() => {
                this.logout() // 调用跳转到登陆页的方法
              }, 1000)
              this.$notify({
                title: '',
                message: '保存成功',
                type: 'success',
                duration: 2000
              })
            }
          })
        }
      })
      //     this.resetForm.parammaps.password = this.temp.password
      //     this.resetForm.parammaps.username = this.$store.state.user.name
      //     PostDataByName(this.resetForm).then(response => {
      //       if (response.msg === 'fail') {
      //         this.$notify({
      //           title: '失败',
      //           message: '修改失败',
      //           type: 'danger',
      //           duration: 2000
      //         })
      //       } else {
      //         setTimeout(() => {
      //           this.logout() // 调用跳转到登陆页的方法
      //         }, 1000)
      //         this.$notify({
      //           title: '成功',
      //           message: '修改成功',
      //           type: 'success',
      //           duration: 2000
      //         })
      //       }
      //       console.log(response)
      //     })
      //   }
      // })
    },
    // 这是修改成功后重新返回登陆页的方法，看个人需要自行调整
    async logout() {
      await this.$store.dispatch('user/logout')
      this.$router.push(`/login`)
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form {
  width: 60%;
  margin: 50px auto 0;
  text-align: center;
  button {
    margin: 20px 0 0;
  }
}
</style>
