<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 arr-r">
						<view class="title-block required">
							<text>设备类型</text>
						</view>
						<view class="desc">
							<picker mode="selector" :range="printerAddrList" @change="onPrinterAddrChange">
								<view :class="[printerAddr?'':'desc-placeholder','input-box']">{{printerAddr || '请选择'}}
								</view>
							</picker>
						</view>
					</view>

					<view class="cell flex flex-y-center">
						<view class="title-block required">
							<text>设备号</text>
						</view>
						<view class="desc">
							<input type="num" v-model="detail.device_id" name="device_id" class="input-box"
								placeholder="请输入" placeholder-class="desc-placeholder">
                                
						</view>
                        <image src="@/static/images/saoma.png" mode="aspectFit" class="icon-saoma" @click="getDeviceId"></image>
					</view>

					<view class="cell flex flex-y-center">
						<view class="title-block requsired">
							<text>播报方式</text>
						</view>
						<view class="desc">
							<view class="flex-center">
								<view class="checklist-box " @click="choicepay(item.type)"
									v-for="(item,index) of integral_arr" :key='index'
									:class="is_integral==item.type?'actt':''">
									<view class="radio__inner" :class="is_integral==item.type?'act_border':''">
										<view :class="is_integral==item.type?'is-default-checked':''"
											class="radio__inner-icon radio__inner-icon ">
										</view>
									</view>
									<text class="checklist-text">{{item.name}}</text>
								</view>
							</view>
						</view>
					</view>

					<block v-if="device_type==3">

			<!-- 			<view class="cell flex flex-y-center">
							<view class="title-block required">
								<text>友商编号</text>
							</view>
							<view class="desc">
								<input type="text" :value="detail.agent_id" name="agent_id" 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" :value="detail.agent_secret" name="agent_secret" 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" :value="detail.sbx_id" name="sbx_id" 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" :value="detail.msg_token" name="msg_token" class="input-box"
									placeholder="请输入消息密钥" placeholder-class="desc-placeholder">
							</view>
						</view>
					</block>

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


		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tableTypeList: [],
				tableTypeActive: "请选择",
				printerAddrList: ['品生', '品生(新)', '百励华','百励华-WiFi' ],
				printerAddr: '品生',
				device_type: 1,
				tableType: "",
				admin_id: "",
				info: {},
				detail: {
                    device_id:''
                },
				integral_arr: [{
					type: 1,
					name: "收款播报",
				}, {
					type: 2,
					name: "排队播报（只支持品生新）",
				}, ],
				is_integral: 1,
				type: 2,
				store_id: "",
			}
		},
		onLoad(e) {
			if (e.id) {
				this.info = e;
				this.details();
				uni.setNavigationBarTitle({
					title: "设置云喇叭"
				})
			}
			this.type = e.type || 2;
			this.admin_id = uni.getStorageSync("admin_id");
			if (e.type == 1) {
				this.shop_store_index();
			} else {
				this.store_id = uni.getStorageSync("store_id");
			}
		},


		methods: {
            getDeviceId(){
                uni.scanCode({
                    success:res=>{
                        console.log(res)
                        this.$set(this.detail,'device_id',res.result)
                    }
                })
            },
			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) {
				this.tableTypeActive = this.tableTypeList[e.detail.value].title;
				this.store_id = this.tableTypeList[e.detail.value].id
			},

			choicepay(index) {
				this.is_integral = index;
			},

			details() {
				this.request({
					url: 'Smdcshop/horn_info',
					data: {
						id: this.info.id
					}
				}).then(res => {
					if (res.code == 200) {
						this.detail = res.data;
						
						this.printerAddr = res.data.device_type == 1 ? '品生' :res.data.device_type == 2 ? '品生(新)':res.data.device_type == 3 ?'百励华':res.data.device_type == 4?'百励华-WiFi':'';
						this.device_type = res.data.device_type;
						this.is_integral = res.data.type;
					}
				})
			},

			onPrinterAddrChange(e) {
				this.printerAddr = this.printerAddrList[e.detail.value];
				this.device_type = Number(e.detail.value) + 1;
			},

			onSubmit(e) {

				let params = {
						store_id: this.store_id,
						device_type: this.device_type,
						device_id: e.detail.value.device_id,
						num: e.detail.value.num,
						admin_id: this.admin_id,
						type: this.is_integral,
						msg_token:e.detail.value.msg_token
					},
					url;
				if (this.info.id) {
					params.id = this.info.id
					url = "Smdcshop/horn_update"
				} else {
					url = 'Smdcshop/horn_add'
				}
				if (!params.store_id) {
					uni.showToast({
						title: "请选择门店",
						icon: 'none'
					})
					return false
				}

				if (!params.device_id) {
					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(() => {
							uni.navigateBack()
						}, 2000)
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				});
			},
		}
	}
</script>

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

	page {
		background-color: #F4F5F7;
	}

	.checklist-box {

		display: -webkit-box;
		display: -webkit-flex;
		display: flex;

		-webkit-box-orient: horizontal;
		-webkit-box-direction: normal;
		-webkit-flex-direction: row;
		flex-direction: row;
		-webkit-box-align: center;
		-webkit-align-items: center;
		align-items: center;
		margin: 5px 0;
		margin-right: 22px;
	}

	.radio__inner {
		-webkit-flex-shrink: 0;
		flex-shrink: 0;

		display: -webkit-box;
		display: -webkit-flex;
		display: flex;

		-webkit-box-pack: center;
		-webkit-justify-content: center;
		justify-content: center;
		-webkit-box-align: center;
		-webkit-align-items: center;
		align-items: center;
		position: relative;
		border: 1px solid #DCDFE6;
		border-radius: 2px;
		box-sizing: border-box;
		width: 16px;
		height: 16px;
		border-radius: 16px;
		background-color: #fff;
		z-index: 1;
		-webkit-transition: border-color .3s;
		transition: border-color .3s;
	}

	.radio__inner-icon {
		width: 8px;
		height: 8px;
		border-radius: 10px;
		opacity: 0;
		-webkit-transition: -webkit-transform .3s;
		transition: -webkit-transform .3s;
		transition: transform .3s;
		transition: transform .3s, -webkit-transform .3s;
	}

	.is-default-checked {
		opacity: 1;
		background-color: #007aff;
		-webkit-transform: rotate(45deg) scaleY(1);
		transform: rotate(45deg) scaleY(1);
	}

	.checklist-text {
		font-size: 28rpx;
		margin-left: 5px;
		-webkit-transition: color 0.2s;
		transition: color 0.2s;
		color: #000;
	}

	.act_border {
		border: 1rpx solid #007aff;
	}
    .icon-saoma{
        display: block;
        width: 48rpx;
        height: 48rpx;
    }
</style>
