<template>
	<view class="mainBox">
		<uni-nav-bar fixed="true" statusBar="true" left-icon="back" title="选择供应商" @clickLeft="handleClose">
		</uni-nav-bar>
		<view class="searchBox">
			<input v-model="searchVal" placeholder="请输入供应商名称" class="searchInput" @confirm="doSearch" />
			<u-button type="icon-shixiangxinzeng" size="30" @click="doSearch" class="searchBtn">
				<view class="iconfont icon-sousuo"></view>
				<view class="text">搜索</view>
			</u-button>
		</view>
		<view class="wrapper">
			<scroll-view scroll-y class="listContent" @scrolltolower="scrolltolower">
				<checkbox-group @change="e => onCheckChange(e)">
					<label v-for="(item, index) in listData" :key="index">
						<view class="listBox">
							<view class="listBox-sel">
								<checkbox :value="item.id" :checked="item.checked" color="#157A2C" style="transform: scale(1.2)" />
							</view>
							<view class="listBox-con">
								<view class="listBox-top">
									<view class="listBox-name">{{ item.name || '-' }}</view>
									<view class="listBox-code">{{ item.code || '' }}</view>
								</view>
								<view class="listBox-bottom">
									<view>代号：{{ item.serialNo || '-' }}</view>
									<view>电话：{{ item.phone || '-' }}</view>
									<view>地址：{{ item.address || '-' }}</view>
								</view>
							</view>
						</view>
					</label>
				</checkbox-group>
				<u-loadmore v-if="listData.length" :status="loadStatus" />
				<u-empty class="noDate" style="margin-top: 20vh" v-if="!listData.length && !loading"></u-empty>
			</scroll-view>
		</view>

		<view class="footer">
			<view class="count-text" v-if="checkedCount > 0">已选 {{ checkedCount }} 个</view>
			<u-button type="success" size="small" :disabled="checkedCount === 0" @click="confirmSelect">
				选择（{{ checkedCount }}）
			</u-button>
		</view>
	</view>
</template>

<script>
	import { contactPage } from '@/api/saleManage/contact/index.js'

	export default {
		data() {
			return {
				page: 1,
				size: 20,
				isEnd: false,
				loading: false,
				searchVal: '',
				listData: [],
				loadStatus: 'loadmore'
			}
		},
		computed: {
			checkedCount() {
				return this.listData.filter(item => item.checked).length
			}
		},
		onLoad() {
			this.getList()
		},
		methods: {
			scrolltolower() {
				if (this.isEnd || this.loading) return
				this.page++
				this.getList()
			},
			doSearch() {
				this.page = 1
				this.listData = []
				this.getList()
			},
			async getList() {
				this.loading = true
				this.loadStatus = 'loading'
				try {
					const result = await contactPage({
						pageNum: this.page,
						size: this.size,
						name: this.searchVal || undefined,
						type: 2,
						status: 1
					})
					const newList = (result.list || []).map(item => ({
						...item,
						checked: false
					}))
					if (this.page === 1) {
						this.listData = newList
					} else {
						this.listData = this.listData.concat(newList)
					}
					const total = result.count || result.total || 0
					this.isEnd = this.listData.length >= total
					this.loadStatus = this.isEnd ? 'nomore' : 'loadmore'
				} catch (e) {
					console.error(e)
					this.loadStatus = 'nomore'
				}
				this.loading = false
			},
			onCheckChange(e) {
				const values = e.detail.value || []
				this.listData.forEach((item, i) => {
					this.$set(this.listData[i], 'checked', values.includes(item.id))
				})
			},
			confirmSelect() {
				const selected = this.listData.filter(item => item.checked)
				if (!selected.length) {
					uni.showToast({ title: '请选择供应商', icon: 'none' })
					return
				}
				const list = selected.map(item => ({
					id: item.id,
					supplierId: item.id,
					supplierName: item.name,
					supplierCode: item.code,
					name: item.name,
					code: item.code,
					files: [],
					preferentialPrice: '',
					settlementMode: '4',
					settlementModeName: '分期付款',
					taxRate: '',
					totalPrice: '',
					resultList: []
				}))
				uni.$emit('supplierManageChange', list)
				uni.navigateBack()
			},
			handleClose() {
				uni.navigateBack()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.mainBox {
		height: 100vh;
		display: flex;
		flex-direction: column;
		background: #f5f5f5;

		.wrapper {
			flex: 1;
			overflow: hidden;
		}

		.searchBox {
			padding: 10rpx 0;
			box-sizing: border-box;
			background-color: #dedede;
			height: 90rpx;
			width: 100%;
			display: flex;
			justify-content: space-around;
			align-items: center;

			input {
				height: 70rpx;
				width: 65%;
				background: #f9f9f9;
				margin: 0 10rpx;
				padding: 0 20rpx;
				box-sizing: border-box;
				border-radius: 8rpx;
				font-size: 28rpx;
			}

			.searchBtn {
				height: 70rpx;
				background: #f9f9f9;
				color: #676767;
				font-size: 28rpx;
				padding: 0 30rpx;
				box-sizing: border-box;
				outline: none;
				border: none;
				width: 240rpx;
				display: flex;
				align-items: center;
				justify-content: center;

				.icon-sousuo {
					font-size: 22px;
				}

				.text {
					font-size: 26rpx;
					margin-left: 10rpx;
				}
			}
		}

		.listContent {
			height: 100%;
			padding: 0 20rpx;

			.listBox {
				display: flex;
				padding: 24rpx 0;
				border-bottom: 2rpx solid #e5e5e5;
				background: #fff;
				margin-bottom: 2rpx;

				.listBox-sel {
					width: 80rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					flex-shrink: 0;
				}

				.listBox-con {
					flex: 1;
					padding-right: 18rpx;

					.listBox-top {
						display: flex;
						justify-content: space-between;
						align-items: center;
						padding-bottom: 8rpx;

						.listBox-name {
							font-size: 28rpx;
							font-weight: bold;
							color: #333;
						}

						.listBox-code {
							font-size: 22rpx;
							color: #999;
						}
					}

					.listBox-bottom {
						font-size: 22rpx;
						color: #666;

						>view {
							padding: 3rpx 0;
							overflow: hidden;
							white-space: nowrap;
							text-overflow: ellipsis;
						}
					}
				}
			}
		}

		.footer {
			height: 100rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-top: 1rpx solid #eeecec;
			background-color: #fff;
			padding: 0 30rpx;
			flex-shrink: 0;

			.count-text {
				font-size: 26rpx;
				color: #666;
			}
		}
	}
</style>
