<template>
  <Container
    :title="title"
    :scrollY="maskStyle.height < 0"
    @onSafeAreaChange="onSafeAreaChange"
    bgColor="#f8f8f8"
  >
    <div class="vip" v-if="isVip">
      <span>您已解锁本科目所有内容（科目{{ day.time }}天会员）</span>
      <span>有效期至：{{ day.timeStr }}</span>
    </div>
    <uni-collapse
      ref="collapse"
      v-model="value"
      class="class-content"
      @change="onChnage"
      v-show="!showContainer"
    >
      <view class="free-content">
        <Tree
          :chaptersList="freeChaptersList"
          @onChnage="onChnage"
          @onClickButton="onClickButton"
        />
      </view>
      <!-- 付费功能 -->
      <view class="pay-content" v-if="chaptersList.length">
        <view
          class="modal-mask"
          :style="{
            height: `${maskStyle.height}px`,
            width: `100vw`,
          }"
          v-if="maskStyle.height > 0"
        >
          <view class="locked">
            <uni-icons type="locked" color="#fff" size="35"></uni-icons>
          </view>
          <view class="modal-wrapper" @click="onClickMask">邀请好友可解锁</view>
        </view>
        <Tree
          :chaptersList="chaptersList"
          @onChnage="onChnage"
          @onClickButton="onClickButton"
        />
      </view>
    </uni-collapse>
    <Modal
      ref="popup"
      background-color="#fff"
      v-model:open="showContainer"
      title="邀请新客户解锁课程"
    >
      <text class="unlock"
        >邀请一名新用户注册并完成第一章全部考点学习，可解锁新两章内容</text
      >
      <template #footer>
        <button>立即邀请</button>
      </template>
    </Modal>
  </Container>
</template>

<script setup name="regulations">
import Tree from "../../components/Tree/Tree.vue";
import Container from "../../components/Container/Container.vue";
import Modal from "@/components/Modal/Modal.vue";
import { ref, getCurrentInstance } from "vue";
import { getRect, arrayToTree } from "../../utils";
import { getRoute, router } from "../../utils/router";
import { request } from "../../utils/request";
import { onShow } from "@dcloudio/uni-app";
const title = ref("");
const collapse = ref(null);
const popup = ref(null);
const showContainer = ref(false);
const value = ref("");
const instance = getCurrentInstance();
const safeArea = ref({});
const maskStyle = ref({
  height: 0,
  width: 0,
});
const isVip = ref(0);
const chaptersList = ref([]);
const freeChaptersList = ref([]);
const day = ref({
  time: 0,
  timeStr: "",
});

// 点击学习按钮
const onClickButton = (item) => {
  router.push({
    url: item.practice
      ? "/pages/regulations/practice"
      : "/pages/regulations/learing",
    params: {
      id: item.id,
      parent_id: item.parent_id,
      name: item.name,
      title: item.name,
    },
  });
};

const resolveHeight = () =>
  setTimeout(() => {
    getRect({
      name: ".free-content",
      instance,
      onSuccess(res) {
        maskStyle.value.height = safeArea.value.height - res.height;
      },
    });
    // 动画300需要等待
  }, 800);

const onSafeAreaChange = (s) => {
  safeArea.value = s;
  resolveHeight();
};

const onChnage = () => {
  resolveHeight();
};

const onClickMask = () => {
  router.push({
    url: "/pages/user/share",
  });
};

onShow(async () => {
  const id = getRoute().params.id;
  request("api/question_bank/question_reception/share_subject_vip/info", {
    subject_id: id,
  }).then((res) => {
    isVip.value = res.data.is_vip;
    const { vip_end_time, vip_start_time } = res.data;
    day.value = {
      // 计算有多少天
      time: (vip_end_time - vip_start_time) / 60 / 60 / 24,
      // 格式化 'yyyy-dd-mm'
      timeStr: new Date(vip_end_time * 1000)
        .toLocaleDateString()
        .replace(/\//g, "-"),
    };
  });
  const res = await request(
    "api/question_bank/question_reception/chapter/get_all_chapter",
    {
      id,
    }
  );
  const list = arrayToTree({
    list: res.data,
    firstId: +id,
  });
  const freeList = list.filter((item) => !item.is_lock);
  const payList = list.filter((item) => item.is_lock);
  // 免费
  freeChaptersList.value = freeList;
  // 付费章节
  chaptersList.value = payList;
  title.value = getRoute().params.title;
});
</script>

<style scoped lang="scss">
@import "@/uni.scss";

.text {
  font-family: PingFang SC, PingFang SC;
  font-weight: 500;
  font-size: 28rpx;
  color: #000000;
}

.vip {
  width: 100%;
  box-sizing: border-box;
  border-radius: 10rpx;
  background: url(https://openwork-oss.oss-cn-shenzhen.aliyuncs.com/uploads/question/2025/06/CAQLLie6UhF9ti3PEzDc1EYlP27oUJpBsY5aTDDe.png);
  margin-bottom: 16px;
  padding: 8rpx 16rpx;
  display: flex;
  flex-direction: column;
  gap: 5rpx;
  font-weight: bold;
  font-size: 28rpx;
  color: #ffffff;
  background-size: 100% 100%;
}

.content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8rpx 20rpx 10rpx 40rpx;
  .buttons {
    display: flex;
    gap: 10rpx;
  }
  .comment {
    width: 132rpx;
    height: 48rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #fff;
    color: $primary;
    border: 1px solid $primary;
    font-family: "PingFang SC, PingFang SC";
    font-weight: 500;
    font-size: 28rpx;
    color: $primary;
    border-radius: 4rpx;
  }

  .comment.success {
    color: $success;
    border: 1px solid $success;
  }

  .comment.warning {
    color: $warning;
    border: 1px solid $warning;
  }
}

.unlock {
  font-family: PingFang SC, PingFang SC;
  font-weight: 500;
  font-size: 28rpx;
  color: #000000;
  margin: 100rpx 0;
  display: block;
}

.class-content {
  height: 100%;
}

.pay-content {
  flex: 1;
  position: relative;

  .modal-mask {
    background-color: rgba($color: #333333, $alpha: 0.8);
    position: absolute;
    height: 100%;
    z-index: 9999;
    left: -24rpx;
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: center;
    gap: 34rpx;
  }
}

.modal-wrapper {
  width: 685rpx;
  height: 64rpx;
  background: $primary;
  font-family: PingFang SC, PingFang SC;
  font-weight: 500;
  font-size: 28rpx;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}

.locked {
  width: 96rpx;
  height: 96rpx;
  background: linear-gradient(159deg, #00d0ff 0%, #008cff 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
