<template>
	<view class=" goods">

		<view class=" u-flex">
			<view v-if="showChecked">
				<label class="radio">
					<radio :color="ColorMain" style="transform: scale(0.7);" @click="radioClick"
						:checked="data.checked" /><text></text>
				</label>
			</view>
			<up-image width="126rpx" height="126rpx" :src="data.pic" :radius="6"></up-image>
			<!-- <image mode="aspectFill" :src="data.pic" class="img"></image> -->
			<view class=" u-p-l-16  u-flex u-p-r-16 u-flex-1 u-col-top">
				<view class="u-flex-1">
					<view class="color-333 font-bold">{{data.name}}</view>
					<view class="color-999 u-m-t-20">
						<text class="">排序:</text>
						<text class="u-m-l-10">{{data.sort}}</text>
					</view>
				</view>
				<view class="u-flex">
					<text class="u-font-28 color-666" @click="changeClick">修改排序</text>
				</view>
			</view>
		</view>

		<view class="u-m-t-24 u-flex u-row-between">
			<view>
				<!-- <template v-if="data.childrenList.length">
					<view  class="u-flex color-999 u-flex u-col-center" @tap="useTypeClick">
						<text class="u-font-24">{{computedUserType}} </text>
						<view class="u-flex">
							<uni-icons type="right" color="#999"></uni-icons>
						</view>
					</view>
				</template> -->
				<view class="u-flex">
					<view class="u-m-r-18 color-999">是否启用</view>
					<my-switch v-model="isShow" @change="isShowChange"></my-switch>
				</view>
			</view>
			<view class="u-m-t-24 u-flex u-row-right">
				<view class="btn-default btn" @click="del">删除</view>
				<view class="btn-primary btn u-m-l-38" @click="toEdit">编辑</view>
			</view>
			<!-- <view class="u-flex no-wrap">
				<view class="btn-default btn" @click="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 mySwitch from '@/components/my-components/my-switch.vue'
	import go from '@/commons/utils/go.js';
	import {
		ColorMain
	} from '@/commons/color.js'

	const emits = defineEmits(['radioClick', 'changeClick', 'useTypeClick', 'del', 'isShowChange'])

	const props = defineProps({
		index: {
			type: Number
		},
		data: {
			type: Object,
			default: () => {
				return {
					isShow: true
				}
			}
		},
		showChecked: {
			type: Boolean,
			default: false
		}
	})
	let isShow = ref(props.data.isShow)
	watch(() => props.data.isShow, (newval) => {
		isShow.value = newval
	})

	function isShowChange() {
		console.log(isShow.value);
		emits('isShowChange', {
			...props.data,
			isShow: isShow.value
		})
	}
	const computedUserType = computed(() => {
		const len = props.data.useTypes.length
		const openArr = props.data.useTypes.filter(v => v.isOpen)
		if (openArr.length <= 0) {
			return '全部隐藏'
		}
		if (openArr.length >= len) {
			return '全部显示'
		}
		let openStr = openArr.reduce((prve, cur) => {
			return prve + cur.name + '、'
		}, '')
		return '显示(' + openStr.slice(0, -1) + ')'
	})

	let isSellNone = ref(true)
	isSellNone.value = props.isSellNone

	function isSellNoneChange() {
		console.log(isSellNone.value);
		console.log('isSellNoneChange');
	}

	let checked = ref(false)

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

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

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

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

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

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

	.btn {
		padding: 6rpx 28rpx;
		border-radius: 100rpx;
		font-size: 24rpx;
		border: 1px solid transparent;
	}

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

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

	.price {
		color: $price-color;
	}

	.h-100 {
		height: $imgSize;
	}

	.img {
		width: $imgSize;
		height: $imgSize;
	}

	.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;
	}

	.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: 28rpx 42rpx;

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

			.skds {
				gap: 10rpx 50rpx;
			}

			.skd {
				padding: 14rpx 40rpx;
				background: #F0F2F5;
				border-radius: 4rpx;
				position: relative;
				color: #666;
				overflow: hidden;

				.tag {
					position: absolute;
					right: 0;
					top: 0;
					font-size: 12rpx;
					right: 0;
					padding: 2rpx 4rpx;
					border-radius: 0rpx 4rpx 4rpx 4rpx;
				}

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


		}
	}
</style>