<template>
	
	<div class="head">
		<el-row>
			<el-form-item>
				<el-input v-model="searchInfo.name" placeholder="请输入服务组名称" style="margin-left: 10px;"/>
			</el-form-item>
			<el-button   @click="GetDeviceListAction()" style="margin-left: 10px;">搜索</el-button>
			<el-button type="primary" @click="Add()" style="margin-left: 10px;">添加</el-button>
		</el-row>
	</div>
	<el-table  :data="tableData" style="width: 100%;" row-key="id" v-loading="loading">
		<el-table-column prop="id" label="Id" width="100"  />
		<el-table-column prop="device_name" label="服务组名称" />
		<el-table-column prop="status" label="状态" >
			<template #default="scope">
				 <el-tag v-if="scope.row.status==1"  class="ml-2" type="success">正常</el-tag>
				 <el-tag v-if="scope.row.status==0"  class="ml-2" type="danger">关闭</el-tag>
			</template>
		</el-table-column>
	 
		<el-table-column prop="" label="操作">
			<template #default="scope">
				 <el-button type="primary" @click="Edit(scope.row)" size="small" style="margin-left: 10px;">修改</el-button>
				 <el-button type="danger" @click="Del(scope.row.id)" size="small" style="margin-left: 10px;">删除</el-button>
			</template>
		</el-table-column>
	</el-table>
	<div class="page">
		<el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize"
			:page-sizes="[15, 50, 100, 200]" layout="total,sizes, prev, pager, next" :total="total"
			@size-change="PageSizeChange" @current-change="PageCurrentChange" />
	</div>
	
	<el-dialog v-model="dialogVisible" title="服务组信息" width="30%">
		<el-form :model="DeviceInfo" label-width="100px" v-loading="loading" style="padding-right: 40px;">
			<el-form-item label="服务组名称:">
				<el-input v-model="DeviceInfo.device_name" />
			</el-form-item>
			<el-form-item label="服务组状态:">
				<el-switch v-model="DeviceInfo.status" size="large" active-text="正常" inactive-text="关闭"
					:active-value="1" :inactive-value="0" />
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="dialogVisible = false">取消</el-button>
				<el-button type="primary" @click="Save">
					确定
				</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup>
	import {
		ref,
		onMounted
	} from 'vue'
	import {
		 GetDeviceList,SaveDeviceList,DelDevice
	} from '@/api/api.js'
	import { ElMessage, ElMessageBox } from 'element-plus'
	
	let loading=ref(false);
	let searchInfo=ref({
		name:''
	})
	let DeviceInfo=ref({
		id:null,
		device_name:'',
		status:1
	})
	
	let dialogVisible=ref(false);
	
	//获取设备list
	let tableData = ref([])
	let currentPage = ref(1) //当前页码
	let pageSize = ref(15) //每页数量
	let total = 0 //总数量
	const GetDeviceListAction=()=>{
		loading.value = true
		GetDeviceList({searchInfo:searchInfo.value,page:currentPage.value,pageSize:pageSize.value}).then(res => {
			loading.value = false
			if (res.status) {
				tableData.value=res.data.list
				total=res.data.count
			} else {
				ElMessage.error(res.msg)
			}
		
		})
	}
	const PageSizeChange = (e) => { // 修改每页数量
		pageSize.value = e
		GetDeviceListAction()
	}
	const PageCurrentChange = (e) => { //切换页码
		currentPage.value = e
		GetDeviceListAction()
	}
	const Add=()=>{
		dialogVisible.value=true
		DeviceInfo.value.id=null
		searchInfo.value.name=''
	}
	const Edit=(row)=>{
		DeviceInfo.value={
			id:row.id,
			device_name:row.device_name,
			status:row.status
		}
		dialogVisible.value=true
	
	}
	const Del=(id)=>{
		 ElMessageBox.confirm(
		    '确定删除吗？',
		    '提示',
		    {
		      confirmButtonText: '确定',
		      cancelButtonText: '取消',
		      type: 'warning',
		    }
		  )
		    .then(() => {
		     DelDevice({id:id}).then(res => {
		     	loading.value = false
		     	if (res.status) {
		     		 GetDeviceListAction()
		     		 
		     	} else {
		     		ElMessage.error(res.msg)
		     	}
		     
		     })
		    }) 
		
	}
	//保存操作
	const Save=()=>{
		loading.value = true
		SaveDeviceList({Info:DeviceInfo.value}).then(res => {
			loading.value = false
			if (res.status) {
				 dialogVisible.value=false
				 GetDeviceListAction()
				 
			} else {
				ElMessage.error(res.msg)
			}
		
		})
	}
	onMounted(()=>{
		GetDeviceListAction()
	})
</script>

<style scoped>
	.page {
		display: flex;
		justify-content: flex-end;
		margin-top: 10px;
	}
</style>