<template>
	<view class="page">
		<scroll-view class="list" scroll-y @scrolltolower="loadMore">
			<view class="card" v-for="(item,index) in list" :key="item.id">
				<view class="card-head">
					<view class="tag">中转配送</view>
					<view class="code" v-if="item.numberCode">#{{item.numberCode}}</view>
				</view>
				<view class="row">
					<view class="label">中转点</view>
					<view class="value">{{item.transferAddressName || item.shopName}}</view>
				</view>
				<view class="row">
					<view class="label">配送员</view>
					<view class="value phone-value" @tap.stop="makeCall(item.workerPhone)">
						<text>{{item.workerName || '未分配'}}</text>
						<text v-if="item.workerPhone" class="phone">{{item.workerPhone}}</text>
					</view>
				</view>
				<view class="row">
					<view class="label">用户</view>
					<view class="value phone-value" @tap.stop="makeCall(item.receiverPhone)">
						<text>{{item.receiverName}}</text>
						<text v-if="item.receiverPhone" class="phone">{{item.receiverPhone}}</text>
					</view>
				</view>
				<view class="address">{{item.receiverAddress}}</view>
				<view class="image-row" v-if="item.transferArriveImage">
					<image :src="item.transferArriveImage" mode="aspectFill"></image>
				</view>
				<view class="actions">
					<view class="photo-btn" @tap="chooseImage(index)">拍照</view>
					<view class="main-btn" @tap="transferArrive(index,item)">送达中转点</view>
				</view>
			</view>
			<view class="empty" v-if="list.length === 0 && loadStatus === 'nomore'">暂无中转配送单</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				loadStatus: 'more',
				totalPages: 1,
				searchForm: {
					pageNum: 1,
					pageSize: 10,
					status: 1,
					deliveryType: 1,
					transferDelivery: 1,
					transferArriveEmpty: true,
					shopId: '',
					regionId: ''
				}
			}
		},
		onShow() {
			this.resetList()
		},
		methods: {
			back() {
				uni.navigateBack()
			},
			resetList() {
				this.searchForm.pageNum = 1
				this.totalPages = 1
				this.list = []
				this.loadStatus = 'more'
				this.searchForm.shopId = JSON.parse(uni.getStorageSync('area')).shop.id
				const area = uni.getStorageSync('area')
				this.searchForm.regionId = area ? JSON.parse(area).shop.regionId : ''
				this.getList()
			},
			loadMore() {
				if (this.searchForm.pageNum >= this.totalPages) return
				this.searchForm.pageNum++
				this.getList()
			},
			getList() {
				this.NB.sendRequest("/mall/delivery/pageTransferDelivery",this.searchForm, false, 'post', 'application/json').then((res) => {
					if (res.code == 200) {
						const records = res.result.records || []
						this.list = this.searchForm.pageNum == 1 ? records : this.list.concat(records)
						this.totalPages = res.result.pages || 1
						this.loadStatus = this.searchForm.pageNum >= this.totalPages ? 'nomore' : 'more'
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
					uni.hideLoading()
				}).catch((res) => {})
			},
			chooseImage(index) {
				uni.chooseMedia({
					count: 1,
					mediaType: ['image'],
					sourceType: ['camera', 'album'],
					success: (res) => {
						const file = res.tempFiles && res.tempFiles[0] ? res.tempFiles[0].tempFilePath : ''
						if (file) this.uploadImage(index, file)
					}
				})
			},
			uploadImage(index, filePath) {
				uni.uploadFile({
					url: this.tui.interfaceUrl() + '/upload/file',
					filePath,
					name: 'file',
					header: {
						'accessToken': uni.getStorageSync('hiver_token'),
						'content-type': 'multipart/form-data'
					},
					success: (res) => {
						const data = JSON.parse(res.data)
						this.$set(this.list[index], 'transferArriveImage', data.result)
					}
				})
			},
			transferArrive(index, item) {
				console.log('快看快看',item)
				this.NB.sendRequest("/mall/delivery/transferArrive",{
					deliveryId: item.id,
					transferArriveImage: item.transferArriveImage || ''
				}, false, 'post', 'application/x-www-form-urlencoded').then((res) => {
					if (res.code == 200) {
						this.list.splice(index, 1)
						uni.showToast({
							title: '操作成功',
							icon: 'none'
						})
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
					uni.hideLoading()
				}).catch((res) => {})
			},
			makeCall(phone) {
				if (!phone) {
					uni.showToast({
						title: '暂无手机号',
						icon: 'none'
					})
					return
				}
				uni.makePhoneCall({
					phoneNumber: phone
				})
			}
		}
	}
</script>

<style>
	.page{min-height:100vh;background:#F5F8F5;padding:120rpx 24rpx 24rpx;box-sizing:border-box;}
	.nav{position:fixed;top:0;left:0;right:0;height:110rpx;background:#F5F8F5;z-index:9;display:flex;align-items:flex-end;justify-content:center;padding-bottom:18rpx;}
	.back{position:absolute;left:24rpx;bottom:12rpx;width:68rpx;height:68rpx;border-radius:50%;background:#fff;display:flex;align-items:center;justify-content:center;}
	.title{font-size:32rpx;font-weight:800;}
	.list{height:calc(100vh - 140rpx);}
	.card{background:#fff;border-radius:24rpx;padding:24rpx;margin-bottom:20rpx;box-shadow:0 10rpx 26rpx rgba(0,35,28,.08);}
	.card-head{display:flex;align-items:center;margin-bottom:20rpx;}
	.tag{background:#fff2e9;color:#f36c21;border-radius:12rpx;padding:8rpx 14rpx;font-weight:700;}
	.code{margin-left:auto;font-weight:800;font-size:32rpx;}
	.row{display:flex;line-height:46rpx;}
	.label{width:120rpx;color:#7a8582;}
	.value{flex:1;font-weight:700;}
	.phone-value{display:flex;align-items:center;gap:16rpx;color:#123d35;}
	.phone{color:#0b9b73;text-decoration:underline;}
	.address{margin-top:10rpx;color:#6c7773;line-height:40rpx;}
	.image-row image{width:160rpx;height:160rpx;border-radius:16rpx;margin-top:16rpx;}
	.actions{display:flex;margin-top:22rpx;gap:18rpx;}
	.photo-btn,.main-btn{flex:1;height:76rpx;line-height:76rpx;text-align:center;border-radius:38rpx;font-weight:700;}
	.photo-btn{background:#f2f6f4;color:#0b9b73;}
	.main-btn{background:linear-gradient(90deg,#e3ff96,#a6ffea);color:#00231c;}
	.empty{text-align:center;color:#8a9692;padding:80rpx 0;}
</style>
