<template>
	<el-container>
		<el-header>
			<div class="right-panel">
				<el-select v-model="queryType" clearable @change="queryTypeChange">
					<el-option v-for="item in typeOptions" 
					:key="item" 
					:label="item" 
					:value="item"
					>
					</el-option>
				</el-select>
				<el-date-picker
					v-if="queryType=='月'"
					v-model="queryDate"
					type="month"
					placeholder="查询月期"
					value-format="YYYY-MM"
					style="width:100%"
				>
				</el-date-picker>
				<el-date-picker
					v-if="queryType=='年'"
					v-model="queryDate"
					type="year"
					placeholder="查询年份"
					value-format="YYYY"
					style="width:100%"
				>
				</el-date-picker>
				<el-button type="primary" icon="el-icon-search" @click="handleQuery"></el-button>
			</div>
		</el-header>
		<el-main id="elMain">
			<el-row :gutter="15">
				<el-col :lg="12">
					<el-card shadow="never" style="margin-bottom: 8px;">
						<el-header>
							<div class="right-panel">
								<h2>棒管交付数</h2>
								<!-- <el-select v-model="product" clearable @change="productChange">
									<el-option v-for="item in inmData" 
									:key="item.id" 
									:label="item.material_.name" 
									:value="item.id"
									>
									</el-option>
								</el-select> -->
							</div>
						</el-header>
						<el-row>
							<el-col>
								<div id="bachart1" style="width:100%;height:300px;"></div>
								<!-- <scEcharts height="300px" :option="option1"></scEcharts> -->
							</el-col>
							<el-card shadow="never" style="width: 100%;position: relative;margin-top: 20px;">
								<el-button @click="handleExport('ZJ')" class="tables" type="primary">导出</el-button>
								<el-table :data="tableData1" id="exportDivZJ" :height="tableHeight">
									<el-table-column type="index" width="50" />
									<el-table-column label="日期" prop="date">
									</el-table-column>
									<el-table-column label="批次" prop="name">
									</el-table-column>
									<el-table-column label="数量" prop="count">
									</el-table-column>
								</el-table>
							</el-card>
						</el-row>
					</el-card>
				</el-col>
				<el-col :lg="12">
					<el-card shadow="never" style="margin-bottom: 8px;">
						<el-header>
							<div class="right-panel">
								<h2>管交付数</h2>
								<!-- <el-select v-model="product" clearable @change="productChange">
									<el-option v-for="item in outData" 
									:key="item.id" 
									:label="item.material_.name" 
									:value="item.id"
									>
									</el-option>
								</el-select> -->
							</div>
						</el-header>
						<el-row>
							<el-col>
								<div id="bachart2" style="width:100%;height:300px;"></div>
								<!-- <scEcharts height="300px" :option="option2"></scEcharts> -->
							</el-col>
							<el-card shadow="never" style="width: 100%;position: relative;margin-top: 20px;">
								<el-button @click="handleExport('ZB')" class="tables" type="primary">导出</el-button>
								<el-table :data="tableData2" id="exportDivZB" :height="tableHeight">
									<el-table-column type="index" width="50" />
									<el-table-column label="日期" prop="date">
									</el-table-column>
									<el-table-column label="批次" prop="name">
									</el-table-column>
									<el-table-column label="数量" prop="count">
									</el-table-column>
								</el-table>
							</el-card>
						</el-row>
					</el-card>
				</el-col>
			</el-row>
		</el-main>
	</el-container>
</template>

