<template>
	<view class="lists">
		<view class="status_bar" :style="navHeight"></view>
		<view class="header-bg">
			<image class="header-bg-img" src="@/static/image/nav_bg@3x.png" mode="scaleToFill"></image>
		</view>
		<view class="main" :style="mainHeight">
			<view class="middle view planout-block-item">
				<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="bindPickerChange($event,'type')" :value="customerType"
									range-key='name' mode="selector" :range="customerTypeArr">
									<text class="middle-date-des">{{customerType || '请选择'}}</text>
								</picker>
							</view>
						</view>
						<image class="icon-arrow" src="@/static/image/arrow_r@3x.png" mode=""></image>
					</view>
				</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">
								<input class="middle-date-des" type="text" v-model="searchCode" placeholder="请填写" />
							</view>
						</view>
					</view>
				</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">
								<input class="middle-date-des" type="text" v-model="searchName" placeholder="请填写" />
							</view>
						</view>
					</view>
				</view>
				<view class="text-block text-block-last">
					<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="bindPickerChange($event,'unit')" :value="marketUnit"
									range-key='MC_SHORT' mode="selector" :range="marketUnitArr">
									<text class="middle-date-des">{{marketUnit || '请选择'}}</text>
								</picker>
							</view>
						</view>
						<image class="icon-arrow" src="@/static/image/arrow_r@3x.png" mode=""></image>
					</view>
				</view>
			</view>
			
			<view class="planout-block">
				<button class="btn" type="default" @tap="search">
					<text class="btn-text">查询</text>
				</button>
			</view>
			<list ref="list" class="scroll-area view" loadmoreoffset='100' @loadmore="scrolltolower"
				:show-scrollbar='false'>
				<refresh @refresh="refresh" :display="refreshing ? 'show' : 'hide'">
				</refresh>
				<cell v-for="(item,index) in lists" :key="index" @click="handleChecked(item)">
					<view class="scroll-item">
						<view class="item-block item-block-code">
							<text class="item-block-label">客户编号：{{item.info.partner}}</text>
						</view>
						<view class="item-block item-block-code">
							<text class="item-block-label">客户名称：{{item.info.name_org1}}</text>
						</view>
						<view class="item-block item-block-code">
							<text class="item-block-label">客户类型：{{item.info.zzfld000016_text}}</text>
						</view>
						<view class="item-block item-block-code">
							<text class="item-block-label">旧编码：{{item.info.zzfld000017}}</text>
						</view>
						<view class="item-block item-block-code">
							<text class="item-block-label">法人姓名：{{item.info.zzfld000018}}</text>
						</view>
						<view class="item-block item-block-code">
							<text class="item-block-label">电话：{{item.info.zzfld000019}}</text>
						</view>
						<view class="item-block item-block-code">
							<text class="item-block-label">统一社会信用代码：{{item.info.zzfld00001a}}</text>
						</view>
						<view class="item-block item-block-code">
							<text class="item-block-label">负责员工编号：{{item.info.sales_emp}}</text>
						</view>
						<view class="item-block item-block-code">
							<text class="item-block-label">负责人姓名：{{item.info.sales_emp_name}}</text>
						</view>
						<view class="item-block item-block-code">
							<text class="item-block-label">创建日：{{item.info.crdat}}</text>
						</view>
					</view>
				</cell>
				<cell>
					<view class="loading-more" v-if='lists.length>2'>
						<text class="loading-more-text">{{loadingText}}</text>
					</view>
				</cell>
				<view class="planout-block-item__last"> </view>
			</list>
			<no-data v-if="lists.length===0"></no-data>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'

	import {
		getCustomerList,
		querySysDictionaryDetailsByCode,
		getCtrbase
	} from '@/servers/purchaseList.js'
	
	import timeFormat from '@/uview-ui/libs/function/timeFormat.js'
	import noData from '@/components/nodata.nvue';
	export default {
		components: {
			noData,
		},
		data() {
			return {
				scrollTop: 0,
				isOpened: 'none',
				loadingText: '加载中...',
				refreshing: false,
				page: {
					"PAGE": "1",
					"PAGESIZE": "10",
				},
				loadParams: {
					total: 0,
					totalPage: 1,
					isDone: false,
					isRefresh: false
				},
				typesIndex: 0,
				lists: [],
				customerType: '',
				customerCode: '',
				searchCode: '',
				searchName: '',
				marketUnit: '',
				marketUnitCode: '',
				orderItem: {},
				selectShuntCode: {},
				customerTypeArr: [],
				marketUnitArr: [],
			}
		},
		computed: {
			...mapState(['sysinfo', 'userInfo']),
			navHeight() {
				return {
					'height': `${this.sysinfo.safeArea.top + 44}px`
				}
			},
			mainHeight() {
				return {
					'height': `${this.sysinfo.safeArea.height - 44 }px`,
				}
			},
			scrollHeight() {
				return {
					'height': `${this.sysinfo.safeArea.height - 44 - 156 }px`
				}
			},

		},
		onLoad(option) {
			console.log('select-saleplan', option)
			this.orderItem = {
				...JSON.parse(option.order)
			}
		},
		created() {
			this.getCtrbase()
			this.getSelectDictDataOrigin('A0009')
			this.reset()
			// this.getSalesPlanTypes(true);
		},
		methods: {
			bindPickerChange(e,type) {
				const index = e.detail.value || 0
				switch (type) {
					case 'type':
					this.customerType = this.customerTypeArr[index].name
					this.customerCode = this.customerTypeArr[index].value
						break;
					case 'unit':
					this.marketUnit = this.marketUnitArr[index].MC_SHORT
					this.marketUnitCode = this.marketUnitArr[index].SOBID
						break;
				}
			},
			async getCtrbase(){
				const params = {
					'role': "WLY001",
					'sobid': '',
					'value': '1',
				}
				const res = await getCtrbase(params)
				this.marketUnitArr = res.DATA || []
			},
			getSelectDictDataOrigin(dictcode) {
			  querySysDictionaryDetailsByCode({'code': dictcode})
				.then(res => {
					if(res.data.Rows.length > 0){
						switch (dictcode){
							case 'A0009':
								this.customerTypeArr = res.data.Rows
								break;
							default:
								break;
						  }
					}
				  
				})
				.catch(() => {})
			},
			handleChecked(item) {
				this.selectShuntCode = item
				uni.$emit('selectShuntCode', {
					selectShuntCode: item
				})
				uni.navigateBack()
			},
			search() {
				this.reset()
				this.getSalesPlan(true, );
			},
			async getSalesPlan(isRefresh) {
				if (this.loadParams.isDone) {
					return
				}

				const params = {
					page: 1,
					pagesize: 50,
					partner: this.userInfo.code,
					role: "WLY001",
					mode: 'HQZY',
					RT_PARTNER: this.searchCode?[{
						SIGN: 'I',
						OPTION: 'CP',
						LOW: '*' + this.searchCode + '*',
						HIGH: '',
					}]:[],
					RT_NAME_ORG1: this.searchName?[{
						SIGN: 'I',
						OPTION: 'CP',
						LOW: '*' + this.searchName + '*',
						HIGH: '',
					}]:[],
					RT_ZZFLD000016: this.customerType?[{
						SIGN: 'I',
						OPTION: 'CP',
						LOW: '*' + this.customerType + '*',
						HIGH: '',
					}]:[],
					RT_SAL_CENTER: this.marketUnit?[{
						SIGN: 'I',
						OPTION: 'CP',
						LOW: '*' + this.marketUnit + '*',
						HIGH: '',
					}]:[],
				}

				uni.showLoading({
					title: '加载中'
				});
				setTimeout(function() {
					uni.hideLoading();
				}, 2000);
				const res = await getCustomerList(params)
				uni.hideLoading();

				if (isRefresh) {
					this.lists = res.data
					this.loadParams.isDone = false
				} else {
					this.lists = this.lists.concat(res.data)
				}
				
				setTimeout(() => {
					this.refreshing = false;
				}, 300)

			},
			refresh(e) {
				this.refreshing = true;
				this.reset()
				this.getSalesPlan(true)
				// #ifdef APP-NVUE
				try {
					// console.log('-------', this.$refs.list)
					this.$refs.list.resetLoadmore();
				} catch (e) {
					console.log('onrefresh', e)
				}
				// #endif

			},
			scrolltolower() {
				console.log('onReachBottom');
				if (this.page.PAGE < this.loadParams.totalPage) {
					this.page.PAGE++
					this.getSalesPlan()
				} else {
					this.loadParams.isDone = true
					this.loadingText = '-- 到底了 --'
				}
			},
			reset() {
				this.loadParams = {
					total: 0,
					totalPage: 1,
					isDone: false,
					isRefresh: false
				}
				this.page.PAGE = 1
				this.lists = []
				this.loadingText = '加载中'
			},
			async getSalesPlanTypes() {
				this.getSalesPlan()
			},
			bindPickerTypesChange(e) {
				console.log('bindPickerTypesChange', e.detail.value)
				this.typesIndex = e.detail.value
				this.getSalesPlan()
			},
			bindPickerYearChange(e) {
				console.log('bindPickerYearChange', e.detail.value)
				this.getSalesPlan()
			},
			handleClick(e, content, index) {
				console.log('click当前索引：', e, content, index);
			},
			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;
		opacity: 0.72;
		margin-top: -88rpx;
	}

	.header-bg-img {
		width: 750rpx;
	}

	.lists {
		position: relative;
		flex-direction: column;
		background: #f0f4f5;
		flex: 1;
	}

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

	.scroll-area {
		overflow-y: scroll;
	}

	.middle {
		background-color: #fff;
		border-radius: 16rpx;
		padding: 32rpx 64rpx 40rpx 48rpx;
	}
	
	.planout-block {
		display: flex;
		justify-content: space-between;
		margin-bottom: 48rpx;
		padding: 0 24rpx;
	}
	
	.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;
		overflow: hidden;
		text-overflow: ellipsis;
		color: #888;
		white-space: nowrap;
	}

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

	.uni-list-cell {
		flex: 1;
		// height: 100%;
	}

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

	.picker-block {
		flex: 1;
	}
	

	.uni-input-text {
		font-size: 14px;
		color: #888;
	}

	.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;
	}

	.planout-block-item {
		margin-bottom: 32rpx;
	}

	.scroll-item {
		flex: 1;
		display: flex;
		flex-direction: column;
		margin-bottom: 32rpx;
		background-color: #fff;
		border-radius: 16rpx;
		padding: 32rpx 64rpx 40rpx 48rpx;
	}

	.planout-block-item__last {
		height: 40px;
	}

	.item-block {
		overflow: hidden;
	}

	.item-block-label {
		font-size: 24rpx;
		color: #333;
	}

	.item-block__label {
		flex: 1;
		text-align: left;
		font-size: 32rpx;
		color: #333;
		font-weight: 500;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.item-block-code {
		padding-bottom: 16rpx;
	}

	.item-block-name {
		padding-top: 24rpx;
		padding-bottom: 32rpx;
	}

	.item-block-label-des {
		flex: 1;
		text-align: left;
		font-size: 28rpx;
		color: #333;
	}

	.detal-item-label {
		color: #999;
		font-size: 24rpx;
	}

	.detal-item-value {
		color: #333;
		font-size: 28rpx;
	}

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

	.btn-text {
		color: #fff;
		font-size: 34rpx;
	}

	.loading-more {
		flex: 1;
		align-items: center;
		justify-content: center;
		padding-top: 14px;
		padding-bottom: 14px;
		text-align: center;
	}

	.loading-more-text {
		font-size: 12px;
		color: #999;
	}
</style>
