<?php
session_start();
ob_start();

require_once './config.php';
require_once './_pdo.php';

require_once './config.php';
require_once './src/D2Functions.php';
require_once './src/D2ByteReader.php';
require_once './src/D2BitReader.php';

error_reporting(E_ALL);
set_time_limit(-1);
ini_set('max_input_time', '-1');
ini_set('max_execution_time', '0');

define('DB_FILE', $_SESSION['modname'] . ".db");
try {
    PDO_Connect("sqlite:" . DB_FILE);
} catch (Exception $e) {
    echo "Connection error: " . $e->getMessage();
}

$query = "SELECT * FROM treasureclassex WHERE `Treasure Class` != ''";
$res = array_filter(PDO_FetchAll($query));

$dropChances = [];

foreach ($res as $r) {
    $total = (int) $r['NoDrop'];

    for ($i = 1; $i <= 10; $i++) {
        if (!empty($r["Item$i"])) {
            $total += (int) $r["Prob$i"]; // total will be denominator

            $ic = $r["Item$i"]; // get item code
            $prob = (int) $r["Prob$i"];

            // now every item divided by denominator = dropchance
            $dropChances[$r['Treasure Class']]["nd"] =$r['NoDrop'];
            $dropChances[$r['Treasure Class']]["nodrop"] = round(((int) $r['NoDrop'] / $total) * 100, 2);
            $dropChances[$r['Treasure Class']]["items"]["Item$i"]["code"] = $ic;
            $dropChances[$r['Treasure Class']]["items"]["Item$i"]["prob"] = $prob;
            $dropChances[$r['Treasure Class']]["items"]["Item$i"]["chance"] = round(($prob / $total) * 100, 2);
        }
    }
}

?>


<!DOCTYPE html>
<html>
    <head>
        <title>Drop Chances Table</title>
        <style>
            body {
                font-family: Input;
            }
            table {
                border-collapse: separate;
                border-spacing: 0 15px;
                width: 100%;
            }

            #second-table th {
                border: 1px solid #ccc;
                padding: 2px;
            }
            #second-table tr {
                text-align: center;
            }
            td {
                border-right: 1px solid #ccc;
            }

        /* Color even rows in the first table */
        #first-table .first-table-row:nth-child(even) {
            background-color: #f9f9f9;
        }

        /* Color odd rows in the first table (if you want a different color than even rows) */
        #first-table .first-table-row:nth-child(odd) {
            background-color: #e5e5e5;
        }
        
        #second-table td {
            border: none;
            
        }
        
        #first-table-row {
            margin-bottom: 10px;
        }
        
        </style>
    </head>
    <body>
        <h1>Drop Chances Table</h1>
        <table id="first-table">
            <thead>
                <tr class="first-table-row">
                    <th>Treasure Class</th>
                    <th>NoDrop %</th>
                    <th>Item1</th>
                    <th>Item2</th>
                    <th>Item3</th>
                    <th>Item4</th>
                    <th>Item5</th>
                    <th>Item6</th>
                    <th>Item7</th>
                    <th>Item8</th>
                    <th>Item9</th>
                    <th>Item10</th>
                </tr>
            </thead>
            <tbody>
                <?
                foreach ($dropChances as $treasureClass => $row) {
                    $nodrop = $row['nodrop']; // no drop chance
                    echo "<tr class='first-table-row'>";
                    echo "<td>$treasureClass</td>";
                    echo "<td>NoDrop : {$row["nd"]}<br>Chance: $nodrop</td>";
                    foreach ($row['items'] as $item => $values) {

                        echo "<td>
                                    <table id='second-table'>
                                        <thead>
                                            <tr>
                                                <th>code</th>
                                                <th>prob</th>
                                                <th>chance</th>
                                            </tr>
                                        <thead>
                                        <tr>
                                            <td>{$values["code"]}</td>
                                            <td>{$values["prob"]}</td>
                                            <td>{$values["chance"]}%</td>
                                        </tr>
                                    </table>
                               </td>";
                    }
                    echo "</tr>";
                }
                ?>
            </tbody>
        </table>
    </body>
</html>