<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">
						<view class="container" >
							<view class="pic" style="position: relative;border-radius: 10upx;overflow: hidden;">
								<image mode="aspectFit" class="img" :src="staticUrl+item.photo" ></image>
								<view class="flex-wrap ali-c jus-c pic2" v-if='item.audit==0'>审核中</view>
							</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?activity_type='+this.activity_type" >
							<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>
		
		
		<!-- 高级搜索弹窗 -->
		<uni-popup :isnonehover="false"  type="top" :show="isshowsearch" @change="changepupse"  >
			<view class="Advancedsearch">
				<view class="searcell">
					<view class="celltit">商品名称</view>
					<view class="cellsinput">
						<input class="input" v-model="searchtit" type="text"  placeholder="商品名称关键词" ></input>
					</view>
				</view>
				<view class="subsearbox flex ali-c jus-c">
					<view class="searbtn1" @tap="searchreset">重置</view>
					<view class="searbtn1 searbtn2" @tap="submitsear">确定</view>
				</view>
			</view>
		</uni-popup>
		
		<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"
	import uniPopup from '@/components/uni-popup/uni-popup.vue';
	export default {
		components: {
			uniSegmentedControl,
			pageScrollTo,
			uniPopup
		},
		data() {
			return {
				list: [],
				items: ['上架商品', '下架商品'],
				current: 0,
				page:0,//分页
				goodsListdata:[],//商品列表
				isshowtop:false,
				isshowall:true,
				isselect:"2",
				sendindex:"",
				money_type:"",
				activity_type:0,
				searchtit:"",//搜索的关键字
				isshowsearch:false,
			}
		},
		onLoad(opdata) {
			this.money_type=opdata.money_type;
			
			this.activity_type = opdata.activity_type//秒杀资格判断
			
			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();
			}
		},
		//标题栏变点击
		onNavigationBarButtonTap(e){
			if(e.index==0){
				//显示高级搜索
				this.isshowsearch = !this.isshowsearch;
			}
		},
		methods: {
			//重置搜索
			searchreset(){
				this.searchtit = "";
				this.isshowsearch = false;//关闭弹窗
				this.page =0;
				this.goodsListdata = [];
				this.isshowall = true;
				this.getgoodsListfun();
			},
			//确认搜索
			submitsear(){
				this.isshowsearch = false;//关闭弹窗
				this.page =0;
				this.goodsListdata = [];
				this.isshowall = true;
				this.getgoodsListfun();
			},
			//搜索弹窗
			changepupse(e){
				this.isshowsearch = e.show;
			},
			//获取商城商品列表
			async getgoodsListfun(){
				this.page++;
				let res = await goodsList({
					closed:this.current,//状态（0上架，1下架）
					page:this.page,
					title:this.searchtit,
				});
				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)
					}
				})

			},
			//删除
			delfun(items,index) {
				//请求 删除商品的接口
				
				this.$showModal({
					title:'温馨提示',
					concent:'你确定要删除此产品吗？',
					cancelVal:'取消',
					confirmVal:'确定',
				}).then(res=>{
					//安卓 ok
					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();
					})
				})
				
			},
			//上架
			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)
					}
				})
				
				
			},
			
			
			//编辑产品
			jump_edit(item) {
				uni.navigateTo({
					//url: '/pages/edit/product/product?id=' + id
					url:"/pages/edit/product/addshop?id="+item.goods_id+'&money_type='+this.money_type+'&activity_type='+this.activity_type,
				})
			},
			
			//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){
				
				//选择商品卡回调
				if(this.isselect==1){
					uni.setStorageSync('selectaddres', {data,sendindex:this.sendindex});//存
					uni.navigateBack({//返回上一页
						delta: 1
					});
				}
			}
		}
	}
</script>

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