<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"
			@emitFun="getDataList">
			<method-btn
				v-if="tableBtn.length"
				slot="handleBtn"
				:width="110"
				label="操作"
				:method-list="tableBtn"
				@clickBtn="handleClick" />
		</base-table>
		<pagination
			:limit.sync="listQuery.pageSize"
			:page.sync="listQuery.pageNo"
			:total="listQuery.total"
			@pagination="getDataList" />
		<base-dialog
			:dialogTitle="addOrEditTitle"
			:dialogVisible="addOrUpdateVisible"
			@cancel="handleCancel"
			@confirm="handleConfirm"
			:before-close="handleCancel"
      destroy-on-close
			width="50%">
			<add-or-update
				ref="addOrUpdate"
				@refreshDataList="successSubmit"></add-or-update>
		</base-dialog>
	</div>
</template>

<script>
import AddOrUpdate from './add-or-updata';
import basicPage from '@/mixins/basic-page';
import { parseTime } from '@/filter/code-filter';
import {
	deleteLineEdgeLibrary,
	getLineEdgeLibraryPage,
	QKLineEdgeLibrary,
} from '@/api/areavisual/lineEdgeLibrary';

const laneRow = [
	{
		id: '1',
		name: '第一排',
	},
	{
		id: '2',
		name: '第二排',
	},
	{
		id: '3',
		name: '第三排',
	},
	{
		id: '4',
		name: '第四排',
	},
	{
		id: '5',
		name: '第五排',
	},
	{
		id: '6',
		name: '第六排',
	},
];
const tableProps = [
	{
		prop: 'lineEdgeLibraryCode',
		label: '线边库编码',
	},
	{
		prop: 'lineEdgeLibraryName',
		label: '线边库名称',
	},
	{
		prop: 'agvGroundCode',
		label: 'agv码',
	},
	{
		prop: 'lineEdgeLibraryState',
		label: '线边库状态',
		width: 90,
		filter: (val) => {
			return val ? '满位' : '空闲';
		},
	},
	{
		prop: 'usableState',
		label: '是否可用',
		width: 90,
		filter: (val) => {
			return ['', '可用', '不可用', '锁定'][val];
		},
	},
	{
		prop: 'trayType',
		label: '托盘类型',
		width: 90,
		filter: (val) => {
			return val ? '笼式' : '盘式';
		},
	},
	{
		prop: 'productionLine',
		label: '产线',
	},
	{
		prop: 'equipmentName',
		label: '设备',
	},
	{
		prop: 'suiteCode',
		label: '套号',
	},
	{
		prop: 'laneRow',
		label: '巷道排',
		filter: (val) => {
			return laneRow.find((i) => i.id == val)
				? laneRow.find((i) => i.id == val).name
				: '-';
		},
	},
	{
		prop: 'materialCode',
		label: '物料',
	},
	{
		prop: 'batch',
		label: '批次',
	},
	{
		prop: 'cableLength',
		label: '线缆长度',
	},
	{
		prop: 'createTime',
		label: '创建时间',
		filter: parseTime,
		width: 150,
	},
];

export default {
	mixins: [basicPage],
	data() {
		return {
			urlOptions: {
				getDataListURL: getLineEdgeLibraryPage,
				deleteURL: deleteLineEdgeLibrary,
			},
			tableProps,
			tableData: [],
			tableBtn: [
				{
					type: 'clear',
					btnName: '清空',
				},
				{
					type: 'add',
					btnName: '添加',
				},
			].filter((v) => v),
			formConfig: [
				{
					type: 'input',
					label: '线边库名称',
					placeholder: '线边库名称',
					param: 'name',
				},
				{
					type: 'input',
					label: '线边库编码',
					placeholder: '线边库编码',
					param: 'code',
				},
				{
					type: 'select',
					label: '巷道排',
					selectOptions: laneRow,
					placeholder: '请选择巷道排',
					param: 'laneRow',
				},
				{
					type: 'button',
					btnName: '搜索',
					name: 'search',
					color: 'primary',
				},
				// {
				// 	type: 'separate',
				// },
				// {
				// 	type: 'button',
				// 	btnName: '重置',
				// 	name: 'reset',
				// },
				// {
				// 	type: 'separate',
				// },
				// {
				// 	type: 'button',
				// 	btnName: '新增',
				// 	name: 'add',
				// 	color: 'success',
				// 	plain: true,
				// },
				// {
				// 	type: this.$auth.hasPermi('base:factory:create') ? 'separate' : '',
				// },
				// {
				// 	type: this.$auth.hasPermi('base:factory:export') ? 'button' : '',
				// 	btnName: '导出',
				// 	name: 'export',
				// 	color: 'warning',
				// },
			],
		};
	},
	components: {
		AddOrUpdate,
	},
	created() {},
	methods: {
		buttonClick(val) {
			switch (val.btnName) {
				case 'search':
					this.listQuery.pageNo = 1;
					this.listQuery.pageSize = 20;
					this.listQuery.lineEdgeLibraryName = val.name;
					this.listQuery.lineEdgeLibraryCode = val.code;
					this.listQuery.laneRow = val.laneRow;
					this.getDataList();
					break;
				case 'reset':
					this.$refs.searchBarForm.resetForm();
					this.listQuery = {
						pageSize: 20,
						pageNo: 1,
						total: 1,
					};
					this.getDataList();
					break;
				case 'add':
					this.addOrEditTitle = '新增';
					this.addOrUpdateVisible = true;
					this.addOrUpdateHandle();
					break;
				case 'export':
					this.handleExport();
					break;
				default:
					console.log(val);
			}
		},
		otherMethods(val) {
			if (val.type == 'clear') {
				if (val.data.usableState == 3) {
					this.$confirm(
						`当前库位为锁定状态，代表该库位有未完成的任务关联，是否确认清空${
							'线边库名称为  ' + val.data.lineEdgeLibraryName + '  '
						}的数据项?`,
						'锁定库位清空提示',
						{
							confirmButtonText: '确定',
							cancelButtonText: '取消',
							type: 'warning',
						}
					)
						.then(() => {
							QKLineEdgeLibrary({...val.data,lineEdgeLibraryState:0,usableState:1 }).then(({ data }) => {
								this.$message({
									message: data,
									type: 'success',
									duration: 1500,
									onClose: () => {
										this.getDataList();
									},
								});
							});
						})
						.catch(() => {});
				} else {
					QKLineEdgeLibrary({...val.data,lineEdgeLibraryState:0,usableState:1 }).then(({ data }) => {
						this.$message({
							message: data,
							type: 'success',
							duration: 1500,
							onClose: () => {
								this.getDataList();
							},
						});
					});
				}
			} else if (val.type === 'add') {
				this.addOrEditTitle = '添加货物';
				this.addOrUpdateVisible = true;
				this.$nextTick(() => {
					this.$refs.addOrUpdate.init();
					this.$refs.addOrUpdate.showLine(val.data);
				});
			}
		},
	},
};
</script>
