<?php
$page_title = 'Semua Transaksi Stok';
require_once('includes/load.php');
page_require_level(5);

// Logika ekspor Excel
if (isset($_POST['export_excel'])) {
    header("Content-Type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=histori_transaksi_stok_" . date('Y-m-d') . ".xls");

    // Query untuk mengambil data
    $sql = "SELECT sm.id, sm.part_number, p.name, sm.movement_type, sm.quantity_changed, sm.movement_date, sm.notes
            FROM stock_movement sm
            JOIN products p ON sm.part_number = p.part_number
            ORDER BY sm.movement_date DESC";
    $transactions_to_export = find_by_sql($sql);

    // Header tabel
    $excel_output = "
    <table>
        <thead>
            <tr>
                <th>No</th>
                <th>Part Number</th>
                <th>Nama Produk</th>
                <th>Jenis Transaksi</th>
                <th>Jumlah</th>
                <th>Tanggal Transaksi</th>
                <th>Catatan</th>
            </tr>
        </thead>
        <tbody>";
    
    // Data tabel
    if (!empty($transactions_to_export)) {
        foreach ($transactions_to_export as $i => $transaction) {
            $excel_output .= "
            <tr>
                <td>" . ($i + 1) . "</td>
                <td>" . remove_junk($transaction['part_number']) . "</td>
                <td>" . remove_junk($transaction['name']) . "</td>
                <td>" . remove_junk($transaction['movement_type']) . "</td>
                <td>" . number_format($transaction['quantity_changed'], 2, '.', '') . "</td>
                <td>" . read_date($transaction['movement_date']) . "</td>
                <td>" . remove_junk($transaction['notes']) . "</td>
            </tr>";
        }
    } else {
        $excel_output .= "<tr><td colspan='7'>Tidak ada data transaksi yang tercatat.</td></tr>";
    }

    $excel_output .= "</tbody></table>";

    echo $excel_output;
    exit;
}

// Ambil semua data pergerakan stok untuk tampilan di halaman
$sql = "SELECT sm.id, sm.part_number, p.name, sm.movement_type, sm.quantity_changed, sm.movement_date, sm.notes
        FROM stock_movement sm
        JOIN products p ON sm.part_number = p.part_number
        ORDER BY sm.movement_date DESC";
$all_transactions = find_by_sql($sql);
?>

<?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">
                <strong>
                    <span class="glyphicon glyphicon-list-alt"></span>
                    <span>Semua Transaksi Pergerakan Stok</span>
                </strong>
                <div class="pull-right">
                    <form action="" method="post" style="display:inline-block;">
                        <button type="submit" name="export_excel" class="btn btn-primary btn-sm">
                            <i class="glyphicon glyphicon-cloud-download"></i> Ekspor ke Excel
                        </button>
                    </form>
                </div>
            </div>
            <div class="panel-body">
                <div class="table-responsive">
                    <table class="table table-bordered table-striped">
                        <thead>
                            <tr>
                                <th class="text-center" style="width: 50px;">#</th>
                                <th> Part Number </th>
                                <th> Nama Produk </th>
                                <th class="text-center"> Jenis Transaksi </th>
                                <th class="text-center"> Jumlah </th>
                                <th class="text-center"> Tanggal Transaksi </th>
                                <th class="text-center"> Catatan </th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (!empty($all_transactions)) : ?>
                                <?php foreach ($all_transactions as $i => $transaction) : ?>
                                    <tr>
                                        <td class="text-center"><?php echo $i + 1; ?></td>
                                        <td><?php echo remove_junk($transaction['part_number']); ?></td>
                                        <td><?php echo remove_junk($transaction['name']); ?></td>
                                        <td class="text-center">
                                            <?php
                                            switch ($transaction['movement_type']) {
                                                case 'inbound':
                                                    echo '<span class="label label-primary">Masuk Gudang</span>';
                                                    break;
                                                case 'outbound':
                                                    echo '<span class="label label-warning">Keluar Gudang</span>';
                                                    break;
                                                case 'transfer':
                                                    echo '<span class="label label-success">Transfer ke Produksi</span>';
                                                    break;
                                                case 'reject':
                                                    echo '<span class="label label-danger">Transfer ke Reject</span>';
                                                    break;
                                                default:
                                                    echo '<span class="label label-info">' . remove_junk($transaction['movement_type']) . '</span>';
                                            }
                                            ?>
                                        </td>
                                        <td class="text-center"><?php echo number_format($transaction['quantity_changed'], 2); ?></td>
                                        <td class="text-center"><?php echo read_date($transaction['movement_date']); ?></td>
                                        <td><?php echo remove_junk($transaction['notes']); ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php else : ?>
                                <tr>
                                    <td colspan="7" class="text-center">Tidak ada data transaksi yang tercatat.</td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

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