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

		<!-- 列表 -->
		<base-table
			:table-props="tableProps"
			:page="queryParams.pageNo"
			:limit="queryParams.pageSize"
			:table-data="list"
			@emitFun="handleEmitFun"
			:max-height="tableH">
			<method-btn
				v-if="tableBtn.length"
				slot="handleBtn"
				label="操作"
				:width="80"
				fixed="right"
				:method-list="tableBtn"
				@clickBtn="handleTableBtnClick" />
		</base-table>

		<!-- 分页组件 -->
		<pagination
			v-show="total > 0"
			:total="total"
			:page.sync="queryParams.pageNo"
			:limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 对话框(添加 / 修改) -->
		<base-dialog
			:dialogTitle="title"
			:dialogVisible="open"
			width="50%"
			@close="cancel"
			@cancel="cancel"
			@confirm="submitForm">
			<DialogForm v-if="open" ref="form" v-model="form" />
		</base-dialog>
	</div>
</template>

<script>
import {
	createQualityInspectionRecord,
	updateQualityInspectionRecord,
	deleteQualityInspectionRecord,
	getQualityInspectionRecord,
	getQualityInspectionRecordPage,
	exportQualityInspectionRecordExcel,
} from '@/api/monitoring/qualityInspectionRecord';
// import Editor from '@/components/Editor';
import moment from 'moment';
import DialogForm from './dialogForm.vue';

