<template>
  <view>
    <z-paging ref="pagingRef" class="container" v-model="taskData" :default-page-size="queryParams.pageSize" :auto="false" 
      @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="taskType" type="2" :label="dropItem('taskType').label"
            :value="dropItem('taskType').value">
          </uv-drop-down-item>
          <uv-drop-down-item name="status" type="2" v-if="type == 'all'" :label="dropItem('status').label" :value="dropItem('status').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>

      <view>
        <view v-for="item in taskData" class="box">
          <view class="section-title">
            <view class="title">{{ item.taskName }}</view>
            <view class="section-status status1" v-show="item.status == 0">待执行</view>
            <view class="section-status status2" v-show="item.status == 1">已完成</view>
            <view class="section-status status3" v-show="item.status == 2">已超时</view>
          </view>
          <view><text>盘点类型：</text>{{ scanTypeItem('taskType', item.taskType) }}</view>
          <view><text>所属仓库：</text>{{ item.warehouseName }}</view>
          <view><text>所属场景：</text>{{ item.sceneName }}</view>
          <view><text>截止时间：</text>{{ item.requireTime }}</view>
          <view><text>备注：</text>{{ item.remark || '-' }}</view>
          <view class="itemBtn">
            <uv-button type="primary" text="去盘点" v-show="item.status == 0" @tap="goInventory(item)"></uv-button>
            <uv-button type="primary" text="去查看" v-show="item.status == 1" @tap="goView(item)"></uv-button>
          </view>
        </view>
      </view>
    </z-paging>
  </view>
</template>

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



const queryParams = ref({
  taskName: "",
  pageNum: 1,
  pageSize: 10,
  taskType: null,
  status: null,
})
const type = ref("")

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

const goInventory = (item) => {
  if (item.taskType == 0) {
    uni.setStorageSync('taskInfo', item)
    uni.navigateTo({
      url: "/pagesInventory/handInventory"
    })
  } else {
    uni.setStorageSync('taskInfo', item)
    uni.navigateTo({
      url: "/pagesInventory/autoMenu"
    })
  }
}

const goView = (item) => {
  uni.setStorageSync('taskInfo', item)
  uni.navigateTo({
    url: '/pagesInventory/matchResult?taskId=' + item.id
  })
}

onLoad((options) => {
  type.value = options.type
  if (type.value !== 'all') {
    queryParams.value.status = 0
    result.value[1].value = 0
  }
})


onShow(() => {
  queryList(1)
})


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

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

// 筛选条件配置
const taskType = ref({
  label: '文档格式',
  value: '',
  activeIndex: 0,
  color: '#333',
  activeColor: '#2878ff',
  child: [{
    label: '全部',
    value: ''
  }, {
    label: '手持盘点',
    value: 0
  }, {
    label: '自动盘点',
    value: 1
  }]
});
// 筛选条件配置
const status = ref({
  label: '文档格式',
  value: '',
  activeIndex: 0,
  color: '#333',
  activeColor: '#2878ff',
  child: [{
    label: '全部',
    value: ''
  }, {
    label: '待执行',
    value: 0
  }, {
    label: '已完成',
    value: 1
  }, {
    label: '已超时',
    value: 2
  }]
});

// 计算属性
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 'taskType': return taskType.value;
    case 'status': return status.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;

  // background-color: #F5F5f5;
  .box {
    background: #efefef;
    border-radius: 8rpx;
    margin-top: 20rpx;
    padding: 20rpx;
    line-height: 50rpx;
    font-size: 28rpx;
    color: #333;

    .section-title {
      display: flex;
      justify-content: space-between;

      .section-status {
        padding: 0 20rpx;
        border-radius: 10rpx;
      }

      .status1 {
        background-color: #FFFFFF;
      }

      .status2 {
        background-color: #199793;
        color: #FFFFFF;
      }

      .status3 {
        background-color: #f56c6c;
        color: #FFFFFF;
      }
    }

    .title {
      font-size: 36rpx;
      font-weight: bold;
    }

    text {
      color: #666;
    }

    .itemBtn {
      display: flex;
      justify-content: flex-end
    }

    :deep(.uv-button) {
      height: 56rpx !important;
    }
  }
}

::v-deep .uv-search {
  flex: unset;
}

::v-deep .uv-drop-down {
  padding: 0 !important;
  justify-content: unset;
}

.uv-drop-down-item {
  width: 160rpx;
  justify-content: center;
  position: relative;

  ::v-deep .uv-text {
    flex: unset;
    width: unset;
  }
}

.uv-drop-down-item::after {
  content: "";
  /* 必须设置content，否则伪类不显示 */
  position: absolute;
  right: 0;
  /* 定位到元素右侧 */
  top: 50%;
  /* 垂直居中 */
  transform: translateY(-50%);
  /* 修正垂直定位 */
  width: 1px;
  /* 竖线宽度 */
  height: 50%;
  /* 竖线高度 */
  background-color: #ccc;
  /* 竖线颜色 */
}
</style>
