<?php
// Start the session and include required files
if (!isset($_SESSION)) {
    session_start();
    include('../includes/dbcon.php');
}

// Redirect if not logged in
if (empty($_SESSION['rm_id'])) {
    header('Location: ../index.php');
    exit;
}

// Initialize variables
$grn_no = 0;

// Fetch the sequence number for GRN
$querydoc = mysqli_query($con, "SELECT * FROM sequence WHERE seq_type='GRN'") or die(mysqli_error($con));
while ($row = mysqli_fetch_array($querydoc)) {
    $grn_no = $row['cno'];
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>SalesMgt | Item Management</title>
    <?php include('../includes/commonheader.php'); ?>
</head>

<body>
    <?php include('../includes/header.php'); ?>
    <?php include('../includes/leftsidebar.php'); ?>

    <div class="main-panel">
        <div class="content-wrapper">

            <!-- Page Header -->
            <div class="page-header">
                <h3 class="page-title">New Item Receipt (GRN)</h3>
            </div>

            <!-- Form Section -->
            <div class="col-12 grid-margin">
                <div class="card">
                    <div class="card-body">
                        <form class="needs-validation" novalidate method="post" action="item_grn_add.php" enctype="multipart/form-data" onsubmit="return validateForm()">
                            <div class="row">
                                <div class="col-md-4">
                                    <div class="form-group row">
                                        <label class="col-sm-3 col-form-label">Item Receipt No:</label>
                                        <div class="col-sm-9">
                                            <input type="text" class="form-control" name="lname" value="<?php echo htmlspecialchars($grn_no); ?>" readonly>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group row">
                                        <label class="col-sm-3 col-form-label">Location Type</label>
                                        <div class="col-sm-9">
                                            <select class="form-control select2" name="loc_id" id="loc_id" required>
                                                <option value="0">~~~ Select ~~~</option>
                                                <?php
                                                $querydoc = mysqli_query($con, "SELECT * FROM stock_locations") or die(mysqli_error($con));
                                                while ($rowd = mysqli_fetch_array($querydoc)) {
                                                    echo '<option value="' . htmlspecialchars($rowd['loc_id']) . '">' . htmlspecialchars($rowd['loc_name']) . '</option>';
                                                }
                                                ?>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group row">
                                        <label class="col-sm-3 col-form-label">GRN Date:</label>
                                        <div class="col-sm-9">
                                            <input type="date" class="form-control" name="gdate"  id="gdate" required>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-4">
                                    <div class="form-group row">
                                        <label class="col-sm-3 col-form-label">Receipt No:</label>
                                        <div class="col-sm-9">
                                            <input type="text" class="form-control" name="supinv" id ="supinv" required>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group row">
                                        <label class="col-sm-3 col-form-label">Note:</label>
                                        <div class="col-sm-9">
                                            <input type="text" class="form-control" name="note">
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <button type="submit" class="btn btn-primary">Save</button>
                            <button type="button" class="btn btn-light" onclick="history.back();">Cancel</button>
                        </form>
                    </div>
                </div>
            </div>

            <!-- Draft Section -->
            <div class="col-lg-12 grid-margin stretch-card">
                <div class="card">
                    <div class="card-body">
                        <h4>History</h4>
                        <button type="button" class="btn btn-warning" onclick="document.location='home.php'"><i class="fas fa-arrow-left"></i> Go Back</button>
 <form method="post" action="#" enctype="multipart/form-data" style="display: inline;">
        <button type="submit" formaction="grn_remove_all.php" class="btn btn-danger" style="background-color: red; border-color: darkred; color: white;">
        <i class="fas fa-trash-alt"></i> Delete All
      </button>
    </form>
                        <div class="table-responsive mt-3">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>No</th>
                                        <th>Date</th>
                                        <th>Location</th>
                                        <th>Receipt No</th>
                                        <th>Edit/Remove</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $rcount = 0;
                                    $query = mysqli_query($con, "SELECT * FROM grn_temp_header LEFT JOIN stock_locations ON grn_temp_header.grand_loc_id=stock_locations.loc_id LEFT JOIN supplier_master ON grn_temp_header.sup_id=supplier_master.sup_id") or die(mysqli_error($con));
                                    while ($row = mysqli_fetch_array($query)) {
                                        $rcount++;
                                        echo '<tr>';
                                        echo '<td>' . htmlspecialchars($rcount) . '</td>';
                                        echo '<td>' . htmlspecialchars($row['grnd_no']) . '</td>';
                                        echo '<td>' . htmlspecialchars($row['grnd_date']) . '</td>';
                                        echo '<td>' . htmlspecialchars($row['loc_name']) . '</td>';
                                          echo '<td>' . htmlspecialchars($row['invoice_no']) . '</td>';
                                        echo '<td>
                                                <form method="post" action="item_grn1.php">
                                                    <input type="hidden" name="grnd_no" value="' . htmlspecialchars($row['grnd_no']) . '">
                                                    <button type="submit" class="btn btn-outline-secondary btn-rounded btn-icon">
                                                        <i class="fas fa-edit text-danger"></i>
                                                    </button>
                                                     <button type="submit" formaction="grn_remove_romovedoc.php" class="btn btn-outline-secondary btn-rounded btn-icon">
            <i class="fas fa-trash text-danger"></i>
            </button>
                                                </form>
                                              </td>';
                                        echo '</tr>';
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
    <?php include('../includes/footer.php'); ?>
    <?php include('../includes/commonjs.php'); ?>

    <script>
        // Form validation
       
       function validateForm() { 

    if($('#supinv').val().trim().length == 0) {
           alert("Please enter the Receipt Number !!!");               
            return false;
    } ////````````````````````````````````````

   if ($('#loc_id').val() === "0") {
        alert("Please select a valid  Stock Location !");
        return false;
    }
    
      if ($('#gdate').val().trim() === "") {
        alert("Please enter the date!");
        return false;
    }

    return true; // If all validations pass, allow form submission
//alevel



 } ///validateForm() { 
       
       //////////////////

        $(function () {
            $('.select2').select2();
        });
        
        
    </script>
</body>

</html>
