<template>
	<div class="app-container">
		<search-bar
			:formConfigs="formConfig"
			ref="searchBarForm"
			@headBtnClick="buttonClick" />
		<base-table
			:table-props="tableProps"
			:page="listQuery.pageNo"
			:limit="listQuery.pageSize"
			:table-data="list"
			:max-height="tableH"></base-table>
		<pagination
			:limit.sync="listQuery.pageSize"
			:page.sync="listQuery.pageNo"
			:total="total"
			@pagination="getList" />
	</div>
</template>

<script>
import { parseTime } from '../mixins/code-filter';
import {
	getMaterialUseLogPage,
	getWorkOrderList,
	exportEnergyPlcExcel,
} from '@/api/quality/materialTraceability';
import tableHeightMixin from '@/mixins/tableHeightMixin';

const tableProps = [
	{
		prop: 'materialName',
		label: '物料名称',
		showOverflowtooltip: true,
	},
	{
		prop: 'materialCode',
		label: '物料编码',
		showOverflowtooltip: true,
		minWidth: 120,
	},
	{
		prop: 'dateName',
		label: '物料批次',
		showOverflowtooltip: true,
	},
	{
		prop: 'equipName',
		label: '使用设备',
		showOverflowtooltip: true,
	},
	{
		prop: 'num',
		label: '使用数量',
		showOverflowtooltip: true,
	},
	{
		prop: 'useTime',
		label: '使用时间',
		filter: parseTime,
		width: 160,
	},
	{
		prop: 'userName',
		label: '操作人',
		showOverflowtooltip: true,
	},
	{
		prop: 'source',
		label: '数据来源',
		filter: (val) => (val == 1 ? '内部' : '外部'),
	},
	{
		prop: 'remark',
		label: '备注',
		showOverflowtooltip: true,
	},
];

export default {
	mixins: [tableHeightMixin],
	data() {
		return {
			heightNum: 256,
			tableProps,
			tableBtn: [
				this.$auth.hasPermi(`base:packaging-print-log:update`)
					? {
							type: 'edit',
							btnName: '编辑',
					  }
					: undefined,
				this.$auth.hasPermi(`base:packaging-print-log:delete`)
					? {
							type: 'delete',
							btnName: '删除',
					  }
					: undefined,
			].filter((v) => v),
			list: [],
			listQuery: {
				pageSize: 10,
				pageNo: 1,
				workOrderId: undefined,
				startTime: undefined,
				endTime: undefined,
			},
			total: 0,
			formConfig: [
				{
					type: 'select',
					label: '工单名称',
					selectOptions: [],
					param: 'workOrderId',
					defaultSelect: '',
					filterable: true,
					clearable: false,
				},
				{
					type: 'datePicker',
					label: '时间段',
					dateType: 'daterange',
					format: 'yyyy-MM-dd',
					valueFormat: 'yyyy-MM-dd',
					rangeSeparator: '-',
					startPlaceholder: '开始时间',
					endPlaceholder: '结束时间',
					param: 'timeVal',
					defaultSelect: [],
					width: 250,
				},
				{
					type: 'button',
					btnName: '查询',
					name: 'search',
					color: 'primary',
				},
				{
					type: 'separate',
				},
				{
					type: this.$auth.hasPermi('monitoring:materiel-date-from:export')
						? 'button'
						: '',
					btnName: '导出',
					name: 'export',
					plain: true,
					color: 'success',
				},
			],
		};
	},
	components: {
		// AddOrUpdate,
	},
	created() {
		this.getDict();
	},
	methods: {
		getList() {
			getMaterialUseLogPage({ ...this.listQuery }).then((res) => {
				this.list = res.data || [];
				this.total = res.data.total || 0;
			});
		},
		getDict() {
			getWorkOrderList().then((response) => {
				this.formConfig[0].selectOptions = response.data.map((item) => {
					return {
						name: item.name,
						id: item.id,
					};
				});
				console.log(this.formConfig[0].selectOptions);
				this.formConfig[0].defaultSelect =
					this.formConfig[0].selectOptions[0].id || '';
				this.listQuery.workOrderId =
					this.formConfig[0].selectOptions[0].id || '';
				this.getList();
			});
		},
		handleExport() {
			// 处理查询参数
			let params = { ...this.listQuery };
			params.pageNo = undefined;
			params.pageSize = undefined;
			this.exportLoading = true;
			exportEnergyPlcExcel(params)
				.then((response) => {
					this.$download.excel(response, '物料信息追溯  ');
					this.exportLoading = false;
				})
				.catch(() => {});
		},
		buttonClick(val) {
			console.log(val);
			if (val.btnName === 'search') {
				this.listQuery.workOrderId = val.workOrderId
					? val.workOrderId
					: undefined;
				if (val.timeVal && val.timeVal.length != 0) {
					this.listQuery.startTime = val.timeVal[0] + ' 00:00:00';
					this.listQuery.endTime = val.timeVal[1] + ' 23:59:59';
				} else {
					this.listQuery.startTime = undefined;
					this.listQuery.endTime = undefined;
				}
				this.getList();
			} else {
				this.handleExport();
			}
		},
		otherMethods(val) {
			this.addOrUpdateVisible = true;
			this.addOrEditTitle = '详情';
			this.$nextTick(() => {
				this.$refs.addOrUpdate.init(val.data.id, true);
			});
		},
	},
};
</script>
