<script>
$("#loading").hide();
$("#myForm").submit(function() {
    $("#info").html("");
    $("#loading").show();
    $("#saveBtn").prop('disabled', true);
    $.ajax({
        url: 'manage-customers.php',
        type: 'post',
        data: $('#myForm').serialize(),
        success: function(response) {
            $("#loading").hide();
            $("#info").html(response);
            $("#saveBtn").prop('disabled', false);
        }
    });
    return false;
});
</script> 
<?php
include_once 'api/config.php';
if (!isset($_SESSION['SESS_UID'])) {
    echo "Not allowed";
    exit();
} 

$editId = 0;
$cust_name = $cust_email = $cust_details = $cust_address = $allowed_ips = "";
$allowed_downloads = $current_downloads = 0;
$cust_status = 1;

if (isset($_GET['editId'])) {
    $editId = $_GET['editId'];
    $sql = "SELECT * FROM customers WHERE cust_id='$editId'";
    $result = mysqli_query($link, $sql);
    if ($result->num_rows == 1) {
        $row = mysqli_fetch_array($result);
        $editId = $row['cust_id'];
        $cust_address = $row['cust_address'];
        $cust_name = $row['cust_name'];
        $cust_email = $row['cust_email'];
        $cust_details = $row['cust_details'];
        $allowed_ips = $row['allowed_ips']; // Added allowed_ips field
        $cust_status = $row['cust_status'];
        $current_downloads = $row['current_downloads'];
        $allowed_downloads = $row['allowed_downloads'];
    }
}
?>
   
<div class="row">
    <div class="col-lg-12" style="padding:0px;">
        <div class="card">
            <div class="card-header bg-primary text-white">
                <?=($editId > 0 ? "Update" : "Add")?> Customer
            </div>
            <div class="card-body" style="min-width:450px;">
                <div class="form-row justify-content-lg-center justify-content-md-center">
                    <div class="form-group col-lg-12 col-md-12 col-sm-12">
                        <div id="info"></div>
                        <div id="loading" style="display:none;" class="card">
                            <div class="card-body">
                                <span> <i class="fas fa-spinner"></i> </span> <strong> Loading </strong>.....Please Wait.... <span class="glyphicon glyphicon-time"></span>
                            </div>
                        </div>
                    </div>
                </div>
                <form id="myForm" action="" method="">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="cust_name" class="">Customer Name</label>
                                <input type="text" class="form-control" name="cust_name" id="cust_name" value="<?=$cust_name?>" placeholder="Name" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="cust_email" class="">Customer Email</label>
                                <input type="text" class="form-control" name="cust_email" id="cust_email" value="<?=$cust_email?>" placeholder="Email" required>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-4">
                            <div class="form-group">
                                <label for="allowed_downloads" class="">Allowed</label>
                                <input type="number" class="form-control" name="allowed_downloads" id="allowed_downloads" value="<?=$allowed_downloads?>" placeholder="Total" required>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label for="current_downloads" class="">Current</label>
                                <input type="number" class="form-control" name="current_downloads" id="current_downloads" value="<?=$current_downloads?>" placeholder="Current" required>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label for="cust_status" class="">Status</label>
                                <select class="form-control" name="cust_status" id="cust_status" required>
                                    <option value="1" <?=($cust_status == 1 ? "selected='selected'" : "")?> >ACTIVE</option>
                                    <option value="0" <?=($cust_status == 0 ? "selected='selected'" : "")?> >BLOCKED</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <div class="form-group">
                                <label for="cust_details" class="">Customer Details</label>
                                <textarea class="form-control" name="cust_details" id="cust_details" placeholder="Details"><?=$cust_details?></textarea>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <div class="form-group">
                                <label for="cust_address" class="">Customer Address</label>
                                <textarea class="form-control" name="cust_address" id="cust_address" placeholder="Address"><?=$cust_address?></textarea>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <div class="form-group">
                                <label for="allowed_ips" class="">Allowed IPs</label> <!-- New field -->
                                <textarea class="form-control" name="allowed_ips" id="allowed_ips" placeholder="Allowed IPs"><?=$allowed_ips?></textarea>
                            </div>
                        </div>
                    </div>          
                    <div class="form-row justify-content-lg-left justify-content-md-center">
                        <div class="form-row col-lg-12 col-md-12 col-sm-12 m-3">
                            <input type="hidden" name="editId" value="<?=$editId?>"/>
                            <input type="submit" id="saveBtn" class="btn btn-primary" value="Save">
                        </div>
                    </div>  
                </form>
            </div>
        </div>
    </div>
</div>
