<template>
  <view>
    <z-paging ref="pagingRef" class="container" v-model="historyData" :default-page-size="queryParams.pageSize" @query="queryList">
      <template #top>
        <uv-search v-model="queryParams.taskName" placeholder="请输入" :showAction="true" actionText="搜索" @search="searchList"
          @custom="searchList"></uv-search>
        <uv-drop-down ref="dropDown" sign="dropDown_1" text-active-color="#3c9cff"
          :extra-icon="{ name: 'arrow-down-fill', color: '#666', size: '26rpx' }"
          :extra-active-icon="{ name: 'arrow-up-fill', color: '#3c9cff', size: '26rpx' }" :defaultValue="defaultValue"
          :custom-style="{ padding: '0 30rpx' }" @click="selectMenu">
          <uv-drop-down-item name="scanType" type="2" :label="dropItem('scanType').label" :value="dropItem('scanType').value">
          </uv-drop-down-item>
        </uv-drop-down>
        <uv-drop-down-popup sign="dropDown_1" :click-overlay-on-close="true" :currentDropItem="currentDropItem"
          @clickItem="clickItem" @popupChange="change"></uv-drop-down-popup>
      </template>
      
      <uv-list>
        <uv-list-item :title="item.taskName" :note="scanTypeItem('scanType', item.scanType)" border :rightText="item.createdAt" clickable v-for="item in historyData" @click="goMathchResult(item)" />
      </uv-list>
    </z-paging>
  </view>
</template>

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




const queryParams = ref({
  taskName: "",
  pageNum: 1,
  pageSize: 10
})
const pagingRef = ref(null)
const historyData = ref([])
// 搜索按钮
const searchList = () => {
  pagingRef.value.reload()
}
// 获取列表
const queryList = (pageNo, pageSize) => {
  queryParams.value.pageNum = pageNo
  console.log(pageNo, pageSize)
  historyList(queryParams.value).then(res => {
    // historyData.value = res.data.rows
    pagingRef.value.complete(res.data.rows)
  }).catch(res => {
    pagingRef.value.complete(false)
  })
}

const goMathchResult = (item) => {
  uni.navigateTo({
    url: "/pagesInventory/matchResult-old?taskName=" + item.taskName
  })
}


onLoad(() => {
})


// 获取到对应的盘点方式
const scanTypeItem = (name, value) => {
  const scanTypeObj = getPropertyByName(name);
  if (scanTypeObj && scanTypeObj.child) {
    const item = scanTypeObj.child.find(item => item.value === value);
    return item ? item.label : null;
  }
  return null;
}

// 数据状态
const dropDown = ref(null);
// 默认值
const defaultValue = ref([]);
// 选择结果
const result = ref([{ name: 'scanType', label: '全部', value: '' }]);
const activeName = ref('');

// 筛选条件配置
const scanType = ref({
  label: '文档格式',
  value: '',
  activeIndex: 0,
  color: '#333',
  activeColor: '#2878ff',
  child: [{
    label: '全部',
    value: ''
  }, {
    label: '手动盘点',
    value: 0
  }, {
    label: '自动盘点',
    value: 1
  }]
});

// 计算属性
const dropItem = computed(() => {
  return (name) => {
    const resultObj = {};
    const find = result.value.find(item => item.name === name);
    if (find) {
      resultObj.label = find.label;
      resultObj.value = find.value;
    } else {
      resultObj.label = getPropertyByName(name).label;
      resultObj.value = getPropertyByName(name).value;
    }
    return resultObj;
  }
});

const currentDropItem = computed(() => {
  return getPropertyByName(activeName.value);
});

// 工具函数：根据名称获取对应的响应式对象
function getPropertyByName(name) {
  switch (name) {
    case 'scanType': return scanType.value;
    default: return {};
  }
}

// 生命周期钩子
onPageScroll(() => {
  // 滚动后及时更新位置
  dropDown.value?.init();
});

const selectMenu = (e) => {
  const { name, active, type } = e;
  activeName.value = name;

  // type 等于1 的需要特殊处理：type不等于1可以忽略
  if (type == 1) {
    clickItem({
      name: 'vip_type',
      label: 'VIP文档',
      value: e.active ? 1 : 0
    });
  } else {
    const find = result.value.find(item => item.name == activeName.value);
    if (find) {
      const findIndex = getPropertyByName(activeName.value).child.findIndex(item =>
        item.label == find.label && item.value == find.value
      );
      getPropertyByName(activeName.value).activeIndex = findIndex;
    } else {
      getPropertyByName(activeName.value).activeIndex = 0;
    }
  }
};

const clickItem = (e) => {
  // 下面有重新赋值，所以用let
  let { label, value } = e;
  const findIndex = result.value.findIndex(item => item.name == activeName.value);

  if (defaultValue.value.indexOf(value) > -1 && getPropertyByName(activeName.value).label) {
    label = getPropertyByName(activeName.value).label;
  }

  // 已经存在筛选项
  if (findIndex > -1) {
    result.value[findIndex] = {
      name: activeName.value,
      label,
      value
    }
  } else {
    result.value.push({
      name: activeName.value,
      label,
      value
    });
  }

  result.value = result.value.filter(item => defaultValue.value.indexOf(item.value) == -1);

  result.value.forEach(item=> {
    queryParams.value[item.name] = item.value
  })
  pagingRef.value.reload()
  console.log("筛选的值：", queryParams.value)
};

const change = (e) => {
  console.log('弹窗打开状态：', e);
}

</script>

<style scoped lang="scss">
.container {
  padding: 32rpx;
}
::v-deep .uv-search {
  flex: unset;
}
::v-deep .uv-drop-down{
  padding: 0 !important;
}
</style>
