<script setup>
	/**
	 * name：
	 * user：sa0ChunLuyu
	 * date：2023年4月11日 17:56:51
	 */
	import {
		onMounted,
		ref
	} from 'vue'
	import {
		ConfigGetAction,
		CarouselListAction,
		HospitalCountAction,
		HospitalExtraInfoAction,
		UserIsCheckedAction,
		$image,
		$response
	} from '@/api'
	import {
		onShow
	} from '@dcloudio/uni-app'
	import CheckLogin from '@/components/check_login/check_login.vue'
	import {
		useStore
	} from '@/store'
	const $store = useStore()
	onMounted(() => {
		getHospitalInfo()
		setComboItemOpenFunc()
		setOrderChooseTypeFunc()
		setActivityPackageFunc()
		getUserInfo()
	})



	const user_info = ref(false)
	const getUserInfo = () => {
		uni.$lu.user((info) => {
			user_info.value = info
			if (!!user_info.value.id) {
				HospitalCount()
			} else {
				$store.buy_info = {
					hospital: 0,
					combo: 0,
					items: [],
					group: '',
					person: [],
					time: 0
				}
			}
		})

	}

	const hospital_info = ref(false)
	const getHospitalInfo = () => {
		uni.$lu.hospital((info) => {
			hospital_info.value = info
			uni.setNavigationBarTitle({
				title: hospital_info.value.name
			})
			CarouselList(1)
			CarouselList(2)
			CarouselList(3)
			CarouselList(4)
		})
	}
	const choose_type_show = ref(false)
	const setOrderChooseTypeFunc = () => {
		uni.$lu.open.order_choose_type = () => {
			if (!!user_info.value && user_info.value.person.id) {
				uni.navigateTo({
					url: '/pages/buy/person/person?type=choose'
				})
			} else {
				uni.$lu.toast("请先添加体检人")
			}
		}
	}
	const setActivityPackageFunc = () => {
		uni.$lu.open.activity_package = (id) => {
			$store.activity_package = Number(id);
			uni.switchTab({
				url: '/pages/main/combo/combo'
			})
		}
	}
	const setComboItemOpenFunc = () => {
		uni.$lu.open.combo_item_open = (combo, items, time) => {
			let items_ids = !!items ? items.split(',') : []
			time = JSON.parse(time)
			$store.buy_info.hospital = hospital_info.value.id
			$store.buy_info.combo = combo
			$store.buy_info.items = items_ids
			$store.buy_info.group = ''
			$store.buy_info.time = time
			if ($store.buy_info.person.length === 0) {
				uni.navigateTo({
					url: '/pages/buy/person/person?type=choose'
				})
			} else {
				uni.navigateTo({
					url: '/pages/buy/info/info'
				})
			}
		}
	}

	const hospital_count = ref(0)
	const HospitalCount = async () => {
		const response = await HospitalCountAction()
		$response(response, () => {
			hospital_count.value = response.data.count
		})
	}
	const carouse_list = ref([])
	const ad_list = ref([])
	const button_list = ref([])
	const h5_list = ref([])
	const CarouselList = async (type) => {
		const response = await CarouselListAction({
			type,
			hospital: hospital_info.value.id
		})
		$response(response, () => {
			switch (type) {
				case 1:
					carouse_list.value = response.data.list
					if (carouse_list.value.length === 0) ConfigGet()
					break;
				case 2:
					ad_list.value = response.data.list
					break;
				case 3:
					button_list.value = response.data.list
					break;
				case 4:
					h5_list.value = response.data.list
					uni.setStorage({
						key: 'h5_list',
						data: h5_list.value[1],
						success: function () {
							console.log('success');
						}
					});
					break;
			}
		})
	}
	const cover_image = ref('')
	const ConfigGet = async () => {
		const response = await ConfigGetAction({
			label_arr: ['小程序默认封面图']
		})
		$response(response, () => {
			cover_image.value = response.data['小程序默认封面图']
		})
	}
	const actionClick = (info) => {
		console.log(info)
		if (info.id == 9) {
			//判断用户下是否有订单
			userischecked(info)


		} else {
			uni.$lu.jump(info)
		}

	}


	const userischecked = async (info) => { //判断是否支付且体检完成
		const response = await UserIsCheckedAction({
			userid: user_info.value.id
		})

		$response(response, () => {

			if (response.data.status) {
				
				//info.jump_path = info.jump_path.replace('question','question2')
				info.jump_path=info.jump_path+"&orderid="+response.data.info[0].id
				console.log(info.jump_path )
				uni.$lu.jump(info)
			}else{
				uni.$lu.toast("完成体检后可参与评价");
			}
			

		})
	}

	const loginCallback = () => {
		getHospitalInfo()
		getUserInfo()
	}

	const changeHospitalClick = () => {
		uni.$lu.jump({
			jump_type: 2,
			jump_path: '/pages/hospital/choose_list/choose_list',
			login_type: 1
		})
	}

	const addPersonClick = () => {
		uni.$lu.jump({
			jump_type: 2,
			jump_path: '/pages/user/edit/edit?id=0',
			login_type: 1
		})
	}

	const mapClick = (info) => {
		uni.openLocation({
			type: 'wgs84',
			latitude: Number(hospital_info.value.latitude),
			longitude: Number(hospital_info.value.longitude),
			scale: 18,
			name: hospital_info.value.hospital_name,
			address: hospital_info.value.address
		})
	}
