<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="productUnitList.length">

				<u-swipe-action class="bg-[#ffffff]" v-for="(item,index) in productUnitList" :key="index">
					<u-swipe-action-item :name="item.id" :ref="setSwipeActionRef(index)"
						@click="(v) => hanldeOptionCallBack(v, item.id)" :options="btnOptions">
						<view class="border-0 border-b-1 border-solid border-[#ccc]">
							<view class="p-[20rpx] leading-normal">
								<text class="text-[12px] text-[#6a6a6a]">{{item.name}}</text>
							</view>
						</view>
					</u-swipe-action-item>
				</u-swipe-action>
				<up-modal :show="show" @confirm="confirm" showCancelButton @cancel="show = false" :title="t('delete')"
					:content="t('productUnitDeleteTips')" ref="uModal"></up-modal>
			</view>
			<mescroll-empty v-if="!productUnitList.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/product/unit/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 { getProductUnitList, deleteProductUnit } from '@/addon/erp/api/product';
	import { onShow, onPageScroll, onReachBottom } from '@dcloudio/uni-app';

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


	const show = ref(false);

	// 获取系统状态栏的高度
	let menuButtonInfo : any = {};
	// 如果是小程序，获取右上角胶囊的尺寸信息，避免导航栏右侧内容与胶囊重叠(支付宝小程序非本API，尚未兼容)
	// #ifdef MP-WEIXIN || MP-BAIDU || MP-TOUTIAO || MP-QQ
	menuButtonInfo = uni.getMenuButtonBoundingClientRect();
	// #endif

	const mescrollTop = "20rpx"

	const productUnitList = 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
		};
		getProductUnitList(data).then((res : any) => {
			let newArr = res.data.data;
			mescroll.endSuccess(newArr.length);
			//设置列表数据
			if (mescroll.num == 1) {
				productUnitList.value = []; //如果是第一页需手动制空列表
			}
			productUnitList.value = productUnitList.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(); // 请求失败, 结束加载
		})
	}

	const btnOptions = ref([{
		text: t('edit'),
		style: {
			backgroundColor: '#3c9cff'
		}
	}, {
		text: t('delete'),
		style: {
			backgroundColor: '#f56c6c'
		}
	}])
	const swipeActionRefs = ref<any[]>([]);

	const setSwipeActionRef = (index : number) => (el : any[] | null) => {
		if (el) {
			swipeActionRefs.value[index] = el;
		}
	};

	// 按钮回调
	const del_id = ref<number>(0);
	const hanldeOptionCallBack = (obj : any, id : number) => {
		// 第一个编辑按钮
		if (obj.index === 0) {
			redirect({ url: '/addon/erp/pages/product/unit/add', param: { id: id } })
		}
		// 第二个删除按钮
		if (obj.index === 1) {
			show.value = true;
			del_id.value = id;
		}
	};
	// 确认删除
	const confirm = () => {
		if (del_id.value == 0) {
			uni.showToast({ icon: 'none', title: t("Invalid ID") });
			return false;
		}
		let index = productUnitList.value.findIndex(item => item.id === del_id.value);
		deleteProductUnit(del_id.value)
			.then(() => {

				productUnitList.value.splice(index, 1);
				del_id.value = 0;
				show.value = false;
				if (swipeActionRefs.value[index]) {
					swipeActionRefs.value[index].closeHandler();
				}
			}).catch(() => { });
	}
</script>

<style>
</style>