<script setup>
import { getCurrentInstance, onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';

const {
  proxy
} = getCurrentInstance()

/* 通用方法： 重置list数据 */
const basicReset = () => {
  finished.value = false;
  loading.value = true;
  list.value = []
}

/* 返回上一级页面 */
const router = useRouter()

/* 查询数据 */
const resultData = ref([])
const fetchData = ref({
  ownerName:'',
})

const onSearch = () => {
  basicReset()
  onLoad()
}

const resetCondition = () => {
  basicReset()
  fetchData.value = {
    ownerName:'',
  }
  onLoad()
}

/* 查询请求 */
const queryFetch = async () => {
  const url = '/sgsafe/show/query'
  const param = {
    params: JSON.stringify(fetchData.value)
  }
  try {
    const res = await proxy.$axios.post(url,param);
    if (res.data.code === 0) {
      resultData.value = res.data.data
    } else {
      console.log('操作失败!' + res.data.msg)
    }
  } catch (error) {
    console.error('请求出错:', error);
  }
};

/* 列表加载与下拉刷新 */
const list = ref([]);
const refreshing = ref(false)
const loading = ref(false)
const finished = ref(false)

const onRefresh = () => {
  basicReset()
  onLoad();
};

const onLoad = async () => {
  if (refreshing.value) {
    list.value = [];
    refreshing.value = false;
  }
  try {
    await  queryFetch();
    list.value = [...list.value,...resultData.value ]
    finished.value = true;
  } catch (error) {
    console.log(error);
    finished.value = true;
  } finally {
    loading.value = false;
  }
};

onMounted(() => {
  basicReset()
  onLoad()
})
</script>

<template>
  <div class="page-container">
    <van-sticky>
      <van-nav-bar title="作业情况" />

      <van-search
        v-model="fetchData.ownerName"
        @search="onSearch"
        @clear="resetCondition"
        placeholder="请输入姓名"
      />
    </van-sticky>

    <div class="scroll-container">
      <van-pull-refresh
        v-model="refreshing"
        success-text="刷新成功"
        @refresh="onRefresh"
      >
        <van-list
          class="listDiv"
          :immediate-check="false"
          v-model:loading="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <div v-for="item in list" :key="item.id" class="card">
            <van-cell
              :border="false" is-link
            >
              <template #title>
                <span class="bold-title" style="margin-left: 4px;margin-right: 10px">拥有者姓名：{{ item.ownerName ? item.ownerName : '暂无拥有者' }}</span>

              </template>
              <template #label>
                <div class="label-content">
                  <span>终端编号：{{ item.clientId }}</span>
                  <span>危险区域名称：{{ item.areaName }}</span>
                  <span>进入危险区域时间：{{ item.inDangerStartTime }}</span>
                  <span>上次传回信息时间：{{ item.currentTime }}</span>
                  <span>已停留时间：{{ item.stayTime }}</span>
                </div>
              </template>
            </van-cell>
          </div>
        </van-list>
      </van-pull-refresh>
    </div>
  </div>
</template>

<style scoped>
.page-container {
  height: 100vh; /* 关键：外层容器高度设为视口高度 */
  display: flex;
  flex-direction: column;

}
/*  overflow-y: auto; !* 启用垂直滚动 *!*/


.scroll-container {
  flex: 1;
  overflow: auto;
  -webkit-overflow-scrolling: touch; /* iOS 平滑滚动 */
}

/* 可选：隐藏滚动条（视觉优化） */
.scroll-container::-webkit-scrollbar {
  display: none;
}

.card {
  margin: 10px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  background-color: #fff;
}

.label-content {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.bold-title {
  font-weight: bold;
  color: #333;
}

</style>
