<template>
	<view class="container">
		<wike-skeleton :count="4" v-if="homeTemplate.length === 0"></wike-skeleton>
		<view class="form" v-if="appInfo.page_template == 1 || !appInfo.page_template">
			<view class="top-notice" v-if="appInfo && appInfo.notice"><u-notice-bar
					:text="appInfo.notice"></u-notice-bar></view>
			<view class="logo"><u--image :src="appInfo.site_logo_path?appInfo.site_logo_path:'/static/images/head.jpg'"
					shape="circle" width="200rpx" height="200rpx"></u--image></view>
			<u-transition :show="true" mode="slide-left">
				<view class="title">{{ appInfo.site_name?appInfo.site_name:'A问答机器人' }}</view>
			</u-transition>
			<u-transition :show="true" mode="slide-right">
				<view class="desc">{{ appInfo.sub_title }}</view>
			</u-transition>
			<view class="btn-group">
				<view class="btn" @click="onToForm"><u-button shape="circle" iconColor="#ffffff" color="#26B3A0"
						icon="chat" text="立即体验"></u-button></view>
				<view v-if="appInfo.is_aipainting&&appInfo.is_aipainting == 1" class="btn" @click="ondraw"><u-button
						shape="circle" iconColor="#ffffff" color="#ffc107" icon="edit-pen-fill" text="Ai绘画"></u-button>
				</view>
				<view class="btn">
					<!-- #ifdef MP-WEIXIN -->
					<u-button shape="circle" open-type="share" color="#26B3A0" :plain="true" icon="share"
						text="分享好友"></u-button>
					<!-- #endif -->
					<!-- #ifdef H5 -->
					<u-button shape="circle" @click="h5share = true" color="#26B3A0" :plain="true" icon="share"
						text="分享好友"></u-button>
					<!-- #endif -->
				</view>
			</view>
		</view>
		<view v-if="appInfo.page_template == 2">
			<view class="directask">
				<!-- 📝 -->
				<view class="askaquestion u-flex align-center">
					<!-- <image src="/static/images/ask.png"></image> -->
					📝直接提问
				</view>
				<view class="textarea">
					<u--textarea @focus="focustextarea" maxlength="-1" v-model="valuechat" height="100" border="none"
						placeholder="您可以向Ai提出任何问题..."></u--textarea>
					<view class="operate u-flex align-center">

						<view @click="valuechat = ''" class="empty">清空</view>
						<view @click="ondraw" v-if="appInfo.is_aipainting&&appInfo.is_aipainting == 1"
							style="background: #ffc107;margin-right: 30rpx;border: #ffc107 1px solid;"
							hover-class="hoversubmit" class="submit">Ai绘画</view>
						<view @click="onSubmit(valuechat)" hover-class="hoversubmit" class="submit">提交问题</view>
					</view>
				</view>
				<view class="askaquestion u-flex align-center justify-between">
					<!-- 🔥 -->
					<view class="u-flex align-center">
						<!-- <image src="/static/images/heat.png"></image> -->
						🔥热门提问
					</view>

					<view @click="updateTemplate" class="tn-flex align-center"
						style="justify-content: flex-end;margin-top: 15rpx;">
						<view style="color: #9e9e9e;margin-right: 10rpx;">换一换</view>
						<u-icon name="reload" color="#9e9e9e" size="18"></u-icon>
					</view>
				</view>
				<block v-for="(item, index) in homeTemplate.hot" :key="index">
					<view class="case" @click="onSubmit(item.text)">{{ item.text }}</view>
				</block>
				<!-- #ifdef MP-WEIXIN -->
				<view class="wxad" v-if="appInfo.wxad_index && appInfo.wxad_index >= 1">
					<view style="width: 100%;">
						<wike-flow-main :flowtype="appInfo.wxad_index" :banner_id="appInfo.banner_id"
							:video_banner_id="appInfo.video_banner_id" :native_id="appInfo.native_id"></wike-flow-main>
					</view>
				</view>
				<!-- #endif -->
				<view class="askaquestion u-flex align-center justify-between">
					<!-- 🛠️ -->
					<view class="u-flex align-center">
						<!-- <image src="/static/images/formwork.png"></image> -->
						🛠️提问模板
					</view>

					<view @click="onhelper" class="tn-flex align-center"
						style="justify-content: flex-end;margin-top: 15rpx;">
						<view style="color: #9e9e9e;margin-right: 6rpx;">更多</view>
						<u-icon name="arrow-right" color="#9e9e9e" size="18"></u-icon>
					</view>
				</view>
				<view style="margin-bottom: 10rpx;" class="u-flex flex-wrap justify-between align-center">
					<navigator class="helpme" :url="'/pages/template/detail?id=' + item.id"
						v-for="(item, index) in homeTemplate.template" :key="index">
						<view class="title">{{ item.title }}</view>
						<view class="sub_title">{{ item.sub_title }}</view>
					</navigator>
				</view>
				<wike-ad></wike-ad>
			</view>
		</view>
		<view v-if="appInfo.page_template == 3"></view>
		<!-- #ifdef H5 -->
		<u-popup :show="h5share" mode="center" @close="h5share = false">
			<view class="cu-dialog">
				<image class="guide-img" src="http://shopro.7wpp.com/imgs/modal/share_guide.png" mode=""></image>
			</view>
		</u-popup>
		<!-- #endif -->
		<!-- <wike-tabbar :onTabbar="onTabbar" :isShowAnimate="isShowAnimate"></wike-tabbar> -->
	</view>
