<template>
	<view class="min-page bg-gray u-p-30 u-font-28">
		<view class="u-font-24">
			<text class="color-red">*</text>
			<text class="color-999">将临时菜添加至购物车，不会影响总商品库</text>
		</view>
		<view class="form">
			<uni-forms ref="refform" label-position="top" :model="form" label-align="left" label-width="300"
				:rules="rules">
				<uni-forms-item required label="" name="name">
					<template #label>
						<view class="u-text-left">
							<text class="color-333">菜品名称</text>
							<text class="color-red">*</text>
						</view>
					</template>
					<view class="border-bottom  ">
						<uni-easyinput :inputBorder="false" paddingNone v-model="form.name"
							placeholder="填写菜品名称"></uni-easyinput>
					</view>
				</uni-forms-item>
				<uni-forms-item required label="" name="category">
					<template #label>
						<view class="u-text-left ">
							<text class="color-333">菜品分类</text>
							<text class="color-red">*</text>
						</view>
					</template>
					<picker @change="categoryChange" :value="pageData.categoryCurrent" range-key="name" :range="pageData.category">
						<view class="  u-flex u-row-between border-bottom  u-relative  ">
							<view class="zhezhao u-absolute  position-all" style="z-index: 1;"></view>
						
							<view class="u-flex-1">
								<uni-easyinput :inputBorder="false" paddingNone v-model="form.category"
									placeholder="选择分类"></uni-easyinput>
							</view>
							<uni-icons type="right" size="18" color="#999"></uni-icons>
						</view>

					</picker>

				</uni-forms-item>

				<uni-forms-item required label="" name="price">
					<template #label>
						<view class="  u-text-left">
							<text class="color-333">价格(元)</text>
							<text class="color-red">*</text>
						</view>
					</template>
					<view class="border-bottom  ">
						<uni-easyinput :inputBorder="false" paddingNone v-model="form.price" placeholder="输入价格"
							type="digit"></uni-easyinput>
					</view>
				</uni-forms-item>
				<uni-forms-item required label="" name="unit">
					<template #label>
						<view class="u-text-left ">
							<text class="color-333">单位</text>
							<text class="color-red">*</text>
						</view>
					</template>
					<picker @change="unitChange" :value="units.current" range-key="name" :range="units.list">
						<view class="  u-flex u-row-between border-bottom  u-relative  ">
							<view class="zhezhao u-absolute  position-all" style="z-index: 1;"></view>
							<view class="u-flex-1">
								<uni-easyinput :inputBorder="false" paddingNone v-model="form.unit"
									placeholder="选择单位"></uni-easyinput>
							</view>
							<uni-icons type="right" size="18" color="#999"></uni-icons>
						</view>

					</picker>

				</uni-forms-item>
				<uni-forms-item required label="" name="num">
					<template #label>
						<view class="  u-text-left">
							<text class="color-333">下单数量</text>
							<text class="color-red">*</text>
						</view>
					</template>
					<view class="border-bottom  ">
						<uni-easyinput :inputBorder="false" paddingNone v-model="form.num" placeholder="填写数量"
							type="digit"></uni-easyinput>
					</view>
				</uni-forms-item>
				<uni-forms-item required label="" name="note">
					<template #label>
						<view class="  u-text-left">
							<text class="color-333">备注</text>
						</view>
					</template>
					<view class="border u-m-t-16">
						<uni-easyinput :inputBorder="false" paddingNone v-model="form.note" placeholder="请输入自定义备注"
							type="textarea"></uni-easyinput>
					</view>
				</uni-forms-item>
			</uni-forms>
		</view>

		<view style="height: 200rpx;"></view>
	</view>

	<view class="fixed-b">
		<my-button shape="circle" @tap="submit">加入购物车</my-button>
	</view>
</template>

