<template>
  <div class="login d-flex justify-center">
    <div class="d-flex flex-column box">
      <!-- 修改密码 -->
      <div class="d-flex justify-center my-4">
        <a-button class="d-flex flex-column baseColor" type="link">
          <span>修改密码</span>
          <div class="head-top mt-1"></div>
        </a-button>
      </div>
      <!-- 修改密码 -->
      <a-row class="d-flex flex-nowrap mt-4" type="flex">
        <a-col :span="8" class="explain" flex="auto">
          <a-form :form="form" @submit="handleChangePassword">
            <a-form-item
              :label-col="formItemLayout.labelCol"
              :wrapper-col="formItemLayout.wrapperCol"
              label="手机号"
            >
              <a-input
                @change="changePhone"
                placeholder="请输入手机号"
                type="tel"
                v-decorator="['phone', { rules: phoneRules }]"
              />
            </a-form-item>
            <a-form-item
              :label-col="formItemLayout.labelCol"
              :wrapper-col="formItemLayout.wrapperCol"
              label="图片验证码"
              required
            >
              <div class="d-flex flex-nowrap">
                <a-input placeholder="请输入图片验证码" required type="number" v-model="codeNum" />
                <img
                  :src="picCode.imageBase64"
                  @click="changePicCode"
                  class="code_img ml-2"
                  v-if="picCode && picCode.imageBase64"
                />
                <a-button
                  @click="changePicCode"
                  class="code_img ml-2"
                  size="small"
                  type="primary"
                  v-else
                >重新获取</a-button>
              </div>
            </a-form-item>
            <a-form-item
              :label-col="formItemLayout.labelCol"
              :wrapper-col="formItemLayout.wrapperCol"
              label="短信验证码"
            >
              <div class="d-flex flex-nowrap">
                <a-input placeholder="请输入验证码" type="number" v-model="code" />
                <a-button
                  class="ml-2"
                  disabled
                  type="primary"
                  v-if="showInterval"
                >重新发送 {{ interval }}</a-button>
                <a-button
                  :disabled="phone && phone.length !== 11"
                  @click="getCode"
                  class="ml-2"
                  type="primary"
                  v-else
                >获取验证码</a-button>
              </div>
            </a-form-item>
            <a-form-item
              :label-col="formItemLayout.labelCol"
              :wrapper-col="formItemLayout.wrapperCol"
              label="新密码"
            >
              <a-input-password
                placeholder="请输入您设置的新密码"
                v-decorator="['credential', { rules: passwordRules }]"
              />
            </a-form-item>
            <div class="d-flex flex-row-reverse">
              <a-button block class="my-5" html-type="submit" style="width: 75%" type="primary">确认修改</a-button>
            </div>
          </a-form>
          <div class="d-flex flex-row-reverse mt-1">
            <div class="d-flex flex-wrap" style="width: 75%">
              <router-link tag="span" to="/register">
                <span class="baseColor">新用户注册</span>
              </router-link>
              <div class="flex-1"></div>
              <router-link tag="span" to="/login">
                <span class="textColor">返回登录</span>
              </router-link>
            </div>
          </div>
        </a-col>
        <a-col :span="4" flex="150px">
          <div class="d-flex flex-column">
            <div>说明</div>
            <div>1.登录账号XXX</div>
          </div>
        </a-col>
      </a-row>
    </div>
  </div>
</template>

<script>
import { mapActions, mapState } from 'vuex';
import mixin from './mixin';

const formItemLayout = {
  labelCol: { span: 6 },
  wrapperCol: { span: 18 },
};
const formTailLayout = {
  labelCol: { span: 6 },
  wrapperCol: { span: 18, offset: 6 },
};

export default {
  name: 'ForgetPassword',
  mixins: [mixin],
  data() {
    return {
      formItemLayout,
      formTailLayout,
      form: this.$form.createForm(this, { name: 'forget-password' }),
      credential: '',
      phone: '',
      code: '',
      codeNum: '',
    };
  },

  computed: mapState('user', ['picCode']),

  created() {
    this.sendPicCode();
  },

  methods: {
    ...mapActions('user', ['changePassword', 'sendCode', 'sendPicCode']),
    changePhone(e) {
      this.phone = e.target.value;
    },

    // 刷新验证码
    changePicCode() {
      this.sendPicCode();
    },

    // 获取验证码
    async getCode() {
      try {
        const params = {
          phone: this.phone,
          verificationCodeId: this.picCode.verificationCodeId,
          verificationCodeValue: this.codeNum,
        };
        await this.sendCode(params);
        this.getCodeInterval();
      } catch (error) {
        throw new Error(`ForgetPassword.vue method getCode: ${error}`);
      }
    },

    // 修改密码
    async handleChangePassword(e) {
      e.preventDefault();
      this.form.validateFields(async (err, values) => {
        if (!err) {
          console.log('Received values of form: ', values);
          try {
            const { phone, credential } = values;
            const { code, type } = this;
            const params = { code, password: credential, phone };
            console.log('params: ', params);
            await this.changePassword(params);
            this.$router.push('/login');
          } catch (error) {
            console.error(error);
          }
        }
      });
      return;
    },
  },
};
</script>

<style lang="stylus" scoped>
.login {
  min-height: 500px;
  max-width: 1260px;
  margin: 24px auto 40px;
  background: white;
}

.head-top {
  width: 58px;
  height: 2px;
  background: #13acc4;
}

.box {
  max-width: 700px;
  width: 50%;
}

.explain {
  padding-right: 28px;
  margin-right: 28px;
  border-right: 1px solid #EEEEEE;
}

.code_img {
  width: 102px !important;
  height: 32px !important;
}
</style>
