<template>
  <view>
    <CuNavbar title="合同管理">
      <template #right>
        <view @click="goPath('/pages/contract/create')" class="text-24rpx text-white">上传</view>
      </template>
    </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="statusFun(item.workflow_check.check_status,'statusExpense','name')"
            :status-color="statusFun(item.workflow_check.check_status,'statusExpense','color')"
            :images="item.images"
            :body="[
              // { label: '创建时间', value: item.created_format }
            ]"
            @click.stop="applyDetail(item)"
          />
        </view>
      </template>
    </MescrollItem>

    <MescrollItem
      ref="mescrollItem1"
      :top="88"
      :i="1"
      :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.name"
            :status-text="statusFun( item.check_status,'statusExpense2','name')"
            :status-color="statusFun( item.check_status,'statusExpense2','color')"
            :images="item.check.subject.images"
            :body="[
              // { label: '创建人', value: item.check.subject.employee.name },
              // { label: '创建时间', value: item.check.subject.created_format }
            ]"
            @click.stop="checkDetail(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 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: '/agreements',
  },
  {
    name: '合同审核',
    apiUrl: '/workflow',
    params: {
      subject_type: 'agreements',
      include: 'check.subject.employee'
    },
  },
])

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

const goPath = (url) => {
  uni.navigateTo({
    url,
  })
}
const tabChange = ({ index }) => {
  tabIndex.value = index
  scrollToLastY()
}

const applyDetail = (item) => {
  uni.navigateTo({
    url: `/pages/contract/detail?id=${item.id}`,
  })
}

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