<?php

require_once '../../../includes/modal_header.php';

enforceUserPermission('module_support', 2);

$network_id = intval($_GET['id']);

$sql = mysqli_query($mysqli, "SELECT network, network_client_id, network_description, network_dhcp_range, network_gateway,
    network_location_id, network_name, network_notes, network_primary_dns,
    network_secondary_dns, network_vlan FROM networks WHERE network_id = $network_id LIMIT 1");

$row = mysqli_fetch_assoc($sql);
$network_name = escapeHtml($row['network_name']);
$network_description = escapeHtml($row['network_description']);
$network_vlan = intval($row['network_vlan']);
$network = escapeHtml($row['network']);
$network_gateway = escapeHtml($row['network_gateway']);
$network_primary_dns = escapeHtml($row['network_primary_dns']);
$network_secondary_dns = escapeHtml($row['network_secondary_dns']);
$network_dhcp_range = escapeHtml($row['network_dhcp_range']);
$network_notes = escapeHtml($row['network_notes']);
$network_location_id = intval($row['network_location_id']);
$client_id = intval($row['network_client_id']);

enforceClientAccess();

ob_start();

?>

<div class="modal-header bg-dark">
    <h5 class="modal-title"><i class="fa fa-fw fa-network-wired me-2"></i>Editing network: <span class="text-bold"><?= $network_name ?></span></h5>
    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
</div>
<form action="post.php" method="post" autocomplete="off">
    <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
    <input type="hidden" name="network_id" value="<?= $network_id ?>">

    <div class="modal-body">

        <ul class="nav nav-pills nav-justified mb-3">
            <li class="nav-item">
                <a class="nav-link active" data-bs-toggle="pill" href="#pillsEditDetails">Details</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" data-bs-toggle="pill" href="#pillsEditNetwork">Network</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" data-bs-toggle="pill" href="#pillsEditDNS">DNS</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" data-bs-toggle="pill" href="#pillsEditNotes">Notes</a>
            </li>
        </ul>

        <hr>

        <div class="tab-content" <?php if (lookupUserPermission('module_support') <= 1) { echo 'inert'; } ?>>

            <div class="tab-pane fade show active" id="pillsEditDetails">

                <div class="mb-3">
                    <label>Location</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-map-marker-alt"></i></span>
                        <select class="form-select select2" name="location">
                            <option value="">- Select Location -</option>
                            <?php
                            $locations_sql = mysqli_query($mysqli, "SELECT location_id, location_name FROM locations WHERE location_client_id = $client_id");
                            while ($row = mysqli_fetch_assoc($locations_sql)) {
                                $location_id = intval($row['location_id']);
                                $location_name = escapeHtml($row['location_name']);
                                ?>
                                <option value="<?= $location_id ?>" <?php if ($location_id == $network_location_id) { echo "selected"; } ?>>
                                    <?= $location_name ?>
                                </option>
                            <?php
                            }
                            ?>
                        </select>
                    </div>
                </div>

                <div class="mb-3">
                    <label>Name <strong class="text-danger">*</strong></label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-ethernet"></i></span>
                        <input type="text" class="form-control" name="name" placeholder="LAN, WAN, VOIP, Uplink" value="<?= $network_name ?>" maxlength="200" required>
                    </div>
                </div>

                <div class="mb-3">
                    <label>Description</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-align-left"></i></span>
                        <input type="text" class="form-control" name="description" placeholder="Guest WiFi, VoIP VLAN, Server LAN, WAN Uplink" value="<?= $network_description ?>">
                    </div>
                </div>

            </div>

            <div class="tab-pane fade" id="pillsEditNetwork">

                <div class="mb-3">
                    <label>VLAN</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-layer-group"></i></span>
                        <input type="text" class="form-control font-monospace" inputmode="numeric" name="vlan" placeholder="e.g. 20" value="<?= $network_vlan ?>">
                    </div>
                </div>

                <div class="mb-3">
                    <label>Network (CIDR) <strong class="text-danger">*</strong></label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-network-wired"></i></span>
                        <input type="text" class="form-control font-monospace" name="network" placeholder="192.168.1.0/24 or 2001:db8::/64" maxlength="200" value="<?= $network ?>" required>
                    </div>
                </div>

                <div class="mb-3">
                    <label>Assignable IP Range</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-arrows-alt-h"></i></span>
                        <input type="text" class="form-control font-monospace" name="dhcp_range" placeholder="192.168.1.100-192.168.1.200" maxlength="200" value="<?= $network_dhcp_range ?>">
                    </div>
                </div>

                <div class="mb-3">
                    <label>Gateway</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-route"></i></span>
                        <input type="text" class="form-control font-monospace" name="gateway" placeholder="192.168.1.1 or 2001:db8::1" maxlength="200" value="<?= $network_gateway ?>">
                    </div>
                </div>

            </div>

            <div class="tab-pane fade" id="pillsEditDNS">

                <div class="mb-3">
                    <label>Primary DNS</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-globe"></i></span>
                        <input type="text" class="form-control font-monospace" name="primary_dns" placeholder="9.9.9.9 or 2620:fe::fe" maxlength="200" value="<?= $network_primary_dns ?>">
                    </div>
                </div>

                <div class="mb-3">
                    <label>Secondary DNS</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-globe"></i></span>
                        <input type="text" class="form-control font-monospace" name="secondary_dns" placeholder="1.1.1.1 or 2606:4700:4700::1111" maxlength="200" value="<?= $network_secondary_dns ?>">
                    </div>
                </div>

            </div>

            <div class="tab-pane fade" id="pillsEditNotes">

                <div class="mb-3">
                    <textarea class="form-control" rows="12" name="notes" placeholder="Enter some notes"><?= $network_notes ?></textarea>
                </div>

                <p class="text-muted text-end"><?= $network_id ?></p>
            </div>

        </div>

    </div>
    <div class="modal-footer">
        <button type="submit" name="edit_network" class="btn btn-primary text-bold"><i class="fa fa-check me-2"></i>Save</button>
        <button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fa fa-times me-2"></i>Cancel</button>
    </div>
</form>

<?php
require_once '../../../includes/modal_footer.php';