import basicPageMixin from '@/mixins/lb/basicPageMixin';
import tableHeightMixin from '@/mixins/tableHeightMixin';
export default {
	name: 'QualityInspectionRecord',
	components: {
		DialogForm,
	},
	mixins: [basicPageMixin, tableHeightMixin],
	data() {
		return {
			searchBarFormConfig: [
				{
					type: 'select',
					label: '工单名称',
					param: 'workOrderId',
					selectOptions: [],
					filterable: true,
				},
				{
					type: 'select',
					label: '检测内容',
					selectOptions: [],
					param: 'inspectionDetContent',
					filterable: true,
				},
				{
					type: 'datePicker',
					label: '时间段',
					dateType: 'daterange', // datetimerange
					// format: 'yyyy-MM-dd HH:mm:ss',
					format: 'yyyy-MM-dd',
					valueFormat: 'yyyy-MM-dd HH:mm:ss',
					rangeSeparator: '-',
					startPlaceholder: '开始日期',
					endPlaceholder: '结束日期',
					defaultTime: ['00:00:00', '23:59:59'],
					param: 'checkTime',
					// width: 350,
				},
				{
					type: 'button',
					btnName: '查询',
					name: 'search',
					color: 'primary',
				},
				{
					type: 'separate',
				},
				{
					type: this.$auth.hasPermi('base:quality-inspection-record:create')
						? 'button'
						: '',
					btnName: '新增',
					name: 'add',
					plain: true,
					color: 'success',
				},
			],
			tableBtn: [
				this.$auth.hasPermi('base:quality-inspection-record:update')
					? {
							type: 'edit',
							btnName: '编辑',
					  }
					: undefined,
				this.$auth.hasPermi('base:quality-inspection-record:delete')
					? {
							type: 'delete',
							btnName: '删除',
					  }
					: undefined,
			].filter((v) => v),
			tableProps: [
				{
					prop: 'createTime',
					label: '添加时间',
					width: 160,
					filter: (val) => moment(val).format('yyyy-MM-DD HH:mm:ss'),
				},
				{
					prop: 'workOrderName',
					label: '工单名称',
					showOverflowtooltip: true,
				},
				{
					prop: 'inspectionDetContent',
					label: '检测内容',
					showOverflowtooltip: true,
				},
				{
					prop: 'productionLineName',
					label: '产线',
					showOverflowtooltip: true,
				},
				{
					prop: 'checkPerson',
					label: '检测人员',
					showOverflowtooltip: true,
				},
				{
					width: 160,
					prop: 'checkTime',
					label: '检测时间',
					filter: (val) =>
						val != null ? moment(val).format('yyyy-MM-DD HH:mm:ss') : '-',
				},
				{
					width: 90,
					prop: 'source',
					label: '来源',
					filter: (val) => ['未知', '手动', '自动'][val],
				},
			],
			// 搜索框需要的 keys, 与上面 queryParams 的除 pageNo, pageSize 之外的 key 一一对应
			searchBarKeys: ['workOrderId', 'inspectionDetContent', 'checkTime'],
			form: {
				id: undefined,
				inspectionDetId: undefined,
				inspectionDetContent: undefined,
				productionLineId: undefined,
				sectionId: undefined,
				checkPerson: undefined,
				workOrderId: undefined,
				checkTime: undefined,
				source: undefined,
				workOrderId: undefined,
				remark: undefined,
			},
			// 查询参数
			queryParams: {
				pageNo: 1,
				pageSize: 20,
				workOrderId: null,
				inspectionDetContent: null,
				checkTime: [],
				// productionLineId: null,
			},
		};
	},
	created() {
		this.getList();
		this.getProductLineList();
	},
	watch: {
		form: {
			handler: function (val) {
				console.log('form change:', val);
			},
			deep: true,
		},
	},
	methods: {
		/** 获取搜索栏的产线列表 */
		getProductLineList() {
			this.$axios('/base/core-work-order/listbyfilter').then((response) => {
				this.searchBarFormConfig[0].selectOptions = response.data.map(
					(item) => {
						return {
							name: item.name,
							id: item.id,
						};
					}
				);
			});
			this.$axios('/base/quality-inspection-det/listAll').then((response) => {
				this.searchBarFormConfig[1].selectOptions = response.data.map(
					(item) => {
						return {
							name: item.content,
							id: item.content,
						};
					}
				);
			});
		},
		/** 查询列表 */
		getList() {
			this.loading = true;
			// 执行查询
			getQualityInspectionRecordPage(this.queryParams).then((response) => {
				this.list = response.data.list;
				this.total = response.data.total;
				this.loading = false;
			});
		},
		/** 取消按钮 */
		cancel() {
			this.open = false;
			this.reset();
		},
		/** 表单重置 */
		reset() {
			this.form = {
				id: undefined,
				inspectionDetId: undefined,
				inspectionDetContent: undefined,
				sectionId: undefined,
				checkPerson: undefined,
				checkTime: undefined,
				source: undefined,
				workOrderId: undefined,
				remark: undefined,
				productionLineId: undefined,
			};
			this.resetForm('form');
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNo = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm('queryForm');
			this.handleQuery();
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset();
			this.open = true;
			this.title = '新增';
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset();
			const id = row.id;
			getQualityInspectionRecord(id).then((response) => {
				/** 因为后端返回的时间是时间戳格式，需转换 */
				const info = {};
				Object.keys(this.form).forEach((key) => {
					info[key] = response.data[key];
				});
				this.form = info;
				this.open = true;
				this.title = '编辑';
			});
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs['form'].validate((valid) => {
				if (!valid) {
					return;
				}
				// 修改的提交
				if (this.form.id != null) {
					updateQualityInspectionRecord(this.form).then((response) => {
						this.$modal.msgSuccess('修改成功');
						this.open = false;
						this.getList();
					});
					return;
				}
				// 添加的提交
				createQualityInspectionRecord(this.form).then((response) => {
					this.$modal.msgSuccess('新增成功');
					this.open = false;
					this.getList();
				});
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const id = row.id;
			this.$modal
				.delConfirm(row.inspectionDetContent)
				.then(function () {
					return deleteQualityInspectionRecord(id);
				})
				.then(() => {
					this.getList();
					this.$modal.msgSuccess('删除成功');
				})
				.catch(() => {});
		},
		/** 导出按钮操作 */
		handleExport() {
			// 处理查询参数
			let params = { ...this.queryParams };
			params.pageNo = undefined;
			params.pageSize = undefined;
			this.$modal
				.confirm('是否确认导出所有质量检查信息记录表数据项?')
				.then(() => {
					this.exportLoading = true;
					return exportQualityInspectionRecordExcel(params);
				})
				.then((response) => {
					this.$download.excel(response, '质量检查信息记录表.xls');
					this.exportLoading = false;
				})
				.catch(() => {});
		},
	},
};
</script>
