<template>
	<div class="app-container">
		<search-bar
			:formConfigs="formConfig"
			ref="searchBarForm"
			@select-changed="handleSearchBarChanged"
			@headBtnClick="buttonClick" />
		<base-table
			v-loading="dataListLoading"
			:table-props="tableProps"
			:page="listQuery.pageNo"
			:limit="listQuery.pageSize"
			:table-data="tableData" />
		<pagination
			:limit.sync="listQuery.pageSize"
			:page.sync="listQuery.pageNo"
			:total="listQuery.total"
			@pagination="getDataList" />
	</div>
</template>

<script>
import basicPage from '../../mixins/basic-page';
import { parseTime } from '../../mixins/code-filter';
import { getLineBindProcessLogPage } from '@/api/core/base/lineBindProcess';
import { getProductionLinePage } from '@/api/core/base/productionLine';
import { getPdList } from '@/api/core/monitoring/auto';
import { getFactoryPage } from '@/api/core/base/factory';

const tableProps = [
	{
		prop: 'factoryName',
		label: '工厂'
	},
	{
		prop: 'lineName',
		label: '产线',
	},
	{
		prop: 'processDictName',
		label: '在制工艺',
	},
	{
		prop: 'recordTime',
		label: '开始时间',
		filter: parseTime,
	},
];

export default {
	mixins: [basicPage],
	data() {
		return {
			urlOptions: {
				getDataListURL: getLineBindProcessLogPage,
			},
			tableProps,
			tableData: [],
			optionArrUrl: [getFactoryPage,getProductionLinePage],
			listQuery: {
				productionLineId: [],
			},
			formConfig: [
				{
					type: 'select',
					label: '工厂',
					selectOptions: [],
					param: 'factoryId',
					onchange: true,
				},
				{
					type: 'select',
					label: '产线',
					selectOptions: [],
					param: 'productionLineId',
					multiple: true,
				},
				{
					type: 'select',
					label: '在制工艺',
					selectOptions: this.getDictDatas(this.DICT_TYPE.PROCESS_TYPE),
					labelField: 'label',
					valueField: 'value',
					param: 'processDict',
					defaultSelect: '',
					filterable: true,
				},
				{
					type: 'datePicker',
					label: '时间',
					dateType: 'daterange',
					format: 'yyyy-MM-dd',
					valueFormat: 'yyyy-MM-dd HH:mm:ss',
					rangeSeparator: '-',
					startPlaceholder: '开始时间',
					endPlaceholder: '结束时间',
					param: 'startTime',
				},
				{
					type: 'button',
					btnName: '搜索',
					name: 'search',
					color: 'primary',
				},
				{
					type: 'button',
					btnName: '重置',
					name: 'reset',
				},
			],
		};
	},
	components: {},
	created() {
		this.getArr();
	},
	methods: {
		handleSearchBarChanged({ param, value }) {
			this.listQuery.productionLineId = [];
			this.$refs.searchBarForm.formInline.productionLineId = undefined;
			getPdList(value).then((res) => {
				this.formConfig[1].selectOptions = res.data || [];
			});
		},
		getArr() {
			const params = {
				page: 1,
				limit: 500,
			};
			this.optionArrUrl.forEach((item, index) => {
				item(params).then((response) => {
					this.formConfig[index].selectOptions = response.data.list;
				});
			});
		},
		buttonClick(val) {
			switch (val.btnName) {
				case 'search':
					this.listQuery.pageNo = 1;
					this.listQuery.pageSize = 10;
					this.listQuery.factoryId = val.factoryId || undefined;
					this.listQuery.productionLineId = val.productionLineId || [];
					this.listQuery.processDict = val.processDict;
					this.listQuery.recordTime = val.startTime
						? [val.startTime[0], val.startTime[1].substr(0, 10) + ' 23:59:59']
						: null;
					this.getDataList();
					break;
				case 'reset':
					this.$refs.searchBarForm.resetForm();
					this.listQuery = {
						pageSize: 10,
						pageNo: 1,
						total: 1,
					};
					this.getDataList();
					break;
				default:
					console.log(val);
			}
		},
	},
};
</script>
