<template>
  <div style="padding:30px 0;">

      <el-form ref="passwordForm" :model="passwordForm" @submit.native.prevent :rules="rulesPassword" style="width:350px; margin:0 auto;">
        <el-form-item prop="current" label="旧密码" :labelWidth="formLabelWidth">
          <el-input
            v-model="passwordForm.current"
            ref="currentPassword"
            type="password"
            maxlength="18"
            placeholder="请输入当前密码">
          </el-input>
        </el-form-item>
        <el-form-item prop="new" label="新密码" :labelWidth="formLabelWidth">
          <el-input
            v-model="passwordForm.new"
            type="password"
            maxlength="18"
            placeholder="请输入新密码">
          </el-input>
        </el-form-item>
        <el-form-item prop="new_repeat" label="再次输入" :labelWidth="formLabelWidth">
          <el-input
            v-model="passwordForm.new_repeat"
            type="password"
            maxlength="18"
            placeholder="请再次输入新密码确认">
          </el-input>
        </el-form-item>



      </el-form>



  </div>
</template>

<script>

  export default {
    name:'EmployeePwd',
    data () {
      let validatePassword = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('请填写密码'));
        }
        if (value.length < 5) {
          return callback(new Error('密码长度应至少在5位或以上'));
        }
        callback();
      };
      let validatePasswordCheck = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('请再次填写密码确认'));
        }
        if (value.length < 5) {
          return callback(new Error('密码长度应至少在5位或以上'));
        }
        if (value != this.passwordForm.new) {
          return callback(new Error('两次输入的密码不一致，请核对后再输入'));
        }
        callback();
      };
      let validatePasswordOld = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('请填写当前密码'));
        }
        callback();
      };
      let profile = {};
      try {
        let profileStr = localStorage.getItem('profile');
        profile = JSON.parse(profileStr);
      } catch (err) {
        console.log(err);
      }
      return {
        dialogPersonalInfoVisible: false,
        dialogPasswordVisible: false,
        passwordForm: {
          current: '',
          new: '',
          new_repeat: ''
        },
        formLabelWidth: '80px',
        rulesPassword: {
          current: [
            { validator:validatePasswordOld, trigger:'blur' }
          ],
          new: [
            { validator:validatePassword, trigger:'blur' }
          ],
          new_repeat: [
            { validator:validatePasswordCheck, trigger:'blur' }
          ],
        },
        profile: profile
      }
    },
    methods: {
      toLoginPage: function () {
        window.location.href = "http://127.0.0.1:8080/login";
        localStorage.removeItem("begin");
      },
      submitPasswordForm: function (formName) {
        let form = this.$refs[formName];
        var self = this;
        form.validate((valid) => {
          if (valid) {
            if (form) {
              self.sendPasswordRequest();
            }
          } else {
            //console.log('验证未通过');
            return false;
          }
        });
      },
      sendPasswordRequest: function () {
        let params = {
          employee_id: this.$store.getters.user_info.id
        }
        for (let key in this.passwordForm) {
          params[key] = this.passwordForm[key];
        }
        var self = this;
        this.$http('post','/integral.php/verify/update_password',params,'','',
          {transformRequest: [(data) => {
            let paramArray = new Array();
            for (let it in data) {
              paramArray.push(encodeURIComponent(it) + '=' + encodeURIComponent(data[it]));
            }
            return paramArray.join("&");
          }]},
          // headers: {
          //   'Content-Type': 'application/x-www-form-urlencoded'
          // }
        ).then(function (response) {
          let message = "";
          let status = 0;
          if (response.status == 200) {
            let data = response.data;
            status = isNaN(data.status) ? 0 : parseInt(data.status);
            switch (status) {
              case 1:
                message = "密码修改成功，请重新登录";
                self.$emit('send_success')
                break;
              case -1:
                message = "表单参数错误";
                break;
              case -2:
                message = data.info ? data.info : "当前密码不正确";
                self.$refs['currentPassword'].focus();
                break;
              default:
                message = data.info ? data.info : "密码修改失败";
            }
          } else {
            message = "服务器出现问题";
          }
          self.$message({
            message: message,
            type: status == 1 ? 'success' : 'error'
          });
          if (status != 1) {
            self.$refs['passwordForm'].clearValidate();
          } else {
            self.$store.dispatch('LogOut');
          }
        }).catch(function (error) {
          console.log(error);
        });
      },

    },
    created() {

    }
  }
</script>
