<!-- 
    filename: BomSelector.vue
    author: liubin
    date: 2023-11-17 16:23:28
    description: 
-->

<template>
	<div class="bom-selector">
		<el-row>
			<el-col :span="8">
				<el-input
					v-model="searchText"
					placeholder="搜索"
					style="margin-bottom: 12px; user-select: none">
					<i slot="prefix" class="el-input__icon el-icon-search"></i>
				</el-input>
			</el-col>
		</el-row>

		<el-row style="border: 1px solid #ccc; display: flex">
			<el-col :span="8">
				<div class="select-list">
					<div class="sl__header" style="background: #f3f4fb; padding: 12px">
						<span style="">可分配设备</span>
						<span>{{ selectedEquipments.length }}/{{ bomList.length }}</span>
					</div>
					<el-checkbox-group v-model="selectedEquipments" class="sl__body">
						<el-checkbox
							v-for="eq in bomList"
							:key="eq.id"
							:label="eq.name"
							@change="(e) => handleEquipmentChange(eq, e)"
							class="sl__body-item"></el-checkbox>
					</el-checkbox-group>
				</div>
			</el-col>
			<el-col :span="8" style="border-left: 1px solid #ccc">
				<div class="select-list">
					<div class="sl__header" style="background: #f3f4fb; padding: 12px">
						<span style="">物料BOM</span>
					</div>

					<BomSelection
						key="materialsBomList"
						:list="materialsBomList"
						:equipment-id="materialsBomList.equipmentId"
						@change="handleMaterialBomChange" />
				</div>
			</el-col>
			<el-col :span="8" style="border-left: 1px solid #ccc">
				<div class="select-list">
					<div class="sl__header" style="background: #f3f4fb; padding: 12px">
						<span style="">参数BOM</span>
					</div>

					<BomSelection
						key="valuesBomList"
						:list="valuesBomList"
						:equipment-id="valuesBomList.equipmentId"
						@change="handleValueBomChange" />
				</div>
			</el-col>
		</el-row>
	</div>
</template>
;
<script>
import BomSelection from './BomSelection.vue';
export default {
	name: 'BomSelector',
	components: { BomSelection },
	props: {
		bomList: {
			type: Array,
			default: () => [],
		},
	},
	data() {
		return {
			searchText: '',
			selectedEquipments: [],
			selected: [],
			materialsBomList: [],
			valuesBomList: [],
		};
	},
	methods: {
		handleEquipmentChange(eq, selected) {
			this.currentEquipment = eq.id;
			this.materialsBomList = eq.materialsBom;
			this.valuesBomList = eq.valuesBom;

			if (selected) {
				this.selected.push({
					equipmentId: eq.id,
					equValueBomId: null,
					equMaterialBomId: null,
				});
			} else {
				// 清空选择状态
				this.selected = this.selected.filter(
					(item) => item.equipmentId !== eq.id
				);
			}
		},

		handleMaterialBomChange(equipmentId, bomId, selected) {
			const selectedItem = this.selected.find(
				(item) => item.equipmentId == equipmentId
			);
			selectedItem.equMaterialBomId = selected ? bomId : null;
		},

		handleValueBomChange(equipmentId, bomId, selected) {
			const selectedItem = this.selected.find(
				(item) => item.equipmentId == equipmentId
			);
			selectedItem.equValueBomId = selected ? bomId : null;
		},
	},
};
</script>

<style scoped lang="scss">
.bom-selector {
	min-height: 200px;
}

.select-list {
}

.sl__body {
	display: flex;
	flex-direction: column;
	gap: 6px;
	padding: 6px;

	> .el-checkbox {
		margin: 0;
		padding: 3px 6px;
		border-radius: 4px;
		transition: background 0.3s ease-in-out;

		&:hover {
			background: #0001;
		}
	}
}

.sl__header {
	border-bottom: 1px solid #ccc;
}
</style>