<template>
    <div>
      <Modal
            v-model="modalShow"
            :title="isForget==true?'忘记密码':'强制修改密码'"
            :footer-hide="true"
            :mask-closable="false"
            :closable="isForget==true?true:false"
            width="26%"
            @on-cancel="ResetForm('formInline')">
          <Form ref="formInline" :model="formInline" :rules="ruleInline" label-position="right" :label-width="100">
             <FormItem :prop="companyEmail" label="邮箱账号：">
                 <i-Input type="text" v-model="formInline[companyEmail]" placeholder="邮箱账号"></i-Input>
             </FormItem>
             <FormItem :prop="code" label="验证码：">
                 <Row :gutter="5">
                   <i-Col span="18">
                     <i-Input type="password" v-model="formInline[code]" placeholder="验证码"></i-Input>
                   </i-Col>
                   <i-Col span="5">
                     <Button type="primary" v-if="!isSend" style="width:80px" @click="SendCode()">验证码</Button>
                     <Button type="primary" style="width:80px" v-else>{{count}}s</Button>
                   </i-Col>
                 </Row>
             </FormItem>
             <FormItem :prop="onePwd" :label="isForget==true?'新的密码：':'原密码：'" class="pwdFormitem">
                 <i-Input :type="oldtype"  v-model="formInline[onePwd]" :placeholder="isForget==true?'新的密码':'原密码'"></i-Input>
                 <Icon v-if="oldShow" type="md-eye" class="eyeIcon" @click="showOldPwd()"/>
                 <Icon v-else type="md-eye-off" class="eyeIcon" @click="showOldPwd()"/>
             </FormItem>
             <FormItem :prop="secPwd" :label="isForget==true?'确认密码：':'新的密码：'">
                 <i-Input :type="newtype" v-model="formInline[secPwd]" :placeholder="isForget==true?'确认密码':'新的密码'">
                 </i-Input>
                 <Icon v-if="newShow" type="md-eye" class="eyeIcon" @click="showNewPwd()"/>
                 <Icon v-else type="md-eye-off" class="eyeIcon" @click="showNewPwd()"/>
             </FormItem>
             <Button type="primary" long @click="login('formInline')">登录</Button>
          </Form>

        </Modal>
    </div>
</template>

<script>
export default {
  name: 'HEditPasswordFrame',
  props: {
    modShow: {
      type: Boolean,
      default: false
    },
    isEye: {
      type: Boolean,
      default: false
    },
    isForget: {
      type: Boolean,
      default: false
    },
    companyEmail: {
      type: String,
      default: 'companyEmail'
    },
    code: {
      type: String,
      default: 'code'
    },
    onePwd: {
      type: String,
      default: 'onePwd'
    },
    secPwd: {
      type: String,
      default: 'secPwd'
    }
  },
  data() {
    var validateNew = (rule, value, cb) => {
      var pattern = /^(?=.*[0-9].*)(?=.*[A-Z].*)(?=.*[a-z].*).{6,18}$/g
      if (value === '') {
        cb(new Error('请输入新的密码'))
      } else if (!pattern.test(value)) {
        cb(new Error('密码必须包含大写字母,小写字母和数字并且长度在6-18位'))
      } else {
        cb()
      }
    }
    var validateTNew = (rule, value, cb) => {
      var pattern = /^(?=.*[0-9].*)(?=.*[A-Z].*)(?=.*[a-z].*).{6,18}$/g
      if (value === '') {
        cb(new Error('请再次输入新的密码'))
      } else if (!pattern.test(value)) {
        cb(new Error('密码必须包含大写字母,小写字母和数字并且长度在6-18位'))
      } else if (value !== this.formInline.onePwd && this.isForget === true) {
        cb(new Error('两次输入的新密码不一致'))
      } else {
        cb()
      }
    }
    return {
      modalShow: false,
      isSend: false,
      timer: null,
      count: 0,
      formInline: {},
      shower: false,
      oldShow: false,
      newShow: false,
      newtype: 'password',
      oldtype: 'password',
      ruleInline: {
        companyEmail: [{ required: true, message: '请输入腾讯企业邮箱地址', trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
        onePwd: [{ required: true, message: this.isForget === true ? '请输入新的密码' : '请输入原密码', trigger: 'blur' },
          { validator: validateNew, trigger: 'blur' }],
        secPwd: [{ required: true, message: this.isForget === true ? '请输入确认密码' : '请输入新的密码', trigger: 'blur' },
          { validator: validateTNew, trigger: 'blur' }]
      }
    }
  },
  watch: {
    modShow(val) {
      this.modalShow = val
    }
  },
  created() {},
  methods: {
    login(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('handleLogin', this.formInline)
        }
      })
    },
    SendCode() {
      this.$emit('handleCode')
    },
    // 倒计时功能
    countSum() {
      const TIME_COUNT = 60
      if (!this.timer) {
        this.count = TIME_COUNT
        this.isSend = true
        this.timer = setInterval(() => {
          if (this.count > 0 && this.count <= TIME_COUNT) {
            this.count--
          } else {
            this.isSend = false
            clearInterval(this.timer)
            this.timer = null
          }
        }, 1000)
      }
    },
    // 展示密码
    showOldPwd() {
      this.oldShow = !this.oldShow
      if (this.oldtype === 'password') {
        this.oldtype = ''
      }
    },
    showNewPwd() {
      this.newShow = !this.newShow
      if (this.newtype === 'password') {
        this.newtype = ''
      }
    },
    Close() {
      this.$emit('Close')
    },
    ResetForm (name) {
      this.$refs[name].resetFields()
      this.modalShow = false
      this.$emit('handleReset', this.modalShow)
    }
  }
}
</script>

<style lang="less" scoped>
.ivu-form-item-error-tip {
    position: absolute;
    top: 100%;
    left: 0;
    line-height: 1;
    padding-top: 6px;
    color: #ed4014;
}
.eyeIcon{
    position: absolute;
    right:5px;
    top:10px;
    cursor: pointer;
}
</style>
