<template>
  <div class="boundtel-container">
    <div class="contenttitle">
      <span v-if="!istelshow">绑定手机</span>
      <span v-else>已绑定手机</span>
    </div>
    <div class="contentbody" v-if="!istelshow">
      <div class="warninginfo">
        您的账号可能存在安全风险，为了确保为您本人操作，请先进行安全验证
      </div>
      <div class="tel">
        <div>手机号</div>
        <input
          type="text"
          v-model.trim="usertelinfo.tel"
          maxlength="11"
        /><br />
        <input type="text" v-model.trim="usertelinfo.code" maxlength="4" />
        <span class="getrulecode"
          ><span
            @click="getPhoneCode"
            data-mark="getcode"
            v-if="timedata.isfinish"
            >获取验证码</span
          ><timmer :timedata="timedata" v-if="!timedata.isfinish"
        /></span>
      </div>
      <button @click="submit">确定</button>
    </div>
    <div class="contentbody" v-else>
      <div>{{ userinfo.hidetel }}</div>
      <button style="width: 106px; margin-top: 16px" @click="istelshow = false">
        更换手机号
      </button>
    </div>
  </div>
</template>

<script>
import { getPhoneCodeApi } from "@/api/login.js";
import { setUserTelApi } from "../../../../api/userinfo";

import timmer from "../../../Login/components/countback.vue";

export default {
  // 组件名称
  name: "",
  // 局部注册的组件
  components: {
    timmer,
  },
  // 组件参数 接收来自父组件的数据
  props: {
    userinfo: {
      require: true,
    },
  },
  // 组件状态值
  data() {
    return {
      usertelinfo: {
        tel: "",
        code: "",
      },
      timedata: {
        isfinish: true,
        deadline: 60,
        timmer: null,
      },
      istelshow: false,
    };
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 生命周期钩子   注：没用到的钩子请自行删除
  /**
   * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
   */
  created() {
    if (this.userinfo.tel) {
      this.istelshow = true;
      console.log(this.istelshow);
    }
  },
  /**
   * el 被新创建的 vm.el 替换，并挂载到实例上去之后调用该钩子。
   * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.el 也在文档内。
   */
  mounted() {},
  // 组件方法
  methods: {
    async submit() {
      try {
        var reg_tel =
          /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
        if (this.usertelinfo.tel.length === 0) {
          this.$message.warning("请输入手机号");
        } else if (!reg_tel.test(this.usertelinfo.tel)) {
          this.$message.warning("请输入正确的手机号");
        } else if (this.usertelinfo.code.length === 0) {
          this.$message.warning("请输入验证码");
        } else {
          this.timedata.isfinish = false;
          const { data } = await setUserTelApi(this.usertelinfo);
          console.log(data);
          if (data.status === 200) {
            this.$message.success("绑定手机成功");
          }
        }
      } catch (error) {
        console.log(error);
      }
    },
    async getPhoneCode() {
      // 获取手机验证码
      var reg_tel =
        /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      if (this.usertelinfo.tel.length === 0) {
        this.$message.warning("请输入手机号");
      } else if (!reg_tel.test(this.usertelinfo.tel)) {
        this.$message.warning("请输入正确的手机号");
      } else {
        this.timedata.isfinish = false;
        const { data } = await getPhoneCodeApi({ tel: this.usertelinfo.tel });
        console.log(data);
      }
    },
  },
};
</script>

<style scoped lang="less">
.boundtel-container {
  .warninginfo {
    font-size: 14px;
    color: #4d575e;
  }
  .tel {
    position: relative;
    margin-top: 24px;
    > div {
      font-size: 16px;
      margin-bottom: 8px;
      line-height: 16px;
    }
    input {
      outline: none;
      width: 430px;
      height: 40px;
      text-indent: 10px;
      margin-bottom: 16px;
      font-size: 16px;
      background: #fff;
      border: 1px solid #dddddd;
      border-radius: 4px;
    }
    .getrulecode {
      padding: 0 0 0 10px;
      position: absolute;
      width: 89px;
      font-size: 14px;
      text-align: center;
      border-left: 1px #cccccc solid;
      line-height: 14px;
      color: #4d575e;
      right: 390px;
      bottom: 30px;
      div {
        margin-bottom: 0;
      }
      span[data-mark="getcode"] {
        cursor: pointer;
      }
    }
  }
  button {
    width: 64px;
    height: 32px;
    background: #ff6a00;
    border-radius: 4px;
    border: none;
    font-size: 14px;
    color: #fff;
    &:hover {
      background-color: #fe8034;
    }
  }
}
</style>
