<template>
  <div>
    <el-form label-position="top" :model="ruleForm" :rules="rules" ref="form" :hide-required-asterisk="true" @submit.prevent autocomplete="off">
      <el-form-item prop="account" :label="$t('password.account.title')">
        <el-input v-model="ruleForm.account" :placeholder="$t('password.account.placeholder')">
          <template #prefix>
            <el-icon class="el-input__icon"><User /></el-icon>
          </template>
        </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')">
          <template #prefix>
            <el-icon class="el-input__icon"><Unlock /></el-icon>
          </template>
          <template #suffix>
            <countdown size="small" :disabled="disabledSend" @start="sendCodeRequest" ref="countdown"></countdown>
          </template>
        </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')" autocomplete="new-password">
          <template #prefix>
            <el-icon class="el-input__icon"><Lock /></el-icon>
          </template>
        </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')">
          <template #prefix>
            <el-icon class="el-input__icon"><Lock /></el-icon>
          </template>
        </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'
import { User, Lock, Unlock } from '@element-plus/icons-vue'

export default {
  name: 'AppPassword',
  props: {
    sendcode: { type: Object, default: () => ({}) },
    headers: { type: Object, default: () => ({}) }
  },
  components: { countdown, User, Lock, Unlock },
  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() {
      this.$alert(this.$t('password.successMessage'), this.$t('password.alertTitle'), {
        type: 'success',
        confirmButtonText: this.$t('password.confirmButtonText'),
        callback: () => {
          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>
