<template>
  <navbar-back title="库存盘点"></navbar-back>
  <view class="p8">
    <u-sticky>
      <up-subsection
        activeColor="#0055A2"
        mode="subsection"
        :list="list"
        :current="current"
        @change="changeTab"
      ></up-subsection>
    </u-sticky>
    <scroll-view style="height: 82vh" scroll-y scroll-with-animation @scrolltolower="handleScrolltolower">
      <view class="x-f border-b" v-for="(item, index) in listData" @click="handleClickItem(item)">
        <view class="data-item">
          <view
            >标题：<text>{{ item.title }}</text></view
          >
          <view
            >盘点人：<text>{{ item.operatorName }}</text></view
          >
          <view
            >盘点时间：<text>{{ nx.$dayjs(item.applyTime).format('YYYY-MM-DD HH:mm:ss') }}</text></view
          >
          <view
            >盘点部门：<text>{{ deptName(item) }}</text></view
          >
          <view
            >盘点事由：<text>{{ item.reason }}</text></view
          >
        </view>
      </view>
      <up-loadmore v-if="listData.length > 0" :status="loadStatus" />
      <up-empty v-else mode="data" text="暂无数据" marginTop="50"> </up-empty>
    </scroll-view>
  </view>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { useListData } from '@/nx/hooks/usePageListData'
import { onShow } from '@dcloudio/uni-app'
import nx from '@/nx'

const userInfo = computed(() => nx.$store('user').userInfo)
const list = ref(['待盘点', '已盘点'])
const allChecked = ref(false)
const detailIds = ref([])
watch(allChecked, value => {
  if (value) {
    detailIds.value = listData.value.map(item => item.id)
  } else {
    detailIds.value = []
  }
})
const current = ref(0)

function changeTab(index) {
  current.value = index
  getInitData()
}
const searchParams = computed(() => {
  if (current.value === 0) {
    return {
      businessType: '库存盘点',
      operatorId: userInfo.value.id,
      progress: 'in_progress'
    }
  } else {
    return {
      businessType: '库存盘点',
      operatorId: userInfo.value.id,
      progress: 'completed'
    }
  }
})
const { listData, scrollToLower, loadStatus, getInitData } = useListData({
  searchParams,
  api: nx.$api.material.getLifecycleList
})

onShow(() => {
  getInitData()
})

function handleScrolltolower() {
  scrollToLower()
}

const deptName = item => {
  if (item.formData) {
    let formData = JSON.parse(item.formData)
    return formData.departmentName
  }
  return ''
}

function handleClickItem(item) {
  nx.$router.go('/pages/material/inventoryCheck/check', { id: item.id, current: current.value })
}
</script>

<style lang="scss" scoped>
.data-item {
  padding: 8px;
  border-bottom: 1px solid #eee;
  color: #909399;

  text {
    color: #000;
  }
}
</style>
