<template>
  <view>
    <z-paging ref="pagingRef" class="container" v-model="listArr" :default-page-size="queryParams.pageSize" @query="queryList">
      <template #top>

        <view style="margin-bottom: 20rpx;display: flex;align-items: center;justify-content: space-between;">
          <view>
            <text v-show="queryParams.status == 0">当前正常数据</text>
            <text v-show="queryParams.status == 1">未扫描到，数据库里有数据</text>
            <text v-show="queryParams.status == 2">已扫描到，数据库里无数据</text>
            共:{{ total }}条。
          </view>
          <!-- <uni-icons @tap="exportArr" type="cloud-download-filled" size="30" style="color: #007aff;"></uni-icons> -->
        </view>
        <uv-tabs :list="tabList" @change="tabChange"></uv-tabs>
      </template>


      <view class="list-item" v-for="(item, index) in listArr" :key="index">
        <view class="item-top">
          <view class="item-name"><text v-show="queryParams.status !== 2">存放位置：</text>{{ item.pcde }}</view>
          <view class="status1 status" v-if="queryParams.status == 1">未扫到</view>
          <view class="status2 status" v-else-if="queryParams.status == 2">无数据</view>
          <view class="status3 status" v-else>正常数据</view>
        </view>
        <view class="item-name" v-show="queryParams.status !== 2">数量：{{ item.count }}</view>
        <!-- <view class="item-name">{{ item.des_pro }}</view>
        <view class="item-desc" v-show="item.des_mat">{{ item.des_mat }}</view>
        <view class="item-time" v-show="item.tme">{{ item.tme }}</view> -->
      </view>

    </z-paging>
  </view>
</template>

<script setup>
import { ref } from "vue";
import { onLoad } from "@dcloudio/uni-app";
import { getTaskName } from "@/api/history";



const listArr = ref([])
const pagingRef = ref(null)
// 获取列表
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  status: 0,
  taskName: ''
})
const total = ref(0)
// const dbExtra = ref(0)  // 未扫描到，数据库里有
// const requestExtra = ref(0)  // 扫描到，数据库里没有
const current = ref(0)  // tab切换
const tabList = ref([
  {name: '正常'},
  {name: '未扫到'},
  {name: '无数据'}

])  // tab切换



// 初始化请求
onLoad((e) => {
  console.log(e)
  queryParams.value.taskName = e.taskName
  // queryParams.value.taskName = "手持08.10"
  // 可以在这里进行数据请求、初始化操作等
});

// 切换tab
const tabChange = (e) => {
  console.log(e)
  queryParams.value.status = e.index
  pagingRef.value.reload()
}

// 获取列表
const queryList = (pageNo, pageSize) => {
  queryParams.value.pageNum = pageNo
  console.log(pageNo, pageSize)
  getTaskName(queryParams.value).then(res => {
    // historyData.value = res.data.rows
    if (queryParams.value.status == 2) {
			res.data.rows.forEach(e => {
				e.pcde = e.pcdeGroup
			});
		}
    total.value = res.data.total
    pagingRef.value.complete(res.data.rows)
  }).catch(res => {
    pagingRef.value.complete(false)
  })
}



// 导出
const exportArr = function () {
  exportByData(listArr.value).then(res => {
    uni.showLoading({
      title: '下载中'
    })
    console.log("数据", res)
    uni.downloadFile({
      url: res.data,
      success: (downloadResult) => {
        console.log("downloadResult", downloadResult)
        if (downloadResult.statusCode === 200) {
          // 保存文件
          uni.saveFile({
            tempFilePath: downloadResult.tempFilePath,
            success: (saveResult) => {
              uni.hideLoading();
              console.log('文件保存成功，保存路径：', saveResult.savedFilePath);
              const oldFilePath = saveResult.savedFilePath;
              const fileExtension = oldFilePath.split('.').pop();

              // 创建一个Date对象
              const now = new Date();

              // 获取当前的年、月、日
              const year = now.getFullYear();
              const month = now.getMonth() + 1; // 月份是从0开始的，需要加1
              const date = now.getDate();

              const hours = now.getHours().toString().padStart(2, '0');
              const minutes = now.getMinutes().toString().padStart(2, '0');
              const seconds = now.getSeconds().toString().padStart(2, '0');

              const newFileName = year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds + '_盘点结果.xls'; // 新的文件名
              const newFilePath = oldFilePath.replace(/[^\/]*$/, newFileName);

              uni.showToast({
                icon: 'none',
                mask: true,
                title: `文件已保存`, //保存路径
                duration: 3000,
              });
              console.log("newFilePath", oldFilePath, newFilePath)
              plus.io.resolveLocalFileSystemURL(oldFilePath, entry => {
                entry.getParent(_oldFile => {
                  entry.moveTo(_oldFile, '/' + newFileName, newFilePath => {
                    console.log('newFilePath', newFilePath.fullPath)
                  })
                })
              })
            },
            fail: (saveError) => {
              console.error('文件保存失败：', saveError);
            }
          });
        } else {
          console.error('文件下载失败，状态码：', downloadResult.statusCode);
        }
      },
      fail: (downloadError) => {
        console.error('文件下载失败：', downloadError);
      }
    });
  })
}
</script>

<style scoped lang="scss">
.container {
  padding: 32rpx;

  .list-item {
    padding: 20rpx 10rpx;
    border-bottom: 1rpx solid #eee;

    .item-top {
      display: flex;
      align-items: center;
      justify-content: space-between;

      .status {
        padding: 8rpx 15rpx;
        background: #E5E5E5;
        border-radius: 10rpx;
        font-size: 18rpx;
      }

      .status1 {
        background: #748041;
        color: white;
      }

      .status2 {
        background: #88413c;
        color: white;
      }
    }

    .item-name {
      font-size: 32rpx;
      color: #3b4144;
      font-weight: 400;
    }

    .item-desc {
      color: #999;
      font-size: 28rpx;
      font-weight: 400;
    }

    .item-time {
      color: #999;
      font-size: 24rpx;
      font-weight: 400;
      text-align: right;
      margin-top: 10rpx;
    }
  }

  .page {
    padding: 40rpx 80rpx;
  }
}
</style>
