<!--
 * @author wangzhuo
 * @date 2025年8月19日
 * @description 主归属人变更审核
-->
<template>
  <view class="con-body">
    <view class="con-bg">
      <!-- 头部 -->
      <customHeader ref="customHeaderRef" :title="'客户人员主归属人变更审核'" :leftFlag="true" :rightFlag="false">
      </customHeader>

      <!-- 高度来避免头部遮挡 -->
      <view class="top-height"></view>

      <!-- 正文内容 -->
      <view class="all-body">
        <!-- 搜索-->
<!--        <view class="search">-->
<!--          <uni-search-bar class="custom-search" radius="28" placeholder="请输入客户人员名称" clearButton="auto"-->
<!--                          cancelButton="none" bgColor="#6FA2F8" textColor="#ffffff"-->
<!--                          v-model="searchValue"-->
<!--          />-->
<!--        </view>-->

        <!-- 分页部分  -->
        <mescroll-uni ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback"
                      :up="upOption" :down="downOption" :fixed="false" textColor="#ffffff" bgColor="#ffffff"
                      class="scroll-h" :class="{'loading-scroll':cssFlag}"
        >
          <view class="white-bg margin-bottom20" v-for="(item, index) in list" :key="index">
            <view class="report-list" @click.stop="handleDetail(item)">
              <view class="r-list title">
                {{ item.cusName }}
                <view class="r-right no-wrap" :class="item.auditStatus ? 'btn-blue' : 'btn-pink' ">
                  {{ item.auditStatus ?  item.auditStatus : '待您审核' }}
                </view>
              </view>
              <view class="r-list">
                <view class="r-left">客户人员名称</view>
                <view class="r-right">{{ item.userName }}</view>
              </view>
              <view class="border-bottom"></view>

              <view class="r-list">
                <view class="r-left">目前业务员</view>
                <view class="r-right">{{ item.belonger }}</view>
              </view>
              <view class="border-bottom"></view>

              <view class="r-list">
                <view class="r-left">创建时间</view>
                <view class="r-right">{{ item.createTime }}</view>
              </view>
              <view class="border-bottom"></view>

              <view class="r-list">
                <view class="r-left">职能</view>
                <view class="r-right">{{ item.function }}</view>
              </view>
              <view class="border-bottom"></view>

              <view class="r-list" style="font-weight: bold">
                <view class="r-left">申请变更人姓名</view>
                <view class="r-right">{{ item.applicantName }}</view>
              </view>
              <view class="border-bottom"></view>

              <view class="r-list">
                <view class="r-left">申请原因</view>
                <view class="r-right">{{ item.reasonForChange }}</view>
              </view>

            </view>
          </view>
        </mescroll-uni>
      </view>
    </view>
  </view>
</template>

<script setup>
import {ref, onMounted, watch} from 'vue'
import customHeader from '@/components/customHeader.vue'
import MescrollUni from 'mescroll-uni/mescroll-uni.vue';
import {getNavBarPaddingTop} from '@/utils/system.js'
import {personnelAwaitingReviewForChange} from "@/api/crm/mainOwner/mainOwner"

// 获取导航栏高度用于内容区域padding
const navBarPaddingTop = ref(0);
onMounted(() => {
  navBarPaddingTop.value = getNavBarPaddingTop() * 2;
})
// 搜索内容
let searchValue = ref(null);
// 分页
const mescrollRef = ref(null);
// 防抖计时
let timerId = null;
// 查询搜索跳转
let handleSearch = () => {
  // 防抖搜索
  console.log(searchValue.value)
  if (timerId) clearTimeout(timerId);

  timerId = setTimeout(async () => {

    // let res = await getList(1, upOption.value.page.size)
    await downCallback(mescrollRef.value.mescroll);
    clearTimeout(timerId);
    timerId = null;
  }, 500)
}

watch(searchValue, (newValue, oldValue) => {
  handleSearch()
})
let clearSearchValue = () => {
  searchValue.value = '';
}
// 查询列表
let list = ref([]);

const upOption = ref({
  page: {num: 0, size: 10},
  noMoreSize: 5,
  empty: {
    tip: '~ 空空如也 ~',
    icon: "../../../../../static/images/mescroll-empty.png"
  },
  textLoading: '加载中...',
  textNoMore: '已经到底了'
});

const downOption = ref({
  auto: true,
  textInOffset: '下拉刷新',
  textOutOffset: '释放更新',
  textLoading: '刷新中...'
});

let cssFlag = ref(false);//控制样式
const mescrollInit = (mescroll) => {
  cssFlag.value = true;
  mescrollRef.value = mescroll;
};

// 下拉刷新
const downCallback = async (mescroll) => {
  cssFlag.value = true;
  uni.showLoading();
  setTimeout(async () => {
    try {
      // 重置页码为第一页
      const res = await getList(1, mescroll.size || upOption.page.size);
      list.value = res.list;
      // 正确传递 total 参数
      mescroll.endSuccess(res.list.length, res.total > (mescroll.size || upOption.page.size));
    } catch (error) {
      console.log(error)
      mescroll.endErr();
    } finally {
      cssFlag.value = false;
    }
  }, 500);

}
// 上拉加载更多
const upCallback = async (mescroll) => {
  setTimeout(async () => {
    try {
      // 使用 mescroll 提供的页码和大小参数
      const res = await getList(mescroll.num, mescroll.size);
      if (mescroll.num === 1) {
        list.value = res.list;
      } else {
        list.value.push(...res.list);
      }
      // 正确判断是否还有更多数据
      mescroll.endSuccess(res.list.length, res.total > mescroll.num * mescroll.size);
    } catch (error) {
      console.log(error)
      mescroll.endErr();
    }
  }, 500);
}

// 获取数据列表
const getList = (pageIndex, pageSize) => {
  return new Promise(async (resolve) => {
    let param = {
      pageNum: pageIndex,
      pageSize,
      searchContent: searchValue.value
    }
    try {
      let res = await personnelAwaitingReviewForChange(param);
      resolve({
        list: res.rows,
        total: res.total
      });
    } catch (e) {
    } finally {
      uni.hideLoading();
    }
  });
}

// 跳转到详情
let handleDetail = (item) => {
  const {userId} = item;
  uni.navigateTo({
    url: "/pages/business/CRM/mainOwner/audit/confirmForm",
    events: {
      refreshOwnerChangeList() {
        handleSearch();
      }
    },
    success(res) {
      res.eventChannel.emit('auditCusOwner', {data: {userId}, editable: false})
    }
  })
}

</script>

<style scoped>
.all-body {
  /* #ifdef APP-PLUS */
  top: 160rpx;
  height: calc(100vh - 160rpx);
  /* #endif */
  /* #ifndef APP-PLUS */
  top: 116rpx;
  height: calc(100vh - 116rpx);
  /* #endif */
}

.scroll-h{
  /* #ifdef APP-PLUS */
  top: 160rpx;
  height: calc(100vh - 160rpx);
  /* #endif */
  /* #ifndef APP-PLUS */
  top: 116rpx;
  height: calc(100vh - 116rpx);
  /* #endif */
}

.white-bg {
  padding-bottom: 10rpx;

  .title {
    align-items: flex-start;
  }
}

.no-wrap {
  white-space: nowrap;
}

.btn-blue {
  border-radius: 10rpx;
}
</style>