<?php

require_once('includes/load.php');
page_require_level(7);

// Menampilkan pesan pemberitahuan
$session->msg('i', "Sistem ini mendukung transfer antar-gudang multi-area. Hubungi administrator untuk mengaktifkan fitur ini.");

/**
 * Fungsi Dummy: Tidak melakukan perubahan database.
 * Hanya mensimulasikan proses transfer dan konfirmasi.
 * Menampilkan pesan sukses sebagai notifikasi.
 */
function dummy_transfer($part_numbers, $quantities, $location) {
    global $session;
    // Logika dummy: Cukup cek apakah data ada
    if (empty($part_numbers) || empty($quantities) || empty($location)) {
        $session->msg('d', "Transfer gagal: Pastikan semua data terisi.");
        return false;
    }
    // Jika semua data ada, anggap transfer berhasil
    $session->msg('s', "Simulasi transfer berhasil dibuat dan menunggu konfirmasi.");
    return true;
}

function dummy_confirm_transfer($transfer_id) {
    global $session;
    // Logika dummy: Cukup cek apakah ID ada
    if (empty($transfer_id)) {
        $session->msg('d', "Konfirmasi transfer gagal: ID tidak valid.");
        return false;
    }
    // Anggap konfirmasi berhasil
    $session->msg('s', "Simulasi konfirmasi transfer berhasil!");
    return true;
}

// Menangani permintaan transfer produk
if (isset($_POST['transfer'])) {
    $part_numbers = $_POST['part_number'];
    $quantities = $_POST['quantity'];
    $location = $_POST['location'];
    
    dummy_transfer($part_numbers, $quantities, $location);
    redirect('wh_transfer.php');
}

// Menangani permintaan konfirmasi transfer
if (isset($_GET['confirm_transfer'])) {
    $transfer_id = (int)$_GET['confirm_transfer'];
    
    dummy_confirm_transfer($transfer_id);
    redirect('wh_transfer.php');
}

// Fungsi find_by_field yang sudah ada
function find_by_field($table, $field, $value)
{
    global $db;
    $sql = "SELECT * FROM {$table} WHERE {$field} = '{$value}' LIMIT 1";
    $result = $db->query($sql);
    return $result->fetch_assoc();
}

?>

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

<style>
    /* Styling Umum untuk tampilan yang lebih modern */
    .panel {
        border-radius: 8px;
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        border: none;
    }

    .panel-heading {
        background-color: #34495e; /* Warna biru gelap */
        color: white;
        font-weight: bold;
        border-top-left-radius: 8px;
        border-top-right-radius: 8px;
        padding: 15px;
        font-size: 18px;
    }

    /* Styling Form */
    .form-group label {
        font-weight: 600;
        color: #555;
    }
    
    .form-control {
        border-radius: 5px;
        border-color: #ddd;
    }

    .btn-primary, .btn-success, .btn-danger {
        border-radius: 50px;
        font-weight: bold;
        transition: transform 0.2s ease-in-out;
    }

    .btn-primary:hover, .btn-success:hover, .btn-danger:hover {
        transform: translateY(-2px);
    }
    
    /* Tombol Hapus */
    .remove-product {
        margin-top: 25px; /* Sesuaikan agar sejajar dengan input */
    }

    /* Styling Tabel */
    .table thead th {
        background-color: #f8f9fa;
        color: #495057;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        font-size: 14px;
        border-bottom: 2px solid #dee2e6;
    }
    
    .table tbody td {
        vertical-align: middle;
    }

    .table-bordered th, .table-bordered td {
        border-color: #e9ecef;
    }

    /* Styling Notifikasi */
    .alert {
        border-radius: 8px;
        font-size: 16px;
        font-weight: 500;
        padding: 15px;
        margin-bottom: 20px;
    }
</style>

