<?php
session_start();
if(!isset($_SESSION['user_id'])){
    header("Location: /beauty_pos/login.php");
    exit();
}
include_once "../config/database.php";

$error = "";
$success = "";

if($_SERVER['REQUEST_METHOD'] == "POST"){

    $name = mysqli_real_escape_string($conn, $_POST['expense_name']);
    $category = mysqli_real_escape_string($conn, $_POST['category']);
    $amount = mysqli_real_escape_string($conn, $_POST['amount']);
    $date = $_POST['expense_date'];
    $notes = mysqli_real_escape_string($conn, $_POST['notes']);

    if(empty($name) || empty($category) || empty($amount) || empty($date)){
        $error = "All required fields must be filled";
    } else {

        $insert = mysqli_query($conn, "
            INSERT INTO expenses (expense_name, category, amount, expense_date, notes)
            VALUES ('$name','$category','$amount','$date','$notes')
        ");

        if($insert){
            header("Location: /beauty_pos/expenses/index.php?success=added");
            exit();
        } else {
            $error = "Something went wrong";
        }
    }
}
?>

<?php include_once "../includes/header.php"; ?>
<?php include_once "../includes/sidebar.php"; ?>

<div class="overlay" id="overlay"></div>

<div class="main-content">
<?php include_once "../includes/topbar.php"; ?>

<div class="container-fluid py-4">

    <div class="d-flex justify-content-between align-items-center mb-4">
        <h4 class="fw-bold">Add Expense</h4>
        <a href="/beauty_pos/expenses/index.php" class="btn btn-dark">
            <i class="fa fa-arrow-left"></i> Back
        </a>
    </div>

    <div class="card rounded-4 shadow border-0">
        <div class="card-body">

            <?php if($error){ ?>
                <div class="alert alert-danger"><?php echo $error; ?></div>
            <?php } ?>

            <form method="POST" class="row g-3">

                <div class="col-md-6 col-12">
                    <label class="form-label">Expense Name *</label>
                    <input type="text" name="expense_name" class="form-control" required>
                </div>

                <div class="col-md-6 col-12">
                    <label class="form-label">Category *</label>
                    <input type="text" name="category" class="form-control" placeholder="Rent / Salary / Electricity" required>
                </div>

                <div class="col-md-4 col-12">
                    <label class="form-label">Amount *</label>
                    <input type="number" step="0.01" name="amount" class="form-control" required>
                </div>

                <div class="col-md-4 col-12">
                    <label class="form-label">Date *</label>
                    <input type="date" name="expense_date" class="form-control" required>
                </div>

                <div class="col-md-12 col-12">
                    <label class="form-label">Notes</label>
                    <textarea name="notes" class="form-control" rows="3"></textarea>
                </div>

                <div class="col-12">
                    <button class="btn btn-primary">
                        <i class="fa fa-save"></i> Save Expense
                    </button>
                </div>

            </form>

        </div>
    </div>

</div>

<?php include_once "../includes/footer.php"; ?>
</div>