<template>
	<view class="content">
		<!-- 分类 -->
		<view class="tabs">
			<view class="item" :class="{'show': status == '1'}" @click="onTabs('1')">已上架</view>
			<view class="item" :class="{'show': status == '3'}" @click="onTabs('3')">已下架</view>
			<view class="item" :class="{'show': status == '0'}" @click="onTabs('0')">审核中</view>
			<view class="item" :class="{'show': status == '2'}" @click="onTabs('2')">已驳回</view>
		</view>
		<!-- 优选商品 -->
		<goodsList :list="goods" priceType="CNY" :status='status' toast="暂无产品权证">
			<template v-slot:statistics="goods">
				<view>库存{{goods.value.stock}}</view>
			</template>
			<template v-slot:footer="goods">
				<view class="footer-btns">
					<block v-if="status == '1'">
						<button class="button-item" size="mini" @click="openLay(goods.value, 'goodsBurn')">燃烧</button>
						<button class="button-item" size="mini" @click="openLay(goods.value, 'goodsMint')">增发</button>
						<button class="button-item" size="mini" @click="goodsOffsale(goods.value.goods_id)">下架</button>
					</block>
					<block v-if="status == '3'">
						<button class="button-item" size="mini" @click="goodsOnsale(goods.value.goods_id)">上架</button>
					</block>
					<block v-if="status == '0' || status == '2'">
						<button class="button-item" size="mini">修改</button>
						<button class="button-item" size="mini" @click="goodsRemove(goods.value.goods_id)">删除</button>
					</block>
				</view>
			</template>
		</goodsList>
		<!-- 燃烧 增发 弹窗 -->
		<uni-popup ref="popupLay" :safe-area="true" background-color="#ffffff">
			<view class="popup">
				<view class="title">库存{{stockType == 'goodsMint' ? '增发': '燃烧'}}</view>
				<view class="des">
					剩余库存
					<text>{{itemGoods.stock}}</text>
				</view>
				<view class="des">
					数量
					<uni-number-box v-model='stock' :min="1" :max="stockType == 'goodsBurn' ? itemGoods.stock : 9999"></uni-number-box>
				</view>
				<view class="btn" @click="additionalOrBurning">确认</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import { managesGoodsIndex, managesGoodsOffsale, managesGoodsOnsale, managesGoodsBurn, managesGoodsMint, managesGoodsCreateBefore } from '@/apis/interfaces/goods'
	import goodsList from '@/components/goods-list/goods-list'
	export default {
		components: {
			goodsList
		},
		data() {
			return {
				status	  : 1,
				goods	  : [],
				pages	  : {},
				itemGoods : {},
				stock	  : 1,
				stockType : ''
			};
		},
		onShow() {
			this.getList()
		},
		methods: {
			// tabs
			onTabs(value){
				if(value == this.status) return
				this.status = value
				this.getList()
			},
			// 权证列表
			getList(){
				managesGoodsIndex({
					status: this.status
				}).then(res => {
					this.goods = res.data
					this.pages = res.page
				})
			},
			// 下架
			goodsOffsale(id){
				let index = this.goods.findIndex(val => val.goods_id == id)
				managesGoodsOffsale(id).then(res => {
					this.goods.splice(index,1)
					if(this.goods.length === 0) this.getList()
				}).catch(err => {
					uni.showToast({
						title: err.message,
						icon: 'none'
					})
				})
			},
			// 燃烧，增发
			openLay(item, type){
				this.itemGoods	= item
				this.stockType	= type
				this.stock		= 1
				this.$refs.popupLay.open('bottom')
			},
			// 提交燃烧，增发
			additionalOrBurning(){
				let data = {
					id	 : this.itemGoods.goods_id,
					stock: this.stock
				}
				switch (this.stockType){
					case 'goodsBurn':
						managesGoodsBurn({...data}).then(res => {
							let goodsIndex = this.goods.findIndex(val => val.goods_id === this.itemGoods.goods_id)
							this.itemGoods.stock -= data.stock
							this.$refs.popupLay.close()
						}).catch(err => {
							uni.showToast({
								title: err.message,
								icon : 'none'
							})
						})
						break;
					case 'goodsMint':
						managesGoodsMint({...data}).then(res => {
							let goodsIndex = this.goods.findIndex(val => val.goods_id === this.itemGoods.goods_id)
							this.itemGoods.stock += data.stock
							this.$refs.popupLay.close()
						}).catch(err => {
							uni.showToast({
								title: err.message,
								icon : 'none'
							})
						})
						break;
					default:
						uni.showToast({
							title: '类型错误',
							icon : 'none'
						})
						break;
				}
			},
			// 上架
			goodsOnsale(id){
				let index = this.goods.findIndex(val => val.goods_id == id)
				managesGoodsOnsale(id).then(res => {
					this.goods.splice(index,1)
					if(this.goods.length === 0) this.getList()
				}).catch(err => {
					uni.showToast({
						title: err.message,
						icon: 'none'
					})
				})
			}
		},
		onNavigationBarButtonTap() {
			this.$Router.push({name: 'GoodsMagAdd'})
		}
	}
</script>

<style lang="scss" scoped>
	.content{
		padding-top: 90rpx;
	}
	// tabs
	.tabs{
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		z-index: 99;
		display: flex;
		justify-content: space-around;
		background: white;
		padding: 15rpx 0;
		font-size: $title-size-lg;
		color: $text-gray;
		.item{
			height: 60rpx;
			line-height: 60rpx;
			&.show{
				color: $text-price;
				border-bottom: solid 4rpx $text-price;
			}
		}
	}
	// 按钮组
	.footer-btns{
		display: flex;
		justify-content: space-between;
		padding-top: $padding/2;
		margin-left: -10rpx;
		margin-right: -10rpx;
		.button-item[size='mini']{
			padding: 0;
			margin: 0 10rpx;
			height: 60rpx;
			line-height: 60rpx;
			border-radius: 0;
			flex: 1;
			background: $border-color-lg;
			color: $text-gray;
			&::after{
				border: none;
			}
		}
	}
	// 增发燃烧弹窗
	.popup {
		width: 100%;
		background-color: #fff;
		padding-bottom: $padding;
		.title {
			font-size: 36rpx;
			text-align: center;
			padding: 50rpx 30rpx 30rpx 30rpx;
			font-weight: bold;
		}
		.btn {
			background-color: $text-price;
			height: 90rpx;
			line-height: 90rpx;
			text-align: center;
			color: #fff;
			font-weight: bold;
			font-size: $title-size;
			margin: $padding * 2;
		}
		.des {
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			box-sizing: border-box;
			padding: $padding $padding * 2;
			color: $text-gray;
			text{
				color: $text-color;
			}
		}
	}
</style>
