<template>
	<layout-page>
		<uni-segmented-control :current="current" :values="controlListText" @clickItem="handleChange" styleType="text" activeColor="#00897B"></uni-segmented-control>
		<scroll-view class="scrollBox defaultBgc" scroll-y="true" @scrolltolower="loadingMore" style="height: calc(100vh - 72rpx);">
		  <view v-if="items.length" class="listBox">
		    <m-list :items="items"></m-list>
		    <uni-load-more :status="more" />
		  </view>
		  <view v-else class="nodata-img-parent">
		    <image
				src="https://minio.citupro.com/dev/static/nodata.png"
				mode="widthFix"
				style="width: 100vw;height: 100vh;"
			></image>
		  </view>
		</scroll-view>
	</layout-page>
</template>

<script setup>
import { ref, watch } from 'vue'
import layoutPage from '@/layout'

import MList from './list'
import { getDict } from '@/hooks/useDictionaries.js'
import { getRecommendationList } from '@/api/position.js'
import { onLoad } from '@dcloudio/uni-app'

import { userStore } from '@/store/user'
const useUserStore = userStore()

watch(() => useUserStore.isLogin, (newVal, oldVal) => {
  if (useUserStore.isLogin) {
		// 监听登录状态
		console.log('重新登录了')
		handleChange({ currentIndex: current.value })
	}
})


const current = ref(0)

// 获取参数
const controlList = ref([])
const controlListText = ref([])
const pageInfo = ref({
	pageNo: 1,
	pageSize: 10
})
const total = ref(0)
const items = ref([])
const loading = ref(false)
const more = ref('more')

async function initDict () {
	try {
		const { data } = await getDict('menduner_hire_job_cv_status')
		if (!data?.data) {
			return
		}
		controlList.value = data.data
		controlListText.value = data.data.map(e => e.label)
		// current.value = +controlList.value[0].value
		init()
	} catch (error) {
		// console.log(error)
	}
}


function handleChange (val) {
	current.value = val.currentIndex
	pageInfo.value.pageNo = 1
	total.value = 0
	items.value = []
	init()
}

function loadingMore () {
	if (total.value === items.value.length) {
		return
	}
	if (loading.value) {
		return
	}
	more.value = 'loading'
  pageInfo.value.pageNo++
	init()
}

async function init () {
	try {
		loading.value = true
		const { data } = await getRecommendationList({
			...pageInfo.value,
			status: current.value
		})
		if (!data?.list) {
			pageInfo.value.pageNo--
			return
		}
		items.value.push(...data.list)
		total.value = +data.total
		more.value = items.value.length === total.value ? 'noMore' : 'more'
	} catch (error) {
		pageInfo.value.pageNo--
	} finally {
		loading.value = false
	}
}

onLoad(async (options) => {
  if (options?.id) {
    current.value = +options.id
	}
	initDict()
})

</script>

<style lang="scss" scoped>

</style>