<template>
	<el-container>
		<el-aside width="50%">
			<el-container>
				<el-header>
					<div class="left-panel">
						<el-space><b>单端口转发列表</b></el-space>
					</div>
					<div class="right-panel">
						<el-button type="primary" icon="el-icon-plus" @click="addRedirect"></el-button>
					</div>
				</el-header>
				<el-main class="nopadding">
					<el-table ref="redirectTable" :data="redirectTable" :key="srcPort" border style="width: 100%">
						<el-table-column prop="name" label="规则名称" align="center"/>
						<el-table-column prop="proto" label="规则协议" width="120" align="center">
							<template #default="scope">
								<el-tag v-if="scope.row.proto==1" type="success">TCP</el-tag>
								<el-tag v-if="scope.row.proto==2" type="success">UDP</el-tag>
								<el-tag v-if="scope.row.proto==3" type="success">TCP和UDP</el-tag>
							</template>
						</el-table-column>
						<el-table-column prop="srcPort" label="外部端口" width="80"  align="center"/>
						<el-table-column prop="destIp" label="内部IP地址" width="120" align="center"/>
						<el-table-column prop="destPort" label="内部端口" width="80" align="center"/>
						<el-table-column label="操作" align="center">
							<template #default="scope">
								<el-button-group>
									<el-popconfirm title="确定删除吗？" @confirm="tableDel(scope.row, 1)">
										<template #reference>
											<el-button text type="primary" size="small">删除</el-button>
										</template>
									</el-popconfirm>
								</el-button-group>
							</template>
						</el-table-column>
					</el-table>
				</el-main>
			</el-container>
		</el-aside>
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-space><b>端口范围转发列表</b></el-space>
				</div>
				<div class="right-panel">
					<el-button type="primary" icon="el-icon-plus" @click="addRangeRedirect"></el-button>
				</div>
			</el-header>
			<el-main class="nopadding">
				<el-table :data="rangeRedirectTable" :key="name" border style="width: 100%">
					<el-table-column prop="name" label="规则名称" align="center"/>
					<el-table-column prop="proto" label="规则协议" width="120" align="center">
						<template #default="scope">
							<el-tag v-if="scope.row.proto==1" type="success">TCP</el-tag>
							<el-tag v-if="scope.row.proto==2" type="success">UDP</el-tag>
							<el-tag v-if="scope.row.proto==3" type="success">TCP和UDP</el-tag>
						</template>
					</el-table-column>
					<el-table-column prop="srcPort.f" label="起始端口" width="80" align="center"/>
					<el-table-column prop="srcPort.t" label="结束端口" width="80" align="center"/>
					<el-table-column prop="destIp" label="目标IP" width="120" align="center"/>
					<el-table-column label="操作" align="center">
						<template #default="scope">
							<el-button-group>
								<el-popconfirm title="确定删除吗？" @confirm="tableDel(scope.row, 2)">
									<template #reference>
										<el-button text type="primary" size="small">删除</el-button>
									</template>
								</el-popconfirm>
							</el-button-group>
						</template>
					</el-table-column>
				</el-table>
			</el-main>
		</el-container>
	</el-container>

	<redirect-dialog v-if="dialog.redirect" ref="redirectDialog" @success="handleSaveSuccess"
					 @closed="dialog.redirect=false"></redirect-dialog>
	<rangeRedirect-dialog v-if="dialog.rangeRedirect" ref="rangeRedirectDialog" @success="handleSaveSuccess"
					 @closed="dialog.rangeRedirect=false"></rangeRedirect-dialog>
</template>

<script>
import redirectDialog from './redirect.vue'
import rangeRedirectDialog from './rangeRedirect.vue'

export default {
	name: "nat",
	components: {
		redirectDialog,
		rangeRedirectDialog
	},
	data() {
		return {
			redirectTable: null,
			rangeRedirectTable: null,
			redirect: {
				save: false
			},
			dialog: {
				redirect: false,
				rangeRedirect: false
			}
		}
	},
	created() {
		this.loadRedirectTable()
		this.loadRangeRedirectTable()
		this.loadGatewayPrefix()
	},
	methods: {
		async loadGatewayPrefix() {
			let res = await this.$API.system.home.gatewayPrefix.get();
			console.log(res.data)
			this.$TOOL.data.set("gateway_prefix", res.data)
		},
		async loadRedirectTable() {
			let res = await this.$API.system.network.nat.list.get({type: 1})
			this.redirectTable = res.data
		},
		async loadRangeRedirectTable() {
			let res = await this.$API.system.network.nat.list.get({type: 2})
			this.rangeRedirectTable = res.data
		},
		async tableDel(row, index) {
			let reqDate = {};
			if (index == 1) {
				reqDate.port = row.srcPort
				reqDate.proto = row.proto
			} else {
				console.log(row, "范围")
				reqDate.port = row.srcPort.f
				reqDate.proto = row.proto
			}
			let res = await this.$API.system.network.nat.del.post(reqDate);
			if (res.code == 200) {
				await this.loadRedirectTable()
				await this.loadRangeRedirectTable()
			}
		},
		//添加单端口映射
		addRedirect(){
			this.dialog.redirect = true
			this.$nextTick(() => {
				this.$refs.redirectDialog.open()
			})
		},
		//添加范围端口映射
		addRangeRedirect(){
			this.dialog.rangeRedirect = true
			this.$nextTick(() => {
				this.$refs.rangeRedirectDialog.open()
			})
		},
		handleSaveSuccess(data, mode){
			console.log(data,mode)
			this.loadRedirectTable()
			this.loadRangeRedirectTable()
		}
	}
}
</script>

<style scoped>

</style>
