<?php
require("../connection.php");
include("../user.php");
// // /ini_set('display_errors', 1);
// error_reporting(E_ALL);

$user_id = $_SESSION['user'];
$username = $_SESSION['user_name'];
$branch_id = $_SESSION['branch_id'];
$ho_access = $_SESSION['ha'];
$brch = $_REQUEST['brch'];
$inoutoption = $_POST['inoutoption'] ?? 'all';
$deptoption = $_POST['deptoption'] ?? 'all';
$brnchoption = $_POST['brnchoption'] ?? 'all';
$divsnoption = $_POST['divsnoption'] ?? 'all';
$from_date = isset($_POST['from_date']) ? $_POST['from_date'] : date('Y-m-01');
$to_date = isset($_POST['to_date']) ? $_POST['to_date'] : date('Y-m-d');
?>

<?php
$b = mysqli_query($con, "select id,branch_id,user_id from branch_mapping where user_id='$user_id'");
$bch = mysqli_fetch_array($b);
$brnh = $bch['branch_id'];

$c = mysqli_query($con, "select id,location from location_master where id='$brnh'");
$bh = mysqli_fetch_array($c);
$br = $bh['location'];
?>

<?php
$cat_name = mysqli_query($con, "select id,location from location_master where id='$brch'");
$catname = mysqli_fetch_array($cat_name);
?>


<table id="example" class="table table-striped table-bordered">
    <thead>
        <tr>
            <th>S.No</th>
            <th>MATERIAL NAME</th>
            <th>TYPE</th>
            <th>DATE</th>
            <th>QUANTITY</th>
            <th>VALUE</th>
            <th>TOTAL AMOUNT</th>
            <th>EXPIRY DATE / TICKET NO</th>
            <th>FA SERIAL NO / TECHNICIAN</th>
            <th>REMARKS / PURPOSE</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $s = 1;
        if ($inoutoption == 'all' || $inoutoption == 'inward') {
            

            // --- INWARD ENTRIES ---
            $inward_query = ($ho_access == "yes") ? "select * from item_master" : "select * from item_master";
            $inward_query .= " where po_date between '$from_date' and '$to_date'";
            if($deptoption != 'all'){
                $inward_query .= " and department = '$deptoption'";
            }
            if($brnchoption != 'all'){
                $inward_query .= " and branch = '$brnchoption'";
            }
            if($divsnoption != 'all'){
                $inward_query .= " and division = '$divsnoption'";
            }
            $inward_result = mysqli_query($con, $inward_query);

            if (!$inward_result) {
                die("Error in inward query: " . mysqli_error($con));
            }
            echo "<!-- Inward rows: " . mysqli_num_rows($inward_result) . " -->";

            while ($type = mysqli_fetch_array($inward_result)) {
                $mat_id = $type['material_name'];
                $mat_query = mysqli_query($con, "SELECT category_type FROM category_master WHERE id = '$mat_id'");
                $mat_row = mysqli_fetch_array($mat_query);
                ?>
                <tr>
                    <td><?= $s++; ?></td>
                    <td><?= $mat_row['category_type'] ?? ''; ?></td>
                    <td class="text-success">INWARD</td>
                    <td><?= $type['po_date'] ?? ''; ?></td>
                    <td><?= $type['quantity'] ?? ''; ?></td>
                    <td><?= $type['value'] ?? ''; ?></td>
                    <td><?= $type['total_amount'] ?? ''; ?></td>
                    <td><?= $type['expiry_date'] ?? ''; ?></td>
                    <td><?= $type['fa_serial_no'] ?? ''; ?></td>
                    <td><?= $type['remarks'] ?? ''; ?></td>
                </tr>

                <?php
            }
        }
        ?>

        <!-- OUTWARD ENTRIES -->
        <?php

        if ($inoutoption == 'all' || $inoutoption == 'outward') {
            $outward_query = ($ho_access == "yes") ? "select * from outward_master" : "select * from outward_master";
            $outward_query .= " where so_date between '$from_date' and '$to_date'";
            if($deptoption != 'all'){
                $outward_query .= " and department = '$deptoption'";
            }
            if($brnchoption != 'all'){
                $outward_query .= " and branch = '$brnchoption'";
            }
            if($divsnoption != 'all'){
                $outward_query .= " and division = '$divsnoption'";
            }
            $outward_result = mysqli_query($con, $outward_query);
            
            if (!$outward_result) {
                die("Error in outward query: " . mysqli_error($con));
            }
            echo "<!-- Outward rows: " . mysqli_num_rows($outward_result) . " -->";

            while ($out = mysqli_fetch_array($outward_result)) {
                echo "<!-- Outward row id: " . $out['id'] . " -->";

                $mat_id = $out['material_name'];
                $mat_query = mysqli_query($con, "SELECT category_type FROM category_master WHERE id = '$mat_id'");
                $mat_row = mysqli_fetch_array($mat_query);
                ?>
                <tr>
                    <td><?= $s++; ?></td>
                    <td><?= $mat_row['category_type'] ?? ''; ?></td>
                    <td class="text-danger">OUTWARD</td>
                    <td><?= $out['so_date'] ?? ''; ?></td>
                    <td><?= $out['qty'] ?? ''; ?></td>
                    <td><?= $out['value'] ?? ''; ?></td>
                    <td><?= $out['total_amount'] ?? ''; ?></td>
                    <td><?= $out['ticket_no'] ?? ''; ?></td>
                    <td><?= $out['technician'] ?? ''; ?></td>
                    <td><?= $out['purpose'] ?? ''; ?></td>
                </tr>

            <?php }
        } ?>
    </tbody>
</table>