<template>
  <div>
    <validator name="v">
      <modal :show.sync="show" v-ref:modal  backdrop="false">
        <header slot="modal-header" class="modal-header">
          <button type="button" class="close" @click="rest"><span>&times;</span></button>
          <h4 class="modal-title">修改密码</h4>
        </header>
        <article slot="modal-body" class="modal-body modifystyle auto form-horizontal">
          <div class="has-feedback form-group"
               :class="{'has-warning':$v.password.required,'has-error': $v.password.required,
          'has-success':!$v.password.required}">
            <label for="password" class="col-sm-4 col-sm-offset-1 control-label">原始密码:&nbsp;&nbsp;</label>
            <div class="col-sm-4">
              <input type="password" v-model="deliver.password" class="form-control" id="password" v-validate:password="{ required: true}">
              <span class="glyphicon glyphicon-ok form-control-feedback" v-if="!$v.password.required"></span>
              <span v-if="$v.password.required">不能为空</span>
            </div>
          </div>
          <div class="has-feedback form-group"
               :class="{'has-warning':$v.newpassword.required,'has-error':($v.newpassword.numbersAndLetter8to16 || $v.newpassword.minlength)&& !$v.newpassword.required && !$v.newpassword.chinesePassword,
          'has-success': !$v.newpassword.required && !$v.newpassword.numbersAndLetter8to16&&!$v.newpassword.minlength  && !$v.newpassword.chinesePassword}">
            <label for="newpassword" class="col-sm-4 col-sm-offset-1 control-label">新的密码:&nbsp;&nbsp;</label>
            <div class="col-sm-4">
              <input type="password" v-model="deliver.newpassword" class="form-control" id="newpassword" v-validate:newpassword='{required:true, numbersAndLetter8to16: true, minlength: 6,chinesePassword: true}'>
              <span class="glyphicon glyphicon-ok form-control-feedback" v-if="!$v.newpassword.required && !$v.newpassword.minlength && !($v.newpassword.numbersAndLetter8to16 && !($v.newpassword.required)) && !($v.newpassword.chinesePassword) "></span>
              <span v-if="$v.newpassword.required">不能为空</span>
              <span v-if="($v.newpassword.numbersAndLetter8to16||$v.newpassword.minlength) && !($v.newpassword.required&&$v.newpassword.minlength)">密码必须是8~16位数字与大小字母外加特殊字符的组合，并且不能出现类似'123','abc'等连贯数字或者字母</span>
              <span v-if="$v.newpassword.chinesePassword">密码不能包含中文</span>
            </div>
          </div>
          <div class="has-feedback form-group"
               :class="{'has-warning':$v.affirmpassword.required,'has-error':$v.affirmpassword.equalValid && !($v.affirmpassword.required),
          'has-success': !$v.affirmpassword.required && !($v.affirmpassword.equalValid && !($v.affirmpassword.required))}">
            <label for="affirmpassword" class="col-sm-4 col-sm-offset-1 control-label">确认密码:&nbsp;&nbsp;</label>
            <div class="col-sm-4">
              <input type="password" v-model="deliver.affirmpassword" class="form-control" id="affirmpassword" v-validate:affirmpassword="{ required: true, equalValid: deliver.newpassword }">
              <span class="glyphicon glyphicon-ok form-control-feedback" v-if="!$v.affirmpassword.required && !($v.affirmpassword.equalValid && !($v.affirmpassword.required))"></span>
              <span v-if="$v.affirmpassword.required">不能为空</span>
              <span v-if="$v.affirmpassword.equalValid && !($v.affirmpassword.required)">两次密码不一致 !!</span>
            </div>
          </div>
        </article>

        <footer slot="modal-footer" class="modal-footer">
          <button  type="button" class="btn btn-success" @click='confirm' :disabled="!$v.valid">确认</button>
          <button  type="button" class="btn btn-default" @click='rest'>取消</button>
        </footer>
      </modal>
    </validator>
  </div>


</template>
<script>
import co from 'co'
import validator from '../../plugins/validation'

let saveGen = function * (self) {
  self.deliver.ename = self.ename || self.functions.ename
  // let res = yield self.$post('rs/user/entity', {data: self.deliver})
  // let res = yield self.$post('rs/db/modifypassword', {data: self.deliver})
  // let res = yield self.$resetpost('/rs/db/modifypwd', {data: self.deliver})
  //  Util.f.password = self.deliver.newpassword
  let res = yield self.$resetpost('/rs/user/modifypwd', {data: self.deliver }, {resolveMsg: null,rejectMsg: null})
  self.$login.f.password = self.deliver.newpassword
  if (res.data) {
    console.log("修改密码返回：" , res.data)
    // 兼容旧版ldao，旧版不返回code
    if (res.data.code === 200) {
      self.deliver.password = ''
      self.deliver.newpassword = ''
      self.deliver.affirmpassword = ''
      self.show = false
      self.$showAlert("修改密码成功", 'success', 2000)
    } else {
      // 兼容旧版ldao，旧版不返回code
      self.deliver.password = ''
      self.deliver.newpassword = ''
      self.deliver.affirmpassword = ''
      self.show = false
      self.$showAlert("修改密码成功", 'success', 2000)
    }
  } else {
    if (res.data.code) {
      self.$showAlert("修改密码失败，原因：" + res.data.msg, 'warning', 2000)
    } else {
      self.$showMessage(res.data)
    }
  }
}
export default {
  title: '系统管理-密码修改',
  data () {
    return {
      deliver: {
        password: '',
        newpassword: '',
        affirmpassword: ''
      },
      functions: this.$login.f
    }
  },
  props: ['show','ename'],
  methods: {
    confirm () {
      let gen = saveGen(this)
      co(gen)
    },
    rest () {
      this.show = false
      this.deliver = {password: '', newpassword: '', affirmpassword: ''}
    }
  }
}
</script>
<style>
/*修改密码body界面样式*/
.modifystyle {
  background: #FCFEEE;
}
.modifystyle span{
  color: red;
}
</style>
