<?php
// File: topsis_restock_view.php

// Include the TOPSIS function
require_once 'topsis_functions.php';

// Database connection
$host = "localhost";
$username = "root";
$password = "";
$database = "ayula_store";

$conn = mysqli_connect($host, $username, $password, $database);
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// Default criteria weights
$default_weights = [
    'stock_level' => 0.4,    // Lower stock is more urgent (40% importance)
    'price_value' => 0.3,    // Higher price might indicate higher priority (30% importance)
    'turnover_rate' => 0.3   // Higher turnover rate means faster selling (30% importance)
];

// Check if form was submitted with custom weights
$weights = $default_weights;
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit_weights'])) {
    $weights = [
        'stock_level' => floatval($_POST['weight_stock']),
        'price_value' => floatval($_POST['weight_price']),
        'turnover_rate' => floatval($_POST['weight_turnover'])
    ];
    
    // Normalize weights to ensure they sum to 1
    $total = array_sum($weights);
    if ($total > 0) {
        foreach ($weights as $key => $value) {
            $weights[$key] = $value / $total;
        }
    } else {
        $weights = $default_weights;
    }
}

// Get restocking priorities using TOPSIS
$restock_priorities = restockWithTOPSIS($conn, $weights);

// Check if any error occurred
$error_message = "";
if (isset($restock_priorities['error'])) {
    $error_message = $restock_priorities['error'];
    $restock_priorities = [];
}

// Calculate total products and those needing restocking (stock < 10)
$total_products = count($restock_priorities);
$low_stock_count = 0;
foreach ($restock_priorities as $product) {
    if ($product['stok'] < 10) {
        $low_stock_count++;
    }
}

// Get current date and time
$current_date = date('Y-m-d H:i:s');

