<template>
  <view>
    <uni-segmented-control :current="current" :values="controlList" @clickItem="handleChange" styleType="text" activeColor="#00B760"></uni-segmented-control>
    <scroll-view class="scrollBox defaultBgc" scroll-y="true" @scrolltolower="loadingMore" style="height: calc(100vh - 36px);">
      <view v-if="items.length">
        <PositionList v-if="current === 0" class="pb-10" :list="items" :noMore="false"></PositionList>
        <Items v-else class="pb-10" :list="items" @action="handleAction"></Items>
        <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>

    <!-- 同意、拒绝面试 -->
    <uni-popup ref="popup" type="dialog">
      <uni-popup-dialog :type="type === 'agree' ? 'success' : 'warn'" cancelText="取消" confirmText="确认" 
        title="系统提示" :content="type === 'agree' ? '确认接受面试吗？' : '确认拒绝面试吗？'" @confirm="handleConfirm" @close="handleClose"
      ></uni-popup-dialog>
    </uni-popup>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import { getJobDeliveryList, getUserInterviewInvitePage, userInterviewInviteConsent, userInterviewInviteReject } from '@/api/user'
import { dealDictObjData } from '@/utils/position'
import PositionList from '@/components/PositionList'
import Items from './item.vue'
import { userStore } from '@/store/user'
import { onLoad } from '@dcloudio/uni-app'

const useUserStore = userStore()
const current = ref(0)
const controlList = ['已投递', '待同意', '待面试', '已完成', '已拒绝']
const statusList = [0, 1, 3, 98]

const more = ref('more')
const items = ref([])
const queryParams = ref({
  pageNo: 1,
  pageSize: 10
})
const popup = ref()
const type = ref('')
const id = ref(null)

onLoad((options) => {
  if (options?.index) {
    current.value = Number(options.index)
    items.value = []
  }
  getList()
})

const getList = async () => {
  const api = current.value === 0 ? getJobDeliveryList : getUserInterviewInvitePage
  if (current.value !== 0) queryParams.value.status = statusList[current.value - 1]
  const { data } = await api(queryParams.value)
  const list = data?.list || []
  if (!list.length && queryParams.value.pageNo === 1) {
    items.value = []
    return
  }
  if (list?.length) {
    list.forEach(e => {
      e.job = { ...e.job, ...dealDictObjData({}, e.job) }
      e.enterprise = { ...e.enterprise, ...dealDictObjData({}, e.enterprise)}
    })
    items.value = items.value.concat(list)
  }
  more.value = items.value?.length === +data.total ? 'noMore' : 'more'
}

const handleChange = (e) => {
  items.value = []
  queryParams.value.pageNo = 1
  current.value = e.currentIndex
  getList()
}

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

// 同意、拒绝
const handleAction = (item, typeVal) => {
  id.value = item.id
  type.value = typeVal
  popup.value.open()
}

const handleClose = () => {
  popup.value.close()
  type.value = ''
  id.value = null
}

const handleConfirm = async () => {
  if (!id.value) return
  const api = type.value === 'agree' ? userInterviewInviteConsent : userInterviewInviteReject
  // 同意需提交手机号
  let phone = ''
  if (useUserStore?.baseInfo?.phone) phone = useUserStore?.baseInfo?.phone
  await api(type.value === 'agree' ? { id: id.value, phone } : id.value)
  handleClose()
  uni.showToast({
    title: '操作成功',
    icon: 'success'
  })
  queryParams.value.pageNo = 1
  items.value = []
  getList()
}
</script>

<style scoped lang="scss">

</style>
