<template name="basics">
	<view>
		<scroll-view scroll-y class="page">
			<cu-custom bgColor="bg-blue"><block slot="content">首页</block></cu-custom>
			<view class="content-area">
				<view class="section-header first-section">
					<view class="header-icon">🏥</view>
					<view class="header-title">医院基本信息</view>
				</view>
				<!-- 今日/本周/本月收集数据 -->
				<view class="collection-stats">
					<view class="stat-card stat-today" @click="buttonClickToday">
						<view class="stat-icon">📅</view>
						<view class="stat-value">{{hospitalBasisInfo.today_collection || 0}}</view>
						<view class="stat-unit">kg</view>
						<view class="stat-label">今日收集</view>
					</view>
					<view class="stat-card stat-week" @click="buttonClickWeek">
						<view class="stat-icon">📊</view>
						<view class="stat-value">{{hospitalBasisInfo.week_collection || 0}}</view>
						<view class="stat-unit">kg</view>
						<view class="stat-label">本周收集</view>
					</view>
					<view class="stat-card stat-month" @click="buttonClickMonth">
						<view class="stat-icon">📈</view>
						<view class="stat-value">{{hospitalBasisInfo.month_collection || 0}}</view>
						<view class="stat-unit">kg</view>
						<view class="stat-label">本月收集</view>
					</view>
				</view>
				<view class="section-header">
					<view class="header-icon">📉</view>
					<view class="header-title">一周数据变化图</view>
				</view>
			<view class="qiun-charts">
				<canvas canvas-id="canvasLineA" id="canvasLineA" disable-scroll=true class="charts" :width="cWidth*pixelRatio" :height="cHeight*pixelRatio" @touchstart="touchLineA"></canvas>
				<image :src="imgSrc" style="width: 750upx;height: 500upx;"></image>
			</view>
			<!-- <canvas canvas-id="canvas1" class="canvas" disable-scroll="true"></canvas> -->
				<view class="section-header">
					<view class="header-icon">📦</view>
					<view class="header-title">医废库存</view>
				</view>
				<!-- 今日入库/今日出库 -->
				<view class="stock-stats">
					<view class="stat-card stat-in">
						<view class="stat-icon">📥</view>
						<view class="stat-value">{{stockBasisInfo.today_in_ware || 0}}</view>
						<view class="stat-unit">kg</view>
						<view class="stat-label">今日入库</view>
					</view>
					<view class="stat-card stat-out">
						<view class="stat-icon">📤</view>
						<view class="stat-value">{{stockBasisInfo.today_out_ware || 0}}</view>
						<view class="stat-unit">kg</view>
						<view class="stat-label">今日出库</view>
					</view>
				</view>
				<view class="section-header">
					<view class="header-icon">🏆</view>
					<view class="header-title">科室排行</view>
				</view>
			 <view class="qiun-charts" >
				<canvas canvas-id="canvasColumn" id="canvasColumn" disable-scroll=true class="charts" @touchstart="touchColumn"></canvas>
				<image :src="imgSrc2" style="width: 750upx;height: 500upx;"></image>
			</view>
				<view class="cu-tabbar-height"></view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import uCharts from '@/pages/component/u-charts.min.js';
	import  { isJSON } from '@/pages/component/checker.js';
	var _self;
	var canvaLineA=null;
	var canvaColumn=null;
	export default {
		data() {
			return {
				cWidth:'',
				cHeight:'',
				pixelRatio:1,
				textarea:'',
				serverData: '',
				hospital: '',
				sortHospital: '',
				department: '',
				duty: '',
				name: '',
				openid: '',
				phone: '',
				hospitalBasisInfo: [],
				stockBasisInfo: [],
				imgSrc: '',
				imgSrc2: ''
			}
		},
		mounted() {
			_self = this;
			//#ifdef MP-ALIPAY
			uni.getSystemInfo({
				success: function (res) {
					if(res.pixelRatio>1){
						//正常这里给2就行，如果pixelRatio=3性能会降低一点
						//_self.pixelRatio =res.pixelRatio;
						_self.pixelRatio =2;
					}
				}
			});
			//#endif
			if(getApp().globalData.hospital == '' || getApp().globalData.hospital == undefined){
				uni.reLaunch({
					url: '/pages/register/login'
				})
			}
			this.cWidth=uni.upx2px(750);
			this.cHeight=uni.upx2px(500);
			this.hospital = getApp().globalData.hospital;
			this.sortHospital = getApp().globalData.sortHospital;
			this.department = getApp().globalData.department;
			this.duty = getApp().globalData.duty;
			this.name = getApp().globalData.name;
			this.phone = getApp().globalData.phone;
			this.openid = getApp().globalData.openid;
			this.getServerData();
			this.getHospitalBasisInfo();
			this.getStockBasisInfo();
			this.getDepartmentRankList();
		},
		methods: {
			getServerData(){
				let that = this;
				uni.request({
					 url: 'https://lekapi.opmonitor.com/?c=app_api&a=getMedicalData7Days',
					data:{
						hospital: that.hospital,
						sortHospital: that.sortHospital,
						department: that.department,
						duty: that.duty
					},
					header: {
						'Content-type': 'application/json'
					},
					success: function(res) {
						let LineA={categories:[],series:[]};
						//这里我后台返回的是数组，所以用等于，如果您后台返回的是单条数据，需要push进去
						LineA.categories=res.data.data.categories;
						LineA.series=res.data.data.series;
						//第二根线为虚线的设置
						LineA.series[1].lineType='dash';
						LineA.series[1].dashLength=10;
						_self.textarea = JSON.stringify(res.data.data);
						_self.showLineA("canvasLineA",LineA);
					},
					fail: () => {
						_self.tips="网络错误，小程序端请检查合法域名";
					},
				});
			},
			showLineA(canvasId,chartData){
				canvaLineA=new uCharts({
					$this:_self,
					canvasId: canvasId,
					type: 'line',
					colors:['#4a90e2', '#1abc9c', '#e67e22', '#9b59b6'],
					fontSize:11,
					padding:[15,15,0,15],
					legend:{
						show:true,
						padding:5,
						lineHeight:11,
						margin:0,
					},
					dataLabel:true,
					dataPointShape:true,
					background:'#FFFFFF',
					pixelRatio:_self.pixelRatio,
					categories: chartData.categories,
					series: chartData.series,
					animation: true,
					xAxis: {
						type:'grid',
						gridColor:'#E8F4F8',
						gridType:'dash',
						dashLength:8
					},
					yAxis: {
						gridType:'dash',
						gridColor:'#E8F4F8',
						dashLength:8,
						format:(val)=>{return val.toFixed(0)}
					},
					width: _self.cWidth*_self.pixelRatio,
					height: _self.cHeight*_self.pixelRatio,
					extra: {
						line:{
							type: 'curve',
							width: 3
						}
					}
				});
				canvaLineA.addEventListener('renderComplete', () => {
					setTimeout(function(){
						uni.canvasToTempFilePath({
							x: 0,
							y: 0,
							width: _self.cWidth*_self.pixelRatio,
							height: _self.cHeight*_self.pixelRatio,
							fileType: 'png',
							canvasId: 'canvasLineA',
							success: function(res) {
								_self.imgSrc=res.tempFilePath;
							},
							fail:function(res){
								console.info(res)
							}
						},_self);
					},50);
				})
			},
			touchLineA(e) {
				canvaLineA.touchLegend(e);
				canvaLineA.showToolTip(e, {
					format: function (item, category) {
						return category + ' ' + item.name + ':' + item.data 
					}
				});
			},
			// 获取医院的基本信息
			getHospitalBasisInfo(){
				let that = this;
				uni.request({
					url: 'https://lekapi.opmonitor.com/?c=app_api&a=getHospitalBasisInfo',
					data: {hospital: that.hospital,sortHospital: that.sortHospital,department: that.department,duty: that.duty},
					header: {
						'Content-type': 'application/json'
					},
					success: function(res){
						that.hospitalBasisInfo = res.data.data;
					},
					fail: () => {
						console.info('小程序域名不正确，请检查域名的正确性')
					}
				})
			},
			// 获取库存的基本信息
			getStockBasisInfo(){
				let that = this;
				uni.request({
					url: 'https://lekapi.opmonitor.com/?c=app_api&a=getStockBasisInfo',
					data: {hospital: that.hospital, sortHospital: that.sortHospital, department: that.department, duty: that.duty},
					header: {
						'Content-type': 'application/json'
					},
					success: function(res){
						that.stockBasisInfo = res.data.data
					},
					fail: () => {
						console.info('小程序域名不正确，请检查域名的正确性')
					}
				})
			},
			// 排行榜uCharts
			getDepartmentRankList(){
				let that = this;
				uni.request({
					url: "https://lekapi.opmonitor.com/?c=app_api&a=getDepartmentRankList",
					data:{
						hospital: that.hospital,
						sortHospital: that.sortHospital,
						department: that.department,
						duty: that.duty
					},
					success: function(res) {
						//下面这个根据需要保存后台数据，我是为了模拟更新柱状图，所以存下来了
						_self.serverData=res.data.data;
						let Column={categories:[],series:[]};
						//这里我后台返回的是数组，所以用等于，如果您后台返回的是单条数据，需要push进去
						Column.categories=res.data.data.categories;
						Column.series=res.data.data.series;
						_self.showColumn("canvasColumn",Column);
					},
					fail: () => {
						_self.tips="网络错误，小程序端请检查合法域名";
					}
				})
			},
			showColumn(canvasId,chartData){
				canvaColumn=new uCharts({
					$this:_self,
					canvasId: canvasId,
					type: 'column',
					legend:true,
					fontSize:11,
					background:'#FFFFFF',
					pixelRatio:_self.pixelRatio,
					animation: true,
					categories: chartData.categories,
					series: chartData.series,
					xAxis: {
						disableGrid:true,
					},
					yAxis: {
						gridColor:'#E8F4F8',
						gridType:'dash',
						dashLength:8,
						splitLine: {
							show: true
						},
						format:(val)=>{return val.toFixed(0)}
					},
					dataLabel: true,
					width: _self.cWidth*_self.pixelRatio,
					height: _self.cHeight*_self.pixelRatio,
					extra: {
						column: {
							type:'group',
							width: _self.cWidth*_self.pixelRatio*0.5/chartData.categories.length,
							linearType:'custom',
							linearOpacity:0.8,
							customColor:[['#4a90e2', '#357abd']]
						}
					  }
				});
				canvaColumn.addEventListener('renderComplete', () => {
					setTimeout(function(){
						uni.canvasToTempFilePath({
							x: 0,
							y: 0,
							width: _self.cWidth*_self.pixelRatio,
							height: _self.cHeight*_self.pixelRatio,
							fileType: 'png',
							canvasId: 'canvasColumn',
							success: function(res) {
								_self.imgSrc2=res.tempFilePath;
							},
							fail:function(res){
								console.info(res)
							}
						},_self);
					},50);
				})
			},
			touchColumn(e){
				canvaColumn.showToolTip(e, {
					format: function (item, category) {
						if(typeof item.data === 'object'){
							return category + ' ' + item.name + ':' + item.data.value 
						}else{
							return category + ' ' + item.name + ':' + item.data 
						}
					}
				});
			},
			buttonClickToday(){
				uni.navigateTo({
					url: "/pages/main/inventory"
				})
			},
			buttonClickWeek(){
				uni.navigateTo({
					url: "/pages/main/inventory_week"
				})
			},
			buttonClickMonth(){
				uni.navigateTo({
					url: "/pages/main/inventory_month"
				})
			}
		}
	}
