<template>
  <view class="container">
    <z-paging ref="pagingRef" class="containerBox" v-model="listData" :default-page-size="queryParams.pageSize" @query="queryList">
      <template #top>
        <!-- 项目号/项目描述/物料号/物料描述/供应商编码/供应商名称/订单编号 -->
        <uv-search v-model="queryParams.keyword" 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="billType" type="2" :label="dropItem('billType').label" :value="dropItem('billType').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 class="box">
        <view v-for="item in listData" class="item" @tap="goDetail(item)">
          <!-- <uv-icon name="file-text" size="36rpx" style="margin-top: 4rpx;" @tap="itemCopy(item)"></uv-icon> -->
          <view class="title">单据号：{{ item.billNo }}</view>
          <view v-show="item.billType == 0"><text>入库类型</text>：{{ item.rkTypeName }}</view>
          <view v-show="item.billType == 1"><text>出库类型</text>：{{ item.ckTypeName }}</view>
          <view><text>所属仓库：</text>{{ item.cangkuName }}</view>
          <view><text>物资类型：</text>{{ item.wlTypeName }}</view>
          <view><text>理货员：</text>{{ item.lihuoYName }}</view>
          <view><text>提交时间：</text>{{ parseTime(item.createTime) }}</view>
          <view class="statusBtn">{{ typeItem('billType', parseInt(item.billType)) }}</view>
        </view>
      </view>
      
    </z-paging>

  </view>
</template>

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

// *****下拉筛选
// 默认值
const defaultValue = ref([]);
// 选择结果
const result = ref([{ name: 'billType', label: '全部', value: '' }]);
const activeName = ref('');
// 筛选条件配置
const billType = 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 'billType': return billType.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);
}

// 获取到对应的入/出库
const typeItem = (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 queryParams = ref({
  keyword: "",
  approverId: uni.getStorageSync('user').userId,
  auditResult: 2,
  billType: "",
  pageNum: 1,
  pageSize: 10
})
const pagingRef = ref(null)
const listData = ref([])
// 获取列表
const queryList = (pageNo, pageSize) => {
  queryParams.value.pageNum = pageNo
  console.log(pageNo, pageSize, queryParams.value)
  auditList(queryParams.value).then(res => {
    console.log(res.rows)
    pagingRef.value.complete(res.rows)
  }).catch(res => {
    pagingRef.value.complete(false)
  })
}
const searchList = () => {
  pagingRef.value.reload()
}

onLoad(() => {
})

onShow(() => {
  pagingRef.value?.reload()
})

const goDetail = (item) => {
  uni.navigateTo({
    url: "/pagesMine/auditDetail",
    success: res => {
        res.eventChannel.emit('auditInfo',{info:item})
    },
  })
}


</script>

<style scoped lang="scss">
.container {
  position: relative;
  .containerBox{
    padding: 32rpx;
    .dropDown{
      padding: 0 30rpx;
      border-bottom: 1px solid #dadbde;
      .item_dropDown{
        padding: 20rpx;
        display: flex;
        align-items: center;
        ::v-deep .uv-text{
          width: unset !important;
          flex: unset !important;
        }
        ::v-deep .item_text{
          max-width: 200rpx;
          margin-right: 10rpx !important;
        }
      }
    }
    ::v-deep .uv-search {
      flex: unset;
    }
    .box{
      // background-color: #ebebeb;
      // background-color: #f5f5f5;
      // padding-top: 20rpx;
      .item {
        background: #efefef;
        padding: 20rpx;
        margin-top: 20rpx;
        line-height: 50rpx;
        font-size: 28rpx;
        border-radius: 8rpx;
        color: #333;
        position: relative;
        .title {
          font-size: 32rpx;
          font-weight: bold;
          display: flex;
          align-items: center;
        }
        text{
          color: #666;
        }
        .statusBtn {
          position: absolute;
          top: 10rpx;
          right: 10rpx;
          background-color: #199793;
          color: white;
          padding: 0 10rpx;
          border-radius: 4rpx;
          font-size: 24rpx;
          line-height: 40rpx;
        }
      }
    }
  }
  ::v-deep .uv-input__content{
    flex-direction: column;
    align-items: flex-start;
  }
}
</style>
