<template>
	<div class="tabPage" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
		<cus-header title='仓储管理' :showback='false' bgColor='transparent'></cus-header>
		<image class="topbg" :src="imgBase+'storage/home_bg.png'" mode="widthFix"></image>
		<div class="inout" :style="{'backgroundImage':'url('+imgBase+'/storage/home_bgimg1.png)'}">
			<div class="pre">
				<text>入库总量</text>
				<p>{{priceFormat(inNum,0)}}</p>
			</div>
			<div class="line"></div>
			<div class="pre">
				<text>出库总量</text>
				<p>{{outNum}}</p>
			</div>
		</div>
		<div class="menus">
			<div class="item" v-for="(item,index) in menuList" :key="index" @tap="toTurn(item.path)">
				<image :src="item.icon" :class="index<4?'img1':'img2'"></image>
				<p :class="index<4?'p1':'p2'">{{item.title}}</p>
			</div>
		</div>
		<div class="chartBox">
			<p class="title">库存数量占比</p>
			<div class="chart">
				<l-echart ref="kcslzbChartRef" :canvas2d="true" @finished="initKcslzb"></l-echart>
			</div>
		</div>
		<div class="chartBox">
			<p class="title">入库统计</p>
			<div class="chart">
				<div class="btns">
					<div class="pre" :class="{'active':rkIdx===0}" @tap="changeRkCahrtData(0)">按月</div>
					<div class="pre" :class="{'active':rkIdx===1}" @tap="changeRkCahrtData(1)">按年</div>
				</div>
				<l-echart ref="rktjChartRef" :canvas2d="true" @finished="initRktj"></l-echart>
			</div>
		</div>
		<div class="chartBox">
			<p class="title">出库统计</p>
			<div class="chart">
				<div class="btns">
					<div class="pre" :class="{'active':ckIdx===0}" @tap="changeCkCahrtData(0)">按月</div>
					<div class="pre" :class="{'active':ckIdx===1}" @tap="changeCkCahrtData(1)">按年</div>
				</div>
				<l-echart ref="cktjChartRef" :canvas2d="true" @finished="initCktj"></l-echart>
			</div>
		</div>
		<Tabbar :tabbarIndex="0"></Tabbar>
	</div>
</template>

