<template>
	<view class="content">
		<header-title :title="'普通二维码'+$test.$test"></header-title>
		<view class="main">
		<form @submit="Submit">
			
			<view class="box-pack-between ft32 item flex-y-center">
					<text>原图模式<text class="tips">(原图通过扫码才能正常访问时开启)</text></text>
					<view class="flex-center ft24">
						<switch :checked="is_real==1" name="is_real" @change="bindOriginal" style="transform:scale(0.7)" />
					</view>
			</view>
				
			<view class="box-pack-between ft32 item flex-y-center" v-if="is_real==2">
				<text>二维码 <text style="color:red">*</text> <text class="tips">(扫码/相册选取)</text></text>
				<image v-if="code_url" @click="qrcode" class="qrupimg" data-type="1" src="../../static/images/pay_bz.png" mode=""></image>
				<image v-else @click="qrcode" class="qrupimg" data-type="1" src="../../static/images/saoma.png" mode=""></image>
			</view>

			<view class="box-pack-between ft32 item flex-y-center" v-if="is_real==1">
				<text>上传原图 <text style="color:red">*</text> <text class="tips"></text></text>
				<image v-if="Original_img" @click="ChooseImage" :data-type="3" class="qrupimg" :src="URL+''+Original_img" mode=""></image>
				<image v-else @click="ChooseImage" :data-type="3" class="qrupimg" src="../../static/images/upimg.png" mode=""></image>
			</view>
	
			<FlowPool :thismoshi="moshi" :rule_arr_val="ruleArr" :this_moshi_type="moshi_type" :list_way="list_way" @ondel="binddel"
			 @oncheck="bindcheck" @onTime='bindtime' @confirm="bindConfirm" @bindlevel="bindlevel"></FlowPool>
			
		
			<view v-if="moshi==1" >
				<view class="box-pack-between ft32 item">
					<text>{{$test.$test}}场景</text>
					<picker class="flex-center add-home ft24" @change="bindPickerChange" :value="index" :range-key="'name'" :range="allwayList">
						添加
					</picker>
				</view>
				<view class="item-bg" v-if="arr.length>0">
					<view class="box-pack-between mar64 flex-y-center" :key='index' v-for="(item,index) of arr">
						<view class="flex-y-center flex-grow-1 pdr-20">
							<text style="margin-right: 10rpx;">{{item.name}}</text>
							<input class="flex-grow-1" type="text" @input="getScene" :data-index="index" 
							placeholder="请填写链接或内容" :value="item.way_power" maxlength="5000"/>
						</view>
						<image class="item-del" @click="delScene" :data-index='index' src="../../static/images/del.png" mode=""></image>
					</view>
				</view>
			</view>
			
			<view>
				<view class="box-pack-between ft32 item">
					<text>配置中转页</text>
					<view class="flex-center ft24">
						<switch :checked="mid_state==1?true:false" name="mid_state" @change="switch1" style="transform:scale(0.7)" />
					</view>
				</view>
				<view class="item-bg" v-if="mid_state==1?true:false">
					<view class="box-pack-between mgr57">
						<view class="flex-row">
							<text class="item-bg-32">中转页标题</text>
							<input type="text" :value="info.mid_title" name="mid_title" placeholder="请填写中转页标题" />
						</view>
					</view>
					
					<view class="box-pack-between mgr57">
						<text class="item-bg-32">广告方式</text>
						<picker @change="bindPickerAdvertising" :range-key="'name'" :range="Advertising">
							<text>{{Advertising[Adver_index].name}} <text class="iconfont icon-prev"></text></text>
						</picker>
					</view>
					
					<view class="box-pack-between flex-y-center" v-if="Advertising[Adver_index].name=='自定义引导页'">
						<text class="item-bg-32">广告引导页</text>
						<image @click="ChooseImage" :data-type="1" v-if="mid_img" class="upimg" :data-url="Advertising[Adver_index].url"
						 :src="URL+''+mid_img"></image>
						<image @click="ChooseImage" :data-type="1" v-else class="upimg" src="../../static/images/upimg.png" mode=""></image>
					</view>
					
					<view class="box-pack-between flex-y-center" v-else-if="Advertising[Adver_index].name=='安全认证'">
						<text class="item-bg-32">广告引导页</text>
						<image mode="aspectFill" data-url="../../merchant/static/images/ali_ua.png" class="upimg" 
						src="../../merchant/static/images/ali_ua.png"></image>
						<image mode="aspectFill" data-url="../../merchant/static/images/wx_ua.png" class="upimg" src="../../merchant/static/images/wx_ua.png"></image>
						<image mode="aspectFill" data-url="../../merchant/static/images/web_ua.png" class="upimg" src="../../merchant/static/images/web_ua.png"></image>
					</view>
					
					<view class="box-pack-between flex-y-center" v-else>
						<text class="item-bg-32">广告引导页</text>
						<image @click="Choose_img" mode="aspectFill" :data-url="URL+Advertising[Adver_index].url" class="upimg" :src="URL+Advertising[Adver_index].url"></image>
					</view>
					
				</view>
				<text style="padding:0 40rpx;font-size: 12px;color: #999;">ps:{{$test.$test}}进入需要授权的公众号链接，请配置中转页</text>
			</view>
 
			<view class="box-pack-between ft32 item" v-if="mid_state!=1">
				<text>开启遮盖指引</text>
				<view class="flex-center ft24">
					<switch :checked="hj_state==1?true:false" name="hj_state" style="transform:scale(0.7)" />
				</view>
			</view>
  
			<view>
				<view class="box-pack-between ft32 item">
					<text>配置微信分享</text>
					<view class=" flex-center ft24">
						<switch name="wx_state" :checked="wx_state==1?true:false" @change="switch2" style="transform:scale(0.7)" />
					</view>
				</view>
				<view class="item-bg" v-if="wx_state==1">
					<view class="box-pack-between mgr57">
						<view class="flex-row">
							<text class="item-bg-32">分享标题</text>
							<input type="text" name="wx_title" placeholder="请填写分享标题" :value="info.wx_title" />
						</view>
					</view>
					<view class=" flex-row mgr57">
						<text class="item-bg-32">分享描述</text>
						<input type="text" name="wx_desc" placeholder="请填写分享描述" :value="info.wx_desc" />
					</view>
					<view class="box-pack-between flex-y-center">
						<text class="item-bg-32">分享图标</text>
						<image v-if="wx_img" @click="ChooseImage" :data-type="2" class="upimg" :src="URL+wx_img" mode="">
						</image>
						<image v-else @click="ChooseImage" :data-type="2" class="upimg" src="../../static/images/upimg.png" mode="">
						</image>
					</view>
				</view>
			</view>

		    <view class="box-pack-between ft32  descp " >
				<textarea :value="info.description" placeholder-class="desc-placeholder" maxlength="200" :placeholder="$test.$test+'备注'" name='description' />
			</view>
			
			<view class="box-pack-between ft32 item">
					<view class="flex-center">
						  	<text>加入换量记录池</text>
						  	
					</view>
					<view class=" flex-center ft24">
						<switch name="is_exchange" :checked="is_exchange=='Y'?true:false" style="transform:scale(0.7)" />
					</view>
			</view>
			
			
			<view class="hg200rpx"></view>

			<view class="bottom-bar">
				<block v-if="card_num">
					<button form-type="submit" data-status="2" class="btn2 flex-center">
						确定修改
					</button>
				</block>
				<block v-else>
					<button form-type="submit" data-status="2" class="btn1 flex-center">
						{{$test.$test}}二维码
					</button>
				</block>
			</view>
			
		</form>
		
		</view>	
	</view>
