<template>
	<!-- 商品/产品管理 -->
	<view class="manage">
		<view class="tabberbox">
			<uni-segmented-control :current="current" :values="items" @clickItem="onClickItem" style-type="text" active-color="#ff6900"></uni-segmented-control>
		</view>
		<view class="content">
			<view>
				<view class="one">
					<block v-for="(item,index) in goodsListdata" :key="index">
						<!-- v-if="item.state!=current" -->
						<view class="container" >
							<view class="pic"><image mode="aspectFit" class="img" :src="staticUrl+item.photo" ></image></view>
							<view class="desc">
								<view class="title">
									<view class='title_l'>{{item.title}}</view>
								</view>
								<view class="price">¥ {{item.mall_price}}</view>
								<view class="kc">
									<view>库存：{{item.num}}</view>
									<view class="xiaol">销量：{{item.sold_num}}</view>
								</view>
								<view class="btn">
									<view class="btn_01" @tap="jump_edit(item)" >编辑</view>
									<view class="btn_01" @tap="pullfun(item,index)" v-if="current==0" >下架</view>
									<view class="btn_01" @tap="pushfun(item,index)" v-if="current==1" >上架</view>
									<view class="btn_01" @tap="delfun(item,index)" >删除</view>
									<view class="btn_01" @click="setSpecs(item.goods_id)" >规格</view>
									<view class="btn_01 "  @tap="Selectiondata(item)"  v-if="current==0 && isselect==1">加入活动</view>
								</view>
							</view>
						</view>
					</block>

					<view class="tjsp">
						<navigator url="/pages/edit/product/addshop">
							<view class="flex flex-direction">
								<button class="cu-btn bg-red margin-tb-sm lg">添加商品</button>
							</view>
						</navigator>
					</view>
				</view>
			</view>
		</view>

		<view style="height:100px;"></view>
		
		<pageScrollTo v-if="isshowtop" ></pageScrollTo>
		
	</view>
</template>