</script>

<style>
	.page {
		background: #f5f5f5;
	}
	
	.content-area {
		background: #f5f5f5;
	}
	
	.first-section {
		margin-top: 10rpx;
	}

	/* 区块标题样式 */
	.section-header {
		display: flex;
		align-items: center;
		margin: 30rpx 20rpx 20rpx 20rpx;
		padding: 24rpx 30rpx;
		background: linear-gradient(135deg, #4a90e2 0%, #357abd 100%);
		border-radius: 20rpx;
		box-shadow: 0 6rpx 24rpx rgba(74, 144, 226, 0.3);
		position: relative;
		overflow: hidden;
	}

	.section-header::before {
		content: '';
		position: absolute;
		top: -50%;
		right: -20%;
		width: 200rpx;
		height: 200rpx;
		background: radial-gradient(circle, rgba(255,255,255,0.15) 0%, transparent 70%);
		border-radius: 50%;
	}

	.header-icon {
		font-size: 44rpx;
		margin-right: 20rpx;
		filter: drop-shadow(0 2rpx 8rpx rgba(0, 0, 0, 0.15));
		position: relative;
		z-index: 1;
	}

	.header-title {
		font-size: 32rpx;
		font-weight: 700;
		color: #fff;
		text-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.2);
		position: relative;
		z-index: 1;
		letter-spacing: 2rpx;
	}

	.section-title {
		margin-top: 20rpx;
	}

	.box {
		margin: 20upx 0;
	}

	.box view.cu-bar {
		margin-top: 20upx;
	}

	.text-custom {
		font-weight: 600;
		font-size: 30rpx;
	}
	
	.qiun-charts {
		margin: 20upx;
		width: 750upx;
		height: 500upx;
		background-color: #FFFFFF;
	}
	
	.charts {
		width: 750upx;
		height: 500upx;
		background-color: #FFFFFF;
	}
	
	canvas{
		position: absolute;
		width: 100%;
		right: -900px;
		top: -600px;
	}

	/* 统计数据容器 */
	.collection-stats, .stock-stats {
		display: flex;
		justify-content: space-around;
		padding: 40rpx 20rpx;
		margin: 20rpx;
		background: rgba(255, 255, 255, 0.95);
		border-radius: 24rpx;
		box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.12);
		backdrop-filter: blur(10rpx);
		border: 1px solid rgba(255, 255, 255, 0.6);
	}

	/* 统计卡片 */
	.stat-card {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 24rpx 20rpx;
		border-radius: 20rpx;
		width: 200rpx;
		position: relative;
		overflow: hidden;
		transition: all 0.3s ease;
	}

	.stat-card::before {
		content: '';
		position: absolute;
		top: -50%;
		right: -50%;
		width: 100%;
		height: 100%;
		background: radial-gradient(circle, rgba(255,255,255,0.2) 0%, transparent 70%);
		border-radius: 50%;
	}

	.stat-icon {
		font-size: 44rpx;
		margin-bottom: 8rpx;
		filter: drop-shadow(0 2rpx 8rpx rgba(0, 0, 0, 0.15));
	}

	.stat-value {
		font-size: 48rpx;
		font-weight: 800;
		color: #fff;
		margin-bottom: 4rpx;
		text-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.2);
	}

	.stat-unit {
		font-size: 22rpx;
		color: rgba(255, 255, 255, 0.8);
		font-weight: 500;
		margin-bottom: 6rpx;
	}

	.stat-label {
		font-size: 24rpx;
		color: rgba(255, 255, 255, 0.85);
		font-weight: 500;
	}

	/* 今日收集卡片 - 蓝色 */
	.stat-today {
		background: linear-gradient(135deg, #4a90e2 0%, #357abd 100%);
		box-shadow: 0 6rpx 24rpx rgba(74, 144, 226, 0.35);
	}

	/* 本周收集卡片 - 青色 */
	.stat-week {
		background: linear-gradient(135deg, #1abc9c 0%, #16a085 100%);
		box-shadow: 0 6rpx 24rpx rgba(26, 188, 156, 0.35);
	}

	/* 本月收集卡片 - 紫色 */
	.stat-month {
		background: linear-gradient(135deg, #9b59b6 0%, #8e44ad 100%);
		box-shadow: 0 6rpx 24rpx rgba(155, 89, 182, 0.35);
	}

	/* 今日入库卡片 - 橙色 */
	.stat-in {
		background: linear-gradient(135deg, #e67e22 0%, #d35400 100%);
		box-shadow: 0 6rpx 24rpx rgba(230, 126, 34, 0.35);
	}

	/* 今日出库卡片 - 红色 */
	.stat-out {
		background: linear-gradient(135deg, #e74c3c 0%, #c0392b 100%);
		box-shadow: 0 6rpx 24rpx rgba(231, 76, 60, 0.35);
	}
</style>
