<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="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>
					</view>
					<view class="u-font-32">
						<text v-if="data.type=='single'">¥</text>
						<text>{{data.lowPrice}}</text>
					
					</view>
				</view>
				<view class="u-m-t-24">
					<template v-if="data.skuList.length>=1">
						<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.specInfo}}</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>
								

							</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-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="isSoldStock" :openDisabledClass="false"  @click="isSoldStockChange"></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="isSale" :openDisabledClass="false" @click="isSaleChange"></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 } from 'vue';
	import { updateProduct,productOnOff,productMarkIsSoldOut } from '@/api/product.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]
			console.log(sku)
			width+=(fontSize*sku.specInfo.length+boxWith+gap)
			if(width>max){
				isOne=false
				break;
			}
		}
		return !isOne
	})
	
	function toggleIsShowSkuAll(){
		isShowSkuAll.value=!isShowSkuAll.value
	}

	async function upDateGoods(par) {
		const res = await updateProduct([{
			id: props.data.id,
			...par
		}])
		uni.showToast({
			title: '修改成功',
			icon: 'none'
		})
		emits('update')
	}
	
	let isSoldStock=ref(props.data.isSoldStock)
	let isSale=ref(props.data.isSale)
	watch(() => props.data.isSoldStock, (newval) => {
		isSoldStock.value=newval
	})
	watch(() => props.data.isSale, (newval) => {
		isSale.value=newval
	})
	
	/**
	 * 修改售罄
	 * @param {Object} e
	 */
	async function isSoldStockChange(e) {
		const res=await hasPermission('允许售罄商品')
		if(!res){
			return
		}
		let res2 = await productMarkIsSoldOut({
			id: props.data.id,
			type: 'product',
			isSoldOut: isSoldStock.value?0:1
		})
		uni.showToast({
			title: '修改成功',
			icon: 'none'
		})
		emits('update')
	}
	
	
	
	
	/**
	 * 修改上下价
	 * @param {Object} e
	 */
	async function isSaleChange(e) {
		const res=await hasPermission('允许上下架商品')
		if(!res){
			return
		}
		let res2 = await productOnOff({
			id: props.data.id,
			type: 'product',
			isSale: isSale.value?0:1
		})
		uni.showToast({
			title: '修改成功',
			icon: 'none'
		})
		emits('update')
	}

	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>