<template>
	<view class="bg-[var(--page-bg-color)] min-h-[100vh]" :style="themeColor()">

		<mescroll-body ref="mescrollRef" @init="mescrollInit" :down="{ use: false }" height="auto" @up="getListFn"
			:top="mescrollTop">
			<view class="sidebar-margin body-bottom" v-if="sellReturnList.length">
				<view class="mb-[15rpx]" v-for="(item,index) in sellReturnList" :key="item.id">
					<view class="bg-[#ffffff] rounded-10rpx"
						@click="redirect({ url: '/addon/erp/pages/sell/sellreturn/detail',param:{id: item.id} })">
						<view class="head border-0 border-b-1 border-solid border-[#ddd]">
							<view class="flex justify-between p-[20rpx]">
								<view class="u-body-item-title u-line-2">{{item.code}}</view>
								<view>
									<up-tag v-if="item.status == 0" :text="item.status_text" type="warning" plain
										size="mini"></up-tag>
									<up-tag v-if="item.status == 1" :text="item.status_text" type="error" plain
										size="mini"></up-tag>
									<up-tag v-if="item.status == 2" :text="item.status_text" plain size="mini"></up-tag>
									<up-tag v-if="item.status == 3" :text="item.status_text" type="success" plain
										size="mini"></up-tag>
								</view>
							</view>
						</view>
						<view class="body">
							<view class="flex justify-start flex-col p-[20rpx]">
								<view class="flex justify-start mb-[18rpx] text-[12px] text-[#6a6a6a]">
									{{ t('customerName') }}:<text
										class="ml-[20rpx] text-[var(--primary-color)]">{{item.customer?.name}}</text>
								</view>
								<view class="flex justify-start mb-[18rpx] text-[12px] text-[#6a6a6a]">
									{{ t('returnQuantity') }}:<text
										class="ml-[20rpx] text-[var(--primary-color)]">{{item.total_num}}</text>
								</view>
								<view class="flex justify-start mb-[18rpx] text-[12px] text-[#6a6a6a]">
									{{ t('billingTime') }}:<text
										class="ml-[20rpx] text-[var(--primary-color)]">{{item.billing_time}}</text>
								</view>

							</view>
						</view>
					</view>
				</view>
			</view>
			<mescroll-empty v-if="!sellReturnList.length && !loading &&!listLoading"></mescroll-empty>

		</mescroll-body>
		<loading-page :loading="loading"></loading-page>
		<view class="fixed bottom-4 right-4 z-2">
			<up-button @click="redirect({ url: '/addon/erp/pages/sell/sellreturn/add'})" type="primary" shape="circle"
				:customStyle="{borderRadius: '50%',padding: 0,width: '50px',height: '50px'}">
				<u-icon size="30" color="#fff" name="plus-circle-fill"></u-icon>
			</up-button>
		</view>

	</view>
</template>

<script setup lang="ts">
	import { ref } from 'vue'
	import { t } from '@/locale';
	import { redirect, pxToRpx } from '@/utils/common';
	import MescrollBody from '@/components/mescroll/mescroll-body/mescroll-body.vue';
	import MescrollEmpty from '@/components/mescroll/mescroll-empty/mescroll-empty.vue';
	import useMescroll from '@/components/mescroll/hooks/useMescroll.js';
	import { getSellReturnList } from '@/addon/erp/api/sell';
	import { onShow, onPageScroll, onReachBottom, onLoad } from '@dcloudio/uni-app';

	const { downCallback, mescrollInit, getMescroll } = useMescroll(onPageScroll, onReachBottom);


	const mescrollTop = "20rpx"

	const sellReturnList = ref<Array<any>>([]),
		loading = ref<boolean>(true),
		listLoading = ref<boolean>(true),
		mescrollRef = ref(null);

	interface mescrollStructure {
		num : number,
		size : number,
		endSuccess : Function,
		[propName : string] : any
	}

	const getListFn = (mescroll : mescrollStructure) => {
		listLoading.value = true;
		let data : Object = {
			page: mescroll.num,
			limit: mescroll.size
		};
		getSellReturnList(data).then((res : any) => {
			let newArr = res.data.data;
			mescroll.endSuccess(newArr.length);
			//设置列表数据
			if (mescroll.num == 1) {
				sellReturnList.value = []; //如果是第一页需手动制空列表
			}
			sellReturnList.value = sellReturnList.value.concat(newArr);
			listLoading.value = false;
			loading.value = false;
		}).catch((err) => {
			// 如果是4001，没有绑定企业账号，强制跳转绑定
			if (err.code == 4001) {
				redirect({ url: '/addon/erp/pages/member/bind' })
			}
			listLoading.value = false;
			loading.value = false;
			mescroll.endErr(); // 请求失败, 结束加载
		})
	}
</script>

<style>
</style>