<!-- 招聘会职位列表 / 招聘会内的企业下-职位列表 -->
<!-- 没有企业ID的就是招聘会下的职位列表。存在企业ID的就是招聘会企业下的职位列表。 -->
<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>
        <!-- 轮播图 -->
        <SwiperAd v-if="swiperAdList.length" :list="swiperAdList" margin="0" borderRadius="0" @click="handleToDetails"></SwiperAd>
        <view class="stick" :style="`background-color: ${backgroundColor}`">
          <!-- 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="index+val"
              @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>
          <!-- entName 企业》企业内职位列表 -->
          <view v-if="entName" class="enterpriseName" :style="`color: ${entNameColor}`">{{ entName }}</view>
        </view>
        <view v-if="listData?.length" class="listDataBox">
          <PositionList
            :list="listData"
            :noMore="false"
            :jobFairId="query.jobFairId"
            :showUpdateTime="false"
            :noDataTextColor="textColor"
          ></PositionList>
          <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': '暂无数据，请切换类型查看~'}" />
        </view>
      </view>
    </scroll-view>
    <view class="shareButtonBox" @tap="handleShare">
      <uni-icons type="redo-filled" size="20" color="#fff" />
    </view>
  </view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app'
import { ref, reactive, computed } from 'vue'
import { dealDictObjData } from '@/utils/position'
import { getJobFairEntJobPage, getJobFair } from '@/api/jobFair'
import PositionList from '@/components/PositionList'
import SwiperAd from '@/components/SwiperAd'
import { getShareQueryById } from '@/api/jobFair.js'

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

const entName = ref('')

let obj = {}
onLoad(async (options) => {
  console.log('options:', options)
  // 网站二维码分享
	if (options.scene) {
    const scene = decodeURIComponent(options.scene)
    const key = scene.split('=')[1]
    console.log('key:', key)
    if (!key) return
    const res = await getShareQueryById({key})
    console.log('res:', res)
    options.jobFairId = res?.data?.jobFairId
    options.enterpriseId = res?.data?.enterpriseId
  }
  if (options?.jobFairId) {
    query.jobFairId = options.jobFairId
    if (options.enterpriseId) {
      query.enterpriseId = options.enterpriseId
      entName.value = options.entName
      getEntPositionList()
    } else {
      getJobFairDetail()
    }
  }
  if (options.backgroundColor || obj?.backgroundColor) backgroundColor.value = options.backgroundColor || obj?.backgroundColor
})

// 招聘会详情
const tabIndex = ref(-1)
const tabList = ref([])
const swiperAdList = ref([])
const backgroundColor = ref('#fff')
const textColor = computed(() => {
  return backgroundColor.value === '#fff' ? '#777' : '#fff'
})
const entNameColor = computed(() => {
  return backgroundColor.value === '#fff' ? '#00B760' : '#fff'
})
const getJobFairDetail = async () => {
  if (!query.jobFairId) return
  const { data } = await getJobFair(query.jobFairId)
  // tab
  if (data?.tag?.length) {
    tabList.value = data?.tag || []
    if (tabList.value?.length) handClickTab(0)
  }
  // 轮播图
  if (data?.headImg?.length) {
    swiperAdList.value = data.headImg
  }
  // 背景色
  if (data?.backgroundColour) {
    backgroundColor.value = data.backgroundColour || '#fff'
  }
}

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

const getEntPositionList = async () => {
  if (!query.jobFairId) {
    uni.showToast({ title: '获取企业岗位失败，请重试！', icon: 'none', duration: 2000 })
    return
  }
  try {
    const params = { ...query }
    // tab对应的职位类型id列表
    const idList = tabIndex.value !== -1 ? tabList.value[tabIndex.value]?.value : []
    idList?.length && idList.forEach((value, index) => { params[`positionId[${index}]`] = value })
    //
    const res = await getJobFairEntJobPage(params)
    const list = res?.data?.list || []
    list.forEach(e => {
      e.job = dealDictObjData({}, e)
      e.enterprise = { ...e.enterprise, ...dealDictObjData({}, e.enterprise)}
    })
    listData.value = listData.value.concat(list)
    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++
  getEntPositionList()
}

// const goBack = () => {
// 	uni.navigateTo({
// 		url: '/pagesB/jobFair/index'
// 	})
// }

const handleShare = () => {
  // 分享招聘会
  let url = `/pagesB/jobFair/${query.enterpriseId ? 'jobFairEntShare' : 'jobFairShare'}?jobFairId=${query.jobFairId}`
  // 分享招聘会企业
  if (query.enterpriseId) url = url + `&enterpriseId=${query.enterpriseId}`
  // 
	uni.navigateTo({ url })
}

</script>

<style scoped lang="scss">
.stick {
  z-index: 1;
  position: sticky;
  top: 0;
}
.shareButtonBox {
  z-index: 1;
  position: fixed;
  right: 20px;
  top: 16px;
  border-radius: 50%;
  padding: 8px;
  background-color: #00B760;
}
.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;
}
.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;
    }
  }
}

.enterpriseName {
  position: relative;
  padding: 18px 30rpx;
  padding-left: 39px;
  height: 24px;
  line-height: 24px;
  color: #fff;
  font-size: 18px;
  font-weight: bold;
  &::before {
    display: block;
    content: '';
    width: 4px;
    height: 24px;
    background-color: #fff;
    position: absolute;
    top: 20px;
    left: 25px;
    border-radius: 2px;
  }
}
</style>
