<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">
				<view v-for="(item, index) in listData" :key="index">
					<view class="listBox" @click="selectItem(item, index)">
						<view class="listBox-sel">
							<view class="radio" :class="{ active: selectedIndex === index }">
								<view v-if="selectedIndex === index" class="radio-dot"></view>
							</view>
						</view>
						<view class="listBox-con">
							<view class="listBox-top">
								<view class="listBox-code">{{ item.planCode }}</view>
								<view class="listBox-status">{{ getStatus(item.status) }}</view>
							</view>
							<view class="listBox-middle">
								<view v-if="item.requirementCode">需求编码：{{ item.requirementCode }}</view>
								<view>需求部门：{{ item.requireDeptName }}</view>
							</view>
							<view class="listBox-bottom">
								<view>需求人：{{ item.requireUserName }}</view>
								<view>负责人：{{ item.responsibleName }}</view>
								<view>明细条数：{{ item.detailCount }}</view>
								<view>完成日期：{{ item.finishDate || '-' }}</view>
							</view>
						</view>
					</view>
				</view>
				<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">
			<u-button type="success" size="small" :disabled="selectedIndex === null" @click="confirmSelect">
				选择
			</u-button>
		</view>
	</view>
</template>

<script>
	import { getTableList } from '@/api/purchasingManage/purchasePlanManage'

	export default {
		data() {
			return {
				page: 1,
				size: 20,
				isEnd: false,
				loading: false,
				searchVal: '',
				listData: [],
				selectedIndex: null,
				loadStatus: 'loadmore',
				initPlanCode: ''
			}
		},
		onLoad(option) {
			if (option && option.planCode) {
				this.initPlanCode = option.planCode
			}
			this.getList()
		},
		methods: {
			scrolltolower() {
				if (this.isEnd || this.loading) return
				this.page++
				this.getList()
			},
			doSearch() {
				this.page = 1
				this.listData = []
				this.selectedIndex = null
				this.getList()
			},
			async getList() {
				this.loading = true
				this.loadStatus = 'loading'
				try {
					const result = await getTableList({
						pageNum: this.page,
						size: this.size,
						status: 2,
						planCode: this.searchVal || undefined
					})
					const newList = result.list || result.records || []
					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'

					// 回显已选数据
					if (this.initPlanCode) {
						const idx = this.listData.findIndex(item => item.planCode === this.initPlanCode)
						if (idx !== -1) this.selectedIndex = idx
					}
				} catch (e) {
					console.error(e)
					this.loadStatus = 'nomore'
				}
				this.loading = false
			},
			selectItem(item, index) {
				this.selectedIndex = index
			},
			getStatus(status) {
				const map = { 0: '未提交', 1: '审核中', 2: '审核通过', 3: '审核不通过' }
				return map[status] || ''
			},
			confirmSelect() {
				if (this.selectedIndex === null) {
					uni.showToast({ title: '请选择一条数据', icon: 'none' })
					return
				}
				const current = this.listData[this.selectedIndex]

				console.log('confirmSelect~~', current)
				// 通过事件总线传递给 taskForm
				uni.$emit('changeInquiryManageList', current)
				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;

					.radio {
						width: 36rpx;
						height: 36rpx;
						border-radius: 50%;
						border: 3rpx solid #ccc;
						display: flex;
						align-items: center;
						justify-content: center;

						.radio-dot {
							width: 20rpx;
							height: 20rpx;
							border-radius: 50%;
							background: #157A2C;
						}

						&.active {
							border-color: #157A2C;
						}
					}
				}

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

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

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

						.listBox-status {
							font-size: 22rpx;
							color: #157A2C;
							background: #e8f5e9;
							padding: 4rpx 12rpx;
							border-radius: 4rpx;
						}
					}

					.listBox-middle {
						font-size: 24rpx;
						color: #666;
						padding-bottom: 6rpx;
						display: flex;
						flex-wrap: wrap;

						>view {
							margin-right: 24rpx;
						}
					}

					.listBox-bottom {
						display: flex;
						flex-wrap: wrap;
						font-size: 22rpx;
						color: #999;

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

		.footer {
			height: 100rpx;
			display: flex;
			justify-content: flex-end;
			align-items: center;
			border-top: 1rpx solid #eeecec;
			background-color: #fff;
			padding: 0 30rpx;
			flex-shrink: 0;
		}
	}
</style>