<script>
	import * as echarts from '@/pagesStorage/components/lime-echart/static/echarts.min.js'
	import lEchart from '@/pagesStorage/components/lime-echart/components/l-echart/l-echart.vue'
	import Tabbar from '@/components/CusTabbar/index.vue'
	export default {
		components:{
			lEchart,
			Tabbar
		},
		data(){
			return {
				inNum:0,
				outNum:0,
				menuList:[
					{
						icon:this.$imgBase+'storage/home_icon_rk.png',
						title:'入库',
						path:'/pagesStorage/inStorage/index'
					},
					{
						icon:this.$imgBase+'storage/home_icon_ck.png',
						title:'出库',
						path:'/pagesStorage/outStorage/index'
					},
					{
						icon:this.$imgBase+'storage/home_icon_pk.png',
						title:'盘库',
						path:'/pagesStorage/checkStorage/index'
					},
					{
						icon:this.$imgBase+'storage/home_icon_yk.png',
						title:'移库',
						path:'/pagesStorage/moveStorage/index'
					},
					{
						icon:this.$imgBase+'storage/home_icon_kcjl.png',
						title:'库存记录',
						path:'/pagesStorage/storageRecord/index'
					},
					{
						icon:this.$imgBase+'storage/home_icon_kctj.png',
						title:'库存统计',
						path:'/pagesStorage/inventoryStatistics/index'
					},
					{
						icon:this.$imgBase+'storage/home_icon_wldw.png',
						title:'业务单位',
						path:'/pagesStorage/units/index'
					},
					{
						icon:this.$imgBase+'storage/home_icon_ckgl.png',
						title:'仓库管理',
						path:'/pagesStorage/storage/index'
					}
				],
				rkIdx:0,
				ckIdx:0,
			}
		},
		onLoad() {
			this.getHeaderInfo();
		},
		methods:{
			toTurn(url){
				if(!url) return;
				uni.navigateTo({ url })
			},
			//头部统计信息
			getHeaderInfo(){
				this.$api.post('/wms/home/getAppHomeHeadGather').then(res=>{
					if(res.data.code===0){
						this.inNum = res.data.data.find(d=>d.orderType==1).total||0;
						this.outNum = res.data.data.find(d=>d.orderType==2).total||0;
					}else this.$showToast(res.data.msg)
				})
			},
			// 库存数量占比
			async getKcslzbData(){
				let res = await this.$api.post('/wms/home/getWarehouseCargoRatio');
				if(res.data.code===0){
					let data = res.data.data.map(d=>{
						return {
							value:d.quantity,
							name:d.warehouseName
						}
					})
					return data;
				}else{
					this.$showToast(res.data.msg);
					return [];
				} 
			},
			async initKcslzb() {
				let data = await this.getKcslzbData();
				const chart = await this.$refs.kcslzbChartRef.init(echarts);
				let option = {
				  legend: {
					top: 'bottom'
				  },
				  tooltip: {
					trigger: 'item'
				  },
				  series: [
					{
					  name: '库存数量占比',
					  type: 'pie',
					  radius: [20, 100],
					  center: ['50%', '42%'],
					  itemStyle: {
						borderRadius: 5
					  },
					  data
					}
				  ]
				};
				chart.setOption(option)
			},
			
			//入库统计
			async getRktjData(){
				let res = await this.$api.get('/wms/home/getReceiptTrend/'+(this.rkIdx+1));
				if(res.data.code===0){
					let xData = res.data.data.map(d=>d.optTime);
					let yData = res.data.data.map(d=>d.quantity);
					return {xData,yData}
				}else{
					this.$showToast(res.data.msg);
					return {xData:[],yData:[]};
				} 
			},
			async initRktj(){
				let {xData,yData} = await this.getRktjData();
				const chart = await this.$refs.rktjChartRef.init(echarts);
				let option = {
				  title: {
					text: '数量',
					textStyle:{
						fontSize:16,
						color:'#86909C'
					}
				  },
				  xAxis: {
					type: 'category',
					data: xData
				  },
				  yAxis: {
					type: 'value'
				  },
				  tooltip: {
					trigger: 'axis',
					axisPointer: {
					  type: 'cross',
					  label: {
						backgroundColor: '#6a7985'
					  }
					}
				  },
				  grid: {
					left: '1%',
					right: '1%',
					bottom: '10%',
					top: '18%',
					containLabel: true
				  },
				  series: [
					{
					  name:'入库数量',
					  data: yData,
					  type: 'line',
					  lineStyle:{
					   color:'#198CFF',
					   width: 3
					  },
					  symbol:'none',
					  areaStyle: {
						color:'rgba(25,140,255,.4)'
					  },
					  smooth: true
					}
				  ]
				};
				chart.setOption(option);
			},
			changeRkCahrtData(type){
				this.rkIdx = type;
				this.initRktj();
			},
			
			//出库统计
			async getCktjData(){
				let res = await this.$api.get('/wms/home/getShipmentTrend/'+(this.ckIdx+1));
				if(res.data.code===0){
					let xData = res.data.data.map(d=>d.optTime);
					let yData = res.data.data.map(d=>d.quantity);
					return {xData,yData}
				}else{
					this.$showToast(res.data.msg);
					return {xData:[],yData:[]};
				} 
			},
			async initCktj(){
				let {xData,yData} = await this.getCktjData();
				const chart = await this.$refs.cktjChartRef.init(echarts);
				let option = {
				  title: {
					text: '数量',
					textStyle:{
						fontSize:16,
						color:'#86909C'
					}
				  },
				  xAxis: {
					type: 'category',
					data: xData
				  },
				  yAxis: {
					type: 'value'
				  },
				  tooltip: {
					trigger: 'axis',
					axisPointer: {
					  type: 'cross',
					  label: {
						backgroundColor: '#6a7985'
					  }
					}
				  },
				  grid: {
					left: '1%',
					right: '1%',
					bottom: '10%',
					top: '18%',
					containLabel: true
				  },
				  series: [
					{
					  name:'出库数量',
					  data: yData,
					  type: 'line',
					  lineStyle:{
					   color:'#198CFF',
					   width: 3
					  },
					  symbol:'none',
					  areaStyle: {
						color:'rgba(25,140,255,.4)'
					  },
					  smooth: true
					}
				  ]
				};
				chart.setOption(option);
			},
			changeCkCahrtData(type){
				this.ckIdx = type;
				this.initCktj();
			},
			
			// 金额格式化
			priceFormat(number, decimals = 0, decimalPoint = '.', thousandsSeparator = ',') {
				number = (`${number}`).replace(/[^0-9+-Ee.]/g, '')
				const n = !isFinite(+number) ? 0 : +number
				const prec = !isFinite(+decimals) ? 0 : Math.abs(decimals)
				const sep = (typeof thousandsSeparator === 'undefined') ? ',' : thousandsSeparator
				const dec = (typeof decimalPoint === 'undefined') ? '.' : decimalPoint
				let s = ''
			
				s = (prec ? round(n, prec) + '' : `${Math.round(n)}`).split('.')
				const re = /(-?\d+)(\d{3})/
				while (re.test(s[0])) {
					s[0] = s[0].replace(re, `$1${sep}$2`)
				}
				
				if ((s[1] || '').length < prec) {
					s[1] = s[1] || ''
					s[1] += new Array(prec - s[1].length + 1).join('0')
				}
				return s.join(dec)
			},
		}
	}