<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">
                <h3 class="panel-title">📝 Transfer Products</h3>
            </div>
            <div class="panel-body">
                <form method="post" action="wh_transfer.php">
                    <div id="product-fields">
                        <div class="row product-group align-items-end">
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label for="part_number">Part Number</label>
                                    <input type="text" class="form-control" name="part_number[]" placeholder="Enter part number" required>
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label for="quantity">Quantity to Transfer</label>
                                    <input type="number" step="0.01" class="form-control" name="quantity[]" placeholder="Enter quantity" required>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label for="remark">Remark</label>
                                    <input type="text" class="form-control" name="remark[]" placeholder="Enter remark">
                                </div>
                            </div>
                            <div class="col-md-1">
                                <div class="form-group mb-0">
                                    <button type="button" class="btn btn-danger remove-product w-100" style="display:none;"><i class="fa fa-times-circle"></i></button>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="form-group mt-3">
                        <button type="button" id="add-product" class="btn btn-success">
                            <i class="fa fa-plus-circle"></i> Add Another Product
                        </button>
                    </div>

                    <div class="form-group">
                        <label for="location">Select Warehouse</label>
                        <select class="form-control" name="location" required>
                            <option value="">Select Warehouse</option>
                            <option value="wh_jakarta">WH Jakarta</option>
                            <option value="wh_makassar">WH Makassar</option>
                            <option value="wh_bali">WH Bali</option>
                            <option value="wh_semarang">WH Semarang</option>
                            <option value="wh_bandung">WH Bandung</option>
                        </select>
                    </div>

                    <div class="form-group text-center">
                        <button type="submit" name="transfer" class="btn btn-primary btn-lg btn-block">
                            <i class="fa fa-truck"></i> Transfer
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

---

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">⏳ Pending Transfers</h3>
            </div>
            <div class="panel-body">
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>Part Number</th>
                            <th>Quantity</th>
                            <th>Location</th>
                            <th>Remark</th>
                            <th>Transfer Date</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        // Data dummy untuk tabel pending transfers
                        $dummy_transfers = [
                            ['id' => 1, 'part_number' => 'PN-001', 'quantity' => 10, 'location' => 'WH Makassar', 'remark' => 'Untuk proyek X', 'transfer_date' => '2025-08-16'],
                            ['id' => 2, 'part_number' => 'PN-002', 'quantity' => 5, 'location' => 'WH Bali', 'remark' => 'Inventaris bulanan', 'transfer_date' => '2025-08-15'],
                        ];

                        if (!empty($dummy_transfers)) {
                            foreach ($dummy_transfers as $transfer) {
                                echo "<tr>";
                                echo "<td>{$transfer['part_number']}</td>";
                                echo "<td>{$transfer['quantity']}</td>";
                                echo "<td>{$transfer['location']}</td>";
                                echo "<td>{$transfer['remark']}</td>";
                                echo "<td>{$transfer['transfer_date']}</td>";
                                echo "<td><a href='wh_transfer.php?confirm_transfer={$transfer['id']}' class='btn btn-success btn-sm'>Confirm</a></td>";
                                echo "</tr>";
                            }
                        } else {
                            echo "<tr><td colspan='6' class='text-center'>Tidak ada transfer yang tertunda.</td></tr>";
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<script>
    document.getElementById('add-product').addEventListener('click', function() {
        var productFields = document.getElementById('product-fields');
        var productGroupCount = productFields.getElementsByClassName('product-group').length;

        var newField = document.createElement('div');
        newField.className = 'row product-group align-items-end';
        newField.innerHTML = `
            <div class="col-md-4">
                <div class="form-group">
                    <label for="part_number">Part Number</label>
                    <input type="text" class="form-control" name="part_number[]" placeholder="Enter part number" required>
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label for="quantity">Quantity to Transfer</label>
                    <input type="number" step="0.01" class="form-control" name="quantity[]" placeholder="Enter quantity" required>
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-group">
                    <label for="remark">Remark</label>
                    <input type="text" class="form-control" name="remark[]" placeholder="Enter remark">
                </div>
            </div>
            <div class="col-md-1">
                <div class="form-group mb-0">
                    <button type="button" class="btn btn-danger remove-product w-100"><i class="fa fa-times-circle"></i></button>
                </div>
            </div>
        `;
        productFields.appendChild(newField);

        if (productGroupCount > 0) {
            var removeButtons = productFields.querySelectorAll('.remove-product');
            removeButtons.forEach(button => button.style.display = 'block');
        }
    });

    document.getElementById('product-fields').addEventListener('click', function(e) {
        if (e.target.closest('.remove-product')) {
            let button = e.target.closest('.remove-product');
            let productGroup = button.closest('.product-group');
            productGroup.remove();

            var productGroupCount = document.getElementById('product-fields').getElementsByClassName('product-group').length;
            if (productGroupCount === 1) {
                document.querySelector('.remove-product').style.display = 'none';
            }
        }
    });
</script>

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