<?php

include 'includes/auth.php';
include 'db.php';
include 'includes/header.php';


$invoice_id = $_GET['invoice_id'] ?? 0;


// ===============================
// FETCH INVOICE
// ===============================

$stmt = $conn->prepare("
    SELECT *
    FROM invoices
    WHERE id=? 
    AND invoice_number LIKE 'INV-BLK-%'
");

$stmt->bind_param("i",$invoice_id);
$stmt->execute();

$result = $stmt->get_result();

$invoice = $result->fetch_assoc();


if(!$invoice){

    die("Bulk invoice not found.");

}



// ===============================
// FETCH GUESTS
// ===============================

$guests = $conn->query("
    SELECT id,name 
    FROM guests
    ORDER BY name ASC
");



// ===============================
// UPDATE INVOICE
// ===============================

if(isset($_POST['update_invoice'])){


    $guest_id     = $_POST['guest_id'];
    $description  = $_POST['description'];
    $amount       = $_POST['total_amount'];
    $status       = $_POST['status'];



    $update = $conn->prepare("
        UPDATE invoices SET

        guest_id=?,
        description=?,
        total_amount=?,
        status=?

        WHERE id=?

    ");


    $update->bind_param(
        "isdsi",
        $guest_id,
        $description,
        $amount,
        $status,
        $invoice_id
    );


    if($update->execute()){


        echo "
        <script>
        alert('Bulk invoice updated successfully');
        window.location='combined_invoices.php';
        </script>
        ";

        exit;


    }else{


        echo "
        <div class='alert alert-danger'>
        Error updating invoice
        </div>
        ";

    }


}


?>



<div class="container mt-4">


<h3>
<i class="fa fa-edit"></i>
Edit Bulk Invoice
</h3>


<div class="card shadow mt-3">

<div class="card-body">


<form method="POST">



<div class="row">


<div class="col-md-6 mb-3">

<label>
Invoice Number
</label>

<input type="text"
class="form-control"
value="<?=htmlspecialchars($invoice['invoice_number'])?>"
readonly>

</div>



<div class="col-md-6 mb-3">

<label>
Guest
</label>

<select name="guest_id" class="form-control" required>

<option value="">
Select Guest
</option>


<?php while($g=$guests->fetch_assoc()): ?>


<option value="<?=$g['id']?>"
<?=($g['id']==$invoice['guest_id'])?'selected':''?>>

<?=htmlspecialchars($g['name'])?>

</option>


<?php endwhile; ?>

</select>

</div>




<div class="col-md-6 mb-3">


<label>
Total Amount (MWK)
</label>


<input type="number"
name="total_amount"
class="form-control"
step="0.01"
value="<?=$invoice['total_amount']?>"
required>


</div>




<div class="col-md-6 mb-3">


<label>
Status
</label>


<select name="status" class="form-control">


<option value="Pending"
<?=($invoice['status']=="Pending")?'selected':''?>>
Pending
</option>


<option value="Paid"
<?=($invoice['status']=="Paid")?'selected':''?>>
Paid
</option>


<option value="Partial"
<?=($invoice['status']=="Partial")?'selected':''?>>
Partial
</option>


</select>


</div>





<div class="col-md-12 mb-3">


<label>
Description
</label>


<textarea 
name="description"
class="form-control"
rows="4"><?=htmlspecialchars($invoice['description'])?></textarea>


</div>


</div>



<button type="submit"
name="update_invoice"
class="btn btn-success">

<i class="fa fa-save"></i>
Update Invoice

</button>



<a href="combined_invoices.php"
class="btn btn-secondary">

Cancel

</a>



</form>


</div>

</div>


</div>