<template>
	<view class="container">
		<form @submit="onSubmit">

			<view class="page-block">

				<view class="content">

					<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 requsired">
							<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">
						<view class="title-block requsired">
							<text>跳转小程序appid</text>
						</view>
						<view class="desc">
							<input type="num" :value="info.appid" name="appid" 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="num" :value="info.appid_name" name="appid_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="num" :value="info.num" class="input-box" 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;">(图片尺寸{{device_type==1?' 750*400':'750*550'}})</text>
					</view>
					<view class="icon_un flex-center">
						<image mode="aspectFit" class="icon_un_icons" :src="URL+thumb" v-if="thumb"></image>
						<image mode="aspectFit" class="icon_un_icon" :src="require('./static/images/icon_18.png')"  v-else></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: 1,
				tableType: "",
				store_id: "",
				info: {},
				id: '',
				type_id: 1,
				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/banner_info',
					data: {
						id: this.id
					}
				}).then(res => {
					if (res.code == 200) {
						this.info = res.data
						this.thumb = res.data.thumb;
						this.device_type = res.data.type_id;
						this.printerAddr = res.data.type_id == 1 ? '支付后广告' : '首页'
					}
				})
			},
			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) + 1;
			},

			onSubmit(e) {
				let params = {
						admin_id: uni.getStorageSync("admin_id"),
						thumb: this.imgid,
						num: e.detail.value.num,
						name: e.detail.value.name,
						type_id: this.device_type,
						appid_name: e.detail.value.appid_name,
						appid: e.detail.value.appid
					},
					url;
				if (this.id) {
					params.id = this.id;
					url = "Smdcshop/banner_update"
				} else {
					url = 'Smdcshop/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>
