<template>
	<view>
		<foodItem style="width:100%;" :food="spindexSP" :shuliang="shuliang" :otherPrice="otherPrice" v-on:Ejiajian="jiajian"></foodItem>
		<danxuanlist style="width:100%;" :infolistD="skuGuige" :isedit="false" v-on:ElistDatachange="listDatachange"></danxuanlist>
		<view class="contentInRowL" style="margin-top: 10rpx;margin-left:6%;align-items: center;">
			<text>{{$t('order.beizhu')}}</text>
			<textarea class="textinput" auto-height="true"
				:always-embed='false' 
				:adjust-position='true'
				cursor-spacing='30'
				confirm-hold='true'
				confirm-type='done'
				:focus='false'
				maxlength='100'
				v-model='beizhu'
				@confirm='mineSendBT' 
			></textarea>
			
		</view>
		<view style="display: flex;width:88%;margin-left:6%; margin-bottom: 20rpx;">
			<text class="querenBt" @click="jiagouwuche()">{{$t('gouwu.jiarugouwc')}}</text>
		</view>
		<uni-popup ref="bzPop":maskClick="false">
			<view class="contentColumnC" style="width:700rpx;background-color: white;margin-left: 25rpx;border-radius: 10rpx;">
				<view class="contentInRowS" style="width: 90%;margin-top: 16rpx;">
					<text style="color: chocolate;">{{$t('order.quxiao')}}</text>
					<text style="font-weight: bold;">{{$t('order.beizhu')}}</text>
					<text style="color: #00A6FF;" @click="querenBZ">{{$t('comp.queren')}}</text>
				</view>
				<textarea class="textinput" auto-height="true"
					:always-embed='false' 
					:adjust-position='true'
					cursor-spacing='30'
					confirm-hold='true'
					confirm-type='done'
					:focus='false'
					maxlength='100'
					v-model='beizhu'
					@confirm='mineSendBT' 
				></textarea>
				<view style="height: 40rpx;"></view>
			</view>
		</uni-popup>
	</view>
</template>


<!-- 
"shId": 1, //商家id
"mdId": 1, //门店id
"amount": 1, //订单合计金额
"remarks": "", //备注
"type": 1, //订单类型：0外送，1自取，2堂食
"delryTime": "", //配送时间
"food": "" //订单商品 
-->


