<template>
	<el-container>
		<el-header>
			<div class="right-panel">
				<el-select v-model="queryType" clearable>
					<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="year"
					placeholder="查询年份"
					value-format="YYYY"
					style="width: 100%"
				>
				</el-date-picker>
				<el-date-picker
					v-else
					v-model="queryDate"
					type="month"
					placeholder="查询月期"
					value-format="YYYY-MM"
					style="width: 100%"
				>
				</el-date-picker>
				<el-button
					type="primary"
					icon="el-icon-search"
					@click="handleQuery"
				></el-button>
			</div>
			<el-button @click="handleExport" type="primary">导出</el-button>
		</el-header>
		<el-main style="background: #ffffff">
			<scTable :data="tableData" id="exportDiv">
				<el-table-column type="index" width="50" />
				<el-table-column label="物料名称" prop="名称">
				</el-table-column>
				<el-table-column label="规格" prop="规格"> </el-table-column>
				<el-table-column label="型号" prop="型号"> </el-table-column>
				<el-table-column label="合格数" prop="合格数量">
				</el-table-column>
				<el-table-column label="总数" prop="总数量"> </el-table-column>
			</scTable>
		</el-main>
	</el-container>
</template>

<script>
export default {
	name: "chart",

	data() {
		return {
			queryType: "月",
			queryDate: "",
			start_date: "",
			end_date: "",
			currentYear: "",
			currentMonth: "",
			typeOptions: ["月", "年"],
			tableData: [],
		};
	},
	mounted() {
		let date = new Date();
		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		let days = new Date(year, month, 0).getDate();
		this.currentYear = year;
		this.currentMonth = month;
		this.start_date = year + "-" + month + "-01";
		this.end_date =
			year + "-" + month + "-" + new Date(year, month, 0).getDate();
		this.queryDate = year + "-" + month;
		this.getData();
	},
	methods: {
		getData() {
			let that = this;
			let exec = "";
			if (that.queryType == "年") {
				exec = "count_ok_year";
			} else {
				exec = "count_ok_month";
			}
			let params = {
				query: {
					end_date: that.end_date,
					start_date: that.start_date,
				},
			};
			that.$API.bi.dataset.exec.req(exec, params).then((res) => {
				that.tableData = res.data2.ds0;
				console.log(that.tableData);
			});
		},
		handleQuery() {
			if (this.queryDate !== "") {
				if (this.queryType == "年") {
					this.start_date = this.queryDate + "-01-01";
					this.end_date = this.queryDate + "-12-31";
				} else {
					this.start_date = this.queryDate + "-01";
					let arr = this.queryDate.split("-");
					this.end_date =
						this.queryDate +
						"-" +
						new Date(arr[0], arr[1], 0).getDate();
				}
			} else {
				if (this.queryType == "年") {
					this.start_date = this.currentYear + "-01-01";
					this.end_date = this.currentYear + "-12-31";
				} else {
					this.start_date =
						this.currentYear + "-" + this.currentMonth + "-01";
					this.end_date =
						this.currentYear +
						"-" +
						this.currentMonth +
						"-" +
						new Date(
							this.currentYear,
							this.currentMonth,
							0
						).getDate();
				}
			}
			this.getData();
		},
		handleExport() {
			this.exportLoading = true;
			this.$XLSX("#exportDiv", "成品检验统计");
			this.exportLoading = false;
		},
	},
};
</script>

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