<template>
  <div>
    <div class="addchild_group" v-if="step==1">
      <img class="bg" src="@/assets/home/bg2.png" alt="">
      <p class="title">集团管理人员认证</p>
      <p class="des">认证完成后可发起集团定制研学活动</p>
      <div class="phone_content">
        <div class="item">
          <p class="phone_title">手机号</p>
          <div class="flex">
            <input class="code phone" type="tel" maxlength="11" v-model="phone" placeholder="请输入您的手机号">
            <span class="getcode" @click="getCode">{{codeText}}</span>
          </div>
        </div>
        <div class="item">
          <p class="phone_title">验证码</p>
          <input class="code" type="text" v-model="code" placeholder="请输入验证码">
        </div>
        <button class="submit" @click="checkPhoneAndCode">下一步(1/2)</button>
      </div>
    </div>

    <div class="addchild_group" v-if="step==2">
      <img class="bg" src="@/assets/home/bg2.png" alt="">
      <p class="title">已查询到相关信息</p>
      <p class="des">请仔细确认个人信息后再认领</p>
      <div class="child_content">
        <div class="school_box">
            <div class="box"  v-for="(v,i) in schoolList" :key="i">
                <div class="box_list">
                    <div class="box_name" style="margin:0 0 20px">
                        <div class="greyFont">学校:</div>
                        <div>{{v.schoolName}}</div>
                    </div>
                    <!-- <div class="box_name" style="margin:12px 0 40px">
                        <div class="greyFont">所在地区:</div>
                        <div>浙江绍兴 越城区</div>
                    </div> -->
                </div>
                <div class="box_check">
                    <van-checkbox v-model="v.isAuth" shape="square"></van-checkbox>
                </div>
            </div>
        </div>
        <button class="submit" @click="claimChild">完成</button>
      </div>
    </div>

    <div class="addchild_group" v-if="step==3">
      <img class="bg" src="@/assets/home/bg2.png" alt="">
      <p class="title">暂无相关信息</p>
      <p class="des">请确认预留手机号码是否正确</p>
      <div class="content">
          <div class="pic">
               <img style="width:100%;height:100%" src="@/assets/Travel/noSchool.png" alt="">
          </div>
        <button class="submit" style="margin-top:50px" @click="handleClose">完成</button>
      </div>
    </div>

  </div>
</template>

<script>