// Page title
$page_title = "Analisis Restok dengan Metode TOPSIS";
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $page_title; ?></title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css">
    <style>
        .priority-high {
            background-color: #ffdddd !important;
        }
        .priority-medium {
            background-color: #ffffdd !important;
        }
        .priority-low {
            background-color: #ddffdd !important;
        }
        .weights-form {
            background-color: #f8f9fa;
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 20px;
        }
        .dashboard-card {
            transition: transform 0.3s;
        }
        .dashboard-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 20px rgba(0,0,0,0.1);
        }
        #weightChart {
            max-width: 300px;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    
    <div class="container mt-4">
        <h1 class="mb-4 text-center"><?php echo $page_title; ?></h1>
        
        <div class="row mb-4">
            <div class="col-md-4">
                <div class="card text-white bg-primary dashboard-card h-100">
                    <div class="card-body text-center">
                        <h5 class="card-title">Total Produk</h5>
                        <h2><?php echo $total_products; ?></h2>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card text-white bg-danger dashboard-card h-100">
                    <div class="card-body text-center">
                        <h5 class="card-title">Produk Stok Rendah</h5>
                        <h2><?php echo $low_stock_count; ?></h2>
                        <small>Stok < 10</small>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card text-white bg-info dashboard-card h-100">
                    <div class="card-body text-center">
                        <h5 class="card-title">Tanggal Analisis</h5>
                        <p><?php echo $current_date; ?></p>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="row mb-4">
            <div class="col-md-8">
                <div class="weights-form">
                    <h4>Kustomisasi Bobot Kriteria</h4>
                    <form method="POST" action="" class="row g-3">
                        <div class="col-md-4">
                            <label for="weight_stock" class="form-label">Bobot Stok:</label>
                            <input type="number" class="form-control" id="weight_stock" name="weight_stock" 
                                   min="0" max="10" step="0.1" value="<?php echo $weights['stock_level'] * 10; ?>">
                            <small class="text-muted">Nilai lebih tinggi = stok rendah lebih penting</small>
                        </div>
                        <div class="col-md-4">
                            <label for="weight_price" class="form-label">Bobot Harga:</label>
                            <input type="number" class="form-control" id="weight_price" name="weight_price" 
                                   min="0" max="10" step="0.1" value="<?php echo $weights['price_value'] * 10; ?>">
                            <small class="text-muted">Nilai lebih tinggi = harga tinggi lebih penting</small>
                        </div>
                        <div class="col-md-4">
                            <label for="weight_turnover" class="form-label">Bobot Perputaran:</label>
                            <input type="number" class="form-control" id="weight_turnover" name="weight_turnover" 
                                   min="0" max="10" step="0.1" value="<?php echo $weights['turnover_rate'] * 10; ?>">
                            <small class="text-muted">Nilai lebih tinggi = penjualan cepat lebih penting</small>
                        </div>
                        <div class="col-12 mt-3">
                            <button type="submit" name="submit_weights" class="btn btn-primary">Terapkan Bobot</button>
                            <button type="button" class="btn btn-secondary" onclick="resetWeights()">Reset ke Default</button>
                        </div>
                    </form>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card h-100">
                    <div class="card-header">
                        Distribusi Bobot
                    </div>
                    <div class="card-body">
                        <canvas id="weightChart"></canvas>
                    </div>
                </div>
            </div>
        </div>
        
        <?php if ($error_message): ?>
            <div class="alert alert-danger" role="alert">
                <?php echo $error_message; ?>
            </div>
        <?php else: ?>
            <div class="card mb-4">
                <div class="card-header">
                    <h4>Hasil Analisis TOPSIS</h4>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover table-striped">
                            <thead class="table-dark">
                                <tr>
                                    <th>Prioritas</th>
                                    <th>ID Barang</th>
                                    <th>Nama Barang</th>
                                    <th>Stok Saat Ini</th>
                                    <th>Harga</th>
                                    <th>Skor TOPSIS</th>
                                    <th>Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($restock_priorities as $product): 
                                    // Determine priority class based on TOPSIS score
                                    $priority_class = '';
                                    if ($product['topsis_score'] > 0.7) {
                                        $priority_class = 'priority-high';
                                    } elseif ($product['topsis_score'] > 0.4) {
                                        $priority_class = 'priority-medium';
                                    } else {
                                        $priority_class = 'priority-low';
                                    }
                                ?>
                                    <tr class="<?php echo $priority_class; ?>">
                                        <td><?php echo $product['rank']; ?></td>
                                        <td><?php echo $product['id_barang']; ?></td>
                                        <td><?php echo $product['nama_barang']; ?></td>
                                        <td><?php echo $product['stok']; ?></td>
                                        <td>Rp <?php echo number_format($product['harga'], 0, ',', '.'); ?></td>
                                        <td><?php echo number_format($product['topsis_score'], 4); ?></td>
                                        <td>
                                            <button class="btn btn-sm btn-primary" onclick="openRestockModal(<?php echo $product['id_barang']; ?>, '<?php echo $product['nama_barang']; ?>')">
                                                Restok
                                            </button>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            
            <!-- Export Options -->
            <div class="mb-4">
                <h5>Ekspor Data</h5>
                <button class="btn btn-success me-2" onclick="exportTable('csv')">Ekspor ke CSV</button>
                <button class="btn btn-danger me-2" onclick="exportTable('pdf')">Ekspor ke PDF</button>
                <button class="btn btn-primary" onclick="printTable()">Cetak</button>
            </div>
        <?php endif; ?>
    </div>
    
    <!-- Restock Modal -->
    <div class="modal fade" id="restockModal" tabindex="-1" aria-labelledby="restockModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="restockModalLabel">Restok Produk</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <form id="restockForm">
                        <input type="hidden" id="productId" name="productId">
                        <div class="mb-3">
                            <label for="productName" class="form-label">Nama Produk</label>
                            <input type="text" class="form-control" id="productName" readonly>
                        </div>
                        <div class="mb-3">
                            <label for="restockAmount" class="form-label">Jumlah Restok</label>
                            <input type="number" class="form-control" id="restockAmount" name="restockAmount" min="1" required>
                        </div>
                        <div class="mb-3">
                            <label for="supplierNote" class="form-label">Catatan Supplier</label>
                            <textarea class="form-control" id="supplierNote" name="supplierNote" rows="3"></textarea>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                    <button type="button" class="btn btn-primary" onclick="submitRestock()">Proses Restok</button>
                </div>
            </div>
        </div>
    </div>

    <!-- JavaScript -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    
    <script>
        // Initialize modal
        const restockModal = new bootstrap.Modal(document.getElementById('restockModal'));
        
        // Open restock modal
        function openRestockModal(productId, productName) {
            document.getElementById('productId').value = productId;
            document.getElementById('productName').value = productName;
            document.getElementById('restockAmount').value = '10'; // Default value
            document.getElementById('supplierNote').value = '';
            restockModal.show();
        }
        
        // Submit restock form
        function submitRestock() {
            const productId = document.getElementById('productId').value;
            const amount = document.getElementById('restockAmount').value;
            const note = document.getElementById('supplierNote').value;
            
            // Here you would typically send an AJAX request to update the database
            alert(`Produk ID: ${productId} akan direstok dengan jumlah: ${amount}`);
            
            // In a real implementation, you would use fetch or XMLHttpRequest to send to server
            // Example:
            /*
            fetch('process_restock.php', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: `productId=${productId}&amount=${amount}&note=${encodeURIComponent(note)}`
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert('Restok berhasil!');
                    window.location.reload();
                } else {
                    alert('Error: ' + data.message);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('Terjadi kesalahan saat memproses restok.');
            });
            */
            
            restockModal.hide();
        }
        
        // Reset weights to default
        function resetWeights() {
            document.getElementById('weight_stock').value = '4';
            document.getElementById('weight_price').value = '3';
            document.getElementById('weight_turnover').value = '3';
        }
        
        // Export table to CSV/PDF
        function exportTable(format) {
            alert(`Mengekspor tabel ke format ${format.toUpperCase()}`);
            // Implement actual export functionality here
        }
        
        // Print table
        function printTable() {
            window.print();
        }
        
        // Initialize weight chart
        const ctx = document.getElementById('weightChart').getContext('2d');
        const weightChart = new Chart(ctx, {
            type: 'pie',
            data: {
                labels: ['Stok', 'Harga', 'Perputaran'],
                datasets: [{
                    data: [
                        <?php echo $weights['stock_level']; ?>, 
                        <?php echo $weights['price_value']; ?>, 
                        <?php echo $weights['turnover_rate']; ?>
                    ],
                    backgroundColor: [
                        'rgba(255, 99, 132, 0.7)',
                        'rgba(54, 162, 235, 0.7)',
                        'rgba(255, 206, 86, 0.7)'
                    ],
                    borderColor: [
                        'rgba(255, 99, 132, 1)',
                        'rgba(54, 162, 235, 1)',
                        'rgba(255, 206, 86, 1)'
                    ],
                    borderWidth: 1
                }]
            },
            options: {
                responsive: true,
                plugins: {
                    legend: {
                        position: 'bottom',
                    },
                    tooltip: {
                        callbacks: {
                            label: function(context) {
                                const label = context.label || '';
                                const value = context.formattedValue || '';
                                return `${label}: ${value}`;
                            }
                        }
                    }
                }
            }
        });
    </script>
</body>
</html>