<template>
  <view>
    <view class="userInfo">
      <image
        class="headIcon"
        :src="inviterInfo?.inviteAvatar ? inviterInfo?.inviteAvatar : defaultAvatar"
        mode="scaleToFill"
      />
      <text class="name">{{ inviterInfo?.inviteUserName }} </text>
      <text class="info">({{ maskPhone(inviterInfo?.invitePhone) }})</text>
    </view>
    <view class="reminder">
      {{ inviterInfo?.inviteUserName }}
      {{
        inviterInfo.bindType === 1
          ? '邀请您一起绑定学小乐学习机，了解大宝的学习情况'
          : '邀请您一起绑定学小乐学习机，管理设备使用情况'
      }}
    </view>
    <view v-if="inviterInfo.bindType === 1" class="binding_info">
      <view class="title">亲子关系</view>
      <view class="relationship">
        <view class="user">
          <image
            :src="inviterInfo?.childAvatar ? inviterInfo?.childAvatar : defaultAvatar"
            mode="scaleToFill"
            class="avatar"
          />
          <text class="accountNumber">{{ inviterInfo?.childName }}</text>
        </view>
        <image class="gling" :src="`${OSS_URL}/urm/bindDevice/gling.png`" mode="scaleToFill" />
        <view class="user">
          <image
            :src="chooseRelation?.icon ? chooseRelation?.icon : lightDefaultAvatar"
            mode="scaleToFill"
            class="avatar"
          />
          <view @click="chooseRelative">
            <view v-if="chooseRelation?.name" class="accountNumber">{{
              chooseRelation?.name
            }}</view>
            <view v-else class="choose_iden"
              >请选择<image
                :src="`${OSS_URL}/iconfont/down_arrow.png`"
                mode=""
                class="arrow_icon"
              />
            </view>
          </view>
        </view>
      </view>
    </view>
    <view v-else class="binding_info">
      <view class="title">设备信息</view>
      <view class="equipment">
        序列号：
        <text class="equipment_serial"> {{ inviterInfo?.simSerialNumber }} </text>
      </view>
    </view>
    <view class="sub" @click="subFun">{{
      inviterInfo.bindType === 1 ? '申请绑定孩子账号' : '申请绑定设备'
    }}</view>
    <CustomPopup
      v-model="showCustomPopup"
      titleText="温馨提示"
      cancelBtnText=""
      @handleConfirmBtn="handleConfirmPopup"
    >
      <view class="popup_box">
        {{ customPopupType === 0 ? tipText : '已提交申请，请耐心等待管理员审核！' }}
      </view>
    </CustomPopup>
  </view>
</template>

<script setup lang="ts">
import { onLoad, onShow, onUnload } from '@dcloudio/uni-app';
import { ref } from 'vue';
import { getInviteInfoApi, parentBindApply, adminInfo } from '@/api';
import { maskPhone } from '@/hooks';
import CustomPopup from '@/components/CustomPopup/index.vue';
const OSS_URL = import.meta.env.VITE_OSS_HOST;
const defaultAvatar = `${OSS_URL}/urm/default_avatar.png`;
const lightDefaultAvatar = `${OSS_URL}/urm/default_avatar_light.png`;
const bg = `url(${OSS_URL}/urm/invite_tip_bg.png)`;
const showCustomPopup = ref(false);
const tipText = ref('您已绑定该学生账号！不用重复绑定哦～');
const customPopupType = ref<number>(0); // 0-已绑定 1-未绑定已提交
const inviterInfo = ref<any>({}); // 邀请信息
const applyBindInfo = ref<any>({}); // 申请绑定

const chooseRelation = ref<any>({}); // 家长选择关系

