<template>
  <view>
    <CuNavbar title="我的任务"></CuNavbar>
    <uv-sticky bgColor="#fff">
      <uv-tabs
        height="44"
        :activeStyle="{ color: '#ee2c37' }"
        :scrollable="false"
        :current="tabIndex"
        lineColor="#ee2c37"
        :list="tabList"
        @change="tabChange"
      ></uv-tabs>
    </uv-sticky>

    <MescrollItem
      ref="mescrollItem0"
      :top="88"
      :i="0"
      :index="tabIndex"
      :apiUrl="tabList[0].apiUrl"
    >
      <template v-slot="{ list }">
        <view class="space-y-15rpx p-base">
          <ListItem
            v-for="item in list"
            :key="item.id"
            :title="item.name"
            :status-text="item.taskable.status_text"
            :status-color="statusFun(item.taskable.status, 'statusExpense' ,'color')"
            :body="[
              {label: '任务时间: ', value: item.start_format + '-' + item.end_format}
            ]"
            @click.stop="applyClick(item)"
          />
        </view>
      </template>
    </MescrollItem>
    <MescrollItem
      ref="mescrollItem1"
      :i="1"
      :top="88"
      :index="tabIndex"
      :apiUrl="tabList[1].apiUrl"
      :params="tabList[1].params"
    >
      <template v-slot="{ list }">
        <view class="space-y-15rpx p-base">
          <ListItem
            v-for="item in list"
            :key="item.id"
            :title="item.check.subject.task.name"
            :status-text="statusFun(item.check_status,'statusExpense2','name')"
            :status-color="statusFun(item.check_status,'statusExpense2','color')"
            :body="[
              {label: '任务时间: ', value: item.check.subject.task.start_format + '-' + item.check.subject.task.end_format}
            ]"
            @click.stop="checkClick(item)"
          />
        </view>
      </template>
    </MescrollItem>
  </view>
</template>
<script setup>
import CuNavbar from '@/components/cu-navbar/index'
import { computed, reactive, ref } from 'vue'
import { onPageScroll, onReachBottom, onLoad } from '@dcloudio/uni-app'
import useMescrollMore from '@/uni_modules/mescroll-uni/hooks/useMescrollMore.js'
import MescrollItem from '@/components/mescroll-api/more.vue'
import statusFun from '@/utils/status'
import { timeFormat } from '@climblee/uv-ui/libs/function/index'
import ListItem from '@/components/list-item/index'

const mescrollItem0 = ref(null)
const mescrollItem1 = ref(null)

const mescrollItems = [mescrollItem0, mescrollItem1]
const { tabIndex, getMescroll, scrollToLastY } = useMescrollMore(
  mescrollItems,
  onPageScroll,
  onReachBottom
)
const tabList = ref([
  {
    name: '任务列表',
    apiUrl: '/tasks',
  },
  {
    name: '任务审核',
    apiUrl: '/workflow',
    params: {
      subject_type: 'task_hygienes',
      include: 'check.subject.task'
    },
  },
])

onLoad(() => {
  uni.$on('refresh', () => {
    mescrollItem0?.value?.getMescroll()?.resetUpScroll()
    mescrollItem1?.value?.getMescroll()?.resetUpScroll()
  })
})

const tabChange = ({ index }) => {
  tabIndex.value = index
  scrollToLastY()
}

const applyClick = (item) => {
  const type = item.taskable_type
  const status = item.taskable.status
  let url
  if (status === 2 || status == 4) {
    url = `/pages/task/${type}_submit?id=${item.id}`
  } else {
    url = `/pages/task/detail?id=${item.id}`
  }
  uni.navigateTo({
    url: url,
  })
}

const checkClick = (item) => {
  uni.navigateTo({
    url: `/pages/audits/detail?id=${item.id}&type=${item.check.subject_type}`,
  })
}
</script>
