<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>

<!-- Font Awesome -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.min.js"></script>

<!-- DataTables -->
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/jquery.dataTables.min.css">
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet">


</script>

 
<style>
.search-card {
    background: #ffffff;
    border-radius: 6px;
    box-shadow: 0 3px 8px rgba(0,0,0,0.08);
    padding: 12px;
}

.search-title {
    background: linear-gradient(90deg, #007bff, #0056b3);
    color: #fff;
    padding: 6px 10px;
    font-size: 14px;
    font-weight: 600;
    border-radius: 6px 6px 0 0;
    margin: 0 0 10px 0;   /* remove negative margin */
}

.form-group {
    margin-bottom: 10px;   /* reduce space between fields */
}

.form-group label {
    font-size: 13px;
    margin-bottom: 4px;
}

.form-control {
    height: 32px;   /* reduce input height */
    font-size: 13px;
    padding: 4px 8px;
}

.btn {
    padding: 4px 10px;
    font-size: 13px;
}

.report-card {
    background: #ffffff;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.08);
    padding: 15px;
}

.report-header {
    background: linear-gradient(90deg, #17a2b8, #117a8b);
    color: #fff;
    padding: 10px;
    border-radius: 6px;
    margin-bottom: 15px;
}

.report-header h4 {
    margin: 0;
    font-size: 16px;
}

.report-header p {
    margin: 5px 0 0;
    font-size: 13px;
}

.table-sm th,
.table-sm td {
    padding: 6px !important;
    font-size: 13px;
}
.search-wrapper {
    background: #f4f6f9;
    padding: 15px;
    border-radius: 4px;
}

.search-container {
    background: #f4f6f9;
    padding: 12px;
    border-radius: 4px;
}

.search-heading {
    background: #1c87a6;
    color: #fff;
    padding: 6px 15px;
    font-weight: 600;
    border-radius: 4px;
    text-align: center;
}

.table-responsive {
    overflow-x: auto;
    min-height: .01%;
    height: 500px;
} 

</style>


<div class="col-sm-12 col-md-12 col-lg-12">

    <!-- Main Panel -->
    <div class="row" style="margin-top:1%;">
        <div class="panel panel-default">

            <!-- Panel Heading with Filter -->
            <div class="panel-heading">
                <form method="post" action="<?= base_url('GeneralPurchaseReport/PurchaseReport/search'); ?>">
                    <table width="100%">
                        <tr>

                            <!-- Heading -->
                            <td width="25%">
                                <h4 style="font-family:arial;font-size:18px;">
                                    <b>PURCHASE REPORT 
                                        <span style="color:#fff703;font-weight:bolder;">>></span>
                                    </b>
                                </h4>
                            </td>

                            <!-- Type -->
                            <td width="12%">
                                <select name="type" class="form-control" required>
                                    <option value="0">Select Type</option>
                                    <option value="NULL">ALL</option>
                                    <option value="1">CASH</option>
                                    <option value="2">CREDIT</option>
                                </select>
                            </td>

                            <!-- From Date -->
                            <td width="13%">
                                <input type="date" 
                                       name="fromDate"
                                       value="<?= $fromDate ?? '' ?>"
                                       class="form-control"
                                       required>
                            </td>

                            <!-- To Date -->
                            <td width="13%">
                                <input type="date" 
                                       name="toDate"
                                       value="<?= $toDate ?? '' ?>"
                                       class="form-control"
                                       required>
                            </td>

                            <!-- Search Button -->
                            <!-- Search + Print + Excel -->
<td width="12%" style="padding-left:3px; white-space:nowrap;">

    <!-- Search -->
    <button type="submit" 
            name="search"
            class="btn btn-primary btn-sm"
            style="background-color:#27c5a8; height:33px; padding:5px 8px;">
        <i class="fa fa-search"></i>
    </button>

    <!-- Print -->
    <a href="<?= base_url('GeneralPurchaseReport/PurchaseReport/printReport?fromDate='.($fromDate ?? '').'&toDate='.($toDate ?? '').'&type='.$this->input->post('type')); ?>" 
       target="_blank"
       class="btn btn-success btn-sm"
       style="background-color:#27c5a8; height:33px; padding:5px 8px; margin-left:2px;"
       title="Print">
        <i class="fa fa-print"></i>
    </a>

    <!-- Excel -->
    <a href="<?= base_url('GeneralPurchaseReport/PurchaseReport/exportCsv?fromDate='.($fromDate ?? '').'&toDate='.($toDate ?? '').'&type='.$this->input->post('type')); ?>" 
        class="btn btn-danger btn-sm"
       style="background-color:#27c5a8; height:33px; padding:5px 8px; margin-left:2px;"
       title="Excel">
        <i class="fa fa-file-excel-o"></i>
    </a>

</td>


                        </tr>
                    </table>
                </form>
            </div>

        </div>
    </div>

</div>


            <!-- Panel Body -->
            <div class="panel-body">

                <?php if(isset($_POST['search'])){ ?>

                <!-- Report Header -->
                <div class="text-center" style="margin-bottom:15px;">
                    <h4><b>Purchase Report Details</b></h4>
                    <p>
                        <strong>Type:</strong> <?= $typeOfTransaction ?> |
                        <strong>Date:</strong> <?= $fromDate ?> to <?= $toDate ?>
                    </p>
                </div>

                <!-- Table -->
                <div class="table-responsive">
                    <table id="my_table" 
                           class="table table-bordered table-striped">

                        <thead>
                            <tr>
                                <th class="text-center">Invoice No</th>
                                <th class="text-center">Invoice Date</th>
                                <th class="text-center">Vendor Name</th>
                                <th class="text-center">Transaction</th>
                                <th class="text-center">Bill Amount</th>
                                <th class="text-center">Discount</th>
                                <th class="text-center">After Discount</th>
                                <th class="text-center">VAT</th>
                                <th class="text-center">Total</th>
                            </tr>
                        </thead>

                        <tbody>
                            <?php
                            if($results!=""){
                                echo $results;
                            } else {
                                echo '<tr>
                                        <td colspan="9" class="text-center text-danger">
                                            No Data Available
                                        </td>
                                      </tr>';
                            }
                            ?>
                        </tbody>

                    </table>
                </div>

                <?php } ?>

            </div>
        </div>
    </div>
</div>





        