</template>

<script>
	import {
		mapMutations,
		mapActions,
		mapState,
		mapGetters
	} from 'vuex';
	let interstitialAd = null,
		timer,
		time2;
	export default {
		components: {},
		data() {
			return {
				onTabbar: false,
				isShowAnimate: false,
				valuechat: '',
				platform: this.$platform.get(),
				h5share: false
			};
		},
		computed: {
			...mapGetters(['appInfo', 'isLogin', 'userInfo', 'homeTemplate', 'userData'])
		},
		watch: {
			appInfo: function(e) {
				let that = this;
				uni.setNavigationBarTitle({
					title: that.appInfo.site_name
				});
			}
		},
		onLoad() {
			var that = this;
			uni.getSystemInfo({
				success: function(res) {}
			});
			setTimeout(() => {
				that.onTabbar = true;
				that.isShowAnimate = true;
			}, 500);
		},
		onReady() {
			let that = this;
			// #ifdef MP-WEIXIN
			clearTimeout(timer);
			// 在适合的场景显示插屏广告
			timer = setTimeout(function() {
				// 在页面onLoad回调事件中创建插屏广告实例
				if (wx.createInterstitialAd && that.appInfo.interstitial_status == 1) {
					interstitialAd = wx.createInterstitialAd({
						adUnitId: that.appInfo.interstitial_id
					});
					interstitialAd.onLoad(() => {});
					interstitialAd.onError(err => {});
					interstitialAd.onClose(() => {
						if (that.appInfo.interstitial_infinite_status == 1) {
							that.showInterstitial();
						}
					});
					interstitialAd.show().catch(err => {
						console.error(err);
					});
				}
			}, 4000);
			// #endif
		},
		onShow() {
			if (this.isLogin) {
				this.getUserData();
			}
		},
		onUnload() {
			clearInterval(time2);
		},
		methods: {
			...mapActions(['appInit', 'logout', 'getUserInfo', 'getUserData']),
			init() {
				var that = this;
				return Promise.all([this.appInit()]).then(res => {

				});
			},
			showInterstitial() {
				time2 = setInterval(
					function() {
						interstitialAd.show().catch(err => {
							console.error(err);
						});
					},
					this.appInfo.gap ? this.appInfo.gap * 1000 : 12000
				);
			},
			updateTemplate() {
				this.$store.dispatch('getTemplate');
			},
			onToForm() {
				uni.navigateTo({
					url: '/pages/chat/chat'
				});
			},
			onhelper() {
				uni.switchTab({
					url: '/pages/template/template'
				});
			},
			focustextarea() {
				if (!this.isLogin) {
					uni.navigateTo({
						url: '/pages/user/signin'
					});
					uni.setStorageSync('route', '/pages/index/index');
					return;
				}
			},
			ondraw() {

				uni.navigateTo({
					url: '/pages/chat/chat?draw=1'
				});
			},
			onSubmit(e) {
				// console.log(e);
				var that = this;
				if (!that.isLogin) {

					uni.navigateTo({
						url: '/pages/user/signin'
					});

					uni.setStorageSync('route', '/pages/index/index');

					return;
				}
				if (!e) {
					uni.showToast({
						title: '请输入关键词或问题',
						icon: 'none'
					});
					return;
				}
				if (that.userData.is_validity == 0 && that.userData.coin <= 0) {
					var alias = that.appInfo.number_alias ? that.appInfo.number_alias : '点数';
					uni.showModal({
						confirmText: '立即获取',
						confirmColor: '#26B3A0',
						content: '您的提问' + alias + '/会员时长不足',
						title: '提示',
						success(res) {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/user/member/member'
								});
							}
						}
					});
					return;
				}
				uni.navigateTo({
					url: '/pages/chat/chat?question=' + e
				});
				that.valuechat = ''
			}
		}
	};
