<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);
require_once('includes/load.php');

page_require_level(3);

// Cek parameter picking_no
if(!isset($_GET['picking_no'])) {
    $session->msg('d', 'Parameter picking_no tidak ditemukan');
    redirect('picking_list.php');
}

$picking_no = $db->escape($_GET['picking_no']);

// Ambil informasi dasar picking list
$picking_info = $db->query("
    SELECT DISTINCT 
        mf.picking_no,
        mf.part_number,
        p.name as part_name
    FROM material_forecasts mf
    LEFT JOIN products p ON mf.part_number = p.part_number
    WHERE mf.picking_no = '{$picking_no}'
    LIMIT 1
");

if($db->num_rows($picking_info) == 0) {
    $session->msg('d', 'Picking number tidak ditemukan');
    redirect('picking_list.php');
}

$header = $db->fetch_assoc($picking_info);

// Ambil daftar komponen/raw material
$components = $db->query("
    SELECT 
        mf.component as part_number,
        p.name as component_name,
        p.uom,
        mf.quantity_needed
    FROM material_forecasts mf
    LEFT JOIN products p ON mf.component = p.part_number
    WHERE mf.picking_no = '{$picking_no}'
    ORDER BY p.name
");

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

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading clearfix">
                <strong>
                    <span class="glyphicon glyphicon-list"></span>
                    <span>Detail Raw Material - Picking No: <?php echo $header['picking_no']; ?></span>
                </strong>
                <div class="pull-right">
                    <a href="picking_list.php" class="btn btn-default btn-sm">
                        <span class="glyphicon glyphicon-arrow-left"></span> Kembali
                    </a>
                </div>
            </div>
            <div class="panel-body">
                                <div class="row">
                    <div class="col-md-6">
                        <table class="table table-condensed">
                            <tr>
                                <th width="30%">Picking No</th>
                                <td><?php echo $header['picking_no']; ?></td>
                            </tr>
                            <tr>
                                <th>Part Number</th>
                                <td><?php echo $header['part_number']; ?></td>
                            </tr>
                            <tr>
                                <th>Part Name</th>
                                <td><?php echo $header['part_name']; ?></td>
                            </tr>
                        </table>
                    </div>
                </div>

                                <h4>Daftar Raw Material/Komponen</h4>
                <div class="table-responsive">
                    <table class="table table-bordered table-striped">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>Part Number</th>
                                <th>Nama Komponen</th>
                                <th>Satuan</th>
                                <th>Qty Dibutuhkan</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php 
                            $no = 1;
                            $total_qty = 0;
                            
                            if($db->num_rows($components) > 0): 
                                while($comp = $db->fetch_assoc($components)): 
                                    $total_qty += $comp['quantity_needed'];
                            ?>
                            <tr>
                                <td><?php echo $no++; ?></td>
                                <td><?php echo $comp['part_number']; ?></td>
                                <td><?php echo $comp['component_name']; ?></td>
                                <td><?php echo $comp['uom']; ?></td>
                                <td class="text-right"><?php echo number_format($comp['quantity_needed'], 2); ?></td>
                            </tr>
                            <?php endwhile; ?>
                            <tr>
                                <td colspan="4"><strong>Total:</strong></td>
                                <td class="text-right"><strong><?php echo number_format($total_qty, 2); ?></strong></td>
                            </tr>
                            <?php else: ?>
                            <tr><td colspan="5" class="text-center">Tidak ada data komponen</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

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