<template>
	<view class=" goods">
		<view class="u-flex u-row-between">
			<view class="u-flex">
				<view class="color-333">
					<text class="">排序</text>
					<text class="u-m-l-20">{{data.sort}}</text>
				</view>
				<view class="color-333 u-m-l-42 u-flex">
					<up-icon @click="editStock" name="edit-pen" :size="16" :color="ColorMain"></up-icon>
					<text class="stock u-m-l-4">库存:{{data.stockNumber}}</text>
				</view>
			</view>
			<view>
				<!-- <text class="u-font-28 color-666" @click="changeClick">修改</text> -->
				<text class="u-font-28 color-666" @click="changePrice">改价</text>
				<text class="u-font-28 color-red u-m-l-24" @click="baosun">报损</text>
			</view>
		</view>


		<view class="u-m-t-48 u-flex u-col-top u-relative">
			<view v-if="props.showChecked">
				<label class="radio">
					<radio :color="ColorMain" style="transform: scale(0.7);" @click="radioClick"
						:checked="props.data.checked" /><text></text>
				</label>
			</view>
			<view class="img">
				<up--image :width="63" :height="63" :radius="3" :src="data.coverImg"></up--image>
			</view>
			<view class="w-full info">
				<view class="info-p-l color-333  u-flex u-row-between">
					<view class="u-flex">
						<text class="u-m-r-24">{{data.name}}</text>
						<!-- 		<uni-tag size="small" type="primary"
							custom-style="background-color: #318AFE;" :text="data.typeEnum"></uni-tag> -->
					</view>
					<view class="u-font-32">
						<text v-if="data.typeEnum=='单规格'">¥</text>
						<text>{{data.lowPrice}}</text>
						<!-- <text>¥</text>
						<text>{{data.lowPrice}}</text>
						<text v-if="data.typeEnum == '多规格'">~￥{{data.maxPrice }}</text> -->
					</view>
				</view>
				<view class="u-m-t-24">
					<template v-if="data.skuList.length>=2">
						<view class="u-flex u-flex-wrap w-full gap-10  u-col-top" :style="skuStyle">
							<view class="u-font-24 info-p-l u-m-t-6">规格：</view>
							<view class="skd" v-for="(item,index) in data.skuList" :key="index"
								@click="guigeClick(index)">
								<text>{{item.specSnap||item.name}}</text>
								<view class="tag-primary tag" v-if="item.isGrounding">上架中</view>
								<view class="tag-gray tag" v-if="item.isPauseSale">已售罄</view>
								<view class="tag-gray tag" v-if="!item.isGrounding">已下架</view>
								
								<!-- <template v-if="item.isPauseSale">
									<view class="tag-gray tag" >已售罄</view>
								</template>
								<template v-else>
									<view class="tag-primary tag" v-if="item.isGrounding">上架中</view>
									<view class="tag-gray tag" v-else>已下架</view>
								</template> -->

							</view>
						</view>
						<template v-if="skuIsNotOne">
							<view class="u-flex u-row-center">
								<view class="u-flex u-flex-y-center" @click="toggleIsShowSkuAll" >
									<view class="u-font-28 u-m-r-10 u-m-t-10 color-666">{{isShowSkuAll?'收起':'展开'}}</view>
									<view class="u-flex u-flex-y-center">
										<up-icon :size="14" name="arrow-down" v-if="!isShowSkuAll"></up-icon>
										<up-icon  :size="14" name="arrow-up" v-else></up-icon>
									</view>
								</view>
								
							</view>
						</template>
					</template>
					<template v-else>
						<view style="height: 44rpx;">

						</view>
					</template>
				</view>


			</view>
		</view>

		<!-- <view class="u-m-t-16 skus u-text-center" v-if="data.skuList.length>=2">
			<view class="u-flex u-flex-wrap  skds">
				<view class="skd" v-for="(item,index) in data.skuList" :key="index"><text>{{item.specSnap}}</text>
					<view class="tag-primary tag">上架中</view>
				</view>
			</view>

		</view> -->
		<view class="u-m-t-24 u-flex u-row-between">
			<view class="u-flex">
				<view class="u-flex">
					<view class="u-m-r-18 color-999">售罄</view>
					<my-switch disabled v-model="isPauseSale" :openDisabledClass="false"  @click="isPauseSaleChange"></my-switch>
				</view>
				<view class="u-flex u-m-l-30">
					<view class="u-m-r-18 color-999">上架产品</view>
					<my-switch disabled v-model="isGrounding" :openDisabledClass="false" @click="isGroundingChange"></my-switch>
				</view>
			</view>
			<view class="u-flex">
				<!-- <view class="btn-default btn" @tap="xiajia">下架商品</view> -->
				<!-- <view class="btn-default btn" @tap="del">删除</view> -->
				<view class="btn-primary btn u-m-l-38" @click="toEdit">编辑</view>
			</view>
		</view>

	</view>
</template>