</script>

<style lang="scss">
	@import './index.scss';

	.container {
		// margin-top: 25%;
	}

	.top-notice {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
	}

	.form {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		margin-top: 15%;

		.title {
			font-size: 38rpx;
			font-weight: bolder;
			// margin-top: 15rpx;
			margin: 15rpx 35rpx 0;
		}

		.desc {
			// margin-top: 15rpx;
			font-size: 28rpx;
			color: #666;
			margin: 15rpx 35rpx;
		}

		.btn-group {
			width: 80%;
			// margin-top: 26%;
			// margin-bottom: 42%;
			position: absolute;
			bottom: 80px;
			padding-bottom: env(safe-area-inset-bottom);

			.btn {
				margin: 30rpx 0rpx;

				.u-button {
					height: 100rpx;
				}
			}
		}
	}

	.directask {
		padding: 30rpx 30rpx 200rpx;
		border-top: 1px solid #ededed;

		.askaquestion {
			font-size: 32rpx;
			margin-bottom: 25rpx;

			image {
				width: 32rpx;
				height: 32rpx;
				margin-right: 12rpx;
			}
		}

		.textarea {
			box-shadow: 0px 0px 10px 5px #9e9e9e36;
			margin-top: 30rpx;
			margin-bottom: 30rpx;
			border-radius: 20rpx;
			padding: 20rpx 30rpx 30rpx 12rpx;

			.u-textarea {
				border-radius: 20rpx;
			}

			.operate {
				justify-content: flex-end;

				.empty {
					margin-right: 30rpx;
					border: 1px solid #9e9e9e;
					color: #9e9e9e;
					padding: 12rpx 42rpx;
					border-radius: 10rpx;
				}

				.submit {
					padding: 12rpx 42rpx;
					border-radius: 10rpx;
					border: 1px solid #26b3a0;
					background: #26b3a0;
					color: #fff;
				}

				.hoversubmit {
					background: #f7f7f7;
					color: #acacb3;
				}
			}
		}

		.case {
			color: #26b3a0;
			background: #f5f8f7;
			width: 100%;
			height: 80rpx;
			line-height: 80rpx;
			font-size: 30rpx;
			padding-left: 30rpx;
			border-radius: 10rpx;
			margin: 25rpx 0;
			border-radius: 10rpx;
			overflow: hidden;
			display: -webkit-box !important;
			text-overflow: ellipsis;
			word-break: break-all;
			-webkit-line-clamp: 1;
			-webkit-box-orient: vertical !important;
		}

		.helpme {
			background: #f5f8f7;
			border-radius: 20rpx;
			padding: 30rpx;
			width: 48%;

			margin-bottom: 25rpx;

			height: 92px;

			.title {
				font-size: 30rpx;
				font-weight: bold;
				margin-bottom: 18rpx;
				color: #000;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap
			}

			.sub_title {
				color: #8f9ca2;
				font-size: 24rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
			}
		}
	}

	.wxad {
		border-radius: 20rpx;
		overflow: hidden;
		margin-bottom: 24rpx;
	}

	.cu-dialog {
		position: fixed;

		top: 60rpx;
		left: 100rpx;
		// background: #000000;
	}

	.guide-img {
		width: 580rpx;
		height: 430rpx;
	}
</style>