<template>
	<div class="app-container">
		<!-- 搜索工作栏 -->
		<search-bar
			:formConfigs="formConfig"
			ref="searchBarForm"
			@headBtnClick="buttonClick" />

		<!-- 列表 -->
		<base-table
			v-loading="dataListLoading"
			:table-props="tableProps"
			:page="listQuery.pageNo"
			:limit="listQuery.pageSize"
			:table-data="tableData"></base-table>
		<pagination
			:limit.sync="listQuery.pageSize"
			:page.sync="listQuery.pageNo"
			:total="listQuery.total"
			@pagination="getDataList" />
	</div>
</template>

<script>
import basicPage from '../mixins/basic-page';
import codeFilter from '../mixins/code-filter';
import {
	getCostMaterialAutoReportPage,
	exportCostMaterialAutoReportExcel,
} from '@/api/cost/costMaterialAutoReport';
import { getHotMaterialList } from '@/api/base/coreHotMaterial';
import { publicFormatter } from '@/utils/dict';

const tableProps = [
	{
		prop: 'reportType',
		label: '维度',
		filter: codeFilter('reportType'),
	},
	{
		prop: 'reportName',
		label: '时间',
		minWidth: 150,
	},
	{
		prop: 'rawMaterialName',
		label: '原料名称',
	},
	{
		prop: 'grade',
		label: '原料等级',
		filter: publicFormatter('material_grade'),
	},
	{
		prop: 'price',
		label: '单价(元/吨)',
		align: 'right',
	},
	{
		prop: 'totalUsed',
		label: '累计使用量(吨)',
	},
	{
		prop: 'totalCost',
		label: '总价(元)',
		align: 'right',
	},
];

export default {
	mixins: [basicPage],
	data() {
		return {
			urlOptions: {
				getDataListURL: getCostMaterialAutoReportPage,
        exportURL: exportCostMaterialAutoReportExcel
			},
			tableData: [],
			tableProps,
			drawerVisible: false,
			listQuery: {
				reportType: 2,
			},
			formConfig: [
				{
					type: 'select',
					label: '维度',
					selectOptions: [
						{ id: 2, name: '日' },
						{ id: 3, name: '周' },
						{ id: 4, name: '月' },
						{ id: 5, name: '年' },
					],
					param: 'reportType',
					filterable: true,
					defaultSelect: 2, // 默认值
					clearable: false,
				},
				{
					type: 'select',
					label: '原料名称',
					selectOptions: [],
					param: 'materialId',
					filterable: true,
				},
				{
					type: 'datePicker',
					label: '时间范围',
					dateType: 'daterange',
					format: 'yyyy-MM-dd',
					valueFormat: 'timestamp',
					rangeSeparator: '-',
					startPlaceholder: '开始时间',
					endPlaceholder: '结束时间',
					param: 'searchTime',
				},
				{
					type: 'button',
					btnName: '查询',
					name: 'search',
					color: 'primary',
				},
				{
					type: this.$auth.hasPermi('cost:rawMaterialCostHis:export')
						? 'button'
						: '',
					btnName: '导出',
					name: 'export',
					color: 'primary',
					plain: true,
				},
			],
		};
	},
	components: {},
	created() {
		getHotMaterialList().then((response) => {
			this.formConfig[1].selectOptions = response.data;
		});
	},
	methods: {
		buttonClick(val) {
			switch (val.btnName) {
				case 'search':
					this.listQuery.pageNo = 1;
					this.listQuery.pageSize = 10;
					this.listQuery.reportType = val.reportType;
					this.listQuery.materialId = val.materialId;
					this.listQuery.times = val.searchTime ? val.searchTime : null;
					this.getDataList();
					break;
				case 'export':
					this.listQuery.reportType = val.reportType;
					this.listQuery.materialId = val.materialId;
					this.listQuery.times = val.searchTime ? val.searchTime : null;
					this.handleExport();
					break;
				default:
					console.log(val);
			}
		},
	},
};
</script>
<style>
.app-container .el-table .el-table__cell {
	padding: 0;
	height: 35px;
}
</style>