<template>
	<view class="invest-summary-page" v-if="flag">
		<tn-navbar
		    fixed
		    bg-color="rgba(255, 255, 255, 0.01)"
		    :bottom-shadow="false"
		    :placeholder="false"
		/>
		
		<view class="invest-summary"
		  	:style="{'background':'url('+ menuData.pic +') no-repeat','backgroundPosition':'center center','backgroundSize':'100% 100%'}"
		  >
		      <view class="summary-content">
		        <view class="summary-info">
		          <view class="title">{{ menuData.title }}</view>
		          <view class="desc">{{ menuData.description }}</view>
		        </view>
		      </view>
		</view>
		
		<view class="invest-container tn-pt-lg"  v-if="flag">
			<view class="tn-radius tn-m-lg">
				<view class="title-container">
				  <view class="left">
				    <view class="title">{{investData.title}}</view>
				    <view class="desc tn-text-lg">
						<tn-icon name="clock" size="36rpx"/>
						&nbsp;{{investData.dateMonth.replace('-','年01-')+'月'}}
					</view>
				  </view>
				</view>
				
				<view class="invest-detail tn-pt-lg">
					<view class="summary introduction-content">
				
						<view class="tn-pb-lg tn-text-bold tn-text-2xl tn-text-center" :style="{'color':menuData.color}">
							招商概况
						</view>
						
						<view class="invest-review ">
							<view class="review-item tn-flex tn-flex-center-between tn-radius tn-p tn-mb-sm" style="width: 100%;">
								<view class="item">
									<TnTitle title="引进项目" mode="vLine" size="lg"/>
									<view class="tn-text-bold tn-text-2xl tn-pl-sm tn-mt">
										<tn-count-scroll :value="investData.introduced" :text-color="menuData.color"/><span style="font-size: 30rpx;">个</span>
									</view>
								</view>
								<view class="item">
									<TnTitle title="协议引资" mode="vLine" size="lg"/>
									<view class="tn-pl-sm tn-text-bold tn-text-2xl tn-mt">
										<tn-count-scroll :value="investData.amount" :text-color="menuData.color"/><span style="font-size: 30rpx;">亿元</span>
									</view>
								</view>
							</view>
							<view class="tn-flex tn-flex-center-between">
								<view class="review-item tn-p tn-radius">
									<view class="">
										<TnTitle title="工业项目" mode="vLine" size="lg"/>
										<view class="tn-text-bold tn-text-2xl tn-pl-sm">
											<tn-count-scroll :value="investData.industry_introduced" :text-color="menuData.color"/><span style="font-size: 30rpx;">个</span>
										</view>
									</view>
									<view class="">
										<view class="tn-text-lg tn-pl-sm">
											协议引资
										</view>
										<view class="tn-text-bold tn-text-2xl tn-pl-sm">
											<tn-count-scroll :value="investData.industry_amount" :text-color="menuData.color"/><span style="font-size: 30rpx;">亿元</span>
										</view>
									</view>
								</view>
								<view class="review-item tn-p tn-radius">
									<view class="">
										<TnTitle title="非工业项目" mode="vLine" size="lg"/>
										<view class="tn-text-bold tn-text-2xl tn-pl-sm">
											<tn-count-scroll :value="investData.normal_introduced" :text-color="menuData.color"/><span style="font-size: 30rpx;">个</span>
										</view>
									</view>
									<view class="">
										<view class="tn-text-lg tn-pl-sm">
											协议引资
										</view>
										<view class="tn-text-bold tn-text-2xl tn-pl-sm">
											<tn-count-scroll :value="investData.normal_amount" :text-color="menuData.color"/><span style="font-size: 30rpx;">亿元</span>
										</view>
									</view>
								</view>
							</view>
						</view>
				
						<view class="tn-p-lg tn-text-bold tn-text-2xl tn-text-center" :style="{'color':menuData.color}">
							工业项目投资情况
						</view>
						<view class="industry">
							<view class="item tn-radius tn-p-sm tn-mb-sm">
								<TnTitle title="独立供地项目" mode="vLine" size="lg"/>
								<view class="tn-flex tn-flex-center-between">
									<view class="data-text tn-p-xs">
										<view class="tn-text-lg">项目数量</view>
										<view>
											<span class="tn-text-2xl tn-text-bold" :style="{'color':menuData.color}">{{investData.independent_land}}</span>个
										</view>	
									</view>
									<view class="data-text tn-p-xs">
										<view class="tn-text-lg">协议引资</view>
										<view>
											<span class="tn-text-2xl tn-text-bold" :style="{'color':menuData.color}">{{investData.independent_amount}}</span>亿元
										</view>	
									</view>
									<view class="data-text tn-p-xs">
										<span class="tn-text-lg">拟用地面积</span>
										<view>
											<span class="tn-text-2xl tn-text-bold" :style="{'color':menuData.color}">{{investData.independent_area}}</span>亩
										</view>
									</view>
								</view>
							</view>
							
							<view class="item tn-radius tn-p-sm">
								<TnTitle title="入驻厂房项目" mode="vLine" size="lg"/>
								<view class="tn-flex tn-flex-center-between">
									<view class="data-text tn-p-xs">
										<view class="tn-text-lg">项目数量</view>
										<view>
											<span class="tn-text-2xl tn-text-bold" :style="{'color':menuData.color}">{{investData.enter_build}}</span>个
										</view>
									</view>
									<view class="data-text tn-p-xs">
										<view class="tn-text-lg">协议引资</view>
										<view>
											<span class="tn-text-2xl tn-text-bold" :style="{'color':menuData.color}">{{investData.enter_amount}}</span>亿元
										</view>	
									</view>
									<view class="data-text tn-p-xs">
										<view class="tn-text-lg">拟使用面积</view>
										<view>
											<span class="tn-text-2xl tn-text-bold" :style="{'color':menuData.color}">{{investData.enter_area}}</span>万㎡
										</view>
									</view>
								</view>
							</view>
						</view>
				
						<view class="tn-p-lg tn-text-bold tn-text-2xl tn-text-center" :style="{'color':menuData.color}">
							投资项目分布情况
						</view>
						
						<view class="charts">
							<ucharts-bar :chartData="barChartData"/>
						</view>
						
						<view class="tn-p-lg tn-text-bold tn-text-2xl tn-text-center" :style="{'color':menuData.color}">
							市外项目考核指标完成情况
						</view>
						
						<view>
							<view  class="tn-mt-sm tn-mb-sm tn-p-xs">
								<TnTitle title="正式合同额" mode="vLine" size="lg"/>
								<view class="tn-pt">
									<TnLineProgress
										:percent="contractProgress"
										active-color="tn-indigo"
										inactive-color="tn-grey-light" 
										height="30rpx"
										show-percent />
								</view>
							</view>
							<view  class="tn-mt-sm tn-mb-sm tn-p-xs">
								<TnTitle title="资金到位额" mode="vLine" size="lg"/>
								<view class="tn-pt">
									<TnLineProgress 
										:percent="fundProgress"
										active-color="tn-indigo"
										inactive-color="tn-grey-light" 
										height="30rpx"
										show-percent />
								</view>
							</view>
							<view class="tn-mt-sm tn-mb-sm tn-p-xs">
								<TnTitle title="新开工数" mode="vLine" size="lg"/>
								<view class="tn-pt">
									<TnLineProgress 
										:percent="projectProgress" 
										active-color="tn-indigo"
										inactive-color="tn-grey-light" 
										height="30rpx"
										show-percent />
								</view>		
							</view>
						</view>
				
						
						<view v-if="investData.review" class="tn-text-lg tn-mt" style="line-height: 1.8em;color: #444444;">
							<mp-html :content="investData.review" @linktap="downloadFile"/>
						</view>
					  	
					</view>
				</view>
			</view>
		</view>
		
		<tn-date-time-picker v-model="dateTimeValue" v-model:open="openDateTimePicker" init-current-date-time format="YYYY-MM" mode="yearmonth" @confirm="getDate"/>

		<view class="picker-btn" @click="showPicker">
			<tn-button shape="circle" type="success" size="lg" shadow shadow-color="tn-type-primary_shadow">
				<tn-icon name="history" :size="50"/>
			</tn-button>
		</view>
	</view>	  
