<script setup lang="ts">
import { getImageUrl, returnAppPage } from "@/utils/index.js";
import { useCustomizeStore } from "@/store";
import { useRouter } from "vue-router";
import { computed, onUnmounted, ref, watch } from "vue";
import { ISingleRecord } from "@/types/customize";
import ShareModal from "@/views/customize/components/ShareModal.vue";
const customizeStore = useCustomizeStore();

const router = useRouter();

const props = defineProps(["isParent", "isMyRoute"]);
const emit = defineEmits(["send", "change"]);

const isShow = ref(false);
const shareShow = ref(false);

const singleRecord = computed<ISingleRecord>(() => {
  return customizeStore.singleRecord;
});

const reportImg = computed(() => {
  return singleRecord.value.joinSemester === 1 ? getImageUrl("joined_report") : getImageUrl("add_report");
});

watch(isShow, (val) => {
  if (val) {
    setTimeout(() => {
      document.addEventListener("click", handleClick);
    }, 10);
  } else {
    document.removeEventListener("click", handleClick);
  }
});

function back() {
  props.isMyRoute === "1" ? router.go(-1) : returnAppPage();
}

function share() {
  console.log("share");
  shareShow.value = true;
}

function handleClick() {
  console.log("handleClick");
  if (isShow.value) isShow.value = false;
}

onUnmounted(() => {
  document.removeEventListener("click", handleClick);
});
</script>

<template>
  <div class="top-safety-distance"></div>
  <div class="header flex-center">
    <div class="header-back" @click="back">
      <img :src="getImageUrl('return_button')" alt="" class="header-back-img" />
    </div>
    <div class="header-send" @click="isShow = true">
      <img :src="getImageUrl('more')" alt="" class="header-send-more" />
    </div>
    <!--<div class="header-send" @click="emit('send')">-->
    <!--  <div class="header-send-btn flex-center">-->
    <!--    {{ singleRecord.sendReport ? "已发送" : "发送家长" }}-->
    <!--  </div>-->
    <!--</div>-->

    <div v-if="isShow" class="header-modal">
      <img :src="getImageUrl('small_triangle')" alt="" class="header-modal-logo" />
      <div class="header-modal-item flex-center" @click="share">分享</div>
      <div
        v-if="!singleRecord.sendReport && !props.isParent"
        class="header-modal-item flex-center"
        @click="emit('send')"
      >
        发送家长
      </div>
      <div class="header-modal-item flex-center" @click="emit('change')">
        <img :src="reportImg" alt="" />
        {{ singleRecord.joinSemester === 1 ? "已加入报告" : "加入报告" }}
      </div>
    </div>
  </div>
  <ShareModal :show="shareShow" @close="shareShow = false" />
</template>

<style scoped lang="scss">
.top-safety-distance {
  width: 100%;
  height: 44px;
}
.header {
  position: relative;
  justify-content: space-between;
  width: 100%;

  &-back {
    padding: 14px 28px;

    &-img {
      width: 15.17px;
      height: 15.17px;
      object-fit: contain;
    }
  }

  &-send {
    padding: 12px 16px;
    // padding: 6px 19px;

    &-more {
      width: 20px;
      height: 20px;
    }

    &-btn {
      width: 78px;
      height: 34px;
      background: #e9f8ff;
      box-shadow: 0 1px 6px 0 rgba(0, 0, 0, 0.2026);
      border-radius: 17px 17px 17px 17px;
      font-size: 14px;
      font-family: PingFang SC;
      font-weight: 600;
      color: #233cd5;
    }
  }

  &-modal {
    position: absolute;
    top: 36px;
    right: 10px;
    z-index: 20;
    padding: 5px 0;
    width: 115px;
    background: #ffffff;
    box-shadow: 0 2px 15px 0 rgba(104, 136, 176, 0.2);
    border-radius: 4px 4px 4px 4px;

    &-logo {
      position: absolute;
      top: -6px;
      right: 8px;
      width: 14px;
      height: 6px;
    }

    &-item {
      width: 115px;
      height: 30px;
      background: #ffffff;
      font-size: 14px;
      font-family:
        PingFang SC-Regular,
        PingFang SC;
      font-weight: 400;
      color: #333333;

      img {
        margin-right: 4px;
        width: 12px;
        height: 12px;
      }
    }

    &-item + &-item {
      border-top: 1px solid rgba(182, 220, 255, 0.1);
    }
  }
}
</style>
