<?php
    $page_title = 'Sales List';
    require_once('includes/load.php');
    page_require_level(4);

    // Proses pencarian jika formulir pencarian dikirimkan
    if (isset($_POST['search'])) {
        $search_query = $_POST['search_query'];

        // Membangun query pencarian
        $sql = "SELECT * FROM sales_list WHERE delivery_date LIKE '%{$search_query}%' OR no_so LIKE '%{$search_query}%' OR client LIKE '%{$search_query}%' OR product LIKE '%{$search_query}%'";
        $sales = find_by_sql($sql);
    } elseif (isset($_POST['filter'])) {
        $start_date = $_POST['start_date'];
        $end_date = $_POST['end_date'];

        // Membangun query filter berdasarkan range tanggal
        $sql = "SELECT * FROM sales_list WHERE delivery_date BETWEEN '{$start_date}' AND '{$end_date}'";
        $sales = find_by_sql($sql);
    } else {
        // Query untuk mendapatkan semua data penjualan dari tabel sales_list jika tidak ada pencarian atau filter
        $sales = find_all('sales_list');
    }
?>

<?php include_once('layouts/header.php'); ?>

<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading clearfix">
                <div class="pull-left">
                    <h4 class="panel-title">Sales List</h4>
                </div>
                <div class="pull-right">
                    <form method="post" action="sales_list.php" class="form-inline">
                        <div class="form-group">
                            <input type="text" class="form-control" name="search_query" placeholder="Search..." value="<?php echo isset($search_query) ? $search_query : ''; ?>">
                        </div>
                        <button type="submit" name="search" class="btn btn-default">Search</button>
                    </form>
                </div>
            </div>
            <div class="panel-body">
                <div class="row">
                    <div class="col-md-12">
                        <form method="post" action="sales_list.php" class="form-inline">
                            <div class="form-group">
                                <label for="start_date">Start Date:</label>
                                <input type="date" class="form-control" name="start_date" id="start_date">
                            </div>
                            <div class="form-group">
                                <label for="end_date">End Date:</label>
                                <input type="date" class="form-control" name="end_date" id="end_date">
                            </div>
                            <button type="submit" name="filter" class="btn btn-primary">Filter</button>
                        </form>
                    </div>
                </div>
                <div class="table-responsive">
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th>Delivery Date</th>
                                <th>SO Number</th>
                                <th>Client</th>
                                <th>Product</th>
                                <th>Part Number</th>
                                <th>Description</th>
                                <th>Quantity</th>
                                <th>Unit Price</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($sales as $sale) : ?>
                                <tr>
                                    <td><?php echo $sale['delivery_date']; ?></td>
                                    <td><?php echo $sale['no_so']; ?></td>
                                    <td><?php echo $sale['client']; ?></td>
                                    <td><?php echo $sale['product']; ?></td>
                                    <td><?php echo $sale['part_number']; ?></td>
                                    <td><a href="#" data-toggle="modal" data-target="#descriptionModal_<?php echo $sale['id']; ?>">View Desc</a></td>
                                    <td><?php echo $sale['quantity']; ?></td>
                                    <td><?php echo $sale['unit_price']; ?></td>
                                    <td>
                                        <a href="edit_sales_list.php?id=<?php echo $sale['id']; ?>" class="btn btn-warning btn-xs">Edit</a>
                                    </td>
                                </tr>
                                <!-- Modal -->
                                <div class="modal fade" id="descriptionModal_<?php echo $sale['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="descriptionModalLabel_<?php echo $sale['id']; ?>" aria-hidden="true">
                                    <div class="modal-dialog" role="document">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <h5 class="modal-title" id="descriptionModalLabel_<?php echo $sale['id']; ?>">Description</h5>
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                    <span aria-hidden="true">&times;</span>
                                                </button>
                                            </div>
                                            <div class="modal-body">
                                                <?php echo $sale['description']; ?>
                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include_once('layouts/footer.php'); ?>