</script>
<template>
	<view>
		<view @click="addPersonClick()" v-if="!!user_info && !user_info.person.id" class="user_status_wrapper">
			<view class="user_status_text_wrapper" v-if="!user_info.id">您还没有登录，<text>点击登录</text></view>
			<view class="user_status_text_wrapper" v-if="!!user_info.id && !user_info.person.id">
				您还没有添加体检人，<text>点击添加</text>
			</view>
		</view>
		<CheckLogin @callback="loginCallback()"></CheckLogin>
		<view class="carousel_wrapper">
			<swiper v-if="carouse_list.length > 0" class="swiper_wrapper" circular :autoplay="true" :duration="500">
				<swiper-item v-for="(i,k) in carouse_list" :key="k">
					<view @click="actionClick(i)" class="swiper_item_wrapper">
						<image mode="aspectFill" :src="$image(i.image)"></image>
					</view>
				</swiper-item>
			</swiper>
			<view v-else class="cover_image_wrapper">
				<image v-if="!!cover_image" mode="aspectFill" :src="$image(cover_image)"></image>
			</view>
		</view>
		<view class="hospital_info_wrapper">
			<view class="hospital_info_box_wrapper">
				<view @click="mapClick()" class="hospital_info_logo_wrapper">
					<image mode="aspectFill" :src="$image(hospital_info.logo)"></image>
				</view>
				<view @click="mapClick()" class="hospital_info_info_wrapper">
					<view class="hospital_info_info_name_wrapper">{{ hospital_info.name }}</view>
					<view class="hospital_info_info_address_wrapper">
						<view class="hospital_info_info_address_icon_wrapper">
							<image :src="$image('/storage/assets/mp/home/医院地址@2x.png')"></image>
						</view>
						<view class="hospital_info_info_address_text_wrapper">
							{{ hospital_info.address }}
						</view>
					</view>
				</view>
				<view @click="changeHospitalClick()" v-if="hospital_count > 1" class="hospital_info_button_wrapper">
					<view class="hospital_info_button_icon_wrapper">
						<image :src="$image('/storage/assets/mp/home/切换医院@2x.png')"></image>
					</view>
					<view class="hospital_info_button_text_wrapper">切换医院</view>
				</view>
			</view>
		</view>
		<view class="button_wrapper">
			<view v-for="(i,k) in button_list" :key="k">
				<view @click="actionClick(i)" class="button_item_wrapper">
					<view class="button_item_icon_wrapper">
						<image mode="aspectFill" :src="$image(i.image)"></image>
					</view>
					<view class="button_item_info_wrapper">
						<view class="button_item_info_name_wrapper">{{ i.name }}</view>
						<view v-if="!!i.desc" class="button_item_info_desc_wrapper">{{ i.desc }}</view>
					</view>
				</view>
				<view v-if="k!==button_list.length - 1" class="button_item_line_wrapper"></view>
			</view>
		</view>
		<view class="ad_wrapper" v-if="ad_list.length > 0">
			<swiper class="ad_swiper_wrapper" circular :autoplay="true" :duration="500">
				<swiper-item v-for="(i,k) in ad_list" :key="k">
					<view @click="actionClick(i)" class="ad_swiper_item_wrapper">
						<image mode="aspectFill" :src="$image(i.image)"></image>
					</view>
				</swiper-item>
			</swiper>
		</view>
		<view class="h5_wrapper">
			<view class="h5_title_wrapper">常用功能</view>
			<view class="h5_list_wrapper">
				<uni-grid :column="2" :showBorder="false" :square="false">
					<uni-grid-item v-for="(i,k) in h5_list" :key="k">
						<view @click="actionClick(i)" class="h5_item_wrapper">
							<view class="h5_item_info_wrapper">
								<view class="h5_item_info_name_wrapper">{{ i.name }}</view>
								<view class="h5_item_info_desc_wrapper">{{ i.desc }}</view>
							</view>
							<view class="h5_item_icon_wrapper">
								<image mode="aspectFill" :src="$image(i.image)"></image>
							</view>
						</view>
					</uni-grid-item>
				</uni-grid>
			</view>
		</view>
		<view class="home_blank_wrapper"></view>
	</view>
