<template>
	<div class="equipment-container">
		<el-input v-model="selectName" placeholder="请输入设备名称">
			<template #append>
				<el-button type="primary" @click="chooseClick">选择</el-button>
			</template>
		</el-input>
		<Table v-if="showTable" ref="tableRef" :selectId="selectId" :params="params" @ok="ok" @close="close" />
	</div>
</template>
<script setup>
import { getApi, postApi, } from "@/api/tobAcco_api.js";
import { ref, onMounted, defineProps, defineEmits, computed, defineExpose } from "vue";
import { ElMessage } from "element-plus";
import Table from "./table.vue";

const props = defineProps({
	modelValue: {
		type: String,
		default: ''
	},
	placeholder: {
		type: String,
		default: '请选择'
	},
	/**所属部位ID*/
	partid: {
		type: String,
		default: ''
	},
	/**危险作业场所类型 01-有限空间 02-熏蒸杀虫*/
	type: {
		type: String,
		default: ''
	},
	/**设备类型 */
	sblx: {
		type: String,
		default: ''
	},
	/**选择设备名称 */
	inputName: {
		type: String,
		default: ''
	}
})
const emits = defineEmits(['update:modelValue', 'ok'])
const options = ref([]);
const selectName = ref('')
const selectId = ref('')
const showTable = ref(false)
const params = computed(() => ({
	partid: props.partid,
	type: props.type,
	sblx: props.sblx
}))
const setEquipmentList = () => { // /hazardousSite/getSbList
	getApi({}, '/mosty-jcgl/hazardousSite/getSbList').then(res => {

		let arr = Array.isArray(res) ? res : []
		options.value = arr.map(item => ({
			sbid: item.sbid,
			sblx: item.sblx,
			sbmc: item.sbmc
		}))
	})
}
const chooseClick = () => {
	if (!props.type) return ElMessage.warning('请先选择危险作业场所类型')
	if (!props.partid) return ElMessage.warning('请先选择所属部位')
	if (!props.sblx) return ElMessage.warning('请先选择设备类型')
	showTable.value = true
}
const ok = (val) => {

	emits('update:modelValue', val)
	emits('ok', val)
	selectId.value = val.sbid
	selectName.value = val.sbmc
	showTable.value = false
}
const close = () => {
	showTable.value = false
}
// const changeEquipment = (val) => {
// 	
// 	emits('update:modelValue',val)
// }
onMounted(() => {
	setEquipmentList()
})
const init = (obj={}) => {
	if (obj.inputName) selectName.value = obj.inputName
	if (obj.selectId) selectId.value = obj.selectId
}
defineExpose({
	init
})
</script>

<style lang="scss" scoped></style>