<script lang="ts" setup>
import { fetchCommonOperate } from '@/api/user'

const props = defineProps<{
  grantFlag: boolean
  agreementList: any[]
}>()

const isShowPopup = ref(false)

const checked = ref(false)

async function onSubmit() {
  if (!checked.value)
    return

  const res = await fetchCommonOperate('01')
  if (res) {
    isShowPopup.value = false
    window.localStorage.setItem('grantFlag', 'true')
  }
}

function onHref(url: string) {
  window.location.href = url
}

onMounted(async () => {
  if (!props.grantFlag)
    isShowPopup.value = true
})
</script>

<template>
  <div class="grant-popup-wrap">
    <van-popup
      v-model:show="isShowPopup"
      teleport="body"
      round
      :close-on-click-overlay="false"
      position="bottom"
    >
      <div class="model-desc-pop-inner">
        <!-- <img class="close" src="@/assets/images/close-b.png" alt="" @click="isShowPopup = false"> -->
        <div class="title">
          欢迎您使用晓得AI助手
        </div>
        <div class="content">
          为帮助您更好了解晓得AI 助手服务内容，保障您的合法权益，请您认真阅读《晓得AI助手服务协议》《晓得AI助手隐私政策》，特别是其中有关使用限制、免责声明、个人信息保护等内容。您需在仔细阅读并确认同意相关协议后方可使用本服务。
        </div>
        <div class="radio-wrap">
          <div @click="checked = !checked">
            <SvgIcon v-show="checked" size="20px" name="radioChecked" />
            <SvgIcon v-show="!checked" size="20px" name="radioNoCheck" />
          </div>
          <div>
            我已阅读并同意
            <span v-for="(item, index) in props.agreementList" :key="index" @click="onHref(item.value)">{{ item.key }}</span>
          </div>
        </div>

        <div class="footer-wrap">
          <div class="submit-btn" :class="{ disabled: !checked }" @click="onSubmit">
            同意
          </div>
        </div>
      </div>
    </van-popup>
  </div>
</template>

<style lang="scss" scoped>
.radio-wrap {
  font-family:
    PingFangSC,
    PingFang SC;
  font-weight: 400;
  font-size: 13px;
  color: #8d9795;
  line-height: 20px;
  box-sizing: border-box;
  padding: 0 24px;
  display: flex;
  align-items: flex-start;
  margin: 8px 0;
  > div {
    &:first-child {
      margin-right: 8px;
    }
  }
  span {
    color: #158ad3;
  }
}
.footer-wrap {
  width: 100%;
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
  .submit-btn {
    &.disabled {
      opacity: 0.5;
    }
    width: 280px;
    height: 44px;
    background: #29b6fd;
    border-radius: 22px;
    font-family:
      PingFangSC,
      PingFang SC;
    font-weight: 500;
    font-size: 15px;
    color: #ffffff;
    line-height: 22px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
.model-desc-pop-inner {
  width: 100%;
  height: 100%;
  background-size: 375px 148px;
  background-repeat: no-repeat;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-sizing: border-box;
  padding-top: 38px;
  position: relative;
  .content {
    font-family:
      PingFangSC,
      PingFang SC;
    font-weight: 400;
    font-size: 15px;
    color: #393d46;
    line-height: 22px;
    text-align: justify;
    font-style: normal;
    box-sizing: border-box;
    padding: 24px;
  }
  .close {
    position: absolute;
    width: 16px;
    height: 16px;
    top: 16px;
    right: 16px;
  }
  .title {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    font-family:
      PingFangSC,
      PingFang SC;
    font-weight: 500;
    font-size: 16px;
    line-height: 22px;
    text-align: center;
    font-style: normal;
    position: relative;
    font-family:
      PingFangSC,
      PingFang SC;
    font-weight: 600;
    font-size: 18px;
    color: #222222;
    line-height: 24px;
    text-align: center;
    font-style: normal;
  }
}
</style>
