<template>
	<view class="u-p-30 min-page bg-gray">
		<view v-for="(item,index) in pageData.list" :key="index">
			<specifications-item @statusClick="specificationsStatusClick" @del="delSpecifications" @edit="editSpecifications" :data="item" :index="index"></specifications-item>
		</view>
		<view class="u-m-t-44">
			<my-pagination  :size="pageData.query.size" :totalElements="pageData.totalElements"   @change="pageChange"></my-pagination>
			<view style="height: 200rpx;"></view>
		</view>

		<my-model :showBtn="modelData.showBtn" ref="model" @confirm="modelConfirm" :title="modelData.title" :desc="modelData.desc">
			<template #desc> 
				<view class="u-p-30 u-m-t-40 u-p-b-60" v-if="specificationsData">
					<view class="u-flex u-row-between">
						<view>当前状态</view>
						<view class="u-flex-1 u-p-l-60">
							<uni-data-checkbox v-model="specificationsData.status"
							@change="specificationsStatusChange"
								:localdata="statusData"></uni-data-checkbox>
						</view>
							
					</view>
					<view class="u-m-t-30 u-p-t-30" style="border-top: 1px solid rgb(245, 245, 245);">
						<view class="u-flex u-row-between">
							<view>售罄</view>
							<my-switch @change="specificationsSellOutChange" v-model="specificationsData.sellOut"></my-switch>
						</view>
					</view>
					
				</view>
			</template>
			
		</my-model>

		<view class="fixed-b">
			<my-button @tap="toAdd" shape="circle">添加规格组</my-button>
		</view>
	</view>
</template>

<script setup>
	import {
		reactive,
		ref
	} from 'vue';
	import {onLoad,onShow} from '@dcloudio/uni-app'
	import go from '@/commons/utils/go.js';
	import myPagination from '@/components/my-components/my-pagination.vue'
	import myModel from '@/components/my-components/my-model.vue'
	import myButton from '@/components/my-components/my-button.vue'
	import mySwitch from '@/components/my-components/my-switch.vue'
	import specificationsItem from './components/specifications-item.vue'
	import {$productSpec} from '@/http/yskApi/goods.js'
	

	const statusData=[
		{
			text: '上架中',
			value: 1
		},
		{
			text: '已下架',
			value: 0
		}
	]
	
	const model = ref(null)
	const specificationsData=reactive({
		index:null,
		optionIndex:null,
		status:1,
		sellOut:false
	})
	function specificationsSellOutChange(isSellOut){
		const {index,optionIndex}=specificationsData
		if(index!==null&&optionIndex!==null){
			pageData.list[index].options[optionIndex].sellOut=isSellOut
		}
	}
	function specificationsStatusChange(e){
		const {index,optionIndex}=specificationsData
		if(index!==null&&optionIndex!==null){
			pageData.list[index].options[optionIndex].status=e.detail.value
		}
	}
	function modelOpen(){
		model.value.open()
	}
	function modelClose(){
		model.value.close()
	}
	function modelConfirm(){
		console.log('modelConfirm');
		modelClose()
	}
	const modelData = reactive({
		title: '',
		desc: '',
		showBtn:true
	})
	const pageData = reactive({
		query:{
			page: 0,
			size:10
		},
		totalElements:0,
		list: []
	})
	
	function delSpecifications(index){
		uni.showModal({
			title: '提示',
			content: '删除后使用此模板的商品将取消此规格,是否继续?',
			success: function (res) {
				if (res.confirm) {
					console.log('用户点击确定');
					$productSpec.del([pageData.list[index].id]).then(res=>{
						uni.showToast({
							title:'删除成功',
							icon:'none'
						})
						init()
					})
				} 
			}
		});
	}
	function specificationsStatusClick(arr){
		console.log(arr);
		modelData.title=pageData.list[arr[0]].name
		modelData.desc=''
		modelData.showBtn=false
		
		specificationsData.status=1
		specificationsData.sellOut=false
		specificationsData.index=arr[0]
		specificationsData.optionIndex=arr[1]
		// modelOpen()
	}
	function pageChange(page) {
		pageData.query.page=page-1
		init()
	}

	let emitName = 'guigeEdit'

	uni.$on(emitName, function(data) {
		console.log(data);
	})
	
	function editSpecifications(index){
		uni.setStorageSync('guige', pageData.list[index])
		emitName='guigeEdit'
		go.to('PAGES_PRODUCT_GUIGE_ADD', {
			emitName: emitName
		})
	}

	function toAdd() {
		// uni.setStorageSync('guige', FormData.specificationsGroup)
		go.to('PAGES_PRODUCT_GUIGE_ADD')
	}
	
	function init(){
		$productSpec.get(pageData.query).then(res=>{
			console.log(res);
			pageData.list=res.content
			pageData.totalElements=res.totalElements
		})
	}
	onShow(()=>{
		init()
	})
</script>

<style lang="scss">
	.fixed-b {
		position: fixed;
		left: 110rpx;
		right: 110rpx;
		bottom: 80rpx;
	}
</style>