<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="form" @submit.native.prevent>
      <el-form-item prop="account" :label="$t('password.account.title')">
        <el-input v-model="ruleForm.account" :placeholder="$t('password.account.placeholder')">
          <i slot="prefix" class="el-input__icon el-icon-user"></i>
        </el-input>
      </el-form-item>
      <el-form-item prop="code" :label="$t('password.code.title')">
        <el-input v-model="ruleForm.code" :placeholder="$t('password.code.placeholder')">
          <i slot="prefix" class="el-input__icon el-icon-unlock"></i>
          <countdown
            slot="suffix"
            size="mini"
            :disabled="disabledSend"
            @start="sendCodeRequest"
            ref="countdown"
          ></countdown>
        </el-input>
      </el-form-item>
      <el-form-item prop="password" :label="$t('password.newPassword.title')">
        <el-input type="password" v-model="ruleForm.password" :placeholder="$t('password.newPassword.placeholder')">
          <i slot="prefix" class="el-input__icon el-icon-lock"></i>
        </el-input>
      </el-form-item>
      <el-form-item prop="passwordR" :label="$t('password.confirmPassword.title')">
        <el-input
          type="password"
          v-model="ruleForm.passwordR"
          :placeholder="$t('password.confirmPassword.placeholder')"
        >
          <i slot="prefix" class="el-input__icon el-icon-lock"></i>
        </el-input>
      </el-form-item>
      <el-button type="primary" class="submit-button" :loading="loading" @click="handleSubmit">
        {{ $t('password.submitText') }}
      </el-button>
    </el-form>
  </div>
</template>

<script>
import * as api from '@/api/account'
import countdown from './countdown.vue'

export default {
  name: 'Password',
  props: {
    sendcode: { type: Object, default: () => ({}) },
    headers: { type: Object, default: () => ({}) }
  },
  components: { countdown },
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error(this.$t('password.confirmPassword.rule')))
      } else if (value !== this.ruleForm.password) {
        callback(new Error(this.$t('password.confirmPassword.rule2')))
      } else {
        callback()
      }
    }
    return {
      ruleForm: { account: '', code: '', password: '', passwordR: '' },
      rules: {
        account: [{ required: true, message: this.$t('password.account.rule'), trigger: 'blur' }],
        code: [{ required: true, message: this.$t('password.code.rule'), trigger: 'blur' }],
        password: [
          { required: true, message: this.$t('password.newPassword.rule'), trigger: 'blur' },
          { min: 6, max: 20, message: this.$t('password.newPassword.rule2'), trigger: 'blur' }
        ],
        passwordR: [
          { required: true, message: this.$t('password.confirmPassword.rule'), trigger: 'blur' },
          { validator: validatePass, trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  computed: {
    disabledSend() {
      const value = this.ruleForm.account
      return !(/^1[3-9]\d{9}$/.test(value) || /@/.test(value))
    }
  },
  methods: {
    // 提交
    handleSubmit() {
      this.$refs.form.validate().then(this.handleRequest)
    },
    // 接口请求
    handleRequest() {
      this.loading = true
      api.resetPassword(this.ruleForm, { headers: this.headers }).then(response => {
        response.code === 0 ? this.handleSuccess(response) : this.$message.error(response.msg)
        this.loading = false
      })
    },
    // 成功
    handleSuccess(response) {
      this.$alert(this.$t('password.successMessage'), this.$t('password.alertTitle'), {
        type: 'success',
        confirmButtonText: this.$t('password.confirmButtonText'),
        callback: action => {
          this.$router.push('login')
        }
      })
    },
    // 验证码
    sendCodeRequest() {
      const params = Object.assign({}, this.sendcode, { account: this.ruleForm.account })
      api
        .sendCode(params, { headers: this.headers })
        .then(response => {
          console.log(response)
          if (response.code === 0) {
            this.$message({ type: 'success', message: this.$t('password.sendCodeMessage') })
          } else {
            // 停止计时
            this.$refs.countdown.stop()
            this.$message({ type: 'error', message: response.msg })
          }
        })
        .catch(this.$refs.countdown.stop)
    }
  }
}
</script>

<style lang="scss">
.el-form-item {
  margin-bottom: 20px;
}
.el-form-item__label {
  line-height: 24px;
}
</style>