</template>

<script>
	import HeaderTitle from '@/components/header/header-title.vue';	
	import FlowPool from '@/components/flow-pool/flow-pool.vue';
	// #ifdef H5
	import getWXSign from "@/static/js/config.js";
	// #endif 
	export default {
		components: {
			HeaderTitle,	FlowPool,
		},
		data() {
			return {				
				$test:this.$test.$test,
				allwayList: [],
				arr: [],
				index: 0,
				result: "",
				is_wx: true,
				mid_img: "",
				wx_img: "",
				mid_state: 2,
				hj_state: 2,
				wx_state: 1,
				info: {},
				code_url: "",
				URL: getApp().globalData.URL,
				card_num: "",
				Advertising: [],
				Adver_index: 0,
				mid_type: '',
				Original_img:"",
				is_real:2,description:"",
				is_exchange:'N',
				moshi:1,
				moshi_type:2,
				list_way:[],
				ruleArr:[],
				powerpool_id:"",
				level_arr:[1,2,3],
				level:"",
				rule:"",
			}
		},
		onLoad(e) {	
			uni.removeStorageSync("list_way");
			this.level=this.level_arr.join(",");
			this.card_num = e.card_num;
			if (!this.card_num) {
				this.allmuban();
			}			
			uni.setNavigationBarTitle({
				title:`二维码${this.$test.$test}`
			})
			this.allways();
		},		
		 onShow() {
			if(uni.getStorageSync("list_way")){
				this.list_way=uni.getStorageSync("list_way");
				if(this.way_index==3){
					for(let i of this.list_way){
						if(i.checked){
							this.powerpool_id=i.id
						}
					}
				}
			}
		 },
		methods: {
			// 流量池
			bindcheck(index) {
						  uni.removeStorageSync("list_way");
			   this.moshi = index;
			},
			
						binddel(index){
							this.list_way.splice(index, 1);
						},
			
			bindtime(index) {
						   console.log("000",index)
						   this.list_way=[];
						   uni.removeStorageSync("list_way");
						   this.moshi_type=index==1?1:2;
						   this.way_index=index;
						   if(index==1){
							   uni.navigateTo({
							   	url: "../public/index"
							   })
						   }
						   // 默认流量池
							if(index==3){
								 uni.navigateTo({
									url: "../public/index?type=2"
								 })
							}
			},
			// 获取时间
			bindConfirm(rule){
						   console.log(rule,"aff")
						   this.rule=rule;
			},
			bindlevel(e){
						   console.log(e,"bindlevel")
						   var level=[];
						   for(var i of e){
							   level.push(i.level)
						   }
						   this.level=level.join(",");
			},
			// 流量池end
			allmuban() {
				this.request({
					url: "funeng/allmuban",
				}).then(res => {
					console.log(res, "res")
					if (res.code === '200') {
						res.data.push({
							name: "自定义引导页",
							id: "0"
						});
						if (this.card_num) {
							for (let i in res.data) {
								if (res.data[i].id == this.mid_type) {
									this.Adver_index = i;
								}
							}
						}
						this.Advertising = res.data;
					}
				}).catch((res) => {});
			},

			bindPickerAdvertising(e) {
				this.Adver_index = e.detail.value;
				this.mid_img = "";
			},
			bindOriginal(e){
				console.log(e)
				this.is_real =e.target.value?1:2;
			},
			Choose_img(e) {
				var url = e.currentTarget.dataset.url
				uni.previewImage({
					urls: [url],
				})
			},

			ChooseImage(e) {
				console.log(e, "eeeeeee")
				let type = e.currentTarget.dataset.type;
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						console.log(res)
					var tempFilesSize = res.tempFiles[0].size;//获取图片的大小，单位B
					if(type==2&&tempFilesSize>1000000){  //图片小于或者等于2M时 可以执行获取图片
										 uni.showToast({
											 title:'上传图片不能大于1M!',  //标题
											 icon:'none'       //图标 none不使用图标，详情看官方文档
										})
									return
						}
						
							var path;
							// #ifdef MP-WEIXIN
								  path = res.tempFiles[0].path;
							// #endif
							
							// #ifdef H5
								 path = res.tempFiles[0].name;
							// #endif

						   var formatImage = path.split(".")[(path.split(".")).length - 1];				
						   console.log("图片格式" + formatImage)
						   if (type == 2) {
							if (formatImage != "png" && formatImage != "jpg" && formatImage != "jpeg") {
								   uni.showToast({
									title: '只能上传png、jpg、jpeg格式',
									icon: 'none',
									duration: 2000,
									mask: true,
								  })
								  return
							}
						}
						res.tempFilePaths.forEach((v, i) => {
							console.log(v)
							this.Upload({
								url: "Spread/upload_img",
								data: {
									url: v,
									type: 0,
									token: uni.getStorageSync("token"),
								}
							}).then(res => {
								console.log(res, "res")
								type == 1 ? (
									this.mid_img = res.file
								) :type == 3 ? (
									this.Original_img= res.file
								):(
							     	this.wx_img = res.file
								)
							}).catch(err => {
								uni.showToast({
									title: err.message,
									icon: 'none'
								})
							});
						})
					},
				});
			},

			getScene(e) {
				var index = e.currentTarget.dataset.index;
				this.arr[index].way_power = e.detail.value
			},

			delScene(e) {
				var index = e.currentTarget.dataset.index;
				this.arr.splice(index, 1)
			},

			bindPickerChange: function(e) {
				if (this.arr.length > 0) {
					for (let i of this.arr) {
						if (i.id == this.allwayList[e.target.value].id) {
							uni.showToast({
								title: "场景重复",
								icon: 'none'
							})
							return
						}
					}
				}
				this.arr.push({
					id: this.allwayList[e.target.value].id,
					name: this.allwayList[e.target.value].name,
					super_id: this.allwayList[e.target.value].super_id,
				});
			},

			allways() {
				this.request({
					url: "funeng/allways",
					data: {},
				}).then(res => {
					console.log(res, "res")
					if (res.code === '200') {
						this.allwayList = res.data;
						this.arr.push({
							id: res.data[0].id,
							name: res.data[0].name,
							super_id: res.data[0].super_id,
						});
						if (this.card_num) {
							this.edit_detail();
						}
					}
				}).catch((res) => {});
			},

			edit_detail(e) {
				this.request({
					url: "funeng/edit_detail",
					data: {
						card_num: this.card_num,
					},
				}).then(res => {
					console.log(res, "res")
					if (res.code === '200') {
						this.info = res.data;
						this.mid_state = res.data.mid_state;
						this.mid_img = res.data.mid_img;
						this.wx_state = res.data.wx_state;
						this.wx_img = res.data.wx_img;
						this.arr = res.data.funeng;

						this.mid_type = res.data.mid_type;
						this.hj_state = res.data.hj_state;
						
                        this.is_real= res.data.is_real;
			
						 this.Original_img = res.data.img_file;
						  
						this.code_url =res.data.url;
						
						this.is_exchange=res.data.is_exchange;
						
						this.moshi = res.data.moshi;
						this.moshi_type=res.data.moshi_type;
						this.ruleArr=res.data.rule_arr;		
						if(this.moshi==2){
							if(this.moshi_type==2){
								var a = []
								for(let i of res.data.rule_arr){
									a.push(i.type)
								}
								// 去除重复的数
								  var b = []
								  for (var i = 0; i < a.length; i++) {
								  if (b.indexOf(a[i]) === -1) {
								     b.push(a[i])
								   }
								  }
								 this.level=b.join(",");
								this.powerpool_id=res.data.powerpool_id;
							}
							for(let i of res.data.powerpool){
								i.checked=true;
							}
							this.list_way=res.data.powerpool;
						}
						
						this.allmuban();
						
					}
					
				}).catch((res) => {

				});
			},

			switch1(e) {
				this.mid_state = e.target.value ? 1 : 2;
			},

			switch2(e) {
				this.wx_state = e.target.value ? 1 : 0;
			},
			
			qrcode(e) {
				// #ifdef MP-WEIXIN
				uni.scanCode({
					success: (res) => {
						console.log(res)
						var result = res.result;
							if (result.substr(0, 3) == "wxp") {
								uni.showToast({
									title: "请勿上传支付码",
									icon: "none"
								})
								return
							} else if (result.substr(0, 17) == "HTTPS://QR.ALIPAY" || result.substr(0, 10) == "https://qr.alipay") {
							uni.showToast({
								title: "请勿上传支付码",
								icon: "none"
							})
							return
							
						} else {
							
							// if(result.substr(0, 8)!="https://"){
							// 	uni.showToast({
							// 		title: "请上传网址链接的二维码",
							// 		icon: "none"
							// 	})
							// 	return
							// }
							
							this.code_url = result
						}
					},
				})
				// #endif
				
				// #ifdef H5
				getWXSign().then(res => {
					console.log('wx_getLocation');
					this.$wx.scanQRCode({
						needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
						scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
						success: (res) => {
							console.log(res, "qrCode")
							var result = res.resultStr;
							if (result.substr(0, 3) == "wxp") {
								uni.showToast({
									title: "请勿上传支付码",
									icon: "none"
								})
								return
							} else if (result.substr(0, 17) == "HTTPS://QR.ALIPAY" || result.substr(0, 10) == "https://qr.alipay") {
								uni.showToast({
									title: "请勿上传支付码",
									icon: "none"
								})
								return
								
							} else {
								// if(result.substr(0, 8)!="https://"){
								// 	uni.showToast({
								// 		title: "请上传网址链接的二维码",
								// 		icon: "none"
								// 	})
								// 	return
								// }
								this.code_url = result
							}
							
							
						}
					})
				})
			   // #endif
			},

			Submit(e) {
				console.log(e, "statusstatus")
				let arr = this.arr,
					way_id = [],
					way_power = [];
					
				for (let i of arr) {
					way_id.push(i.id);
					way_power.push(i.way_power)
				}
				
				if (way_id.length <= 0) {
					uni.showToast({
						title: "场景为空",
						icon: 'none'
					})
					return
				}
				if (e.detail.value.wx_state==1&&!e.detail.value.wx_title) {
					uni.showToast({
						title: "分享标题不能为空",
						icon: 'none'
					})
					return
				}
				
				
				var powerpool_id ="";
				if(this.moshi_type==1){
					var powerpool_arr = [];
					for (let i of this.list_way) {
						if (i.checked) {
							powerpool_arr.push(i.id);
						}
					}
				    powerpool_id=powerpool_arr.join(",");
				}else{
					powerpool_id=this.powerpool_id;
				}	
				let data = {
					token: uni.getStorageSync("token"),
					type: 4,
					is_real: e.detail.value.is_real ? 1:2, //是原图2非原图
					img_file:this.Original_img,
					url:e.detail.value.is_real?"":this.code_url,
					way_id: way_id.join(","),
					way_power: way_power.join(","),
					save_type: 2,
					qrcode: this.result,
					mid_state: e.detail.value.mid_state ? 1 : 2,
					mid_title: e.detail.value.mid_title,
					hj_state: e.detail.value.hj_state ? 1 : 2, //1开启2不开启
					description: e.detail.value.description, 
					mid_type: this.Advertising[this.Adver_index].id,
					mid_img: this.Advertising[this.Adver_index].id == "0" ? this.mid_img : this.Advertising[this.Adver_index].thumb,
				
					wx_state: e.detail.value.wx_state ? 1 : 2,
					// wx_title: e.detail.value.wx_title,
					// wx_desc: e.detail.value.wx_desc,
					// wx_img: this.wx_img,
					wx_title:e.detail.value.wx_state ? e.detail.value.wx_title :
					"",
					wx_desc:e.detail.value.wx_state? e.detail.value.wx_desc:"",
					wx_img:e.detail.value.wx_state? this.wx_img:"",
					
					
					
					is_exchange:e.detail.value.is_exchange?'Y':'N',
					moshi:this.moshi,
					moshi_type:this.moshi_type,	
					powerpool_id:powerpool_id,
					level:this.level,
					rule:this.rule,
				}
				//请求验证码
				let url;
				this.card_num ? (data.code = this.info.code, url = "funeng/save_edit") : url = "funeng/weixin";
				this.request({
					url,
					data,
				}).then(res => {
					console.log(res, "res")
					if (res.code === '200') {
						this.card_num ? (
							uni.showToast({
								title: "设置成功",
								icon: 'none'
							}),
							setTimeout(() => {
								uni.navigateBack({
									delta: 1
								})
							}, 2e3)
						) : (
							uni.showToast({
								title: "生成成功",
								icon: 'none'
							}),
							uni.redirectTo({
								url: "code?type=4" + '&card_num=' + res.data.card_num
							})
						)
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				});
			},
		}
	}
