<?php
$page_title = 'Inventaris Lokasi';
require_once('includes/load.php');
page_require_level(5);

// Default location to display
$location = isset($_GET['loc']) ? $_GET['loc'] : 'production';
$location_name = '';
$table_name = '';

// Tentukan nama tabel dan nama lokasi berdasarkan parameter GET
switch ($location) {
    case 'reject':
        $table_name = 'reject_area';
        $location_name = 'Reject Area';
        break;
    case 'other':
        $table_name = 'other_area';
        $location_name = 'Area Lainnya';
        break;
    case 'production':
    default:
        $table_name = 'inventory';
        $location_name = 'Produksi';
        break;
}

// Ambil data ringkasan untuk dashboard
$summary_sql = "SELECT SUM(quantity) AS total_quantity, COUNT(part_number) AS total_parts FROM {$table_name}";
$summary_data = find_by_sql($summary_sql);
$total_quantity = $summary_data[0]['total_quantity'] ?? 0;
$total_parts = $summary_data[0]['total_parts'] ?? 0;


// Ambil data inventaris dari tabel yang sesuai, tanpa kolom stok gudang
$sql = "SELECT p.part_number, p.name, i.quantity, i.last_updated
        FROM {$table_name} i
        JOIN products p ON i.part_number = p.part_number
        ORDER BY p.name ASC";
$inventory_data = 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>Inventaris Berdasarkan Lokasi: <?php echo $location_name; ?></span>
                </strong>
            </div>
            <div class="panel-body">
                <div class="row">
                    <div class="col-md-6">
                        <div class="panel panel-primary">
                            <div class="panel-heading">
                                <i class="glyphicon glyphicon-th-list"></i>
                                Total Jumlah Barang
                            </div>
                            <div class="panel-body text-center">
                                <h1><?php echo number_format($total_quantity, 2); ?></h1>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="panel panel-warning">
                            <div class="panel-heading">
                                <i class="glyphicon glyphicon-tag"></i>
                                Total Jenis Part
                            </div>
                            <div class="panel-body text-center">
                                <h1><?php echo number_format($total_parts); ?></h1>
                            </div>
                        </div>
                    </div>
                </div>
                <ul class="nav nav-tabs">
                    <li role="presentation" <?php echo ($location == 'production') ? 'class="active"' : ''; ?>>
                        <a href="inventory_location?loc=production">Produksi</a>
                    </li>
                    <li role="presentation" <?php echo ($location == 'reject') ? 'class="active"' : ''; ?>>
                        <a href="inventory_location?loc=reject">Reject Area</a>
                    </li>
                    <li role="presentation" <?php echo ($location == 'other') ? 'class="active"' : ''; ?>>
                        <a href="inventory_location?loc=other">Area Lainnya</a>
                    </li>
                </ul>

                <div class="table-responsive" style="margin-top: 20px;">
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th class="text-center" style="width: 50px;">#</th>
                                <th> Part Number </th>
                                <th> Nama Produk </th>
                                <th class="text-center"> Stok Lokasi </th>
                                <th class="text-center"> Terakhir Diperbarui </th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (!empty($inventory_data)) : ?>
                                <?php foreach ($inventory_data as $i => $data) : ?>
                                    <tr>
                                        <td class="text-center"><?php echo $i + 1; ?></td>
                                        <td> <?php echo remove_junk($data['part_number']); ?></td>
                                        <td> <?php echo remove_junk($data['name']); ?></td>
                                        <td class="text-center"> <?php echo remove_junk($data['quantity']); ?></td>
                                        <td class="text-center"> <?php echo read_date($data['last_updated']); ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php else : ?>
                                <tr>
                                    <td colspan="5" class="text-center">Tidak ada data inventaris untuk lokasi ini.</td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

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