</template>

<script lang="ts" setup>
	import { onMounted, reactive, ref } from 'vue';
	import request from '@/utils/request';
	import { onLoad } from "@dcloudio/uni-app";
	import UchartsBar from '@/pages/components/ucharts-bar.vue';
	import TnCountScroll from '@/uni_modules/tuniaoui-vue3/components/count-scroll/src/count-scroll.vue'
	import TnDateTimePicker from '@/uni_modules/tuniaoui-vue3/components/date-time-picker/src/date-time-picker.vue'
	import onShare from '@/utils/share';
	import TnLineProgress from '@/uni_modules/tuniaoui-vue3/components/line-progress/src/line-progress.vue'
	
	const { onShareAppMessage,onShareTimeline } = onShare()
	
	const shareTitle = ref()
	
	const shareParam = ref()
	
	const color = ref();
	
	const flag = ref(false);
	
	const openDateTimePicker = ref(false)
	
	const orgId = ref();
	
	const investData = reactive({
		parkId: '',
		dateMonth:'',
		title:'',
		parkName:'',
		introduced:0,
		amount:0,
		industry_introduced:0,
		normal_introduced:0,
		industry_amount:0,
		normal_amount:0,
		independent_land:0,
		independent_amount:0,
		independent_area:0,
		enter_build:0,
		enter_amount:0,
		enter_area:0,
		review:''
	})
	
	const barChartData = ref({})
	
	const contractProgress = ref(0)
	
	const fundProgress = ref(0)
	
	const projectProgress = ref(0)
	
	const dateTimeValue = ref();
	
	const menuData = reactive({
		pic:'',
		title:'',
		color:'',
		description:''
	})
	
	const showPicker = () => {
		openDateTimePicker.value = true
	}
	
	const getDate = (value:string) => {
		getInvestData(value)
	}
	
	const getMenuData = (id:number) => {
		request({
			url:'/front/report/menu',
			method:'GET',
			data:{ menuId:id }
		}).then((res:any)=>{
			if(res.data.code === 0){
				Object.assign(menuData,res.data.data)
				shareParam.value = res.data.data.menuId
				color.value = res.data.data.color
			}else{
				uni.showToast({
					icon:'none',
					title:res.data.message
				})
			}
		})
	}
	
	const getInvestData = (date:string) => {
		request({
			url:'/front/report/invest/summary',
			method:'GET',
			data:{id:orgId.value,month:date}
		}).then((res:any) => {
			if(res.data.code === 0){
				Object.assign(investData,res.data.data)
				shareTitle.value = res.data.data.title
				contractProgress.value = parseFloat((res.data.data.contract_amount / res.data.data.contract_aim * 100).toFixed(2))
				fundProgress.value = parseFloat((res.data.data.fund_amount / res.data.data.fund_aim * 100).toFixed(2))
				projectProgress.value = parseFloat((res.data.data.started_project / res.data.data.project_aim * 100).toFixed(2))
				setTimeout(() => {
				    let res1 = {
				        categories: ["10~20亿","20~50亿","50~100亿","100亿以上"],
				            series: [
				              {
				                name: "项目数",
				                data: [res.data.data.over10,res.data.data.over20,res.data.data.over50,res.data.data.over100,]
				              },
				            ]
				          };
				        barChartData.value = JSON.parse(JSON.stringify(res1));
				}, 500);
	
			}else{
				uni.showToast({
					icon:'none',
					title:res.data.message
				})
			}
			flag.value = true
		})
	}
	
	const downloadFile = (file:any) => {
		uni.downloadFile({
			url:file.href,
			success(res) {
				if (res.statusCode === 200) {
					uni.saveFile({
						tempFilePath: res.tempFilePath, //临时路径
						success: function(res) {
							uni.showToast({
								icon: 'none',
								mask: true,
								title: '文件已保存：' + res.savedFilePath, //保存路径
								duration: 3000,
							});
							setTimeout(() => {
								//打开文档查看
								uni.openDocument({
									filePath: res.savedFilePath,
									showMenu: true,
									success: function() {
										console.log('打开文档成功');
									}
								});
							}, 3000)
						}
					})
					console.log('下载成功');
				}
			},
			fail: (err) => {
				console.log(err);
				uni.showToast({
					icon: 'none',
					mask: true,
					title: '失败请重新下载',
				});
			},
		})
	}
	
	onShareAppMessage(() => {
		return {
			title:shareTitle.value,
			path:'/pages/report/invest/child/index?mid=' + shareParam.value + '&id=' + orgId.value
		}
	})
	
	onShareTimeline(() => {
		return {
			title:shareTitle.value,
			path:'/pages/report/invest/child/index?mid=' + shareParam.value  + '&id=' + orgId.value
		}
	})
	
	onLoad((option)=>{
		getMenuData(option.mid)
		orgId.value = option.id
	})
	
	onMounted(()=>{
		getInvestData('')
	})
	
