<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">
			<method-btn
				v-if="tableBtn.length"
				slot="handleBtn"
				:width="120"
				label="操作"
				:method-list="tableBtn"
				@clickBtn="handleClick" />
		</base-table>
		<pagination
			:limit.sync="listQuery.pageSize"
			:page.sync="listQuery.pageNo"
			:total="listQuery.total"
			@pagination="getDataList" />
	</div>
</template>

<script>
import basicPage from '../../../core/mixins/basic-page';
import { parseTime } from '../../../core/mixins/code-filter';
import {
	getEqAnalysis,
	exportEqAnalysisExcel
} from '@/api/equipment/analysis/statistics';

const tableProps = [
	{
		prop: 'recordTime',
		label: '时间段',
		filter: parseTime
	},
	{
		prop: 'lineName',
		label: '产线'
	},
	{
		prop: 'sectionName',
		label: '工段'
	},
	{
		prop: 'equipmentName',
		label: '设备名称'
	},
	{
		prop: 'equipmentType',
		label: '设备类型'
	},
	{
		prop: 'workTime',
		label: '工作时间累积(h)'
	},
	{
		prop: 'repairCount',
		label: '维修次数'
	},
	{
		prop: 'maintainCount',
		label: '保养次数'
	}
];

export default {
	mixins: [basicPage],
	data() {
		return {
			urlOptions: {
				getDataListURL: getEqAnalysis,
				exportURL: exportEqAnalysisExcel,
			},
			tableProps,
			tableBtn: [].filter((v)=>v),
			tableData: [],
			formConfig: [
				{
					type: 'input',
					label: '设备名称',
					placeholder: '设备名称',
					param: 'name',
				},
				{
					type: 'datePicker',
					label: '时间段',
					dateType: 'daterange', // datetimerange
					// format: 'yyyy-MM-dd HH:mm:ss',
					format: 'yyyy-MM-dd',
					// valueFormat: 'timestamp',
					valueFormat: 'yyyy-MM-dd HH:mm:ss',
					rangeSeparator: '-',
					startPlaceholder: '开始日期',
					endPlaceholder: '结束日期',
					defaultTime: ['00:00:00', '23:59:59'],
					param: 'recordTime',
					defaultSelect: [],
				},
				{
					type: 'button',
					btnName: '查询',
					name: 'search',
					color: 'primary',
				},
				{
					type: 'separate',
				},
				{
					type: this.$auth.hasPermi('equipment:analysis-statistics:export') ? 'button' : '',
					btnName: '导出',
					name: 'export',
					color: 'warning',
					plain: true
				},
			],
		};
	},
	created() {},
	methods: {
		// 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.urlOptions.getDataListURL(this.listQuery).then(response => {
        this.tableData = response.data.list;
        this.listQuery.total = response.data.total;
        this.dataListLoading = false;
      });
    },
		buttonClick(val) {
			switch (val.btnName) {
				case 'search':
					this.listQuery.pageNo = 1;
					this.listQuery.pageSize = 10;
					this.listQuery.equipmentName = val.name ? val.name : undefined;
					this.listQuery.recordTime = val.recordTime ? val.recordTime : undefined;
					this.getDataList();
					break;
				case 'reset':
					this.$refs.searchBarForm.resetForm();
					this.listQuery = {
						pageSize: 10,
						pageNo: 1,
						total: 1,
					};
					this.getDataList();
					break;
				case 'export':
					this.handleExport();
					break;
				default:
					console.log(val);
			}
		},
		 /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = { ...this.listQuery };
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有数据项?').then(() => {
        this.exportLoading = true;
        return this.urlOptions.exportURL(params);
      }).then(response => {
        this.$download.excel(response, '设备统计分析.xls');
        this.exportLoading = false;
      }).catch(() => { });
    }
	},
};
</script>
