<template>
	<view class="container">
		<form @submit="onSubmit">
			<view class="page-block">
				
				<view class="content">
	
					<view class="cell flex flex-y-center">
						<view class="title-block required">
							<text>标题</text>
						</view>
						<view class="desc">
							<input type="num" :value="info.name" name="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>跳转方式</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" v-if="device_type!=0">
						<view class="title-block requsired">
							<text>跳转链接</text>
						</view>
						<view class="desc">
							<input type="num" :value="info.url" name="url" 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" :value="info.num" class="input-box" name='num' placeholder="请输入"
								placeholder-class="desc-placeholder">
						</view>
					</view>	
				</view>
	
				<view class="icon-page" @click="ChooseImage">	
					<view class="flex-y-center">
						上传广告图<text style="color: red;font-size: 26rpx;">(图片尺寸300*300)</text>
					</view>
					<view class="icon_un flex-center">
						<image  mode="aspectFit" :class="thumb?'icon_un_icons':'icon_un_icon'" 
						:src="thumb?URL+thumb:'../../static/images/icon_18.png'" ></image>
					</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 {
				printerAddrList: [
				'关闭',
				'小程序','内部','外链','会员授权弹窗',
				],
				printerAddr: '关闭',
				
				device_type: 0,
				tableType: "",
				store_id: "",
				info: {},id:'',
				type:0,
				thumb:"",
				URL:getApp().globalData.URL,
				imgid:"",
			}
		},
		
		onLoad(e) {
			if (e.id) {
				this.id = e.id;
				this.banner_info();
			}
			this.store_id = uni.getStorageSync("store_id");
		},
		
		methods: {
			banner_info(){
				this.request({
					url: 'Smdcshop/index_banner_info',
					data: {
				     	id:this.id
					}
				}).then(res => {
					if (res.code == 200 ) {
						
						this.info=res.data;
						
						this.imgid=res.data.thumb;
						this.thumb=res.data.thumbs;
						this.device_type=res.data.type;
						this.printerAddr=res.data.type==1?'小程序':res.data.type==2?'内部':res.data.type==3?'外链':res.data.type==4?'会员授权弹窗':'关闭'
						
					} 
				})
			},
			ChooseImage(e) {
				let type = e.currentTarget.dataset.type;
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'], 
					sourceType: ['album'], //从相册选择
					success: (res) => {
						console.log(res)
						res.tempFilePaths.forEach((v, i) => {
							this.Upload({
								url: "Smdcshop/upload",
								data: {
									url: v,
									type: "",
									token: "",
									admin_id: uni.getStorageSync("admin_id")
								}
							}).then(res => {
								console.log(res, "res")
								this.imgid = res.id;
								this.thumb = res.src;
							}).catch(err => {
								uni.showToast({
									title: err.message,
									icon: 'none'
								})
							});
						})
					},
				});
			},
			
			onPrinterAddrChange(e) {
				this.printerAddr = this.printerAddrList[e.detail.value];
				this.device_type = Number(e.detail.value);
			},

			onSubmit(e) {
				let params = {
						admin_id: uni.getStorageSync("admin_id"),
						thumb:this.imgid,
						num: e.detail.value.num,
						name: e.detail.value.name,
						type:this.device_type,
						url:e.detail.value.url,
						// appid:e.detail.value.appid
					},
					url;
				if (this.id) {
					params.id = this.id;
					url = "Smdcshop/index_banner_update"
				} else {
					url = 'Smdcshop/index_banner_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(() => {
							uni.navigateBack()
						}, 2000)
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				});
			},
		}
	}
</script>

<style>
	@import url("../../static/css/common.css");
	page {
		background-color: #F4F5F7;
	}
	.icon-page{
		padding:  20rpx;
		background: #FFFFFF;
		
	}
	
	.icon_un{
	    width: 100%;
	    height:400rpx;
	    margin-top: 20px;
		text-align: center;
	}
	.icon_un_icons{
		width: 100%;
		height:300rpx;
		margin-top: 20px;
	}
	
	.icon_un_icon{
		width: 150rpx;
		height:150rpx;
		margin-top: 20px;
	}
</style>
