<template>
  <div style="width: 100%;background: #fff;min-height: 800px;position: relative;"
       class="userCenterSecuritySettingChangePhone">
    <div
        style="height: 80px;line-height: 80px;margin-top: 20px;margin: 0 20px; color: #666;font-size: 15px; border-bottom: 1px solid #bcbcbc;">
      {{ $t('common.Change Mobile Phone Number') }}
      <!-- <span style="float: right;margin-top: 27px;"> <i class="el-icon-edit"></i> 修改</span> -->
    </div>
    <div style="margin-top: 100px;margin-left: 236px;" v-if="active==1 || active==2">
      <el-steps :space="200" :active="active" finish-status="success">
        <el-step :title="$t('common.VerifyIdentidy')"></el-step>
        <el-step :title="$t('common.ModifyPhoneNumber')"></el-step>
        <el-step :title="$t('common.CompleteReplacement')"></el-step>
      </el-steps>
    </div>
    <!-- 验证身份 -->
    <div style="margin-left: 350px;margin-top: 25px;" v-if="active==1">
      <div style="font-size: 18px;color: #666;font-weight: 700;">
        {{ $t('common.remarkSecPhone1') }}：{{ this.salfPhone ? this.salfPhone : $t('common.nothing') }}
      </div>
      <div style="font-size: 14px;color: #999;margin-top: 10px;margin-left: 5px;">{{ $t('common.remarkSecPhone2') }}
      </div>
      <el-form ref="form" :model="form" label-width="160px" class="demo-ruleForm"
               style="margin-left: -177px;margin-top: 40px;width: 700px;">
        <el-form-item :label="$t('common.VerificationCode')">
          <el-input v-model="form.vervification1" style="width: 50%;" maxlength="10" show-word-limit>

          </el-input>
          <span v-show="show" @click="getCode" style="margin-left: 20px;color: #2c5589;"
                class="cursor">{{ $t('common.getVerificationCode') }}</span>
          <span v-show="!show" class="count"
                style="margin-left: 20px;color: #999;">{{ count }} {{ $t('common.SendInSeconds') }}</span>
        </el-form-item>
      </el-form>
    </div>
    <!-- 修改手机号码 -->
    <div style="width: 600px;" v-if="active==2">
      <el-form ref="form" :model="form" label-width="160px" class="demo-ruleForm"
               style="margin-left: 120px;margin-top: 40px;width: 700px;">
        <el-form-item :label="$t('common.MobilePhone')" prop='mobilePhone' style="width: 70%;" maxlength="10"
                      show-word-limit>
          <GlobalRoaming ref='GlobalRoaming' :size="{width:'760px'}" v-model="form.mobilePhone"
                         @getAreaCode="getPassAreaCode"></GlobalRoaming>
        </el-form-item>
        <el-form-item :label="$t('common.VerificationCode')">
          <el-input v-model="form.vervification2" style="width: 70%;" maxlength="10" show-word-limit>

          </el-input>
          <span v-show="show" @click="getCode" style="margin-left: 20px;color: #2c5589;"
                class="cursor">{{ $t('common.getVerificationCode') }}</span>
          <span v-show="!show" class="count"
                style="margin-left: 20px;color: #999;">{{ count }}{{ $t('common.SendInSeconds') }}</span>
        </el-form-item>
      </el-form>
    </div>
    <div style="width: 600px;margin-left: 425px;margin-top: 150px;color: #666;" v-if="active==3">
      <div>
        <i class="el-icon-mobile-phone" style="font-size: 65px;"></i>
      </div>
      <div style="margin-left: -67px;font-size: 26px;line-height: 56px;">
        {{ $t('common.remarkSecPhone3') }}
      </div>
      <div style="margin-left: -79px;color: #999;">
        {{ $t('common.remarkSecPhone4') }}
      </div>
    </div>
    <div style="height: 100px;">
      <div style="margin-top: 50px;">
        <el-button type="primary" style="margin-left: 310px;width: 296px;" @click="goAithentication">
          {{ active == 3 ? $t('common.return') : $t('common.next') }}
        </el-button>
      </div>
    </div>
    <div style="height: 180px;position: absolute;bottom: 20px;left: 0;width: 100%;">
      <div class="securBox" :style="active==1?'height:170px':'height:129px'">
        <div class="div">{{ active == 1 ? $t('common.CommonProblem') : $t('common.SecurityTips') }}</div>
        <div v-if="active==1" class="div1">{{ $t('common.remarkSecPhone5') }}</div>
        <div class="div2">{{ active == 1 ? $t('common.remarkSecPhone6') : $t('common.remarkSecPhone7') }}</div>
        <div v-if="active==1" class="div1">{{ $t('common.remarkSecPhone8') }}</div>
        <div v-if="active==1" class="div2">
          {{ $t('common.remarkSecPhone9') }}{{ $t('common.remarkSecPhone10') }}{{ $t('common.remarkSecPhone11') }}
        </div>
        <div v-else class="div2">{{ $t('common.remarkSecPhone12') }}</div>

      </div>
    </div>
  </div>
</template>
<script>
import Base from "@/views/base/Base";
import GlobalRoaming from "@/components/GlobalRoaming";
import {sendTelCode, getUserInfoById, validCheckCode, updateMobile} from "@/api/user";
import {getUserInfoByTel} from "@/api/login";

