<template>
	<div>
		<el-dialog v-model="dialogVisible" title="资源信息" width="30%"  @close="close()">
			<el-form :model="ResourcesInfo" label-width="100px" v-loading="loading" style="padding-right: 40px;">
				<el-form-item label="所属科室:">
					<el-select  :filterable="true" clearable v-model="ResourcesInfo.department_id" placeholder="选择科室">
						<el-option v-for="(item,index) in EnableDepartmentList" :key="index" :label="item.department_name" :value="item.id" /> 
					</el-select> 
				</el-form-item>
				<el-form-item label="资源名称:">
					<el-input v-model="ResourcesInfo.department_resources_name" />
				</el-form-item>
				<el-form-item label="资源位置:">
					<el-input v-model="ResourcesInfo.department_resources_addr" />
				</el-form-item>
				<el-form-item label="资源状态:">
					<el-switch v-model="ResourcesInfo.department_resources_status"  active-text="可用" inactive-text="关闭"
						:active-value="1" :inactive-value="0" />
				</el-form-item>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="close()">取消</el-button>
					<el-button type="primary" @click="Save">
						确定
					</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup>
	import {
		ref,
		onMounted,watch
	} from 'vue'
	import {
		 GetEnableDepartmentList,
		 SaveDepartmentResource
	} from '@/api/api.js'
	import {
		ElMessage
	} from 'element-plus'
	let loading=ref(false);
	let dialogVisible=ref(true);
	let ResourcesInfo=ref({
		id:null,
		department_id:null,
		department_resources_name:'',
		department_resources_status:1,
		department_resources_addr:''
	})
	// defineProps: 接收父组件传递的数据
		const props = defineProps({
			 departmentId: {
			      
			    },
			resourceInfo:{
				type:Object
			},
		})
 
	
	let EnableDepartmentList =ref([]);
	const GetDepartmentEnableList=()=>{
		loading.value = true
		GetEnableDepartmentList({}).then(res => {
			loading.value = false
			if (res.status) {
				EnableDepartmentList.value = res.data.list
			} else {
				ElMessage.error(res.msg)
			}
		
		})
	}
	const Save=()=>{
		if(!(isNotNullOrEmpty(ResourcesInfo.value.department_id) && isNotNullOrEmpty(ResourcesInfo.value.department_resources_name))){
			ElMessage.error("请填写完整内容")
			return false;
		}
		loading.value = true
		SaveDepartmentResource({info:ResourcesInfo.value}).then(res => {
			loading.value = false
			if (res.status) {
				ElMessage({
					message: '保存成功',
					type: 'success',
				})
				close()
				 
			} else {
				ElMessage.error(res.msg)
			}
		
		})
	}
	//传值给父
	const emit = defineEmits(['closeAddResources'])
	const close = () => {
	  emit('closeAddResources')
	}
	const isNotNullOrEmpty=(str)=> {
		return str !== null && str !== '';
	}
	onMounted(()=>{
		
		 console.log(props.departmentId);
		  console.log(props.resourceInfo);
		let	r_departmentId=props.departmentId
		 
		if(props.resourceInfo!=undefined &&props.resourceInfo.id!=undefined ){
			 
			r_departmentId=props.resourceInfo.department_id
			ResourcesInfo.value.id=props.resourceInfo.id
			ResourcesInfo.value.department_resources_name=props.resourceInfo.department_resources_name
			ResourcesInfo.value.department_resources_addr=props.resourceInfo.department_resources_addr
			ResourcesInfo.value.department_resources_status=props.resourceInfo.department_resources_status
		}
		ResourcesInfo.value.department_id=r_departmentId
		
		GetDepartmentEnableList();
	})
</script>

<style>
</style>