</script>

<style lang="scss" scoped>
	.invest-summary {
	  position: relative;
	  width: 100%;
	  height: 450rpx;
	  z-index: 2;
	
	  &::before {
	    content: '';
	    position: absolute;
	    left: 0;
	    top: 0;
	    width: 100%;
	    height: 100%;
	    background: linear-gradient(
	      0deg,
	      rgba(0, 0, 0, 0.4) 0%,
	      rgba(0, 0, 0, 0) 100%
	    );
	    z-index: 1;
	  }
		
	  .summary-content {
	    position: absolute;
	    left: 0;
	    bottom: 80rpx;
	    width: 100%;
	    padding: 0rpx 30rpx;
	    z-index: 2;
	    display: flex;
	    align-items: center;
	
	    .summary-info {
	      flex: 1;
	      color: #fff;
	
	      .title {
	        font-size: 52rpx;
	        font-weight: bold;
	        letter-spacing: 8rpx;
	        text-shadow: 4rpx 4rpx 10rpx #585858;
	      }
	      .desc {
	        font-size: 34rpx;
	        margin-top: 20rpx;
	        letter-spacing: 4rpx;
	        text-shadow: 4rpx 4rpx 10rpx #585858;
	      }
	    }
	  }
	}
	.invest-container{
		position: relative;
		top: -40rpx;
		z-index: 99;
		background-color: #fff;
		border-radius: 40rpx 40rpx 0 0;
		.title-container {
		  width: 100%;
		  display: flex;
		  align-items: center;
		  justify-content: space-between;
		  line-height: 1;
		
		  .left {
		    flex: 1;
		
		    .title {
		      font-size: 40rpx;
		      font-weight: bold;
		    }
		    .desc {
		      margin-top: 32rpx;
		    }
		  }
		
		  .right {
		    flex-grow: 0;
		    flex-shrink: 0;
		    display: flex;
		    flex-direction: column;
		    align-items: center;
		    margin-left: 20rpx;
		
		    .icon {
		      font-size: 70rpx;
		    }
		
		    .text {
		      font-size: 26rpx;
		      margin-top: 10rpx;
		    }
		  }
		}
		.invest-detail {
		  position: relative;
		  border-radius: 20rpx 20rpx 0 0;
		  z-index: 10;
		  .text{
			  letter-spacing: 4rpx;
			  text-align: justify;
		  }
		
		  .topic-item {
		    & + .topic-item {
		      margin-top: 36rpx;
		    }
		  }
		.invest-review{
			.review-item{
				width: calc(100% / 2 - 10rpx);
						background:linear-gradient(-45deg, rgba(0, 150, 255, 0.4), rgba(0, 150, 255, 0.05));
			}
		}
		.industry{
					  .item{
						  background: linear-gradient(-45deg, rgba(0, 150, 255, 0.4), rgba(0, 150, 255, 0.05));
						  .data-text{
							  width: calc(100% / 3 - 15rpx);
						  }
						  
					  }
		}
		}
	}
	.park-container {
		width: fit-content;
		height:100%;
		.scroll-item {
		    flex-grow: 1;
		    .empty {
		      width: 100rpx;
		      height: 100rpx;
		      border-radius: 15rpx;
		    }
		    .title {
		      margin-top: 10rpx;
		    }
		} 
	}
	.picker-btn{
		position: fixed;
		z-index: 999;
		right: 40rpx;
		bottom: 60rpx;
	}
</style>