<route lang="json">
{
  "style": {
    "navigationBarTitleText": "详情",
    "navigationBarBackgroundColor": "#fff",
    "navigationStyle": "custom"
  }
}
</route>
<script setup lang="ts">
import Tag from '@/components/tag.vue'
import {
  createCircleReview,
  getCircle,
  getCircleReviews,
  getCircleUpvotes,
  shareCircle,
} from '../../../core/libs/requests'
import { handleUpvoteClick } from '../../../core/libs/actions'
import CommentItem from '../components/comment-item.vue'
import { useUserStore } from '../../../store'
import { storeToRefs } from 'pinia'
import { isImageOrVideo } from '../../../core/utils/common'
import dayjs from 'dayjs'
import SectionHeading from '@/components/section-heading.vue'
import BottomAppBar from '@/components/bottom-app-bar.vue'
import { likeActived, likeBlack } from '@designer-hub/assets/src/icons'
import NavBarEvo from '@/components/navbar-evo.vue'
import { useRouter } from '../../../core/utils/router'

const userStore = useUserStore()
const { userInfo } = storeToRefs(userStore)
const router = useRouter()
const id = ref()
const { data, run } = useRequest(() => getCircle(id.value), { initialData: {} })
const { data: reviews, run: runGetReviews } = useRequest(
  () => getCircleReviews({ circleId: id.value }),
  {
    initialData: {
      list: [],
    },
  },
)
const { data: circleUpvotes, run: setCircleUpvotes } = useRequest(
  () => getCircleUpvotes(id.value),
  { initialData: { list: [], total: 0 } },
)
const swiperSizes = ref()
const swiperStyle = ref()
const reviewContent = ref('')
const isVideo = ref(false)
const handleChange = ({ detail: { current } }) => {
  swiperStyle.value = {
    height: swiperSizes.value[current].height + 'px',
  }
}
const setSwiperStyle = async () => {
  if (!data.value.bannerUrls.length) return
  const { screenWidth } = await uni.getSystemInfo()
  if (data.value.bannerUrls.length === 1 && isImageOrVideo(data.value.bannerUrls[0]) === 'video') {
    isVideo.value = true
    return
  }
  swiperSizes.value = (
    await Promise.all(data.value.bannerUrls.map((src) => uni.getImageInfo({ src })))
  ).map(({ width, height }) => ({ width: screenWidth, height: height / (width / screenWidth) }))
  swiperStyle.value = {
    height: swiperSizes.value[0].height + 'px',
  }
}
const handleSend = async () => {
  const { code, msg } = await createCircleReview({
    circleId: id.value,
    userId: userInfo.value.userId,
    userName: userInfo.value.nickname,
    reviewContent: reviewContent.value,
  })
  if (code !== 0) {
    uni.showToast({ title: msg, icon: 'none' })
  } else {
    reviewContent.value = ''
    uni.showToast({ title: '评论成功', icon: 'none' })
    await runGetReviews()
  }
}
onMounted(async () => {})
onLoad(async (query: { id: string }) => {
  id.value = query.id
  await run()
  await runGetReviews()
  await setSwiperStyle()
  await setCircleUpvotes()
})
onShareAppMessage(async () => {
  await shareCircle(id.value)
  return { title: data.value?.circleDesc }
})
</script>
<template>
  <view class="bg-white flex-grow">
    <NavBarEvo placeholder>
      <template #prepend>
        <div
          class="flex items-center gap-2"
          @click="() => router.push(`/pages/mine/homepage/index?id=${data.stylistId}`)"
        >
          <wd-img width="24" height="24" round :src="data.headUrl"></wd-img>
          <div class="text-black/90 text-sm font-normal font-['PingFang_SC'] leading-[10.18px]">
            {{ data.stylistName }}
          </div>
        </div>
      </template>
    </NavBarEvo>
    <!-- <div class="my-4 text-black/90 text-lg font-normal font-['PingFang_SC'] leading-[10.18px]">
      {{ data?.detailsDesc }}
    </div> -->
    <template v-if="swiperSizes && !isVideo">
      <div>
        <swiper class="" :style="swiperStyle" @change="handleChange">
          <template v-for="it of data?.bannerUrls" :key="it">
            <swiper-item>
              <wd-img width="100%" :src="it" mode="widthFix"></wd-img>
            </swiper-item>
          </template>
        </swiper>
      </div>
    </template>
    <template v-if="isVideo">
      <video width="100%" class="w-full aspect-[1.64/1]" :src="data?.bannerUrls[0]"></video>
    </template>
    <!-- <wd-swiper
      v-model="current"
      custom-class="my-1"
      autoplay="false"
      :list="data?.images"
      :indicator="{ type: 'fraction' }"
      indicatorPosition="top-right"
      imageMode="widthFix"
    ></wd-swiper> -->
    <view class="m-3.5">
      <div class="text-black/90 text-base font-normal font-['PingFang_SC'] leading-[10.18px]">
        {{ data?.circleDesc }}
      </div>
      <view class="my-5.5 flex gap-3.5 flex-wrap">
        <!-- <TiltedButton>按钮</TiltedButton> -->
        <template v-if="data?.tagName !== ''">
          <template v-for="it of data?.tagName?.split(',')" :key="it">
            <Tag>{{ it }}</Tag>
          </template>
        </template>
      </view>
      <div class="text-black/30 text-xs font-normal font-['PingFang_SC'] leading-[10.18px]">
        {{ dayjs(data.createTime).format('YYYY-MM-DD HH:mm') }}
      </div>
      <!-- <view class="flex items-center my-4">
        <view class="flex items-center">
          <avatar-group-casual
            :show-number="3"
            :urls="[
              'https://via.placeholder.com/20x20',
              'https://via.placeholder.com/20x20',
              'https://via.placeholder.com/20x20',
            ]"
          ></avatar-group-casual>
          <div
            class="ml-1 text-black/60 text-sm font-normal font-['PingFang_SC'] leading-[10.18px]"
          >
            {{ circleUpvotes.total }}人赞过
          </div>
        </view>
        <view class="flex-1"></view>
        <view><wd-icon class="text-black/65" name="arrow-right" size="22px"></wd-icon></view>
      </view> -->
      <div class="h-0.25 bg-[#dadada] my-7"></div>
      <SectionHeading :title="`评论`" size="base">
        <template #append>
          <view v-if="reviews?.list" class="flex">
            <div class="text-black/90 text-xs font-normal font-['PingFang_SC'] leading-[10.18px]">
              按热度
            </div>
            <div
              class="mx-2 text-black/40 text-xs font-normal font-['PingFang_SC'] leading-[10.18px]"
            >
              |
            </div>
            <div class="text-black/40 text-xs font-normal font-['PingFang_SC'] leading-[10.18px]">
              按时间
            </div>
          </view>
        </template>
      </SectionHeading>

      <view clas="mt-8.25">
        <template v-if="reviews?.list.length">
          <template v-for="it of reviews?.list" :key="it.id">
            <CommentItem
              :options="it"
              :isChild="false"
              @upvote="runGetReviews()"
              @delete="runGetReviews()"
            ></CommentItem>
            <!-- <template v-for="child of it.childrens" :key="child.id">
              <CommentItem :options="child" :isChild="true"></CommentItem>
            </template> -->
          </template>
        </template>
        <template v-else>
          <view class="flex items-center justify-center mt-26 mb-36">
            <div class="text-black/40 text-xs font-normal font-['PingFang_SC'] leading-[10.18px]">
              这里空空的
            </div>
            <div
              class="ml-1.5 text-[#2f4471]/90 text-xs font-normal font-['PingFang_SC'] leading-[10.18px]"
            >
              点击评论~
            </div>
          </view>
        </template>
      </view>
    </view>
    <BottomAppBar fixed placeholder border custom-class="">
      <div class="bg-white flex items-center">
        <div class="w-[168px] bg-[#f6f6f6] rounded-[60px] px-3.5 py-2 flex items-center">
          <wd-input
            custom-class="bg-transparent!"
            no-border
            confirm-type="send"
            v-model="reviewContent"
            placeholder="说点什么..."
            @confirm="handleSend"
          ></wd-input>
        </div>
        <view class="flex justify-around flex-1">
          <div>
            <button open-type="share" class="bg-transparent! p-0!">
              <view
                class="flex flex-col items-center text-[rgba(0,0,0,0.85)] text-3.5 font-400 line-height-5.5"
              >
                <div class="w-4.5 h-4.5 flex items-center justify-center">
                  <wd-img width="15" height="15" src="/static/svgs/share.svg"></wd-img>
                </div>
                <view class="">{{ data?.shareCount || 0 }}</view>
              </view>
            </button>
          </div>
          <view
            class="flex flex-col items-center text-[rgba(0,0,0,0.85)] text-3.5 font-400 line-height-5.5"
          >
            <div class="w-4.5 h-4.5 flex items-center justify-center">
              <wd-img width="15" height="15" src="/static/svgs/comment.svg"></wd-img>
            </div>
            <view class="">{{ data?.reviewCount }}</view>
          </view>
          <view
            class="flex flex-col items-center text-[rgba(0,0,0,0.85)] text-3.5 font-400 line-height-5.5"
            @click="
              handleUpvoteClick(
                {
                  upvote: data.ownUpvote,
                  circleId: data.id,
                  userId: userInfo.userId,
                  userName: userInfo.nickname,
                },
                () => run(),
              )
            "
          >
            <template v-if="data.ownUpvote">
              <wd-img width="18" height="18" :src="likeActived"></wd-img>
            </template>
            <template v-else>
              <wd-img width="18" height="18" :src="likeBlack"></wd-img>
            </template>
            <view>{{ data.upvoteCount }}</view>
          </view>
        </view>
      </div>
    </BottomAppBar>
  </view>
</template>