<script>
	export default {
		name:"pdPurchase",
		props: {
			food:'',
			mendId:''
		},
		data() {
			return {
				spindexSP:'',
				skuGuige:'',
				shuliang:1,
				otherPrice:0,
				tempsku:'',
				beizhu:'',
				gouwuche:{
					foodlist:[],
					mendid:'',
					userid:'',
					mendname:''
				}
			};
		},
		watch: {
			food(val){
				//console.log(this.food);
				this.spindexSP = this.food;
				this.skuGuige = this.food.shangpin.foodSku;
				this.otherPrice = this.getotherPrice(this.skuGuige);
				this.tempsku = this.gettempSKU(this.skuGuige);
				this.beizhu='';
				this.shuliang = this.food.shuliang==0?1:this.food.shuliang;
				this.$refs.bzPop.close();
			}
		},
		methods: {
			jiagouwuche(){
				this.gengxingouwuche();
				this.$emit('Ejiagouwuche');
			},
			
			gengxingouwuche(){
				if(this.shuliang<1){
					uni.showToast({
					    title: this.$t('comp.qingxzsp'),
					    icon: 'none',
					    duration: 2500
					})
					return;
				}
				var sp = this.food.shangpin;
				var gwc = uni.getStorageSync('gouwuche');
				if(''==gwc||null==gwc||undefined==gwc){
					
					var item={
						"shId":sp.store.userId, //商家id
						"mdId": sp.store.id, //门店id
						"logo":sp.store.logo,
						"posName":sp.store.posName,
						"lat": sp.store.latitude, //
						"lng": sp.store.longitude, //
						"jvli": 0, //
						"freight": 0, //
						"amount": 0, //订单合计金额
						"foodAmount": 0, //商品金额
						"remarks": "", //备注
						"type": 0, //订单类型：0外送，1自取，2堂食
						"zuohao":'',
						"delryTime": "", //配送时间
						"food":[] //订单商品 
					}
					this.gouwuche=[];
					this.gouwuche.push(item);
					
				}
				else{
					this.gouwuche = JSON.parse(gwc);
				}
				
				var mark=0;
				for(var i=0;i<this.gouwuche.length;i++){
					var item = this.gouwuche[i];
					if(item.mdId==sp.store.id){
						mark = 1;
						for(var j=0;j<item.food.length;j++){
							var gwf = item.food[j];
							var ask = JSON.stringify(gwf.ask);
							var tempsku = JSON.stringify(this.tempsku);
							if(gwf.id == sp.id &&ask === tempsku){
								item.food[j].number = this.shuliang;
								this.tongjigouwuche();
								return;
							}
						}
						var f={
							"id": sp.id,
							"name": sp.name, 
							"image": sp.image, 
							"price": sp.price, 
							"otherPrice":this.otherPrice,
							"number": this.shuliang, 
							"ask": this.tempsku,
							"beizhu":this.beizhu
						}
						item.food.push(f);
						this.gouwuche[i]=item;
						this.tongjigouwuche();
						return;
					}
				}
				
				if(mark==0){
					
					var item={
						"shId":sp.store.userId, //商家id
						"mdId": sp.store.id, //门店id
						"logo":sp.store.logo,
						"posName":sp.store.posName,
						"lat": sp.store.latitude, //
						"lng": sp.store.longitude, //
						"jvli": 0, //
						"freight": 0, //
						"amount": 0, //订单合计金额
						"foodAmount": 0, //商品金额
						"remarks": "", //备注
						"zuohao":'',
						"type": 0, //订单类型：0外送，1自取，2堂食
						"delryTime": "", //配送时间
						"food":[] //订单商品 
					}
					var f={
						"id": sp.id,
						"name": sp.name, 
						"image": sp.image, 
						"price": sp.price, 
						"otherPrice":this.otherPrice,
						"number": this.shuliang, 
						"ask": this.tempsku,
						"beizhu":this.beizhu
					}
					item.food.push(f);
					this.gouwuche.push(item);
					this.tongjigouwuche();
				}
			},
			
			tongjigouwuche(){
				for(var i=0;i<this.gouwuche.length;i++){
					var item = this.gouwuche[i];
					var goumaijine=0;
					for (var j=0;j<item.food.length;j++) {
						var food =  item.food[j];
						var yidongjine=food.number*(parseFloat(food.price)+parseFloat(food.otherPrice));
						goumaijine = goumaijine+yidongjine;
					}
					this.gouwuche[i].foodAmount=goumaijine;
					this.gouwuche[i].amount=goumaijine;
					
				}
				let tgwc = JSON.stringify(this.gouwuche);
				uni.setStorageSync('gouwuche',tgwc);
			},
			
			jiajian(e){
				this.shuliang = this.shuliang+e; 
				console.log(this.food);
			},
			listDatachange(listData){
				this.tempsku = this.gettempSKU(listData);
				this.otherPrice = this.getotherPrice(listData);
				//console.log(this.tempsku,this.otherPrice)
			},
			gettempSKU(foodSku){
				var selectsku=[];
				if(''==foodSku||null==foodSku||undefined==foodSku){
					return selectsku;
				}
				for(var i=0;i<foodSku.length;i++){
					if(foodSku[i].objects.length>0){
						for(var j=0;j<foodSku[i].objects.length;j++){
							if(foodSku[i].objects[j].state==1){
								selectsku.push(foodSku[i].objects[j].name);
							}
						}
					}
				}
				return selectsku;
			},
			getotherPrice(foodSku){
				console.log('getotherPrice',foodSku);
				var jiner=0;
				if(''==foodSku||null==foodSku||undefined==foodSku){
					return 0;
				}
				for(var i=0;i<foodSku.length;i++){
					if(foodSku[i].objects.length>0){
						for(var j=0;j<foodSku[i].objects.length;j++){
							if(foodSku[i].objects[j].state==1){
								if(foodSku[i].objects[j].price==0||!foodSku[i].objects[j].price){
									jiner=jiner+0;
								}
								else{
									jiner=jiner+parseFloat(foodSku[i].objects[j].price);
								}
							}
						}
					}
				}
				if(parseFloat(jiner)){
					console.log('111',jiner);
					return parseFloat(jiner);
				}
				else{
					console.log('222',jiner);
					return 0;
				}
			},
			editBeizhu(){
				console.log('11111111')
				this.$refs.bzPop.open('top');
			},
			mineSendBT(){
				console.log(this.beizhu)
				this.$refs.bzPop.close();
			},
			querenBZ(){
				console.log(this.beizhu)
				this.$refs.bzPop.close();
			},
		},
	}
</script>

<style>
.querenBt{
	margin-top: 30rpx;
	width: 100%;
	height: 60rpx;
	line-height: 60rpx;
	padding: 10rpx;
	border-radius: 20rpx;
	border: solid #40AE36;
	border-width: 0px 0px 6rpx 0px;
	text-align: center;
	justify-content: center;
	background-color: #40AE36;
	color: white;
	box-shadow: 0rpx 0rpx 10rpx 0rpx #40AE36;
}
.beizhuBt{
	padding: 10rpx 16rpx 10rpx 16rpx;
	color: white;
	border-radius: 8rpx;
	background-color:chocolate;
}
.beizhutext{
	color:darkgray;
	font-size: 30rpx;
	margin-left: 30rpx;
	width: 500rpx;
}
.textinput{
	margin-left: 8rpx;
	padding-top: 10rpx;
	padding-bottom: 10rpx;
	width: 80%;
	border-radius: 6rpx;
	border: 1rpx solid darkgray;
	font-size: 32upx;
	background-color: white;
}

</style>