<?php 
include 'includes/header.php';
include 'includes/sidebar.php';
require 'includes/db_connect.php';

$street_id = $_GET['id'] ?? '';

/* Redirect if no street selected */
if(empty($street_id)){
    echo "<script>alert('Street ID not found'); window.location='street_renewal.php';</script>";
    exit;
}

/* Fetch street info */
/* Fetch street info */
$streetQuery = "SELECT * FROM registered_streets WHERE id='$street_id'";
$streetResult = $conn->query($streetQuery);
if(!$streetResult){ die("SQL Error: " . $conn->error); }

$street = $streetResult->fetch_assoc();
if(!$street){
    echo "<script>alert('Street not found'); window.location='street_renewal.php';</script>";
    exit;
}

/* Fetch all renewal history for this street */
$historyQuery = "SELECT * FROM renewal WHERE street_id='$street_id' ORDER BY renewal_to DESC";
$historyResult = $conn->query($historyQuery);
if(!$historyResult){ die("SQL Error: " . $conn->error); }

/* Handle Renewal Submission */
if(isset($_POST['renew_street'])){
    $renew_from = $_POST['renewal_from'];
    $renew_to   = $_POST['renewal_to'];
    $amount     = $_POST['amount'];

    $stmt = $conn->prepare("INSERT INTO renewal(street_id, renewal_from, renewal_to, amount) VALUES (?, ?, ?, ?)");
    $stmt->bind_param("iiid", $street_id, $renew_from, $renew_to, $amount);
    $stmt->execute();
    $stmt->close();

    echo "<script>alert('Street Renewal Recorded Successfully'); window.location='street_renewal.php?id=$street_id';</script>";
}

?>
<style>
.text-orange{color:#ff7a00;}
.bg-orange{background:#ff7a00;}
.btn-orange{background:#ff7a00;color:white;border:none;}
.btn-orange:hover{background:#e36d00;color:white;}
.content-wrapper{padding:20px;}
.table-responsive{overflow-x:auto;}
.pagination{flex-wrap:wrap;}
</style>

<div class="content-wrapper">
    <div class="container">

        <h3 class="text-orange fw-bold">Street Renewal</h3>
        <p class="text-muted">Record renewal payment and period</p>

        <!-- RENEW STREET FORM -->
        <div class="card shadow mb-4">
            <div class="card-header bg-orange text-dark">Renew Street</div>
            <div class="card-body">
                <form method="POST">
                    <div class="mb-3">
                        <label class="form-label">Street Name</label>
                        <input type="text" class="form-control" value="<?php echo $street['street_name']; ?>" readonly>
                    </div>
                    <div class="row">
                        <div class="col-md-4 mb-3">
                            <label>Renewal From</label>
                            <input type="number" name="renewal_from" class="form-control" required>
                        </div>
                        <div class="col-md-4 mb-3">
                            <label>Renewal To</label>
                            <input type="number" name="renewal_to" class="form-control" required>
                        </div>
                        <div class="col-md-4 mb-3">
                            <label>Amount Paid</label>
                            <input type="number" name="amount" class="form-control" required>
                        </div>
                    </div>
                    <button type="submit" name="renew_street" class="btn btn-orange">Save Renewal</button>
                </form>
            </div>
        </div>

        <!-- RENEWAL HISTORY AJAX TABLE -->
        <div class="card shadow">
            <div class="card-header bg-orange text-dark">
                Renewal History
            </div>
            <div class="card-body table-responsive">
                <input type="text" id="streetSearch" class="form-control mb-3" placeholder="Search street...">
                <div id="streetRenewalTable">
                    <?php include 'ajax_street_renewal_table.php'; ?>
                </div>
            </div>
        </div>

    </div>
</div>

<?php include 'includes/footer.php'; ?>
