<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 class="item_name">网站链接 <text style="color:red">*</text></text>
					<block v-if="card_num">
						<view class="content">{{info.url}}</view>
						<input style="display: none;" type="text" name="url" :value="info.url" />
					</block>
					<input type="text" v-else name="url" :value="info.url" maxlength="5000" placeholder="请填写网站链接" />
				</view>


				<!-- <block v-if="moshi==2"> -->
				<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>
				<!-- </block> -->

				<view v-if="moshi==1">
					<view class="box-pack-between ft32 item">
						<text>{{$test.$test}}场景</text>
						<view class="add-home flex-center ft24">
							<picker @change="bindPickerChange" :value="index" :range-key="'name'" :range="allwayList">
								添加
							</picker>
						</view>
					</view>
					<view class="item-bg" v-if="arr.length>0">
						<view class="box-pack-between mar64 flex-y-center" v-for="(item,index) of arr" :key='index'>
							<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 class="">

					<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:如遇到无法访问的网站链接，请配置中转页</text>
				</view>

				<block v-if="mid_state==1?false:true">
					<view class="box-pack-between ft32 item">
						<text>开启遮盖指引</text>
						<view class="flex-center ft24">
							<switch :checked="hj_state==1?true:false" @change="switch5" name="hj_state" style="transform:scale(0.7)" />
						</view>
					</view>
					<view class="box-pack-between" v-if="hj_state==1?true:false" style="padding: 0 50rpx;">
						<text></text>
						<view class="flex">
							<view class="look-icon-right" @click="bindhj(index)" v-for="(item,index) of model" :key='index'>
								<view class="por">
									<image @click="Choose_img" class="look-icon" :src="item.img" mode=""></image>
									<image class="look-jiao" v-if="item.checked" src="../../static/images/jiao.png" mode=""></image>
								</view>
								<view class="mr-top24">
									{{item.name}}
								</view>
							</view>
						</view>
					</view>
				</block>

				<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>


							<!-- 		<block v-if="wximg_url">
								<image @click="ChooseImage" :data-type="2" class="upimg" :src="wximg_url" mode="">
								</image>
							</block> -->

							<!-- <block v-else> -->
							<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>
							<!-- </block> -->
						</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>
					
				<!-- is_exchange  Y是添加N是不 -->
				<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="1" class="btn1 flex-center">
						绑定二维码
					</button> -->
					<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,
				
				model:[
					{
					img:"https://empwment.zx-xcx.com/forward/img/look1.png",
					name:"页面遮盖层模式",
					checked:true,
					},{
					img:"https://empwment.zx-xcx.com/forward/img/look2.png",	
					name:"查看更多模式",
					checked:false,
					},
				],
				
				hj_index:0,
				
				hj_type:1,
				allwayList: [],
				arr: [],
				index: 0,
				result: "",
				// is_mid: false,
				mid_img: "",
				mid_state: 2,
				info: {},
				URL: getApp().globalData.URL,
				card_num: "",
				Advertising: [],
				Adver_index: 0,
				mid_type: '',
				hj_state: 2,
				wx_state: 1,
				wx_img: "",
				// wximg_url:"",
				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
			bindhj(index){
				for(var i of this.model){
					i.checked=false;
				}
				this.model[index].checked=true;
				this.hj_index=index;
			},

			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 = "";
			},

			Choose_img(e) {
				var url = e.currentTarget.dataset.url
				// 预览图片
				uni.previewImage({
					urls: [url],
				})
			},
	
			ChooseImage(e) {
				let type = e.currentTarget.dataset.type;
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					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];
						 
						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,
									this.url = res.url
								) : (
									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(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.hj_state = res.data.hj_state;
						this.info = res.data;
						this.mid_img = res.data.mid_img;
						this.arr = res.data.funeng;
						this.mid_type = res.data.mid_type;
						this.mid_state = res.data.mid_state;
						
						this.wx_state = res.data.wx_state;
						
						this.wx_img = res.data.wx_img;
						
						// if (!res.data.wx_title) {
						//    this.Crawler(res.data.url);
						// }
						
						this.hj_index=res.data.hj_type-1;

						for(var i in this.model){
							this.model[i].checked=false;
							if(i== (res.data.hj_type-1)){
								this.model[i].checked=true;
							}
						}
                        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) => {});
			},

   //         Crawler(url){
			// 	this.request({
			// 		url:"Exchange/Crawler",
			// 		data: {
			// 			url:url,
			// 			token: uni.getStorageSync("token"),
			// 		},
			// 		method:"GET",
			// 	}).then(res => {
			// 		console.log(res, "res")
			// 		if (res.code === '200') {
			// 					this.info.wx_title=res.data.title;
			// 					this.info.wx_desc= res.data.desc;
			// 				    this.wximg_url = res.data.img;
			// 		}
			// 	}); 
			// },


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

			switch2(e) {
				this.wx_state = e.target.value ? 1 : 0;
			},
			
			switch4(e){
				this.wx_state = e.target.value ? 1 : 0;
			},
			switch5(e){
				this.hj_state = e.target.value?1:2;
			},
			
			
			Submit(e) {
				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
				// }
	
				let str = e.detail.value.url;
				str = ltrim(str);
				//去左空格;
				function ltrim(s) {
					return s.replace(/(^\s*)/g, "");
				}
				if (str.substr(0, 4) != "http") {
					uni.showToast({
						title: "请填写http开头的合法链接",
						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 = {
					hj_type:this.hj_index+1,	//  1默认2其他  
					token: uni.getStorageSync("token"),
					type: 3,
					url: e.detail.value.url,
					way_id: way_id.join(","),
					way_power: way_power.join(","),
					save_type: 2,
					qrcode: this.result,
					hj_state: e.detail.value.hj_state ? 1 : 2, //1开启2不开启
					mid_state: e.detail.value.mid_state ? 1 : 2,
					description:e.detail.value.description,
					mid_title: e.detail.value.mid_title,
					
	                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_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=3" + '&card_num=' + res.data.card_num
							})
						)
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				});
			}
		}
	}
</script>

<style>
	.item_name {
		-webkit-box-flex: 0;
		-webkit-flex: none;
		flex: none;
		width: 176rpx;

	}

	.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;
		/* height: 298rpx; */
		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;
	}
	
	.look-icon{
		width: 200rpx;
		height: 300rpx;
		display: block;
	}
	.por{
		position: relative;
	  }
	.look-icon-left{
			font-size: 26rpx;
		    text-align: center;
	}
	.look-icon-right{
		font-size: 26rpx;
		margin-left: 20rpx;		
		text-align: center;
	}
	.mr-top24{
		margin-top: 24rpx;
		color: #999;
	}
	.look-jiao{
		width: 50rpx;
		height: 50rpx;
		position: absolute;
		bottom: 0;
		right: 0;
	}
</style>