<script setup>
	import {
		computed,
		ref,
		watch,
		watchEffect
	} from 'vue';
	import {
		$goodsIsHot,
		$tbProskuConV2,$updateProductData
	} from '@/http/yskApi/goods.js'
	import go from '@/commons/utils/go.js';
	import {hasPermission} from '@/commons/utils/hasPermission.js';
	import {
		ColorMain
	} from '@/commons/color.js'
	const emits = defineEmits(['radioClick', 'changeClick', 'xiajia', 'del', 'changePrice', 'baosun', 'guigeClick','update',
		'editStock'
	])
	const props = defineProps({
		index: {
			type: Number
		},
		data: {
			type: Object,
			default: () => {
				return {
				}
			}
		},
		showChecked: {
			type: Boolean,
			default: false
		},
		showDetail: {
			type: Boolean,
			default: false
		}
	})
	
	let isShowSkuAll=ref(false)
	const skuStyle=computed(()=>{
		if(isShowSkuAll.value){
			return ''
		}else{
			return 'height: 30px;overflow: hidden'
		}
	})
	const skuIsNotOne=computed(()=>{
		let isOne=true;
		let width=0;
		const fontSize=12;
		const gap=5;
		const boxWith=40;
		const max=247;
		for(let i in  props.data.skuList){
			const sku=props.data.skuList[i]
			width+=(fontSize*sku.name.length+boxWith+gap)
			if(width>max){
				isOne=false
				break;
			}
		}
		return !isOne
	})
	
	function toggleIsShowSkuAll(){
		isShowSkuAll.value=!isShowSkuAll.value
	}

	async function upDateGoods(par) {
		const res = await $updateProductData([{
			id: props.data.id,
			isSku: 0,
			shopId: uni.getStorageSync('shopId'),
			...par
		}])
		uni.showToast({
			title: '修改成功',
			icon: 'none'
		})
		emits('update')
	}
	
	let isPauseSale=ref(props.data.isPauseSale)
	let isGrounding=ref(props.data.isGrounding)
	watch(() => props.data.isPauseSale, (newval) => {
		isPauseSale.value=newval
	})
	watch(() => props.data.isGrounding, (newval) => {
		isGrounding.value=newval
	})

	async function isPauseSaleChange(e) {
		const res=await hasPermission('允许售罄商品')
		if(!res){
			return
		}
		upDateGoods({
			key: 'pauseSale',
			value: isPauseSale.value?0:1
		})
	}
	async function isGroundingChange(e) {
		const res=await hasPermission('允许上下架商品')
		if(!res){
			return
		}
		upDateGoods({
			key: 'grounding',
			value: isGrounding.value?0:1
		})
	}

	let checked = ref(false)


	function radioClick() {
		console.log(props.index);
		emits('radioClick', props.index)
	}

	function changeClick() {
		emits('changeClick', props.index)
	}
	
	function reportDamage () {
		emits('reportDamage', props.index)
	}
	function xiajia() {
		emits('xiajia', props.index)
	}

	function del() {
		emits('del', props.index)
	}

	async function changePrice() {
		const res=await hasPermission('允许修改商品')
		if(!res){
			return
		}
		emits('changePrice', props.index)
	}

	function baosun() {
		emits('baosun', props.index)
	}

	function guigeClick(guigeIndex) {
		emits('guigeClick', props.index, guigeIndex)
	}

	function editStock() {
		emits('editStock', props.index)
	}
	//携带参数type edit跳转到商品添加页面，编辑与添加同一页面，根据type值来判断
	function toEdit() {
		emits('edit', props.data.id)
		// go.to('PAGES_PRODUCT_ADD', {
		// 	type: 'edit',
		// 	productId: props.data.id
		// })
	}
</script>

<style lang="scss" scoped>
	$imgSize: 126rpx;
	$price-color: #F02C45;

	.btn {
		padding: 6rpx 28rpx;
		border-radius: 100rpx;
		border: 2rpx solid transparent;
	}

	.gap-10 {
		gap: 10rpx;
	}

	.btn-primary {
		border-color: $my-main-color;
		color: $my-main-color;
	}

	.btn-default {
		border-color: #F4F4F4;
		color: #999;
	}

	.price {
		color: $price-color;
	}

	.h-100 {
		height: $imgSize;
	}

	.img {
		position: absolute;
		left: 0;
		top: 0;
		// width: $imgSize;
		// height: $imgSize;
	}

	.info-p-l {
		padding-left: 71px;
	}

	.icon-arrow-right {
		width: 32rpx;
		height: 32rpx;
	}

	.stock {
		// padding-right: 46rpx;
		position: relative;
	}

	.stock::after {
		// content: '';
		// position: absolute;
		// right: 10rpx;
		// top: 50%;
		// transform: translateY(-50%);
		// display: block;
		// width: 16rpx;
		// border: 2rpx solid #333333;
	}

	.color-red {
		color: #F0465B;
	}

	.goods {
		border-radius: 10rpx 10rpx 10rpx 10rpx;
		background-color: #fff;
		padding: 24rpx 28rpx 16rpx 28rpx;
		font-size: 28rpx;

		.skus {
			background: #F9F9F9;
			border-radius: 14rpx 14rpx 14rpx 14rpx;
			padding: 20rpx;

			.sku {
				color: #000;
				font-weight: 700;
				padding: 6rpx 40rpx;
			}

			.skds {
				gap: 10rpx 50rpx;
			}
		}
	}

	.skd {
		padding: 10rpx 38rpx 8rpx 40rpx;
		background: #F0F2F5;
		border-radius: 4rpx;
		position: relative;
		color: #666;
		overflow: hidden;
		margin-bottom: 10rpx;
		font-size: 24rpx;

		.tag {
			position: absolute;
			right: 0;
			top: 0;
			font-size: 12rpx;
			height: 18rpx;
			line-height: 18rpx;
			right: 0;
			border-radius: 0rpx 2rpx 2rpx 8rpx;
		}

		.tag-primary {
			background-color: $my-main-color;
			color: #fff;
		}

		.tag-gray {
			background-color: rgb(144, 147, 153);
			color: #fff;
		}
	}
</style>