<?php
session_start();
require('routeros_api.class.php');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $API = new RouterosAPI();
    $API->debug = false;
    if ($API->connect($_SESSION['ip'], $_SESSION['user'], $_SESSION['pass'])) {
        $API->comm("/ip/firewall/nat/add", [
            "chain" => $_POST['chain'],
            "src-address" => $_POST['src'],
            "dst-address" => $_POST['dst'],
            "action" => $_POST['action'],
            "to-addresses" => $_POST['toaddr'],
            "comment" => $_POST['comment']
        ]);
        $API->disconnect();
        echo "<script>alert('✅ NAT berhasil ditambahkan');window.location='firewall_nat.php';</script>";
        exit;
    } else {
        echo "<script>alert('❌ Gagal konek ke Mikrotik');history.back();</script>";
    }
}
?>
<!DOCTYPE html>
<html>
<head><title>Tambah NAT</title>
<style>
body { font-family: Arial; background: #f9f9f9; }
.container { max-width: 500px; margin: 40px auto; background: #fff; padding: 20px; border-radius: 10px; box-shadow: 0 0 10px rgba(0,0,0,0.1); }
input { width: 100%; padding: 8px; margin-bottom: 10px; }
button { padding: 8px 15px; border-radius: 5px; border: none; cursor: pointer; }
.btn-save { background: #28a745; color: #fff; }
.btn-cancel { background: #6c757d; color: #fff; }
</style>
</head>
<body>
<div class="container">
<h3>Tambah NAT</h3>
<form method="post">
<label>Chain:</label>
<input type="text" name="chain" value="srcnat" required>
<label>Src. Address:</label>
<input type="text" name="src">
<label>Dst. Address:</label>
<input type="text" name="dst">
<label>Action:</label>
<input type="text" name="action" value="masquerade" required>
<label>To-Addresses:</label>
<input type="text" name="toaddr">
<label>Comment:</label>
<input type="text" name="comment">
<button class="btn-save">💾 Simpan</button>
<button class="btn-cancel" type="button" onclick="window.location='firewall_nat.php'">Batal</button>
</form>
</div>
</body>
</html>