</script>

<style>
	.qrupimg {
		width: 80rpx;
		height: 80rpx;
	}

	.content {
		word-wrap: break-word;
		word-break: break-all;
	}

	.pdr-20 {
		padding-right: 20rpx;
	}

	.item {
		padding: 32rpx 40rpx;
	}

	.item input {
		text-align: right;
	}

	.upimg {
		width: 126rpx;
		height: 126rpx;
	}

	.add-home {
		color: #FFFFFF;
		width: 126rpx;
		height: 54rpx;
		background: linear-gradient(315deg, #3387ff 2%, #569bff 99%);
		border-radius: 8rpx;
		box-shadow: 0px 0px 8rpx 0px rgba(51, 135, 255, 0.2);
	}

	.item-bg {
		width: 670rpx;
		background: #f6f8fe;
		border-radius: 12rpx;
		margin: 0 auto;
		font-size: 28rpx;
		font-family: PingFang SC, PingFang SC-Regular;
		font-weight: 400;
		color: #333333;
		padding: 36rpx 32rpx;
	}

	.item-del {
		width: 40rpx;
		height: 40rpx;
	}

	.item-bg-32 {
		margin-right: 32rpx;
	}

	.mgr57 {
		margin-bottom: 57rpx;
	}

	.hg200rpx {
		height: 100rpx;
	}

	.mar64 {
		margin-bottom: 64rpx;
	}

	.item-bg .mar64:last-child {
		margin-bottom: 0rpx;
	}
	.tips{
		font-size: 26rpx;
		color: #999;
	}
</style>