function subFun() {
  if (inviterInfo.value.bindFlag === 1) {
    showCustomPopup.value = true;
    customPopupType.value = 0;
    tipText.value =
      inviterInfo.value.bindType === 1
        ? '您已绑定该学生账号！不用重复绑定哦～'
        : '您已绑定该设备！不用重复绑定哦～';

    return;
  }
  if (inviterInfo.value.bindType === 0) {
    // 设备
    applyBindInfo.value.deviceDto = {
      simSerialNumber: inviterInfo.value.simSerialNumber,
      parentId: inviterInfo.value.inviteUserId,
    };
  } else {
    // 孩子
    applyBindInfo.value.childDto = {
      childId: inviterInfo.value.childId,
      identityType: chooseRelation.value.id,
      parentId: inviterInfo.value.inviteUserId,
    };
  }
  if (inviterInfo.value.bindType === 1 && !chooseRelation.value.id) {
    uni.showToast({
      title: '请选择亲子关系',
      icon: 'none',
    });
    return;
  }

  parentBindApply(applyBindInfo.value).then(res => {
    showCustomPopup.value = true;
    customPopupType.value = 1;
    // uni.showToast({ title: '已提交申请，请耐心等待管理员审核！', icon: 'none' });
  });
}
// 选择关系
function chooseRelative() {
  if (inviterInfo.value.bindFlag === 1) {
    showCustomPopup.value = true;
    customPopupType.value = 0;
    tipText.value = '您已绑定该学生账号！不用重复绑定哦～';
    return;
  }
  uni.navigateTo({
    url: '/pages/mine/inviteBind/relationShips',
  });
}
function handleConfirmPopup() {
  showCustomPopup.value = false;
}
// 获取邀请信息
async function getInviteInfo(id: string) {
  try {
    uni.showLoading({
      title: '加载中...',
    });
    const { data } = await getInviteInfoApi(id);
    inviterInfo.value = data;
  } catch (err) {
    console.log('邀请绑定错误', err);
  } finally {
    uni.hideLoading();
  }
}

onLoad(option => {
  getInviteInfo(option.id);
});
onShow(() => {
  uni.$on('updateChooseRelation', data => {
    chooseRelation.value = JSON.parse(decodeURIComponent(data.relation));
  });
});
onUnload(() => {
  uni.$off('updateChooseRelation');
});
</script>

<style lang="scss" scoped>
.binding_info {
  margin: 50rpx 30rpx 0 30rpx;
  padding: 30rpx 69rpx;
  background-color: #fff;
  border-radius: 20rpx;

  .title {
    margin-bottom: 30rpx;
    text-align: center;
    color: rgba(51, 51, 51, 1);
    font-weight: 500;
    font-size: 30rpx;
    line-height: 45rpx;
  }

  .equipment {
    color: rgba(134, 134, 134, 1);
    font-size: 28rpx;
    line-height: 60rpx;
    margin-left: -39rpx;

    &_serial {
      color: rgba(51, 51, 51, 1);
    }
  }
  .relationship {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;

    .user {
      flex: 1;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      margin-top: 30upx;

      .avatar {
        width: 100rpx;
        height: 100rpx;
      }
      .accountNumber {
        margin-top: 20upx;
        font-size: 30upx;
        color: rgba(102, 102, 102, 1);
        max-width: 182rpx;
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
      }
      .choose_iden {
        display: flex;
        align-items: center;
        padding: 0 12rpx;
        margin-top: 10rpx;
        background-color: rgba(249, 249, 251, 1);
        border-radius: 6rpx;
        font-size: 30rpx;
        font-weight: 400;
        line-height: 32px;
        color: rgba(169, 170, 181, 1);
        .arrow_icon {
          width: 30rpx;
          height: 30rpx;
          margin-left: 30rpx;
          transform: rotate(-90deg);
        }
      }
    }

    .gling {
      width: 40upx;
      height: 8upx;
    }
  }
}

.userInfo {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;

  .headIcon {
    width: 120upx;
    height: 120upx;
    margin-top: 50upx;
    border-radius: 50%;
  }
  .name {
    font-size: 32upx;
    color: rgba(51, 51, 51, 1);
    margin-top: 20upx;
  }
  .info {
    margin-top: 10upx;
    font-size: 28upx;
    color: rgba(169, 170, 181, 1);
  }
}
.reminder {
  width: 604rpx;
  height: 146rpx;
  margin: 20rpx auto 50rpx;
  padding: 36rpx 30rpx 20rpx 30rpx;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  background-image: v-bind(bg);
  background-size: 100%;
  font-size: 30upx;
  color: rgba(102, 102, 102, 1);
}

.sub {
  width: 690upx;
  height: 90upx;
  border-radius: 20upx;
  color: #fff;
  font-size: 30upx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 120upx;
  background-color: rgba(97, 93, 255, 1);
  margin-left: 30upx;
}
.popup_box {
  margin: 50rpx auto;
  width: 468rpx;
  text-align: left;
}
:deep(.custom_popup .popup_box .footer .confirm_btn) {
  color: #333 !important;
}
</style>