</script>

<style scoped lang="less">
	.tabPage{
		padding: 0 24rpx 188rpx;
		background: #F4F8FB;
		
		.topbg{
			width: 100%;
			position: fixed;
			top: 0;
			left: 0;
			z-index: 0;
		}
		
		.inout{
			width: 100%;
			height: 180rpx;
			margin-top: 20rpx;
			border-radius: 16rpx;
			background-repeat: no-repeat;
			background-size: 100% 100%;
			position: relative;
			display: flex;
			align-items: center;
			box-sizing: border-box;
			.line{
				width: 1rpx;
				height: 98rpx;
				background: rgba(255,255,255,.35);
				position: absolute;
				left: 50%;
				margin-left: -0.5rpx;
				top: 50%;
				margin-top: -49rpx;
			}
			.pre{
				width: 50%;
				padding-left: 30rpx;
				text{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #FFFFFF;
					line-height: 28rpx;
				}
				p{
					font-family: D-DIN, D-DIN;
					font-weight: bold;
					font-size: 48rpx;
					color: #FFFFFF;
					line-height: 48rpx;
					margin-top: 20rpx;
				}
			}
		}
	
		.menus{
			width: 100%;
			background: #FFFFFF;
			border-radius: 16rpx;
			padding: 3rpx 0 33rpx;
			box-sizing: border-box;
			display: flex;
			flex-wrap: wrap;
			position: relative;
			margin-top: 20rpx;
			.item{
				width: 25%;
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-top: 37rpx;
				.img1{
					width: 80rpx;
					height: 80rpx;
				}
				.img2{
					width: 55rpx;
					height: 55rpx;
				}
				p{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #4E5969;
					line-height: 33rpx;
					text-align: center;
				}
				.p1{
					margin-top: 20rpx;
				}
				.p2{
					margin-top: 14rpx;
				}
			}
		}
	
		.chartBox{
			width: 100%;
			background: #FFFFFF;
			border-radius: 16rpx;
			margin-top: 20rpx;
			padding: 40rpx 24rpx;
			box-sizing: border-box;
			position: relative;
			.title{
				font-family: PingFang-SC, PingFang-SC;
				font-weight: bold;
				font-size: 36rpx;
				color: #1D2129;
				line-height: 36rpx;
				letter-spacing: 2rpx;
			}
			.chart{
				width: 100%;
				position: relative;
				
				.btns{
					border: 1rpx solid #ECECEC;
					border-radius: 6rpx;
					display: flex;
					position: absolute;
					right: 12rpx;
					top: 32rpx;
					z-index: 999;
					.pre{
						padding: 12rpx 16rpx;
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: #86909C;
						line-height: 24rpx;
						position: relative;
						&.active{
							color: #198CFF;
						}
						&:last-child{
							border-left: 1rpx solid #ECECEC;
						}
					}
				}
			}
		}
	}
</style>