</template>
<style>
	page {
		background: #ffffff;
	}
</style>
<style scoped>
	.user_status_text_wrapper {
		margin-left: 41rpx;
	}

	.user_status_text_wrapper text {
		color: #F69C44;
	}

	.user_status_wrapper {
		position: fixed;
		width: 750rpx;
		height: 60rpx;
		background: #f2f7f7;
		top: 0;
		z-index: 1;
		font-size: 22rpx;
		font-weight: 400;
		color: #019AA7;
		line-height: 60rpx;
	}

	.h5_item_info_wrapper {
		margin-left: 10rpx;
	}

	.h5_item_info_desc_wrapper {
		font-size: 23rpx;
		font-weight: 500;
		color: #999A9A;
		line-height: 1;
		margin-top: 15rpx;
		display: block;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		width: 160rpx;
	}

	.h5_item_info_name_wrapper {
		width: 160rpx;
		font-size: 27rpx;
		font-weight: 500;
		color: #222222;
		line-height: 1;
		display: block;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	.h5_item_icon_wrapper image {
		width: 131rpx;
		height: 120rpx;
	}

	.h5_item_icon_wrapper {
		position: absolute;
		width: 131rpx;
		height: 120rpx;
		top: 0;
		right: 14rpx;
	}

	.home_blank_wrapper {
		height: 30rpx;
	}

	.h5_list_wrapper {
		margin-top: 20rpx;
	}

	.h5_item_wrapper {
		width: 338rpx;
		height: 120rpx;
		background: #F6F8F8;
		border-radius: 10rpx;
		margin: 10rpx auto;
		position: relative;
		display: flex;
		align-items: center;
	}

	.h5_title_wrapper {
		font-size: 32rpx;
		font-weight: 500;
		color: #222222;
		line-height: 1;
		margin-top: 40rpx;
		margin-left: 29rpx;
	}

	.ad_swiper_item_wrapper image {
		height: 150rpx;
		width: 690rpx;
		display: block;
	}

	.ad_swiper_item_wrapper {
		height: 150rpx;
		width: 690rpx;
	}

	.ad_wrapper {
		height: 150rpx;
		width: 690rpx;
		margin: 30rpx auto 0;
		border-radius: 10rpx;
		overflow: hidden;
	}

	.ad_swiper_wrapper {
		height: 150rpx;
		width: 690rpx;
	}

	.button_item_line_wrapper {
		width: 654rpx;
		height: 1rpx;
		margin: 0 auto;
		background: #E8E8E8;
		box-shadow: 0rpx 0rpx 0rpx 1rpx #FFFFFF;
	}

	.button_item_info_wrapper {
		margin-left: 26rpx;
	}

	.button_item_info_desc_wrapper {
		font-size: 23rpx;
		font-weight: 500;
		color: #999A9A;
		line-height: 1;
		margin-top: 14rpx;
	}

	.button_item_info_name_wrapper {
		font-size: 30rpx;
		font-weight: 500;
		color: #222222;
		line-height: 1;
	}

	.button_item_icon_wrapper image {
		width: 70rpx;
		height: 70rpx;
		display: block;
	}

	.button_item_icon_wrapper {
		width: 70rpx;
		height: 70rpx;
	}

	.button_item_wrapper {
		height: 137rpx;
		width: 654rpx;
		margin: 0 auto;
		display: flex;
		align-items: center;
	}

	.button_wrapper {
		width: 690rpx;
		background: #EEF5F5;
		border-radius: 10rpx;
		margin: 45rpx auto 0;
		overflow: hidden;
	}

	.hospital_info_button_text_wrapper {
		font-size: 22rpx;
		font-weight: 500;
		color: #FFFFFF;
		line-height: 1;
		margin-left: 6rpx;
	}

	.hospital_info_button_icon_wrapper image {
		width: 20rpx;
		height: 18rpx;
		display: block;
	}

	.hospital_info_button_icon_wrapper {
		width: 20rpx;
		height: 18rpx;
	}

	.hospital_info_button_wrapper {
		width: 140rpx;
		height: 40rpx;
		background: linear-gradient(90deg, #23D3AF, #0DC5CF);
		border: 2px solid #D0EEEF;
		border-radius: 20rpx;
		display: flex;
		align-items: center;
		line-height: 40rpx;
		justify-content: center;
	}

	.hospital_info_info_address_text_wrapper {
		font-size: 22rpx;
		font-weight: 500;
		color: #AFB0B1;
		line-height: 24rpx;
		margin-left: 11rpx;
		display: block;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	.hospital_info_info_name_wrapper {
		font-size: 28rpx;
		font-weight: 500;
		color: #2C2C2C;
		line-height: 1;
		display: block;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	.hospital_info_info_wrapper {
		margin-left: 24rpx;
		width: 413rpx;
	}

	.hospital_info_info_address_wrapper {
		display: flex;
		align-items: center;
		margin-top: 17rpx;
	}

	.hospital_info_info_address_icon_wrapper image {
		width: 20rpx;
		height: 24rpx;
		display: block;
	}

	.hospital_info_info_address_icon_wrapper {
		width: 20rpx;
		height: 24rpx;
	}

	.hospital_info_logo_wrapper image {
		width: 82rpx;
		height: 82rpx;
		display: block;
		border: 6rpx;
	}

	.hospital_info_logo_wrapper {
		width: 82rpx;
		height: 82rpx;
		margin-left: 12rpx;
	}

	.hospital_info_box_wrapper {
		width: 690rpx;
		height: 150rpx;
		background: linear-gradient(0deg, #EBFAFB, #F9FEFE);
		border: 2px solid rgba(255, 255, 255, 0.38);
		box-shadow: 0rpx 4rpx 6rpx 4rpx rgba(32, 211, 179, 0.16);
		border-radius: 10rpx;
		position: absolute;
		top: -20rpx;
		display: flex;
		align-items: center;
		left: 0;
		right: 0;
	}

	.hospital_info_wrapper {
		height: 130rpx;
		width: 690rpx;
		margin: 0 auto;
		position: relative;
	}

	.swiper_item_wrapper image {
		width: 750rpx;
		height: 389rpx;
		display: block;
	}

	.swiper_wrapper {
		width: 750rpx;
		height: 389rpx;
	}

	.swiper_item_wrapper {
		width: 750rpx;
		height: 389rpx;
	}

	.cover_image_wrapper image {
		width: 750rpx;
		height: 389rpx;
		display: block;
	}

	.cover_image_wrapper {
		width: 750rpx;
		height: 389rpx;
	}

	.carousel_wrapper {
		width: 750rpx;
		height: 389rpx;
	}
</style>