<template>
	<view class="container">
		<view class="page-block">
			<view class="page-title-block">
				<view class="title">{{table_manager}}信息</view>
			</view>
			<view class="content">
				<view class="cell flex flex-y-center">
					<view class="title-block required">
						<text>{{table_manager}}名称</text>
					</view>
					<view class="desc">
						<input type="text" v-model="name" class="input-box" placeholder="请输入" placeholder-class="desc-placeholder">
					</view>
				</view>
				<view class="cell flex flex-y-center arr-r">
					<view class="title-block required">
						<text>{{table_manager}}类型</text>
					</view>
					<view class="desc">
						<picker mode="selector" :range="tableTypeList" range-key="name" @change="onTableTypeChange">
							<view :class="[tableTypeActive == -1?'':'desc-placeholder','input-box']">
							<!-- {{tableTypeList[tableTypeActive].name || '请选择'}} -->
							
							<block v-if="tableTypeActive>-1">
							   {{tableTypeList[tableTypeActive].name}}
							</block>
							<block v-else>
								请选择
							</block>
							
							</view>
						</picker>
					</view>

				</view>
				<view class="cell flex flex-y-center">
					<view class="title-block required">
						<text>可使用人数</text>
					</view>
					<view class="desc">
						<input type="text" v-model="number" class="input-box" placeholder="请输入" placeholder-class="desc-placeholder">
					</view>
				</view>
				<view class="cell flex flex-y-center">
					<view class="title-block required">
						<text>排序</text>
					</view>
					<view class="desc">
						<input type="text" v-model="num" class="input-box" placeholder="请输入" placeholder-class="desc-placeholder">
					</view>
				</view>
			</view>
		</view>
		<view class="footer-bar">
			<view class="content flex flex-center" style="background: none;">
				<view class="btn" @click="onSubmit">{{table_id?'保存':'立即添加'}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				store_id:"",
				admin_id:"",
				table_id: "",
				tableTypeList: [],
				tableTypeActive: -1,
				table_type_id:"",
				name: "",
				number: "",
				num: 1,
				store_id:'',
				table_manager:""
			}
		},
		onLoad(options) {
			this.store_id=uni.getStorageSync('store_id')
			this.table_manager = options.table_manager
			uni.setNavigationBarTitle({
				title:`${options.table_id?'编辑':'新增'}`
			})
			if (options.table_id) {
				this.table_id = options.table_id
				let info = {}
				wx.getStorage({
				  key: 'tabDetail',
				  success:(res)=> {
					  info =res.data;
					  this.name = info.name
					  this.number = info.number
					  this.num = info.num
					  this.table_type_id = info.type_id
					  this.store_id = info.store_id
					  this.admin_id = info.admin_id
				  }
				})
			}else{
				// this.store_id = options.store_id
				this.admin_id = uni.getStorageSync('admin_id')
			}
			
			this.getTableTypeList()
		},
		methods: {
			
			onTableTypeChange(e) {
				console.log(e)
				this.tableTypeActive = e.detail.value
			},
			
			//获取桌台分类
			getTableTypeList() {
				this.request({
					url: 'Smdcshop/table_type_list',
					data: {
						store_id: this.store_id
					}
				}).then(res => {
					if (res.code == '200'||res.code == 200) {
						this.tableTypeList = res.data
						if(this.table_id){
							for(let i in res.data){
								if(res.data[i].id == this.table_type_id){
									this.tableTypeActive = i
									break;
								}
							}
						}
					}
				})
			},
			onSubmit() {
				let params = {
					name: this.name,
					num: this.num,
					number: this.number,
				}
				if (params.name == "") {
					uni.showToast({
						title: `请输入${this.table_manager}名称`,
						icon: 'none'
					})
					return false
				} else if (this.tableTypeActive == -1) {
					uni.showToast({
						title: `请选择${this.table_manager}类型`,
						icon: 'none'
					})
					return false
				} else if (params.number == "") {
					uni.showToast({
						title: "请输入就餐人数",
						icon: 'none'
					})
					return false
				} else if (params.num == "") {
					uni.showToast({
						title: "请输入排序",
						icon: 'none'
					})
					return false
				}
				params.type_id = this.tableTypeList[this.tableTypeActive].id
				
				if(this.table_id){
					//修改
					params.id = this.table_id
				}else{
					//新增
					params.admin_id = this.admin_id
					params.store_id = this.store_id
				}
				this.request({
					url: this.table_id ? 'Smdcshop/table_update':'Smdcshop/table_add',
					data: params
				}).then(res => {
					if (res.code == 200) {
						uni.showModal({
							content: this.table_id ? '编辑成功' : '添加成功',
							showCancel: false,
							success: (r) => {
								// let pages = getCurrentPages();
								// let prevPage = pages[pages.length - 2];
								//
								// // #ifdef H5
								// prevPage.getData(1)
								// // #endif
								// // #ifndef H5
								// prevPage.$vm.getData(1)
								// // #endif
								uni.navigateBack();
							}
						})
					}
				})
			},
		}
	}
</script>

<style>
	@import url("../static/css/common.css");

	page {
		background-color: #F4F5F7;
	}
</style>
