<template>
	<view class="lists">
		<view class="status_bar" :style="navHeight"></view>
		<view class="header-bg"></view>
		<view class="main">
			<view class="middle view">
				<view class="text-block">
					<text class="middle-date">团购单位类型</text>
					<view class="middle-date-des uni-list picker-year">
						<view class="uni-list-cell">
							<view class="uni-list-cell-db">
								<picker class="picker-block" @change="bindPickerTypesChange" range-key='VALUE'
									mode="selector" :value="typesIndex" :range="selectedSalePlanTypes">
									<view class="uni-input">{{selectedSalePlanTypes[typesIndex].VALUE}}</view>
								</picker>
							</view>
						</view>
						<image class="icon-arrow" src="@/static/image/arrow_r@3x.png" mode="aspectFit"></image>
					</view>
				</view>
				<view class="text-block text-block-last">
					<text class="middle-date">团购单位名称</text>
					<input class="middle-date-des" type="text" v-model="condition.COMPANY_NAME" placeholder="请填写" />
				</view>
			</view>

			<view class="planout-block">
				<button class="btn" type="default" @tap="search">
					<text class="btn-text">查询</text>
				</button>
			</view>

			<uni-swipe-action class="planout-block-lists">
				<uni-swipe-action-item v-for="(item,index) in lists" :key='item.ID' :auto-close="false" :disabled='true'
					:show="isOpened" @click="handleClick">
					<template v-slot:left>
						<view class="swipe-right-block" @click="handleChecked($event,item, index)"><text
								class="swipe-right-block__text"
								:class="{'swipe-right-block__active':item.checked}"></text>
						</view>
					</template>
					<view class="middle view planout-block-item">
						<view class="text-block">
							<text class="middle-date middle-date-select">团购编码</text>
							<text class="middle-date-des middle-date-des-select">{{item.ID}}</text>
						</view>
						<view class="text-block">
							<text class="middle-date middle-date-select">团购名称</text>
							<text class="middle-date-des middle-date-des-select">{{item.OBJNAME}}</text>
						</view>
						<view class="text-block">
							<text class="middle-date middle-date-select">城市</text>
							<text class="middle-date-des middle-date-des-select">{{item.CITYDESC}}</text>
						</view>
						<view class="text-block text-block-last">
							<text class="middle-date middle-date-select">地址</text>
							<text class="middle-date-des middle-date-des-select">{{item.ADDRESS}}</text>
						</view>
					</view>
				</uni-swipe-action-item>
			</uni-swipe-action>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'

	import {
		apiGroupClientSelect
	} from '@/servers/common.js'

	export default {
		data() {
			return {
				isOpened: 'none',
				condition: {
					'INFOTP': '',
					'COMPANY_NAME': ''
				},
				page: {
					"PAGE": "1",
					"PAGESIZE": "10",
				},
				lists: [],
				selectedGroupClient: {},
				selectedSalePlanTypes: [{
						KEY: '',
						VALUE: '无'
					}, {
						KEY: '个人',
						VALUE: '个人'
					},
					{
						KEY: '单位',
						VALUE: '单位'
					}
				],
				typesIndex: 0,
				toPageParams: {
					page: '',
					head: {}
				}
			}
		},
		computed: {
			...mapState(['sysinfo']),
			navHeight() {
				return {
					'height': `${this.sysinfo.safeArea.top + 44}px`
				}
			},
		},
		onNavigationBarButtonTap(e) {

			// console.log('onNavigationBarButtonTap：', e, this.isOpened, this.selectedGroupClient);
			if (this.isOpened === 'left' && this.selectedGroupClient && this.selectedGroupClient.ID) {
				uni.$emit('selectedGroupClient', {
					selectedGroupClient: this.selectedGroupClient,
					condition: this.condition
				})
				uni.navigateBack({})
				return
			}
			this.isOpened = this.isOpened === 'none' ? 'left' : 'none';
			// #ifdef APP-PLUS  
			let pages = getCurrentPages()
			let page = pages[pages.length - 1]
			let webView = page.$getAppWebview();
			console.log('webView', webView)
			webView.setTitleNViewButtonStyle(0, {
				text: this.isOpened === 'none' ? '选择' : '完成',
			});
			// let titleObj = webView.getStyle().titleNView;
			//  if(!titleObj.buttons){
			// 	 returnl
			//  }
			//  titleObj.buttons[0].text = 'dd';
			//  webView.setStyle({
			// 	 titleNView:titleObj
			//  })
			// #endif
		},
		onLoad(option) {
			console.log('select-saleplan', option)
			if (option && option.from) {
				const {
					from
				} = option
				this.toPageParams.page = from
			}
		},
		created() {
			this.getGroupClient()
		},
		methods: {
			async getGroupClient() {
				const {
					INFOTP,
					COMPANY_NAME
				} = this.condition
				const params = {
					...this.page,
					"input": {
						INFOTP,
						COMPANY_NAME
					},
				}
				const res = await apiGroupClientSelect(params)
				this.lists = res.DATA.map((item) => {
					item.checked = false
					return item
				})
				console.log('ddd2', res)
			},
			// async getSalesPlanTypes() {
			// 	const res = await apiSalesPlanTypesSelect()
			// 	this.selectedSalePlanTypes = res.DATA
			// 	console.log('getSalesPlanTypes', res)
			// },
			search() {
				this.page.PAGE = 1
				this.getGroupClient()
			},
			bindPickerTypesChange(e) {
				console.log('picker发送选择改变，携带值为', e.detail.value)
				this.typesIndex = e.detail.value
				this.condition.INFOTP = this.selectedSalePlanTypes[this.typesIndex].KEY
			},
			handleClick(e, content, index) {
				console.log('click当前索引：', e, content, index);
			},
			handleChecked(e, item, index) {
				console.log('当前索引：', e, index);
				this.lists.forEach((cur) => {
					if (cur.ID === item.ID) {
						cur.checked = !cur.checked
					} else {
						cur.checked = false
					}
				})

				if (item.checked === true) {
					// this.selectedGroupClient.push(item)
					this.selectedGroupClient = item
				} else {
					// const selectIndex = this.selectedGroupClient.findIndex((saleplan)=>{
					// 	return saleplan.id === item.id
					// })
					// this.selectedGroupClient.splice(selectIndex,1)
					this.selectedGroupClient = {}
				}
			},
			nextStep() {
				this.$emit('click', 'orderUpload')
			},
			preStep() {
				uni.switchTab({
					url: `/pages/go-order/go-order`
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.view {
		flex-direction: column;
	}

	.header-bg {
		height: 284rpx;
		margin-top: -88rpx;
		background: linear-gradient(232deg, #f4e2e2 25%, #faf3f3 98%);
	}

	.lists {
		position: relative;
		flex-direction: column;
		overflow-y: scroll;
		background: #f0f4f5;
		overflow-x: hidden;
		height: 100%;
	}

	.main {
		flex-direction: column;
		margin-top: -164rpx;
		padding: 0 16rpx;
	}

	.middle {
		background-color: #fff;
		border-radius: 16rpx;
		padding: 32rpx 64rpx 32rpx 48rpx;
	}

	.text-block {
		display: flex;
		overflow: hidden;
		height: 96rpx;
		margin-bottom: 14rpx;
		border-bottom: 1px solid #f4f5f6;
	}

	.text-block-last {
		margin-bottom: 0;
		border-bottom: 0;
	}

	.middle-date {
		flex: 2;
		font-size: 28rpx;
		line-height: 96rpx;
		color: #333;
		margin-bottom: 8rpx;
	}

	.middle-date-des {
		flex: 3;
		font-size: 28rpx;
		line-height: 96rpx;
		height: 96rpx;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		color: #888;
	}


	.middle-date-select {
		color: #888;
	}

	.middle-date-des-select {
		color: #333;
	}

	.picker-year {
		position: relative;
		border-radius: 4px;
		align-items: center;
	}

	.uni-list-cell {
		flex: 1;
	}

	.uni-list-cell-db {
		flex: 1;
	}

	.picker-block {
		flex: 1;
	}

	.icon-arrow {
		position: absolute;
		right: 0;
		top: 16px;
		width: 12rpx;
		height: 20rpx;
		z-index: 10;
	}

	.planout-block-lists {
		display: flex;
		flex-direction: column;
		overflow: hidden;
	}

	.uni-swipe {
		flex: 1;
		margin-bottom: 26rpx;
	}

	/deep/ .uni-swipe_box {
		flex: 1;
	}

	.swipe-right-block {
		display: flex;
		width: 100rpx;
		justify-content: center;
		align-items: center;
	}

	.swipe-right-block__text {
		display: inline-block;
		width: 52rpx;
		height: 52rpx;
		border-radius: 50%;
		background-color: #fff;
	}

	.swipe-right-block__active {
		width: 52rpx;
		height: 52rpx;
		border-radius: 50%;
		background-color: $wly-primary-color;
	}

	.planout-block {
		display: flex;
		justify-content: space-between;
		height: 84rpx;
		margin-top: 64rpx;
		margin-bottom: 48rpx;
		padding: 0 24rpx;
	}

	.planout-block-item {
		flex: 1;
		width: 750rpx;
	}

	.btn {
		flex: 1;
		height: 92rpx;
		background: $wly-primary-color;
		border-radius: 46rpx;
	}

	.btn-text {
		color: #fff;
		font-size: 34rpx;
	}
</style>