export default {
  name: 'userCenterSecuritySettingChangePhone',
  extends: Base,
  components: {GlobalRoaming},
  data() {
    return {
      informationsClass: '1',
      dialogVisible: false,
      realnameAuthenticationPersonal: true,
      ifDiaslog: '',
      phone: '',
      salfPhone: '',
      title: '',
      active: '1',
      show: true,
      isSendCode: false,
      count: '',
      timer: null,
      userInfo: '',
      select: '0086',
      phoneUserNum: '',
      form: {
        mobilePhone: '',
        vervification1: '',
        vervification2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
    }
  },
  mounted() {
    this.initData();//默认消息类型为全部
  },
  methods: {
    initData() {
      getUserInfoById().then(result => {
        if (result) {
          this.phone = result.data.userPreferredMobile;
          this.salfPhone = this.phone.substr(4, 3) + '****' + this.phone.substr(11, 4);
        }
      }).catch((error) => {
        this.msg(error.msg, "error");
      })
    },
    goAithentication() {
      if (this.active == 1) {
        this.validCode();
      } else if (this.active == 2) {
        this.update();
      } else {
        this.$router.go(-1)
      }
    },
    async update() {
      if (!this.form.mobilePhone) {
        this.$message.warning(this.$t('common.Mobilephonenumbercannotbeempty'));
        return;
      }
      /* if(!this.isSendCode){
         this.$message.warning("请先发送验证码！");
         return false;
     }*/
      if (!this.form.vervification2) {
        this.$message.warning(this.$t('common.Verificationcodecannotbeempty'));
        return false;
      }
      this.phoneUserNum = this.select.concat(this.form.mobilePhone);
      await updateMobile(this.phoneUserNum, this.form.vervification2).then(result => {
        if (result.status == 200) {
	        this.msg(result.msg, "success");
          this.active++
        }
      }).catch((error) => {
        this.msg(error.msg, "error");
      })

    },
    validCode() {
      /* if(!this.isSendCode){
          this.$message.warning("请先发送验证码！");
          return false;
      }*/
      if (!this.form.vervification1) {
        this.$message.warning(this.$t('common.Verificationcodecannotbeempty'));
        return false;
      }
      validCheckCode(this.phone, this.form.vervification1).then(result => {
        this.active++;
        this.show = true;
        this.timer = null;
        clearInterval(this.timer);
        this.isSendCode = false;
      }).catch((error) => {
        // this.msg(error.msg, "error");
        this.$message.warning(this.$t('common.Verificationcodeerror'));
      })
    },
    getCode() {
      if (this.active == 2) {
        if (!this.form.mobilePhone) {
          this.$message.warning(this.$t('common.Mobilephonenumbercannotbeempty'));
          return;
        }
        this.phoneUserNum = this.select.concat(this.form.mobilePhone);
        this.code(this.phoneUserNum);
      } else {
        this.code(this.phone);
      }
    },
    code(phoneOrEmail) {
      let locale = window.localStorage.getItem('locale');
      //获取验证码信息
      sendTelCode(phoneOrEmail, "TX", locale).then(result => {
        if (result.status == 200) {
          this.isSendCode = true;
	        this.msg(result.msg, "success");
          // this.msg(this.$t('common.Verificationcodehasbeensent'), "success");
        }
      }).catch((error) => {
        this.msg(error.msg, "error");
      })
      const TIME_COUNT = 60;
      if (!this.timer) {
        this.count = TIME_COUNT;
        this.show = false;
        this.timer = setInterval(() => {
          if (this.count > 0 && this.count <= TIME_COUNT) {
            this.count--;
          } else {
            this.show = true;
            clearInterval(this.timer);
            this.timer = null;
          }
        }, 1000)
      }
    },
    getPassAreaCode(code) {
      this.select = code;
      console.log("select=" + this.select);
    },
    msg: function (msg, type) {
      if (msg == 'error') {
        msg = this.$t('common.ThesystemisbusyPleasetryagainlater')
      }
      return this.$message({
        message: msg,
        type: type
      })
    },
  }
}
</script>
<style scoped>
.el-button {
  width: 200px;
  height: 45px;
}

.authenticationBox1 {
  margin-left: 35px;
  margin-right: 150px;
  height: 100px;
  border: 1px solid rgba(153, 153, 153, 1);
  border-radius: 10px;
  margin-top: 50px;
  position: relative;
}

.authenticationBox2 {
  margin: 50px 150px 0 35px;
  height: 100px;
  border: 1px solid rgba(153, 153, 153, 1);
  border-radius: 10px;
  position: relative;
}

.authenticationBox1:hover,
.authenticationBox2:hover {
  border: 1px solid #ff8400;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.userCenterSecuritySettingChangePhone >>> .el-step.is-horizontal .el-step__line {
  top: 24px;
}

.userCenterSecuritySettingChangePhone >>> .el-step__icon {
  width: 50px;
  height: 50px;
  font-size: 25px;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.securBox {
  margin: 25px 20px 20px 20px;
  height: 170px;
  background: #f2f2f2;
  line-height: 29px;
  padding-left: 10px;
}

.securBox .div {
  font-size: 14px;
  color: #666;
  font-weight: 600;
}

.securBox .div1 {
  font-size: 14px;
  color: #666;
}

.securBox .div2 {
  font-size: 14px;
  color: #999;
  margin-left: 20px;
}

.el-form >>> .el-select {
  width: 150px;
}

.el-form >>> .dcp_input .el-input__inner[type="text"] {
  width: 227px !important;
}

.el-form >>> .dcp_input .el-input-group__prepend .el-input__inner[type="text"] {
  width: 120px !important;
}
</style>