<script setup>
	import { reactive, onMounted, ref, onBeforeMount } from 'vue';
	import { onLoad, onShow } from '@dcloudio/uni-app'
	import { getCategoryList, getProdUnitList } from '@/http/api/product.js'
	import { inject } from 'vue';
	
	const websocketUtil = inject('websocketUtil'); // 注入 WebSocket 工具类实例
	const units = reactive({
		list: [],
		current: ''
	})
	const pageData = reactive({
		category: [],
		categoryCurrent: ''
	})

	const refform = ref(null)
	const form = reactive({
		name: '',
		category: '',
		price: '',
		unit: '',
		num: ''
	})
	// 校验规则
	const rules = {
		name: {
			rules: [{
				required: true,
				errorMessage: '菜品名称不能为空'
			}]
		},
		category: {
			rules: [{
				required: true,
				errorMessage: '请选择菜品分类'
			}]
		},
		num: {
			rules: [{
				required: true,
				errorMessage: '请输入价格'
			}]
		},
		unit: {
			rules: [{
				required: true,
				errorMessage: '请选择单位'
			}]
		},
		price: {
			rules: [{
				required: true,
				errorMessage: '请填写下单数量'
			}]
		},
	}
	let timer = null
	const option = reactive({
	
	})
	onLoad((opt) => {
		Object.assign(option, opt)
		setTimeout(() => {
			getTbShopUnit()
			getCategory()
		}, 600)
	})
	
	onBeforeMount(() => {
		clearInterval(timer)
	})
	
	/**
	 * 获取单位数据
	 */
	async function getTbShopUnit() {
		const res = await getProdUnitList({
			page: 1,
			size: 200,
			sort: "id"
		})
		units.list = res.map(v => {
			return {
				...v,
				value: v.id
			}
		})
	}
	/**
	 * 获取分类数据
	 */
	async function getCategory() {
		const res = await getCategoryList({
			page: 1,
			size: 600
		})
		pageData.category = res.map(v => {
			return {
				...v,
				value: v.id
			}
		})
	}
	
	function categoryChange(e) {
		pageData.categoryCurrent = e.detail.value
		form.category = pageData.category[e.detail.value].name
	}
	
	function unitChange(e) {
		units.current = e.detail.value
		form.unit = units.list[e.detail.value].name
	}
	
	
	function submit() {
		refform.value.validate(res => {
			console.log(res)
			if (!res) {
				let params = {
					type:'onboc',
					account: uni.getStorageSync("iToken").loginId,
					shop_id: uni.getStorageSync("shopInfo").id,
					operate_type: 'add',
					table_code: option.tableCode,
					product_name: form.name,
					sku_name: form.category,			 //	
					number: form.num,			 //数量
					discount_sale_amount: form.price,			 //数量
					pack_number: 0,			 //数量
					is_gift: 0,
					remark:form.note,
					is_temporary: 1, 	 //是否是临时菜
				}
				websocketUtil.send(JSON.stringify(params))
				
				uni.$emit('add:cashCai',{
					name: form.name,
					lowPrice: form.price,
					number: form.num,
					remark:form.note,
					is_temporary: 1, 	 //是否是临时菜
				})
				clearInterval(timer)
				timer = setTimeout(() => {
					uni.navigateBack()
				}, 500)
				
				
				
				
			}
		}).catch(err => {
			console.log(err);
		})
	}

	

	
</script>

<style lang="scss" scoped>
	.u-text-left {}

	.form {
		margin-top: 32rpx;
		background-color: #fff;
		padding: 32rpx 24rpx 32rpx 24rpx;
		border-radius: 18rpx 18rpx 18rpx 18rpx;
		// background-color: transparent;
	}

	.border {
		border: 1px solid #F4F4F4;
		border-radius: 12rpx;
		overflow: hidden;
		padding-left: 24rpx;
	}

	::v-deep.uni-forms-item {
		align-items: inherit;
	}

	::v-deep.uni-forms-item__error {
		display: none;
	}

	::v-deep .uni-easyinput__content-input {
		height: inherit;
		padding-top: 16rpx;
		padding-bottom: 24rpx;
	}

	::v-deep .uni-forms-item:not(:first-child) {
		margin-top: 24rpx;
	}

	.fixed-b {
		position: fixed;
		left: 100rpx;
		right: 100rpx;
		bottom: 100rpx;
	}
</style>