<template name="basics">
	<view>
		<scroll-view scroll-y class="page">
			<cu-custom bgColor="bg-cyan"><block slot="content">首页</block></cu-custom>
			<view class="cu-bar bg-white">
				<view class="action">
					<text class="cuIcon-title text-green"></text>
					<text>医院基本信息</text>
				</view>
			</view>
			<view class="grid col-3 padding-sm">
				<view class="padding-sm">
					<view class="padding radius text-center shadow-blur bg-blue" @click="buttonClickToday">
						<!-- <view class="text-lg" v-if="hospitalBasisInfo.today_collection === undefined ">0kg</view> -->
						<view class="text-lg">{{hospitalBasisInfo.today_collection}}kg</view>
						<view class="margin-top-sm text-Abc">今日收集</view>
					</view>
				</view>
				<view class="padding-sm">
					<view class="padding radius text-center shadow-blur bg-cyan" @click="buttonClickWeek">
						<view class="text-lg">{{hospitalBasisInfo.week_collection}}kg</view>
						<view class="margin-top-sm text-Abc">本周收集</view>
					</view>
				</view>
				<view class="padding-sm">
					<view class="padding radius text-center shadow-blur bg-red" @click="buttonClickMonth">
						<view class="text-lg">{{hospitalBasisInfo.month_collection}}kg</view>
						<view class="margin-top-sm text-Abc">本月收集</view>
					</view>
				</view>
			</view>
			<view class="cu-bar bg-white margin-top">
				<view class="action">
					<text class="cuIcon-title text-green"></text>
					<text>一周数据变化图</text>
				</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="cu-bar bg-white margin-top">
				<view class="action">
					<text class="cuIcon-title text-green"></text>
					<text>医废库存</text>
				</view>
			</view>
			<view class="grid col-3 padding-sm">
				<view class="padding-sm">
					<view class="padding radius text-center shadow-blur bg-blue">
						<view class="text-lg">{{stockBasisInfo.now_stock}}kg</view>
						<view class="margin-top-sm text-Abc">当前库存</view>
					</view>
				</view>
				<view class="padding-sm">
					<view class="padding radius text-center shadow-blur bg-cyan">
						<view class="text-lg">{{stockBasisInfo.today_in_ware}}kg</view>
						<view class="margin-top-sm text-Abc">今日入库</view>
					</view>
				</view>
				<view class="padding-sm">
					<view class="padding radius text-center shadow-blur bg-red">
						<view class="text-lg">{{stockBasisInfo.today_out_ware}}kg</view>
						<view class="margin-top-sm text-Abc">今日出库</view>
					</view>
				</view>
			</view>
			<view class="cu-bar bg-white margin-top">
				<view class="action">
					<text class="cuIcon-title text-green"></text>
					<text>科室排行</text>
				</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>
		</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.openid == '' || getApp().globalData.openid == undefined){
			// 	uni.reLaunch({
			// 		url: '/pages/register/registerRegulatory'
			// 	})
			// }
			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:['#f04864', '#facc14', '#8543e0', '#90ed7d'],
					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:'#CCCCCC',
						gridType:'dash',
						dashLength:8
					},
					yAxis: {
						gridType:'dash',
						gridColor:'#CCCCCC',
						dashLength:8,
						format:(val)=>{return val.toFixed(0)}
					},
					width: _self.cWidth*_self.pixelRatio,
					height: _self.cHeight*_self.pixelRatio,
					extra: {
						line:{
							type: 'straight'
						}
					}
				});
				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: {
						//disabled:true
						splitLine: {
							show: false
						},
						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.45/chartData.categories.length
						}
					  }
				});
				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>
	.box {
		margin: 20upx 0;
	}
	
	.box view.cu-bar {
		margin-top: 20upx;
	}
	
	.text-custom {
		font-weight: 600;
		font-size: 30rpx;
	}
	
	.qiun-charts {
		margin: 20upx 0;
		width: 750upx;
		height: 500upx;
		background-color: #FFFFFF;
	}
	.charts {
		width: 750upx;
		height: 500upx;
		background-color: #FFFFFF;
	}
	canvas{
		position: absolute;
		width: 100%;
		right: -900px;
		top: -600px;
	}
</style>