<script>
	import * as echarts from "echarts";
	import T from '@/components/scEcharts/echarts-theme-T.js';
	echarts.registerTheme('T', T);
	import scEcharts from '@/components/scEcharts';
	function deepCopy(obj) {
		return JSON.parse(JSON.stringify(obj));
	}
	export default {
		name: 'chart',
		components: {
			scEcharts
		},
		data() {
			return {
				queryType:'月',
				queryDate:'',
				start_date:'',
				end_date:'',
				currentYear:'',
				currentMonth:'',
				xAxisOrigin:[],
				xAxisData:[],
				typeOptions:['月','年'],
				basicOption: {
					backgroundColor: 'transparent',
					title: {
						text: '',
					},
					grid: {
						top: '80px'
					},
					tooltip: {
						trigger: 'axis'
					},
					xAxis: {
						type: 'category',
						data: []
					},
					yAxis: {
						type: 'value'
					},
					lenged:[],
					series: [{
						data: [0, 0, 0, 0, 0, 0, 0],
						stack: 'Ad',
						type: 'bar',
						barWidth: '15px',
					}]
				},
				// option1: {
				// 	grid: {
				// 		top: '80px'
				// 	},
				// 	tooltip: {
				// 		trigger: 'axis'
				// 	},
				// 	xAxis: {
				// 		type: 'category',
				// 		data:xAxisData
				// 	},
				// 	yAxis: {
				// 		type: 'value'
				// 	},
				// 	series: [{
				// 		data: [120, 200, 150, 80, 70, 110, 130],
				// 		type: 'bar',
				// 		barWidth: '15px',
				// 	}]
				// },
				// option2: {
				// 	grid: {
				// 		top: '80px'
				// 	},
				// 	tooltip: {
				// 		trigger: 'axis'
				// 	},
				// 	xAxis: {
				// 		type: 'category',
				// 		data: xAxisData
				// 	},
				// 	yAxis: {
				// 		type: 'value'
				// 	},
				// 	series: [{
				// 		data: [120, 200, 150, 80, 70, 110, 130],
				// 		type: 'bar',
				// 		barWidth: '15px',
				// 	}]
				// },
				tableHeight:0,
				tableData:[
					{id:1,date:'2023-12-11',name:'ZJ2-1',count:200},
					{id:1,date:'2023-12-11',name:'ZJ2-2',count:200},
					{id:1,date:'2023-12-11',name:'ZJ2-3',count:200},
					{id:1,date:'2023-12-11',name:'ZJ2-4',count:200},
				],
				tableData1:[],
				tableData2:[],
				firstWeekNum:1,
				endWeekNum:4,
				monthList:['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月']
			}
		},
		mounted(){
			let that =this;
			let date = new Date();
			let year = date.getFullYear();
			let month = date.getMonth()+1;
			let days = new Date(year, month, 0).getDate();
			that.currentYear = year;
			that.currentMonth = month;
			that.start_date =year+'-'+month+'-01';
			that.end_date = year+'-'+month+'-'+new Date(year, month, 0).getDate();
			let month1=month;
			if(month1<10){month1='0'+month1}
			that.queryDate = year+'-'+month1;
			that.firstWeekNum = that.getWeekOfYear(new Date(that.start_date));
			that.endWeekNum = that.getWeekOfYear(new Date(that.end_date));
			let duration = that.endWeekNum - that.firstWeekNum + 1;
			for(let i =1;i<=duration;i++ ){
				that.xAxisOrigin.push('第'+i+'周');
			}
			that.xAxisData = that.xAxisOrigin;
			let height= document.getElementById('elMain').clientHeight-140;
			let chartheight= document.getElementById('bachart1').clientHeight;
			that.tableHeight = height-chartheight;
			this.getTableData();
			// this.getTableData2();
		},
		methods:{
			setChart(name, option = null) {
				// 根据name 渲染数据, option需填写，否则option为模拟数据
				var myChart = echarts.getInstanceByDom(document.getElementById(name));
				if (myChart == undefined) {
					myChart = echarts.init(document.getElementById(name), 'T');
				}
				if (option == null) {
					option = Object.assign({}, this.basicOption)
				}
				setTimeout(() => {
					try {
						myChart.setOption(option);
					} catch (error) { }
				}, 500)
			},
			queryTypeChange(value){
				console.log(value)
				this.queryDate = '';
			},
			getWeekOfYear(date) {
				var target = new Date(date.getFullYear(), date.getMonth(), date.getDate()); // 创建目标日期对象
				var firstDayOfYear = new Date(target.getFullYear(), 0, 1); // 创建当前年份第一天的日期对象
				if(target.getTime()==firstDayOfYear.getTime()){
					return 1;
				}else{
					return Math.ceil((target - firstDayOfYear + 8 * (firstDayOfYear.getTimezoneOffset() / 480)) / 604800000);
				}
				 // 计算两者之间相隔的周数并返回结果
			},
			getTableData(){
				let that = this;
				let option1 = deepCopy(that.basicOption);
				let option2 = deepCopy(that.basicOption);
				option1.xAxis.data = that.xAxisData;
				option2.xAxis.data = that.xAxisData;
				let exec = that.queryType=='月'?'saleOutWeek':'saleOutMonth';
				let obj = {
					query: { start_date: that.start_date, end_date: that.end_date,mio_type:'sale_out',material_id:'' },
				};
				that.$API.bi.dataset.exec.req(exec, obj).then((res) => {
					// console.log('交付数:',res);
					if(res.length>0){
						let data1 = [],data2 = [],seriesData1=[],seriesData2=[];
						for(let i=0;i<res.length;i++){
							if(res[i].物料名.indexOf('棒')){
								data1.push(res[i]);
							}else if(res[i].物料名.indexOf('管')){
								data2.push(res[i]);
							}
						}
						that.tableData1 = data1;
						that.tableData2 = data2;
						data1.forEach(item1=>{
							let index =0;
							if(that.queryType=='月'){
								index = item1.月-1;
							}else{
								index = item1.周-that.firstWeekNum;
							}
							seriesData1[index]=item1.交付数;
						})
						data2.forEach(item2=>{
							let index2 =0;
							if(that.queryType=='月'){
								index2 = item2.月-1;
							}else{
								index2 = item2.周-that.firstWeekNum;
							}
							seriesData2[index2]=item2.交付数;
						})
						option1.seriesData1 = seriesData1;
						option2.seriesData2 = seriesData2;
						that.setChart("bachart1", option1);
						that.setChart("bachart2", option2);
					}else{
						that.setChart("bachart1", option1);
						that.setChart("bachart2", option2);
					}
				})
			},
			handleQuery(){
				let that = this;
				if(this.queryType=='月'){
					if(this.queryDate!==''&&this.queryDate!==null){
						this.start_date =this.queryDate+'-01';
						let arr = this.queryDate.split('-');
						this.end_date = this.queryDate+'-'+new Date(arr[0], arr[1], 0).getDate();
						this.firstWeekNum = this.getWeekOfYear(new Date(this.start_date));
						this.endWeekNum = this.getWeekOfYear(new Date(this.end_date));
						let duration = this.endWeekNum - this.firstWeekNum + 1;
						let xAxisData = [];
						for(let i =1;i<=duration;i++ ){
							xAxisData.push('第'+i+'周');
						}
						that.xAxisData = xAxisData;
					}else{
						this.start_date =this.currentYear+'-'+this.currentMonth+'-01';
						this.end_date = this.currentYear+'-'+this.currentMonth+'-'+new Date(this.currentYear, this.currentMonth, 0).getDate();
						let date = new Date(this.start_date);
						this.firstWeekNum = this.getWeekOfYear(date);
						this.xAxisData = this.xAxisOrigin;
					}
				}else{
					if(this.queryDate!==''&&this.queryDate!==null){
						this.start_date = this.queryDate+'-01-01';
						this.end_date = this.queryDate+'-12-31';
					}else{
						this.start_date = this.currentYear+'-01-01';
						this.end_date = this.currentYear+'-12-31';
					}
					that.xAxisData = that.monthList;
				}
				this.getTableData();
			},
			handleExport(val) {
				this.exportLoading = true;
				let id = '#exportDiv' + val;
				let name = val == 'ZJ' ? '棒交付统计' : '管交付统计';
				this.$XLSX(id, name);
				this.exportLoading = false;
			},
		},
	}
</script>

<style scoped>
.tables {
    position: absolute;
    top: 4px;
    right: 6px;
    z-index: 10;
}
</style>
