<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="warehouseList.length">
				<view class="mb-[15rpx]" v-for="(item,index) in warehouseList" :key="index">

					<view class="bg-[#ffffff] rounded-10rpx">
						<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.name}}</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]">联系人:<text
										class="ml-[20rpx] text-[var(--primary-color)]">{{item.linkman}}</text></view>
								<view class="flex justify-start mb-[18rpx] text-[12px] text-[#6a6a6a]">联系电话:<text
										class="ml-[20rpx] text-[var(--primary-color)]">{{item.phone}}</text></view>
								<view class="flex justify-start mb-[18rpx] text-[12px] text-[#6a6a6a]">库存:<text
										class="ml-[20rpx] text-[var(--primary-color)]">{{item.inventory}}</text></view>
								<view class="flex justify-start mb-[18rpx] text-[12px] text-[#6a6a6a]">创建时间:<text
										class="ml-[20rpx] text-[var(--primary-color)]">{{item.create_time}}</text>
								</view>
							</view>
						</view>
						<view class="foot  border-0 border-t-1 border-solid border-[#ddd]">
							<view class="flex justify-end p-[20rpx]">
								<view><up-button :text="t('info')" size="mini"
										@click="redirect({ url: '/addon/erp/pages/base/warehouse/detail',param:{id: item.id} })"></up-button>
								</view>
								<view><up-button type="error" @click="hanldeDelete(item.id)" :text="t('delete')"
										size="mini"></up-button></view>
							</view>
						</view>
					</view>
					<!-- 删除模态框 -->
					<up-modal :show="show" @confirm="confirm" showCancelButton @cancel="show = false"
						:title="t('delete')" :content="t('warehouseDeleteTips')" ref="uModal"></up-modal>
				</view>
			</view>
			<mescroll-empty v-if="!warehouseList.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/base/warehouse/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 { getWarehouseList, deleteWarehouse } from '@/addon/erp/api/base';
	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 warehouseList = 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
		};
		getWarehouseList(data).then((res : any) => {
			let newArr = res.data.data;
			mescroll.endSuccess(newArr.length);
			//设置列表数据
			if (mescroll.num == 1) {
				warehouseList.value = []; //如果是第一页需手动制空列表
			}
			warehouseList.value = warehouseList.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 del_id = ref<number>(0);
	const hanldeDelete = (id : number) => {
		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 = warehouseList.value.findIndex(item => item.id === del_id.value);
		deleteWarehouse(del_id.value)
			.then(() => {
				warehouseList.value.splice(index, 1);
				del_id.value = 0;
				show.value = false;
			}).catch(() => { });
	}
</script>

<style>
</style>