<script>
	import uniSegmentedControl from "@/components/uni/uni-segmented-control/uni-segmented-control.vue"
	import {goodsList,deleteGoods,autoManageGoods} from "@/utils/api/api.js";
	import pageScrollTo from "@/components/pageScrollTo/pageScrollTo.vue"
	export default {
		components: {
			uniSegmentedControl,
			pageScrollTo
		},
		data() {
			return {
				list: [],
				items: ['上架商品', '下架商品'],
				current: 0,
				page:0,//分页
				goodsListdata:[],//商品列表
				isshowtop:false,
				isshowall:true,
				isselect:"2",
				sendindex:"",
			}
		},
		onLoad(opdata) {
			if(opdata.isselect==1){
				this.isselect = 1;
				this.sendindex = opdata.sendindex;//插入索引
			}else{
				this.isselect = 2;
			}
		},
		onPageScroll(e){
			//处理返回顶部
			if(e.scrollTop>400){
				this.isshowtop = true;
			}else{
				this.isshowtop = false;
			}
		},
		onShow() {
			this.page =0;
			this.goodsListdata = [];
			this.isshowall = true;
			this.getgoodsListfun();
		},
		computed:{
			staticUrl(){
				return this.$store.state.staticUrl;
			}
		},
		//瀑布流
		onReachBottom() {
			if(this.isshowall){
				this.getgoodsListfun();
			}
		},
		methods: {
			//获取商城商品列表
			async getgoodsListfun(){
				this.page++;
				let res = await goodsList({
					closed:this.current,//状态（0上架，1下架）
					page:this.page,
				});
				if(res.code==0){
					
					if(res.data.length!=0){
						this.goodsListdata = [...this.goodsListdata,...res.data];
					}else{
						this.$api.msg("没有更多了哦~");
						this.isshowall = false;
					}
					
				}else{
					this.$api.msg(res.msg);
				}
			},
			//下架方法 商城商品上下架 下架
			pullfun(items,index) {
				//this.$api.msg("下架");
				//上下架同
				autoManageGoods({
					goods_id:items.goods_id,
					closed:"1",//上下架（0上架，1下架）
				}).then((res)=>{
					if(res.code==0){
						this.$api.msg(res.msg);
						
						this.goodsListdata.splice(index,1);//本地删除
					}else{
						this.$api.msg(res.msg)
					}
				})
				
				// const that = this
				// uni.showModal({
				// 	title: '确定下架？',
				// 	confirmColor:"#ff6900",
				// 	success:(res)=> {
				// 		if (res.confirm == true) { 
				// 			//上下架同
				// 			autoManageGoods({
				// 				goods_id:items.goods_id,
				// 				closed:"1",//上下架（0上架，1下架）
				// 			}).then((res)=>{
				// 				if(res.code==0){
				// 					this.$api.msg(res.msg);
									
				// 					this.goodsListdata.splice(index,1);//本地删除
				// 				}else{
				// 					this.$api.msg(res.msg)
				// 				}
				// 			})
							
							
				// 		}
				// 	}
				// })
			},
			//删除
			delfun(items,index) {
				//请求 删除商品的接口
				deleteGoods({
					goods_id:items.goods_id,
				}).then((res)=>{
					
					
					if(res.code==0){
						this.$api.msg(res.msg);
						// this.goodsListdata.splice(index,1);//本地删除
						
						
					}else{
						this.$api.msg(res.msg)
					}
					
					//刷新列表
					this.goodsListdata = [];
					this.page = 0;
					this.isshowall = true;
					this.getgoodsListfun();
					
					
				})
				
				// this.$api.msg("删除");
				// uni.showModal({
				// 	title: '确定删除？',
				// 	confirmColor:"#ff6900",
				// 	success:(res)=> {
				// 		if (res.confirm == true) {
							
				// 			//请求 删除商品的接口
				// 			deleteGoods({
				// 				goods_id:items.goods_id,
				// 			}).then((res)=>{
				// 				if(res.code==0){
				// 					this.$api.msg(res.msg);
				// 					// this.goodsListdata.splice(index,1);//本地删除
									
				// 					//刷新列表
				// 					this.goodsListdata = [];
				// 					this.page = 0;
				// 					this.isshowall = true;
				// 					this.getgoodsListfun();
				// 				}else{
				// 					this.$api.msg(res.msg)
				// 				}
								
								
				// 			})
				// 		}
				// 	}
				// })
			},
			//上架
			pushfun(items,index) {
				//上下架同
				autoManageGoods({
					goods_id:items.goods_id,
					closed:"0",//上下架（0上架，1下架）
				}).then((res)=>{
					if(res.code==0){
						this.$api.msg(res.msg);
						this.goodsListdata.splice(index,1);//本地删除
					}else{
						this.$api.msg(res.msg)
					}
				})
				
				// this.$api.msg("上架");
				// const that = this
				// uni.showModal({
				// 	title: '确定上架？',
				// 	success:(res)=> {
				// 		if (res.confirm == true) {
				// 			//上下架同
				// 			autoManageGoods({
				// 				goods_id:items.goods_id,
				// 				closed:"0",//上下架（0上架，1下架）
				// 			}).then((res)=>{
				// 				if(res.code==0){
				// 					this.$api.msg(res.msg);
				// 					this.goodsListdata.splice(index,1);//本地删除
				// 				}else{
				// 					this.$api.msg(res.msg)
				// 				}
				// 			})
							
				// 		}
				// 	}
				// })
			},
			//编辑产品
			jump_edit(item) {
				uni.navigateTo({
					//url: '/pages/edit/product/product?id=' + id
					url:"/pages/edit/product/addshop?id="+item.goods_id,
				})
			},
			//tab切换事件
			onClickItem(index) {
				if (this.current !== index) {
					this.current = index;
					
					this.goodsListdata = [];
					this.page = 0;
					this.isshowall = true;
					this.getgoodsListfun();
				}
			},
			setSpecs(id){
				//处理规格
				uni.navigateTo({
					url:`/pages/specsShop/specsShop?id=${id}`,
				})
			},
			//处理 分享
			share(id){
				
			},
			//活动抽奖选择商品
			Selectiondata(data){
				console.info(data);
				
				//选择商品卡回调
				if(this.isselect==1){
					uni.setStorageSync('selectaddres', {data,sendindex:this.sendindex});//存
					uni.navigateBack({//返回上一页
						delta: 1
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	@import  "./pro_manage.scss";
</style>
