<template>
	<view class="content_specs">
		<view class="main_specs">
			<view class="title_specs box-pack-between">
				<text>常用规格</text>
				<view size="mini" type="primary" class="SetupComplete flex-center" @click="Finish">设置完成</view>
			</view>
			<view class="flex wrap">
				<view class="item" v-for="(item,index) of defaultList" :key='index' @click="regular(item)">
					{{item}}
				</view>
			</view>
		</view>

		<view class="main_specs">
			<view class="title_specs">
				<text class="red">*</text>我的规格
			</view>
			<view class="box-pack-between">
				<view class="flex wrap">
					<view class="item" v-for="(item,index) of mySpecList" :key='index'>
						{{item.title}}
						<view class='del flex-center' @click="del(index,item.id)"> <text class="iconfont iconquchu"></text> </view>
					</view>
				</view>
				<view>
					<view @click="addClickSpec" v-if="!is_spec" class="addSpec flex-center">
						添加规格值
					</view>
					<block v-else>
						<form @submit="addClickFinish">
							<view class="flex flex-y-center addcontent">
								<input class="tc" type="text" confirm-type="done" @confirm="onConfirm" name='name' 
								v-model="spec_name" placeholder-class="placeholderinput" placeholder="请输入规格" />
								<button form-type="submit" class="addgou flex-center"
									style="background: none;max-width: 60rpx;">
									<image src="../static/images/gou.png"></image>
								</button>
							</view>
						</form>
					</block>
				</view>
			</view>
		</view>

		<view class="main_specs">
			<view class="title_specs">
				<text class="red">*</text>设置规格值
			</view>
			<view class="box-pack-between main_specs_item" v-for="(item,index) of mySpecList" :key='index'>
				<view class="flex flex-y-center wrap">
					<text class="item-title">{{item.title}}</text>
					<view class="item"  v-if="item.arr" v-for="(spec,idx) of item.arr" :key='idx'>
						{{spec.title}}
						<view  @click="delChildren(index,idx,spec.id)" class='del flex-center'> <text class="iconfont iconquchu"></text> </view>
					</view>
				</view>
				<view>
					<view @click="addClickSpecChild(index)" v-if="!item.is_spec_children" class="addSpec flex-center">
						添加规格值
					</view>
					<block v-else>
						<form @submit="addClickFinishChild" :data-index='index' :data-id='item.id'>
							<view class="flex flex-y-center addcontent">
								<input class="tc" v-model="item.spec_name_child" :data-index='index' :data-id='item.id' @confirm="onConfirmChild" type="text" name='name' placeholder="请输入规格" 
								placeholder-class="placeholderinput" />
								<button form-type="submit" class="addgou flex-center"
									style="background: none;max-width: 60rpx;">
									<image src="../static/images/gou.png"></image>
								</button>
							</view>
						</form>
					</block>
				</view>
			</view>
		</view>

		<view class="main_specs">
			<view class="title_specs box-pack-between">
				<view class="flex-y-center">
					<text class="red">*</text><text>设置规格详情</text>
				</view> 
				<view class="flex-y-center" @click="enit(null)">一键设置 <image class="enit" src="../static/images/enit.png"></image> </view>
			</view>
			<view class="flex main_detail box-pack-between" v-for="(item,index) of tableList" :key='index'>
				<view class="flex">
					<!-- <view class="main_round"></view> -->
					<view class="main_item">
						<view class="main_title">
							{{item.spec}}
						</view>
						<view class="">
							售价：{{item.growth}}, 库存：{{item.inventory}}
						</view>
					</view>
				</view>
				<view class="flex-y-center" @click="enit(index)">
					编辑 <image class="enit" src="../static/images/enit.png"></image>
				</view>
			</view>
		</view>
		
		<uni-popup ref="popup" type="center">
			<view class="popup-content">
				<form @submit="setup">
					  <view class="popup-content-setup">设置</view>
					  <view class="flex-center popup-content-title">
						 <text class="ft26">售价：</text>
						<view class="popup-content-price">
							<input type="digit" name='growth' placeholder="请输入价格" />
						</view> 
					  </view>
					<view class="flex-center popup-content-title">
						<text class="ft26">库存：</text> 
						<view class="popup-content-price">
							<input type="number" name='inventory' placeholder="请输入库存"/>
						</view>
					</view>  
					<button form-type="submit" class="btn" type="default">确定</button>
			   </form>
			</view>
		</uni-popup>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				defaultList: ['规格','口味','主食','套餐','款式', '尺寸', '颜色'],
				mySpecList: [],
				is_spec: false,
				is_spec_children:false,
				spec_name_child:'',
				spec_name:'',
				edit_value: null,
				tableList:[],
				set_index:null,
				id:null,
			}
		},
		onLoad(e) {
			if(e.id){
				this.id = e.id=='spec'?'':e.id;
				let {edit_value,spec,spec_val} = uni.getStorageSync('obj'+ e.id)
				console.log(uni.getStorageSync('obj'+ e.id))
				spec.forEach((v)=>{
					v.is_spec_children = false
					v.spec_name_child=''
				})
				this.edit_value = edit_value
				this.mySpecList = spec
				this.tableList = spec_val
			}
			
		},
		methods: {
			// 删除父规格
			del(index,id){
				this.request({
					url: "Smdcshop/delspec",
					data:{
						edit_value:this.edit_value,
						id,
						goods_id:this.id||'',
					},
				}).then(res => {
					if (res.code == '200') {
						this.edit_value = res.data;
						this.mySpecList.splice(index,1);
						this.setTableList();
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				});
			},
			// 删除子规格
			delChildren(index,idx,id){
				this.request({
					url: "Smdcshop/delspecson",
					data:{
						edit_value:this.edit_value,
						id,
						goods_id:this.id||'',
					},
				}).then(res => {
					if (res.code == '200') {
						this.edit_value = res.data;
						this.mySpecList[index].arr.splice(idx,1);
						this.setTableList();
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				});
			},
			
			// 完成
			Finish(e){
				
				if(!this.tableList.length){
					uni.showToast({
						title: "请设置规格值",
						icon: "none"
					})
					return
				}
				
				let hasGoodStudent = this.mySpecList.some((v, i) => v.arr.length <= 0)
				// console.log(hasGoodStudent); // true
				if(hasGoodStudent){
						uni.showToast({
							title: "规格缺少规格值",
							icon: "none"
						})
					return
				}
				let hasGoodStudentType = this.tableList.some((v, i) => Number(v.growth) <= 0)
				// console.log(hasGoodStudentType); // true
				if(hasGoodStudentType){
					uni.showToast({
						title: "库存价格不能小于0",
						icon: "none"
					})
					return
				}
				let pages = getCurrentPages(); //获取所有页面栈实例列表
				let nowPage = pages[pages.length - 1]; //当前页页面实例
				let prevPage = pages[pages.length - 2]; //上一页页面实例
				prevPage.$vm.tableList = JSON.stringify(this.tableList); //修改上一页data里面的sh参数值为100
				prevPage.$vm.edit_value =  this.edit_value;
				prevPage.$vm.mySpecList =  JSON.stringify(this.mySpecList);
				uni.navigateBack({
					delta: 1
				})
			},
			
			enit(index){
				if(!this.tableList.length && index==null){
					uni.showToast({
						title: "请设置规格",
						icon: "none"
					})
					return
				}
				this.set_index = index;
				this.$refs.popup.open();
			},
			
			// 设置价格,库存
			setup(e){
				let {growth,inventory} = e.detail.value
				if(this.set_index == null){
					this.tableList.forEach((v)=>{
						v.growth = growth
						v.inventory = inventory 
					})
				}else{
					this.tableList[this.set_index].growth = growth
					this.tableList[this.set_index].inventory = inventory 
				}
				this.$refs.popup.close();
			},
			
			addClickSpec() {
				this.is_spec = !this.is_spec
			},
			
			addClickSpecChild(index) {
				this.mySpecList[index].is_spec_children = !this.mySpecList[index].is_spec_children 
			},
			
			// 常用规格
			regular(e){
				let data = {
					value: e,
					edit_value: this.edit_value,
				}
				this.addspec(data)
			},
			
			// 新增父规格
			onConfirm({detail}){
				if(!detail.value){
					uni.showToast({
						title: "请输入规格",
						icon: "none"
					})
					return
				}
				let data = {
					value: detail.value,
					edit_value: this.edit_value,
				}
				this.addspec(data)
			},

			addClickFinish({
				detail
			}) {
				if(!detail.value.name){
					uni.showToast({
						title: "请输入规格",
						icon: "none"
					})
					return
				}
				let data = {
					value: detail.value.name,
					edit_value: this.edit_value,
				}
				this.addspec(data)
			},
			
			addspec(data){
				this.request({
					url: "Smdcshop/addspec",
					data,
				}).then(res => {
					if (res.code == '200') {
						this.mySpecList.push({
							title: data.value,
							id:res.data.id,
							is_spec_children:false,
							arr:[],
						})
						this.edit_value = res.data.edit_value
						this.is_spec = false	
						this.spec_name='';
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				});
			},

			// 新增子规格
			onConfirmChild({detail,currentTarget}){
				let {index,id} = currentTarget.dataset;
				if(!detail.value){
					uni.showToast({
						title: "请输入规格值",
						icon: "none"
					})
					return
				}
				let data = {
					value: detail.value,
					edit_value: this.edit_value,
					id,
				}
				this.addspecson(data,index);
			},
			
			addClickFinishChild({detail,currentTarget}) {
				let {index,id} = currentTarget.dataset;
				if(!detail.value.name){
					uni.showToast({
						title: "请输入规格值",
						icon: "none"
					})
					return
				}
				let data = {
					value: detail.value.name,
					edit_value: this.edit_value,
					id,
				}
				this.addspecson(data,index)
			  },
			  
			  addspecson(data,index){
					this.request({
						url: "Smdcshop/addspecson",
						data,
					}).then(res => {
						if (res.code == '200') {
							this.mySpecList[index].arr.push({
								title:data.value,
								id:res.data.id
							})
						    this.edit_value = res.data.edit_value
							this.mySpecList[index].spec_name_child=''
							this.setTableList();
						}
					}).catch((res) => {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					});
			},
	          
			   /**
			   * 设置表格数据 该方法在新增规格、新增规格值、规格和规格值的文本改变都要调用
			   */
			setTableList(){
					   let newList=[];
					   this.getRowList(null,0,newList)
					   this.tableList = newList;
						// console.log(this.tableList)
				},
				
			  /**
			   * 获取行数据 mySpecList规格集合 tableList表格集合
			   * @param spec 多规格值通过逗号拼接组成每一行唯一值
			   * @param specIndex 规格索引
			   * @param newList 新表格数据
			   */
			getRowList(spec,specIndex,newList){
				  if(specIndex < this.mySpecList.length){
					this.mySpecList[specIndex].arr.forEach((t)=>{
					  let newValues=(spec===null?t.title:(spec+":"+t.title))
					  //当前规格值，一直往下规格值循环完
					  this.getRowList(newValues,specIndex+1,newList)
					})
					//当添加新规格（第二个规格的时候）没有规格值时，默认一个空值
					if(this.mySpecList[specIndex].arr.length===0 && spec!==null){
					  let newValues = spec+":"
					  this.getRowList(newValues,specIndex+1,newList)
					}
				  }
				  //当最后规格的规格值循环到最后后添加行数据
				  else{
				   //判断表格行数据是否已经存在，存在直接添加不创建新行
					let row = this.tableList.find((r)=>{return r.spec===spec});
					// console.log(row,"row")
					
					if(row){
					  newList.push({...row})
					}
					else{
					  //默认的字段
					  row = {
						spec:spec,
						growth:0,
						inventory:0,
						vipprice:0
					  }
					  //通过规格值循环出规格值列数
					 //  spec.split(':').forEach((c,index)=>{
						// row["sku-"+index]=c
					 //  })
					  newList.push(row);
					}	
			    }
		   },	
		},
	}
</script>

<style lang="scss" scoped>
	.content_specs {
		background: #F1F1F1;
	}
	.tc{
		text-align: center;
	}
	.enit {
		width: 40rpx;
		height: 40rpx
	}
	.popup-content{
		background: #FFFFFF;
		width: 600rpx;
		height: 450rpx;
		padding: 20rpx;
		border-radius: 10rpx;
		
		.popup-content-setup{
			text-align: center;
			padding-bottom: 20rpx;
		}
		.popup-content-title{
			padding-bottom: 20rpx;
			.popup-content-price{
				padding: 15rpx;
				border: 1rpx solid #F1F1F1;
			}
		}
        .btn {	
			   width: 400rpx;
			   height: 80rpx;
		       background: linear-gradient(315deg, #3387ff 2%, #569bff 99%);
		       border-radius: 8rpx;
		       box-shadow: 0px 0px 8rpx 0px rgba(51, 135, 255, 0.2);
		       font-size: 32rpx;
		       font-weight: 500;
		       text-align: center;
		       color: #fff;
		       line-height: 80rpx;
		       margin: 0 auto;
		       margin-top:50rpx;
		       margin-bottom: 30rpx;
	   }

	}
	.SetupComplete{
		padding: 10rpx 20rpx;
		height: 60rpx;
		background: linear-gradient(315deg, #3387ff 2%, #569bff 99%);
		border-radius: 8rpx;
		box-shadow: 0px 0px 8rpx 0px rgba(51, 135, 255, 0.2);
		font-size: 32rpx;
		font-weight: 500;
		text-align: center;
		color: #fff;
	}
	.content_specs {
		.main_specs {
			padding: 20rpx 30rpx;
			background: #fff;
            margin-bottom: 1rpx;
			.title_specs {
				font-size: 30rpx;
				padding: 10rpx 0;
				margin-bottom: 20rpx;
			}

			.main_specs_item {
				margin-bottom: 20rpx;
			}
		}

		.main_detail {
			font-size: 26rpx;
			margin-bottom: 20rpx;
			border-bottom: 1px solid #F1F1F1;
			padding-bottom: 10rpx;
			.main_round {
				width: 100rpx;
				height: 100rpx;
				border: 1px dashed #ccc;
				margin-right: 20rpx;
			}

			.main_item {	
				margin-left: 25rpx;
				.main_title {
					margin-bottom: 20rpx;
					max-width: 450rpx;
				}
			}
		}
	}

	.item-title {
		font-size: 28rpx;
		margin-right: 30rpx;
	}

	.item {
		position: relative;
		border: 1px solid #ccc;
		border-radius: 50rpx;
		padding: 10rpx 20rpx;
		font-size: 26rpx;
		margin-right: 30rpx;
		margin-bottom: 4rpx;

		.del {
			position: absolute;
			right: 0;
			top: -6rpx;
			width: 30rpx;
			height: 30rpx;
			background: #DD524D;
			border-radius: 100rpx;
			color: #FFFFFF;

			.iconquchu {
				font-size: 14rpx;
			}
		}
	}

	.addSpec {
		font-size: 26rpx;
		color: #000000;
		border-radius: 40px;
		border: 1rpx dashed #ccc;
		min-width: 180rpx;
		padding: 10rpx 20rpx;
	}

	.addcontent {
		border-radius: 40px;
		font-size: 26rpx;
		max-width: 230rpx;
		min-width: 230rpx;
		border: 1rpx solid #DD524D;
		padding-left: 10rpx;
		height: 68rpx;

		.addgou {
			height: 100%;

			image {
				width: 37rpx;
				height: 29rpx;
				margin-bottom: 8rpx;
			}
		}
	}

	.red {
		color: red;
	}

	.main_specs	button {
		// display: block !important;
		background: none !important;
		height: 100% !important;
		width: 100% !important;
		border: none !important;
		border-radius: 0 !important;
		padding: 0 !important;
		margin: 0 !important;
		line-height: inherit !important;
		font-family: inherit !important;
		color: inherit !important;
		text-align: inherit;
	}

	.main_specs button:before,
	.main_specs button:after {
		border-radius: 0 !important;
		border: none !important;
	}
	.placeholderinput{
		font-size: 20rpx;
	}
</style>
