<!-- 招聘会/企业详情 -->
<template>
  <view class="box" :style="`background-color: ${backgroundColor}`">
    <scroll-view class="scrollBox" :scroll-y="true" :scroll-top="scrollTop" @scrolltolower="loadingMore" @scroll="onScroll" style="position:relative;">
      <view style="position: relative;">
        <!-- 轮播图 -->
        <SwiperAd v-if="swiperAdList.length" :list="swiperAdList" margin="0" borderRadius="0" @click="handleToDetails"></SwiperAd>
        <view class="stick ss-p-y-30" :style="`background-color: ${backgroundColor}`">
          <view style="position: relative;">
            <uni-search-bar
              v-model="query.keyword"
              placeholder="输入关键字"
              cancelButton="none"
              :focus="false"
              bgColor="#fff"
              @confirm="onSearch($event.value)"
              @clear="query.keyword = ''; onSearch()"
            >
            </uni-search-bar>
            <button class="search-btn" @tap.stop="onSearch">搜索</button>
          </view>
          <!-- tab页签 -->
          <scroll-view v-if="tabList?.length" scroll-x="true" class="scroll-container">
            <view
              class="scroll-item"
              :style="`margin-left: ${index ? '24px' : ''};`"
              v-for="(val, index) in tabList" :key="val.key"
              @tap="handClickTab(index)"
            >
              <view>
                <view class="text">{{ val.title }}</view>
                <view v-if="index === tabIndex" class="choose" style="background-color: #fff;"></view>
                <view v-else class="choose" style="background-color: #ffffff00;"></view>
              </view>
            </view>
          </scroll-view>
        </view>
        <view v-if="listData?.length" class="listDataBox">
          <uni-card v-for="val in listData" :key="val.id" @click="toDetail(val)" :is-shadow="true" :border='false' shadow="0px 0px 3px 1px rgba(0,0,0,0.1)">
            <view class="d-flex align-center ss-m-30" @click="null">
              <image class="enterAvatar" :src="val.logoUrl ? val.logoUrl : 'https://minio.citupro.com/dev/menduner/company-avatar.png'"></image>
              <view class="ss-m-l-20" style="flex: 1;">
                <view class="font-size-16 enterpriseName">{{ formatName(val.anotherName || val.name) }}</view>
                <view class="ss-m-t-5">
                  <span class="color-999">{{ val?.industryName || '' }}</span>
                  <span class="divider tag-gap1" v-if="val?.industryName && val?.scaleName"> | </span>
                  <span class="color-999">{{ val?.scaleName || '' }}</span>
                </view>
                <view class="ss-m-t-10" style="overflow: hidden; height: 57px;">
                  <uni-tag 
                    v-for="(job,i) in val?.lastJobTop5 || []"
                    :key="i"
                    :text="formatName(job.name)"
                    inverted="false"
                    size="default"
                    custom-style="background-color: #00B760; color: #fff; border-color: #00B760; display: inline-block; border-radius: 15px; margin: 0 5px 5px 0;"
                  />
                </view>
              </view>
            </view>
            <view class="jobCount">{{ val.jobCount }}个在线职位招聘中 >>></view>
          </uni-card>
          <uni-load-more :status="more" :color="textColor" />
        </view>
        <view v-else class="nodata-img-parent">
          <uni-load-more class="ss-m-t-50" :color="textColor" status="noMore" :content-text="{'contentnomore': loading ? '加载中. . .' : tabList?.length ? '暂无数据，请切换类型查看~': '暂无数据'}" />
        </view>
        <!-- 招聘会分享按钮 -->
        <view v-if="showShareBtn" class="shareButtonBox" @tap="handleShare">
          <uni-icons type="redo-filled" size="30" color="#00B760" />
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script setup>
import { onLoad, onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'
import { ref, reactive, computed } from 'vue'
import { dealDictArrayData } from '@/utils/position'
import { getJobFairEnterprisePage, getJobFair } from '@/api/jobFair'
import SwiperAd from '@/components/SwiperAd'
import { formatName } from '@/utils/getText'
import { getShareQueryById } from '@/api/jobFair.js'

const loading = ref(true)
const more = ref('more')
const listData = ref([])
const query = reactive({
  pageSize: 20, 
  pageNo: 1,
  keyword: '',
  jobFairId: undefined,
})

const showShareBtn = ref(false)

onLoad(async (options) => {
  // 网站二维码分享
	if (options.scene) {
    const scene = decodeURIComponent(options.scene)
    const key = scene.split('=')[1]
    if (!key) return
    const res = await getShareQueryById({key})
    options.jobFairId = res?.data?.jobFairId
  }
  if (options?.jobFairId) {
    query.jobFairId = options.jobFairId
    getJobFairDetail()
	}
  
  // 转发朋友
  onShareAppMessage(() => {
    if(!jobFairTitle.value){
      setTimeout(() => {}, 1000)
    }
    return {
      title: jobFairTitle.value,
      path: `/pagesB/jobFair/enterprisesClassification?jobFairId=${options?.jobFairId}`
    }
  })
  // 转发朋圈
  onShareTimeline(() => {
    if(!jobFairTitle.value){
      setTimeout(() => {}, 1000)
    }
    return {
      title: jobFairTitle.value || '门墩儿 专注顶尖招聘',
      path: `/pagesB/jobFair/enterprisesClassification?jobFairId=${options?.jobFairId}`
    }
  })
})

// 招聘会详情
const tabIndex = ref(-1)
const tabList = ref([])
const swiperAdList = ref([])
const backgroundColor = ref('#fff')
const jobFairTitle = ref('')
const textColor = computed(() => {
  return backgroundColor.value === '#fff' ? '#777' : '#fff'
})
const getJobFairDetail = async () => {
  if (!query.jobFairId) return
  const { data } = await getJobFair(query.jobFairId)
  // 类型 -1为不传tag参数
  tabList.value = data?.tag || []
  handClickTab(tabList.value?.length ? 0 : -1)
  // 轮播图
  if (data?.headImg?.length) {
    swiperAdList.value = data.headImg
  }
  // 背景色
  if (data?.backgroundColour) {
    backgroundColor.value = data.backgroundColour || '#fff'
  }
  if (data?.title) {
    jobFairTitle.value = data.title
  }
  showShareBtn.value = Boolean(data?.shareImg)
}
getJobFairDetail()

// 切换类型
const handClickTab = (index) => {
  tabIndex.value = index
  query.pageNo = 1
  listData.value = []
  getEnterpriseList()
}

const onSearch = () => {
  query.pageNo = 1
  listData.value = []
  getEnterpriseList()
}

const getEnterpriseList = async () => {
  if (!query.jobFairId) return
  try {
    const params = { ...query }
    // tab对应的职位类型id列表
    const idList = tabIndex.value !== -1 ? tabList.value[tabIndex.value]?.content : []
    idList?.length && idList.forEach((value, index) => { params[`enterpriseId[${index}]`] = value.value })
    //
    const res = await getJobFairEnterprisePage(params)
    const list = res?.data?.list || []
    // list.push(...[].concat(...Array(10).fill(list))) // 测试滚动效果
    listData.value = listData.value.concat(dealDictArrayData([], list))
    loading.value = false
    if (listData.value?.length === +res?.data?.total) {
      more.value = 'noMore'
      return
    }
  } catch (error) {
    query.pageNo--
    more.value = 'more'
  }
}

const scrollTop = ref(0)
const old = ref({
  scrollTop: 0
})
const onScroll = (e) =>{
  old.value.scrollTop = e.detail.scrollTop
}

// 加载更多
const loadingMore = () => {
  more.value = 'loading'
  query.pageNo++
  getEnterpriseList()
}

const toDetail = (item) =>{
  if (!item?.id || !(item?.jobFairId ?? query.jobFairId)) return
  let url = `/pagesB/jobFair/positionClassification?jobFairId=${query.jobFairId || item.jobFairId}&enterpriseId=${item.id}&entName=${item.anotherName}`
  url = url + `&backgroundColor=${backgroundColor.value}`
  uni.navigateTo({ url })
}
// const goBack = () => {
// 	uni.navigateTo({
// 		url: '/pagesB/jobFair/index'
// 	})
// }

// 分享招聘会
const handleShare = () => {
	uni.navigateTo({
    url: `/pagesB/jobFair/jobFairShare?jobFairId=${query.jobFairId}`
	})
}
</script>

<style scoped lang="scss">
.stick {
  z-index: 1;
  position: sticky;
  top: 0;
}
.shareButtonBox {
  z-index: 1;
  position: absolute;
  right: 20px;
  top: 16px;
  border-radius: 8px;
  padding: 7px 9px;
  background-color: #fff;
}
.box {
  height: 100vh;
  overflow: hidden;
  // padding-bottom: 120rpx;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
}
.listDataBox {
  // padding: 1px 0 120rpx;
  padding-bottom: 120rpx;
  margin: 0 5rpx;
  margin-top: -10px;
  .enterpriseName {
    color: #404040;
    font-weight: 700;
  }
  .enterAvatar {
    width: 60px;
    height: 60px;
    // border-radius: 50%;
    margin: auto;
  }
  .jobCount {
    height: 40px;
    line-height: 40px;
    color: #00B760;
    text-align: center;
    // padding: 0 50rpx;
    font-size: 15px;
    // background: linear-gradient(to right, #12ebb0, #7ec04c);
    background: #F5F5F5;
  }
}
.scrollBox{
  flex: 1;
  height: 0 !important;
  padding-bottom: 24rpx;
  box-sizing: border-box;
}

// :deep(.uni-load-more__text) {
//   color: #fff !important;
// }

:deep(.uni-card) {
  padding: 0 !important;
  .uni-card__content {
    padding: 0 !important;
  }
}

.scroll-container {
  width: calc(100vw - 20px);
  padding: 0 20rpx;
  white-space: nowrap; /* 确保子元素在一行内排列 */
  .scroll-item {
    display: inline-block; /* 子元素内联块显示 */
    color: #fff;
    font-size: 17px;
    font-weight: 500;
    .text {
      padding: 20px 2px 0;
    }
    .choose {
      width: 28px;
      height: 2px;
      border-radius: 8px;
      margin: 8px auto;
    }
  }
}

:deep(.uni-searchbar) {
  // background-color: #fff !important;
  padding: 0;
  margin: 0 10px;
  border-radius: 5px;
}

:deep(.uni-searchbar__box) {
  width: calc(100% - 100px);
  height: 40px !important;
  // border: 1px solid #00B760;
  padding-right: 20px;
  flex: none;
  background-color: #fff;
}

.search-btn {
  position: absolute;
  right: 13px;
  top: 3px;
  width: 106px;
  height: 34px;
  line-height: 34px;
  font-size: 16px;
  // margin: 4px 4px 4px 0;
  background-color: #00B760;
  color: #fff;
  border-radius: 5px;
  z-index: 9;
}
</style>
