<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" 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>
				<view class="content">
					<view class="cell flex flex-y-center">
						<view class="title-block required">
							<text>终端号</text>
						</view>
						<view class="desc">
							<input type="text" :value="info.device_sn" name="device_sn" 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;">
					<button class="btn" hover-class="none" form-type="submit">
						立即添加
					</button>
				</view>
			</view>
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				store_id: "",
				tableTypeList: [],
				tableTypeActive: "请选择",
				admin_id: '',
				type: 2
			}
		},

		onLoad(e) {
			this.type = e.type || 2;
			this.admin_id = uni.getStorageSync("admin_id");
			this.store_id = uni.getStorageSync("store_id");
			if (e.type == 1) {
				this.shop_store_index();
			}
		},

		methods: {
			
			shop_store_index() {
				this.request({
					url: 'Smdcshop/shop_store_index',
					data: {
						admin_id: this.admin_id,
					}
				}).then(res => {
					if (res.code == 200) {
						this.tableTypeList = res.data
					}
				})
			},

			onTableTypeChange(e) {
				console.log(e)
				this.tableTypeActive = this.tableTypeList[e.detail.value].title;
				this.store_id = this.tableTypeList[e.detail.value].id
			},
			onSubmit(e) {
				if (e.detail.value.device_sn == "") {
					uni.showToast({
						title: "请输入设备号",
						icon: 'none'
					})
					return false
				}
				this.request({
					url: 'Litepay/LotAdd',
					data: {
						store_id: this.store_id,
						device_sn: e.detail.value.device_sn,
						type: 1,
					},
					is_agent: "admin",
				}).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						setTimeout(() => {
							uni.navigateBack()
						}, 2000)
					}
				}).catch((res) => {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				});
			},


		}
	}
</script>

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

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