import chooseSchool from '@/views/Home/component/chooseSchool'
var codeInterval;
export default {
  components: { chooseSchool },
  data () {
    return {
      step: 1,
      phone: '',
      codeText: '获取验证码',//获取验证码按钮文字
      code: '',//验证码
      studentName: '',
      showDialog: true,

      claimChildList: [],

      appId: '',
      chooseSchoolisShow: false,

      checked: '',
      schoolList: [
        
      ]
    }
  },

  mounted () {
    let userInfo = localStorage.getItem('userInfo')
    this.userInfo = JSON.parse(userInfo);
  },
  methods: {
    // 获取验证码
    getCode () {
      if (this.codeText != '获取验证码') return;
      if (!this.phone) {
        this.$toast('请输入手机号')
        return;
      }
      if (!this.checkPhone(this.phone)) {
        this.$toast('请输入正确的手机号')
        return;
      }
      this.$toast.loading({
        message: '加载中',
        duration: 0,
        forbidClick: true
      })
      this.http.getMsg({
        phone: this.phone
      }).then((res) => {
        this.$toast.clear()
        if (res.success) {
          let i = 60;
          codeInterval = setInterval(() => {
            if (i == 0) {
              this.codeText = `获取验证码`;
              clearInterval(codeInterval);
              return;
            }
            this.codeText = `重试(${i})`;
            i--
          }, 1000)
        } else {
          this.$toast.fail(res.message)
        }
      })
    },
    // 获取手机号白名单信息
    checkPhoneAndCode () {
      if (!this.phone) {
        this.$toast('请输入手机号')
        return;
      }
      if (!this.checkPhone(this.phone)) {
        this.$toast('请输入正确的手机号')
        return;
      }
      if (!this.code) {
        this.$toast('请输入验证码')
        return;
      }
      this.$toast.loading({
        message: '加载中',
        duration: 0,
        forbidClick: true
      })
      const userInfo = JSON.parse(localStorage.getItem('userInfo'))

      this.yxAxios.post(`${this.baseUrl}/prod/user/info/checkPhoneAndCode`, {
        "code": this.code,
        "phone": this.phone,
        "unionId": userInfo.unionId,
        'requestFlag': 2
      }).then((res) => {
        this.$toast.clear()
        if (res.data.code == 200) {
          if (res.data.data.length == 0) {
            // 白名单无用户
            this.step = 3;
          } else {
            // 白名单有用户
            this.schoolList = res.data.data
            this.step = 2;
          }
        } else if(res.data.code == 200222) {
          // 白名单无用户
            this.step = 3;
        }
        else{
          this.$toast.fail(res.data?.message)
        }
      })
    },

    // 完成
    claimChild () {
      const schoolList = this.schoolList
      let dataList = ''
      schoolList.forEach(item => {
        if(item.isAuth) {
            dataList = dataList + item.schoolName + ','
        }
      })
      if(dataList) {
        dataList = dataList.substring(0 , dataList.length-1);
      }
      console.log(dataList )
      this.$toast.loading({
        message: '加载中...',
        duration: 0,
        forbidClick: true
      })
      this.yxAxios.post(`${this.baseUrl}/prod/user/info/claimSchool?loginMobile=${this.phone}&schoolNames=${dataList}`).then((res) => {
        this.$toast.clear()
        console.log(res.data)
        if (res.data.code == 200) {
          this.$toast.success('认证成功')
          this.$emit('closeJT')
        } else {
          this.$toast.fail(res.data?.message)
        }
      })
    },

    handleClose () {
      this.$emit('closeJT')
    },
    checkPhone (phone) {
      if ((/^1[3456789]\d{9}$/.test(phone))) {
        return true
      } else {
        return false
      }
    },
  },
  destroyed () {
    clearInterval(codeInterval);
  },
}
</script>
<style lang="scss" scoped>
.addchild_group {
  width: 638px;
  height: 836px;
  background: #ffffff;
  border-radius: 24px;
  position: relative;
  .bg {
    width: 100%;
    position: absolute;
    top: 0;
    left: 0;
  }
  .title {
    position: relative;
    width: 100%;
    padding-top: 70px;
    padding-bottom: 8px;
    text-align: center;
    font-size: 34px;
    font-weight: bold;
    color: #0857ff;
  }
  .des {
    position: relative;
    width: 100%;
    box-sizing: border-box;
    padding: 0 15%;
    text-align: center;
    font-size: 24px;
    color: #6596ff;
  }
  .content {
    width: 100%;
    box-sizing: border-box;
    padding: 0 40px;
    position: absolute;
    top: 236px;
    left: 0;
    .name {
      width: 558px;
      height: 50px;
      font-size: 28px;
      color: #000;
      padding-left: 20px;
      box-sizing: border-box;
    }
    .item {
      width: 558px;
      height: 72px;
      border-radius: 36px;
      background: #f7f7f7;
      box-sizing: border-box;
      font-size: 28px;
      padding: 0 32px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 28px;
      p {
        color: #333333;
        &.nodata {
          color: #999999;
        }
      }
      img {
        width: 40px;
      }
      input {
        width: 100%;
        background: transparent;
        border: 0;
        color: #333333;
        padding: 0;
        &::-webkit-input-placeholder {
          color: #999999;
        }
      }
      .btn {
        width: 240px;
        height: 55px;
        float: right;
        font-size: 28px;
        background: linear-gradient(135deg, #cdf8cf 0%, #8af36f 100%);
        box-shadow: 0px 4px 8px 0px rgba(189, 189, 189, 0.5),
          0px 8px 12px 0px rgba(89, 199, 171, 0.5);
        border-radius: 34px;
        border: transparent;
        color: #333333;
      }
    }
    .school_select {
      font-size: 28px;
      padding: 0 32px;
      margin-bottom: 28px;
    }
    .submit {
      width: 294px;
      height: 72px;
      border: 0;
      background: linear-gradient(135deg, #99c2ff 0%, #1f59ff 100%);
      box-shadow: 0px 4px 8px 0px rgba(189, 189, 189, 0.5),
        0px 8px 12px 0px rgba(87, 137, 255, 0.5);
      border-radius: 34px;
      font-size: 34px;
      color: #ffffff;
      margin: 0 auto;
      display: block;
    }
    .pic {
        width: 350px;
        height: 350px;
        margin: auto
    }
  }
  .child_content {
    position: relative;
    margin-top: 40px;
    .child_box {
      height: 500px;
      overflow: auto;
      padding-left: 100px;
      .child_item {
        position: relative;
        margin-bottom: 50px;
        p {
          font-size: 28px;
          padding: 8px 0;
          span {
            display: inline-block;
            width: 140px;
            margin-right: 28px;
            color: #999;
          }
        }
        .checkbox {
          position: absolute;
          top: 115px;
          right: 20px;
        }
      }
    }
    .school_box {
        width: 100%;
        height: 500px;
        overflow: auto;
        font-size: 3.7vw;
        padding: 20px 60px 20px 90px ;
        box-sizing: border-box;

        .box {
            display: flex;
            justify-content: space-between;

            .box_list {
                .box_name {
                    display: flex;

                    .greyFont {
                        // width: 160px;
                        width: 110px;
                        color: #999999;
                    }
                }
            }
            .box_check {

            }
        }
    }
    .submit {
      width: 438px;
      height: 78px;
      background: #3385ff;
      border-radius: 37px;
      border-radius: 39px;
      border: 0;
      font-size: 34px;
      color: #ffffff;
      margin: 0 auto;
      display: block;
    }
  }
  .phone_content {
    width: 100%;
    position: absolute;
    top: 180px;
    left: 0;
    box-sizing: border-box;
    padding: 40px;
    .item {
      border-bottom: 1px solid #e2e2e2;
      margin-bottom: 30px;
    }
    .phone_title {
      font-size: 34px;
      font-weight: bold;
      margin-bottom: 28px;
    }
    .flex {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 22px;
    }
    .code {
      border: 0;
      background: transparent;
      font-size: 28px;
      margin-bottom: 32px;
      width: 100%;
    }
    .phone {
      width: 50%;
      margin: 0;
    }
    .getcode {
      color: #5789ff;
      font-size: 28px;
      padding: 10px;
    }
    .submit {
      width: 438px;
      height: 78px;
      background: #3385ff;
      border-radius: 37px;
      border-radius: 39px;
      border: 0;
      font-size: 34px;
      color: #ffffff;
      margin: 0 auto;
      margin-top: 120px;
      display: block;
    }
  }
}
/deep/.van-search {
  background: transparent;
  padding: 0;
  width: 100%;
}
/deep/.van-search__content {
  padding: 0;
}
.schoolBoxF {
  width: 100vw;
  height: 100vh;
  position: absolute;
  top: 0;
  left: 0;
  background-color: #fff;
}
</style>