<template>
	<view class="container">
		<form @submit="onSubmit">
			<view class="page-block">
				<view class="page-title-block">
					<view class="title">标签信息</view>
				</view>
				<view class="content">

					<view class="cell flex flex-y-center arr-r" v-if="type==1">
						<view class="title-block required">
							<text>所属门店</text>
						</view>
						<view class="desc">
							<picker mode="selector" :range="tableTypeList" range-key="title"
								@change="onTableTypeChange">
								<view>
									{{tableTypeActive}}
								</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" :value="info.tag_name" name="name" class="input-box"
								placeholder="请输入标签名称" placeholder-class="desc-placeholder">
						</view>
					</view>

					<view class="cell flex flex-y-center">
						<view class="title-block requsired">
							<text>排序</text>
						</view>
						<view class="desc">
							<input type="number" name='num' :value="info.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">立即添加</view> -->
					<button class="btn" hover-class="none" form-type="submit">
						{{info.id?'修改标签':'添加标签'}}
					</button>
				</view>
			</view>
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tableTypeList: ['大桌', '中桌', '小桌'],
				tableType: "",
				store_id: "",
				info: {},
				type: 2,
				tableTypeList: [],
				tableTypeActive: '请选择',
			}
		},
		
		onLoad(e) {
			if (e.id) {
				this.info = e;
			}
			this.type = e.type || 2;
			if (e.type == 1) {
				this.shop_store_index();
			} else {
				this.store_id = uni.getStorageSync("store_id");
			}
			// this.store_id = uni.getStorageSync("store_id");
		},
		
		methods: {
			shop_store_index() {
				this.request({
					url: 'Smdcshop/shop_store_index',
					data: {
						admin_id: uni.getStorageSync("admin_id"),
					}
				}).then(res => {
					if (res.code == 200) {
						this.tableTypeList = res.data;
					}
				})
			},
			onTableTypeChange(e) {
				this.tableTypeActive = this.tableTypeList[e.detail.value].title;
				this.store_id = this.tableTypeList[e.detail.value].id;
			},
			
			onSubmit(e) {
				let params = {
						store_id: this.store_id,
						tag_name: e.detail.value.name,
						num: e.detail.value.num,
					},
					url;
				if (this.info.id) {
					params.id = this.info.id
					url = "Smdcshop/tag_update"
				} else {
					url = 'Smdcshop/tag_add'
				}
				if (params.name == "") {
					uni.showToast({
						title: "请输入名称",
						icon: 'none'
					})
					return false
				}
				this.request({
					url,
					data: params
				}).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						setTimeout(() => {
							if (this.info.id) {
								uni.navigateBack({
									delta: 2
								})
							} else {
								uni.navigateBack()
							}
						}, 2000)
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				});
			},


		}
	}
</script>

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

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