<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>
      <view class="x-bc pt8" v-if="listData.length > 0 && current === 0">
        <checkbox-switch v-model="allChecked">全选</checkbox-switch>
        <view class="pl16">
          <up-button size="small" style="width: 60px" type="primary" text="确认" @click="handleSubmit"></up-button>
        </view>
      </view>
    </u-sticky>
    <scroll-view style="height: 82vh" scroll-y scroll-with-animation @scrolltolower="handleScrolltolower">
      <u-checkbox-group placement="column" v-model="detailIds">
        <view class="x-f border-b" v-for="(item, index) in listData">
          <u-checkbox v-show="current === 0" :name="item.id"></u-checkbox>
          <view class="data-item">
            <view
              >危化品名称：<text>{{ item.infomationName }}</text></view
            >
            <view
              >使用人：<text>{{ item.userName }}</text></view
            >
            <view
              >使之前量：<text>{{ item.beforeQuantity }}</text></view
            >
            <view
              >本次用量：<text>{{ item.operationQuantity }}</text></view
            >
            <view
              >余量：<text>{{ item.afterQuantity }}</text></view
            >
            <view
              >说明：<text>{{ item.reason }}</text></view
            >
            <view
              >使用日期：<text>{{ nx.$dayjs(item.useDate).format('YYYY-MM-DD') }}</text></view
            >
          </view>
        </view>
      </u-checkbox-group>
      <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 {
      supervisorId: userInfo.value.id,
      reviewStatus: '0',
      pageSize: 999
    }
  } else {
    return {
      supervisorId: userInfo.value.id,
      reviewStatus: '1',
      pageSize: 999
    }
  }
})
const { listData, scrollToLower, loadStatus, getInitData } = useListData({
  searchParams,
  api: nx.$api.material.getUseRecord
})

onShow(() => {
  getInitData()
})
function handleScrolltolower() {
  scrollToLower()
}
function handleSubmit() {
  if (detailIds.value.length === 0) {
    return uni.showToast({
      title: '请选择要确认的使用记录',
      icon: 'none'
    })
  }
  nx.$api.material.confirmUseRecord(detailIds.value).then(res => {
    uni.showToast({
      title: '确认成功',
      icon: 'none'
    })
    getInitData()
  })
}
</script>

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