<template>
	<view class="home">
		<TopCard :info="info" @update="onUpdate" />
		<!-- tab导航栏 -->
		<view class="tabs">
			<view v-for="(item, index) in navList" :key="index" class="tab" :class="{'is-active': tabCurId === item.id}"
				@click="tabClick(index)">
				{{item.text}}
			</view>
		</view>
		<swiper :current="tabCurId" class="swiper-box" duration="300" @change="onChangeTab">
			<swiper-item class="tab-content" v-for="(_tabItem,tabIndex) in navList" :key="tabIndex">
				<CfStatus v-if="tabCurId==0" />
				<CfLogistics v-if="tabCurId==1" />
				<CfContent v-if="tabCurId==2" />
				<CfAppraise v-if="tabCurId==3" />
			</swiper-item>
		</swiper>
	</view>
</template>

<script lang="ts" setup>
	import { ref, nextTick, onMounted } from 'vue'
	import rest from '@/stores/rest'
	import { BasicInfoVO } from '../../lib/type'

	import TopCard from './TopCard.vue'
	import CfStatus from './CfStatus.vue'
	import CfLogistics from './CfLogistics.vue'
	import CfContent from './CfContent.vue'
	import CfAppraise from './CfAppraise.vue'

	import {
		getSexByCardNo,
		maskCardNo,
		maskName,
		} from '@/lib/util'
	

	const topCardRef = ref()
	const originalInfo = ref({
		name: '',
		address: '',
		addressDetails:'',
		addr: '',
		sex: '',
		text: '近三个月就诊记录',
		idCard: '',
		date: ''
	})
	const info = ref({ ...originalInfo.value})
	
	const tabCurId = ref(2)//当前tab选中Id
	// const loaded = ref(false)
	// const status = ref('loading')
	// const loadText = ref('数据加载中...')
	const navList = ref([{
		id: 0,
		text: '处方状态',
	},
	{
		id: 1,
		text: '物流信息',
	},
	{
		id: 2,
		text: '处方内容',
	},
	{
		id: 3,
		text: '处方评价',
	}
	])

	const swiperBoxStyle = () => {
		let _h = `calc(100% - 72rpx - 
	cfListDtos: boolean;${topCardRef.value?.offsetHeight})`;
		console.log("_h", _h)
		return {
			height: '200px',
		};
	};

	//顶部tab点击
	const tabClick = (idx : number) => {
		tabCurId.value = idx;
	}
	//滑动标签页
	const onChangeTab = (e) => {
		let _inx = e.detail.current;
		tabClick(_inx);
	}
	
	const dataList = ref({
		hzsfzh: '510224196901293189',
		hzxm: '刘秀碧',
	})
	const cfDataList = ref([]) // 处方列表
	
	// 地址信息拼接
	const addressInfSplice = (basic:string, details:string, isShow) => {
	    if (isShow) {
	        return basic + details;
	    } else {
	        return basic + '*'.repeat(details.length);
	    }
	}

	const getInfo = async () => {
		// todo 基础信息
		let basicInfo = await rest.get('/app-api/bmfw/findList',dataList.value) as BasicInfoVO
		console.log("基础信息", basicInfo)
		// todo 地址信息
		let addressInfo = await rest.get('/default/consignee-info/list',{hzid:dataList.value.hzsfzh})
		console.log("地址信息", addressInfo)
		   if (Array.isArray(basicInfo) && basicInfo.length > 0) {
			const firstBasicInfo = basicInfo[0]
			originalInfo.value.name = firstBasicInfo.hzxm
			originalInfo.value.sex = getSexByCardNo(firstBasicInfo.hzsfzh)
			originalInfo.value.idCard = firstBasicInfo.hzsfzh
			originalInfo.value.date = firstBasicInfo.cfListDtos?.[0]?.kfrq || ''
		}


		if (Array.isArray(addressInfo) && addressInfo.length > 0) {
		    const firstAddressInfo = addressInfo[0]
		    originalInfo.value.address = `${firstAddressInfo.province || ''}${firstAddressInfo.city || ''}${firstAddressInfo.area || ''}`
			originalInfo.value.addressDetails = firstAddressInfo.shrdzxxdz
		}
		cfDataList.value = basicInfo[0]?.cfListDtos || []
		console.log("处方列表", cfDataList.value)
		onUpdate(false)
	}

	const onUpdate = (isMasked: boolean) => {
	    info.value.name = isMasked ? maskName(originalInfo.value.name) : originalInfo.value.name
	    info.value.addr = addressInfSplice(originalInfo.value.address, originalInfo.value.addressDetails, !isMasked)
	    info.value.idCard = isMasked ? maskCardNo(originalInfo.value.idCard) : originalInfo.value.idCard
	    info.value.sex = getSexByCardNo(originalInfo.value.idCard)
	    info.value.date = originalInfo.value.date

	    console.log("onUpdate", info.value)
	}

	onMounted(() => {
		getInfo()
	})
</script>

<style lang="scss" scoped>
	.home {
		display: flex;
		flex-direction: column;
		height: 100%;

		.tabs {
			margin: $uni-spacing-row-s4 0 $uni-spacing-row-s3;
			height: 88rpx;
			background-color: #fff;
			padding: 0 24rpx;
			@include flex-between;

			.tab {
				height: 100%;
				@include flex-center;

				&.is-active {
					color: $uni-color-primary;
					position: relative;

					&::after {
						content: '';
						position: absolute;
						bottom: 0;
						left: 0;
						background: $uni-color-primary;
						width: 100%;
						height: 4rpx;
					}
				}
			}
		}

		.swiper-box {
			flex: 1;

			.tab-content {
				:deep(.pub-loading) {
					height: 100%;
				}
			}
		}
	}
</style>