<style>
    .filter-container {
        width: 100%;
        overflow-x: auto;
        white-space: nowrap;
        margin-bottom: 10px;
        padding-bottom: 10px; /* Add some space for scrollbar */
    }
    
    .filters-wrapper {
        display: inline-block;
        min-width: 80%;
    }
    
    .filters {
        display: inline-flex;
        gap: 7px;
        align-items: center;
        padding: 7px 0;
    }

    label {
        font-weight: bold;
        font-size: 15px;
        white-space: nowrap;
    }

    select,
    input[type="text"],
    input[type="date"] {
        font-size: 15px;
        width: 180px;
        padding: 3px;
        border-radius: 5px;
        border: 1px solid #ccc;
    }
    
    button[type="submit"] {
        background-color: blue;
        color: white;
        padding: 3px 20px;
        border: none;
        border-radius: 5px;
        cursor: pointer;
        font-size: 15px;
        white-space: nowrap;
    }
    
    /* Optional: Style the scrollbar */
    .filter-container::-webkit-scrollbar {
        height: 8px;
    }
    
    .filter-container::-webkit-scrollbar-track {
        background: #f1f1f1;
    }
    
    .filter-container::-webkit-scrollbar-thumb {
        background: #888;
        border-radius: 4px;
    }
    
    .filter-container::-webkit-scrollbar-thumb:hover {
        background: #555;
    }
</style>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">

    <section class="content-header">
        <h1>
            <i class="fa fa-mortar-board"></i> Inventory Warehouse Transfer </h1>
											 <a href="<?php  echo base_url(); ?>inventoryreport" class="col-md pull-right">Inventory Warehouse Transfer </a>
								 <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>
								 <a href="<?php  echo base_url(); ?>admin/itemsupplier" class="col-md pull-right">Item Supplier</a>
								 <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>	
						 		 <a href="<?php  echo base_url(); ?>admin/itemcategory" class="col-md pull-right">Item Category</a>
								 <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>											 
								 <a href="<?php  echo base_url(); ?>admin/item" class="col-md pull-right">Add Item</a>
								 <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>
								 <a href="<?php  echo base_url(); ?>admin/itemstock" class="col-md pull-right">Add Item Stock</a> 
								 <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>
								 <a href="<?php  echo base_url(); ?>admin/issueitem" class="col-md pull-right">Issue Item</a>
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-md-8">
                <!-- general form elements -->
                <div class="box box-primary">
                    <div class="box-header ptbnull">
                        <h3 class="box-title titlefix">Inventory Warehouse Report</h3>
                        <div class="box-tools pull-right">
                        </div><!-- /.box-tools -->
                    </div><!-- /.box-header -->
                    <div class="box-body">
                        <div class="table-responsive mailbox-messages">
                            <div class="download_label"><?php echo $title_list; ?></div>
                            <table class="table table-striped table-bordered table-hover example">
                                <thead>
                                    <tr>
                                        <th>Summary</th>
                                        <th class="text-right">Result</th>
                                    </tr>
                                </thead>
                                <tbody>
                                        <tr>
                                            <td class="mailbox-name"> Total Inventory Received</td>
                                            <td class="mailbox-date pull-right"><?php  echo "<b style='color:blue;font-size:14px;'>".$receivedqnty."</b>";?></td>
                                        </tr>                                      
										<tr>
                                            <td class="mailbox-name"> Total Inventory Issued</td>
                                            <td class="mailbox-date pull-right"><?php echo "<b style='color:blue;font-size:14px;'>".$soldqnty."</b>";?></td>
                                        </tr>										
										<tr>
                                            <td class="mailbox-name"> Total Inventory On Hand</td>
                                            <td class="mailbox-date pull-right"><?php echo "<b style='color:blue;font-size:17px;'>".$leftqnty."</b>";?></td>
                                        </tr>
                                </tbody>
                            </table><!-- /.table -->
                        </div><!-- /.mail-box-messages -->
                    </div><!-- /.box-body -->
                </div>
            </div><!--/.col (left) -->
            <!-- right column -->
        </div>
        <div class="row">
            <div class="col-md-12">
                <!-- Horizontal Form -->
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title">Inventory Report</h3>
                    </div><!-- /.box-header -->
                    <div class="box-body">
                        <div class="table-responsive mailbox-messages">
                            <div class="download_label"><?php echo $this->lang->line('issue_item'); ?></div>
                            <div class="filter-container">
                                <form method="post" action="<?php echo base_url('inventoryreport'); ?>">
                                    <input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" 
                                        value="<?php echo $this->security->get_csrf_hash(); ?>">
                                    <div class="filters-wrapper">
                                        <div class="filters">
                                            <!-- From Date -->
                                        <!-- From Date -->
                                        <label for="filter_from_date">Date From:</label>
                                        <input type="date" name="filter_from_date" id="filter_from_date" 
                                            value="<?php 
                                                echo set_value('filter_from_date', 
                                                    isset($filter_from_date) ? $filter_from_date : date('Y-m-d')
                                                ); 
                                            ?>"
                                            onchange="formatDate('filter_from_date')">
                                        <!-- To Date -->
                                        <label for="filter_to_date">Date To:</label>
                                        <input type="date" name="filter_to_date" id="filter_to_date" 
                                            value="<?php 
                                                echo set_value('filter_to_date', 
                                                    isset($filter_to_date) ? $filter_to_date : date('Y-m-d')
                                                ); 
                                            ?>"
                                            onchange="formatDate('filter_to_date')">
                                            <!-- Item Code From Dropdown -->
                                           <!-- Item Code From Dropdown -->
                                            <label for="filter_start_item">Item From:</label>
                                            <select name="filter_start_item" id="filter_start_item">
                                                <option value="">Select Item</option>
                                                <?php foreach ($itemlist as $item) { ?>
                                                    <option value="<?php echo $item['id']; ?>" 
                                                        <?php echo (set_value('filter_start_item', $filter_start_item) == $item['id']) ? 'selected' : ''; ?>>
                                                        <?php echo $item['name']; ?>
                                                    </option>
                                                <?php } ?>
                                            </select>

                                            <!-- Item Code To Dropdown -->
                                            <label for="filter_end_item">Item To:</label>
                                            <select name="filter_end_item" id="filter_end_item">
                                                <option value="">Select Item</option>
                                                <?php foreach ($itemlist as $item) { ?>
                                                    <option value="<?php echo $item['id']; ?>" 
                                                        <?php echo (set_value('filter_end_item', $filter_end_item) == $item['id']) ? 'selected' : ''; ?>>
                                                        <?php echo $item['name']; ?>
                                                    </option>
                                                <?php } ?>
                                            </select>

                                            <button type="submit" class="btn btn-primary">View</button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                            <table class="table table-hover table-striped table-bordered example">
                                <thead>
                                    <tr>
                                        <th>Issued Date</th>
                                        <th><?php echo $this->lang->line('item'); ?></th>
                                        <th><?php echo $this->lang->line('item_category'); ?></th>
                                        <th><?php echo $this->lang->line('issue_to'); ?></th>
                                        <th>Issued From</th>
                                        <th><?php echo $this->lang->line('issued_by'); ?></th>
                                        <th>Received By</th>
                                        <th>Stock Qnty </th>
                                        <th>Issued Qnty</th>
                                        <th>Available Qnty</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    if (empty($itemissueList)) {
                                        ?>

                                        <?php
                                    } else {
                                        foreach ($itemissueList as $item) {
                                            ?>
                                            <tr>
											    <td class="mailbox-name"><?php echo $item['issue_date']; ?></td>
                                                <td class="mailbox-name">
                                                    <a href="#" data-toggle="popover" class="detail_popover"><?php echo $item['item_name'] ?></a>

                                                    <div class="fee_detail_popover" style="display: none">
                                                        <?php
                                                        if ($item['note'] == "") {
                                                            ?>
                                                            <p class="text text-danger"><?php echo $this->lang->line('no_description'); ?></p>
                                                            <?php
                                                        } else {
                                                            ?>
                                                            <p class="text text-info"><?php echo $item['note']; ?></p>
                                                            <?php
                                                        }
                                                        ?>
                                                    </div>
                                                </td>
                                                <td class="mailbox-name"><?php echo $item['item_category']; ?></td>
                                                <td class="mailbox-name"><?php echo $item['issue_to']; ?></td>
                                                <td class="mailbox-name"><?php echo $item['issue_from']; ?></td>
                                                <td class="mailbox-name"><?php echo $item['issue_by']; ?></td>
                                                <td class="mailbox-name"><?php echo $item['received_by']; ?></td>
                                                <td class="mailbox-name"><?php 
												                                $item_id= $item['item_id'];
																				$this->db->select('*');
																				$this->db->from('item_stock');
																				$this->db->where('item_id', $item_id);
																				$query = $this->db->get();
																				$result = $query->row();
																				
																				$boughtqnty = $result->quantity;
																				
																				$available = $boughtqnty - $item['quantity'];
																				
																				echo $boughtqnty ;
												?></td>
                                                <td class="mailbox-name"><?php echo $item['quantity']; ?></td>
                                                <td class="mailbox-name"><?php echo $available;?></td>
                                            </tr>
                                            <?php
                                        }
                                    }
                                    ?>
						<tr>
													 <td>           
															<div class="btn-group pull-right"> 														
																<button  class="btn btn-xs btn-info printInv"                  
																		 title="<?php echo $this->lang->line('print'); ?>"
																		 ><i class="fa fa-print"></i> Print Statement</button>
															</div> 
														</td>
												</tr>
                                </tbody>
                            </table><!-- /.table -->
                        </div><!-- /.mail-box-messages -->
                    </div><!-- /.box-body -->
                </div>
            </div><!--/.col (right) -->
        </div>
    </section><!-- /.content -->
</div><!-- /.content-wrapper -->

<script type="text/javascript">
    function getSectionByClass(class_id, section_id) {
        if (class_id != "" && section_id != "") {
            $('#section_id').html("");
            var base_url = '<?php echo base_url() ?>';
            var div_data = '<option value=""><?php echo $this->lang->line('select'); ?></option>';
            $.ajax({
                type: "GET",
                url: base_url + "sections/getByClass",
                data: {'class_id': class_id},
                dataType: "json",
                success: function (data) {
                    $.each(data, function (i, obj)
                    {
                        var sel = "";
                        if (section_id == obj.section_id) {
                            sel = "selected";
                        }
                        div_data += "<option value=" + obj.section_id + " " + sel + ">" + obj.section + "</option>";
                    });
                    $('#section_id').append(div_data);
                }
            });
        }
    }

    $(document).ready(function () {
        var class_id = $('#class_id').val();
        var section_id = '<?php echo set_value('section_id') ?>';
        getSectionByClass(class_id, section_id);
        $(document).on('change', '#class_id', function (e) {
            $('#section_id').html("");
            var class_id = $(this).val();
            var base_url = '<?php echo base_url() ?>';
            var div_data = '<option value=""><?php echo $this->lang->line('select'); ?></option>';
            $.ajax({
                type: "GET",
                url: base_url + "sections/getByClass",
                data: {'class_id': class_id},
                dataType: "json",
                success: function (data) {
                    $.each(data, function (i, obj)
                    {
                        div_data += "<option value=" + obj.section_id + ">" + obj.section + "</option>";
                    });
                    $('#section_id').append(div_data);
                }
            });
        });
    });
</script>
<script>
function resetFilters() {
    document.getElementById('filter_from_date').value = '<?php echo date('Y-m-d'); ?>';
    document.getElementById('filter_to_date').value = '<?php echo date('Y-m-d'); ?>';
    document.getElementById('filter_start_item').value = '';
    document.getElementById('filter_end_item').value = '';
    document.forms[0].submit(); // Submit the form after reset
}
</script>
<script type="text/javascript">
    $(document).ready(function () {
        var date_format = '<?php echo $result = strtr($this->customlib->getSchoolDateFormat(), ['d' => 'dd', 'm' => 'mm', 'Y' => 'yyyy',]) ?>';
        $(".date").datepicker({
            format: date_format,
            autoclose: true,
            todayHighlight: true
        });
        $('.detail_popover').popover({
            placement: 'right',
            title: '',
            trigger: 'hover',
            container: 'body',
            html: true,
            content: function () {
                return $(this).closest('td').find('.fee_detail_popover').html();
            }
        });
    });
</script>

<script type="text/javascript">
    $(document).ready(function () {
        $.extend($.fn.dataTable.defaults, {
            ordering: false,
            paging: false,
            bSort: false,
            info: false
        });
    })
    $(document).ready(function () {
        $('.table-fixed-header').fixedHeader();
    });

    (function ($) {

        $.fn.fixedHeader = function (options) {
            var config = {
                topOffset: 50

            };
            if (options) {
                $.extend(config, options);
            }

            return this.each(function () {
                var o = $(this);

                var $win = $(window);
                var $head = $('thead.header', o);
                var isFixed = 0;
                var headTop = $head.length && $head.offset().top - config.topOffset;

                function processScroll() {
                    if (!o.is(':visible')) {
                        return;
                    }
                    if ($('thead.header-copy').size()) {
                        $('thead.header-copy').width($('thead.header').width());
                    }
                    var i;
                    var scrollTop = $win.scrollTop();
                    var t = $head.length && $head.offset().top - config.topOffset;
                    if (!isFixed && headTop !== t) {
                        headTop = t;
                    }
                    if (scrollTop >= headTop && !isFixed) {
                        isFixed = 1;
                    } else if (scrollTop <= headTop && isFixed) {
                        isFixed = 0;
                    }
                    isFixed ? $('thead.header-copy', o).offset({
                        left: $head.offset().left
                    }).removeClass('hide') : $('thead.header-copy', o).addClass('hide');
                }
                $win.on('scroll', processScroll);

                // hack sad times - holdover until rewrite for 2.1
                $head.on('click', function () {
                    if (!isFixed) {
                        setTimeout(function () {
                            $win.scrollTop($win.scrollTop() - 47);
                        }, 10);
                    }
                });

                $head.clone().removeClass('header').addClass('header-copy header-fixed').appendTo(o);
                var header_width = $head.width();
                o.find('thead.header-copy').width(header_width);
                o.find('thead.header > tr:first > th').each(function (i, h) {
                    var w = $(h).width();
                    o.find('thead.header-copy> tr > th:eq(' + i + ')').width(w);
                });
                $head.css({
                    margin: '0 auto',
                    width: o.width(),
                    'background-color': config.bgColor
                });
                processScroll();
            });
        };

    })(jQuery);

</script>

<script type="text/javascript">
    $(document).ready(function () {
        $(document).on('click', '.printDoc', function () {
            var main_invoice = $(this).data('main_invoice');
            var sub_invoice = $(this).data('sub_invoice');
            var student_session_id = '<?php echo $student_session_id; ?>';
            $.ajax({
                url: '<?php echo site_url("studentfee/printFeesByName") ?>',
                type: 'post',
                data: {'student_session_id': student_session_id, 'main_invoice': main_invoice, 'sub_invoice': sub_invoice},
                success: function (response) {
                    Popup(response);
                }
            });
        });																			
        $(document).on('click', '.printInv', function () {
            $.ajax({
                url: '<?php echo site_url("inventoryreport/printInventoryReport") ?>',  
                type: 'post',
                success: function (response) {
                    Popup(response);
                }
            });
        });
    });
</script>

<script>

    var base_url = '<?php echo base_url() ?>';
    function Popup(data)
    {

        var frame1 = $('<iframe />');
        frame1[0].name = "frame1";
        frame1.css({"position": "absolute", "top": "-1000000px"});
        $("body").append(frame1);
        var frameDoc = frame1[0].contentWindow ? frame1[0].contentWindow : frame1[0].contentDocument.document ? frame1[0].contentDocument.document : frame1[0].contentDocument;
        frameDoc.document.open();
        //Create a new HTML document.
        frameDoc.document.write('<html>');
        frameDoc.document.write('<head>');
        frameDoc.document.write('<title></title>');
        frameDoc.document.write('<link rel="stylesheet" href="' + base_url + 'backend/bootstrap/css/bootstrap.min.css">');
        frameDoc.document.write('<link rel="stylesheet" href="' + base_url + 'backend/dist/css/font-awesome.min.css">');
        frameDoc.document.write('<link rel="stylesheet" href="' + base_url + 'backend/dist/css/ionicons.min.css">');
        frameDoc.document.write('<link rel="stylesheet" href="' + base_url + 'backend/dist/css/AdminLTE.min.css">');
        frameDoc.document.write('<link rel="stylesheet" href="' + base_url + 'backend/dist/css/skins/_all-skins.min.css">');
        frameDoc.document.write('<link rel="stylesheet" href="' + base_url + 'backend/plugins/iCheck/flat/blue.css">');
        frameDoc.document.write('<link rel="stylesheet" href="' + base_url + 'backend/plugins/morris/morris.css">');
        frameDoc.document.write('<link rel="stylesheet" href="' + base_url + 'backend/plugins/jvectormap/jquery-jvectormap-1.2.2.css">');
        frameDoc.document.write('<link rel="stylesheet" href="' + base_url + 'backend/plugins/datepicker/datepicker3.css">');
        frameDoc.document.write('<link rel="stylesheet" href="' + base_url + 'backend/plugins/daterangepicker/daterangepicker-bs3.css">');
        frameDoc.document.write('</head>');
        frameDoc.document.write('<body>');
        frameDoc.document.write(data);
        frameDoc.document.write('</body>');
        frameDoc.document.write('</html>');
        frameDoc.document.close();
        setTimeout(function () {
            window.frames["frame1"].focus();
            window.frames["frame1"].print();
            frame1.remove();
        }, 500);


        return true;
    }
    $(document).ready(function () {
        $('.delmodal').modal({
            backdrop: 'static',
            keyboard: false,
            show: false
        })
        $('#confirm-delete').on('show.bs.modal', function (e) {
            $('.invoice_no', this).text("");
            $('#main_invoice', this).val("");
            $('#sub_invoice', this).val("");

            $('.invoice_no', this).text($(e.relatedTarget).data('invoiceno'));
            $('#main_invoice', this).val($(e.relatedTarget).data('main_invoice'));
            $('#sub_invoice', this).val($(e.relatedTarget).data('sub_invoice'));


        });

        $('#confirm-discountdelete').on('show.bs.modal', function (e) {
            $('.discount_title', this).text("");
            $('#discount_id', this).val("");
            $('.discount_title', this).text($(e.relatedTarget).data('discounttitle'));
            $('#discount_id', this).val($(e.relatedTarget).data('discountid'));
        });

        $('#confirm-delete').on('click', '.btn-ok', function (e) {
            var $modalDiv = $(e.delegateTarget);
            var main_invoice = $('#main_invoice').val();
            var sub_invoice = $('#sub_invoice').val();

            $modalDiv.addClass('modalloading');
            $.ajax({
                type: "post",
                url: '<?php echo site_url("studentfee/deleteFee") ?>',
                dataType: 'JSON',
                data: {'main_invoice': main_invoice, 'sub_invoice': sub_invoice},
                success: function (data) {
                    $modalDiv.modal('hide').removeClass('modalloading');
                    location.reload(true);
                }
            });


        });

        $('#confirm-discountdelete').on('click', '.btn-discountdel', function (e) {
            var $modalDiv = $(e.delegateTarget);
            var discount_id = $('#discount_id').val();


            $modalDiv.addClass('modalloading');
            $.ajax({
                type: "post",
                url: '<?php echo site_url("studentfee/deleteStudentDiscount") ?>',
                dataType: 'JSON',
                data: {'discount_id': discount_id},
                success: function (data) {
                    $modalDiv.modal('hide').removeClass('modalloading');
                    location.reload(true);
                }
            });


        });


        $(document).on('click', '.btn-ok', function (e) {
            var $modalDiv = $(e.delegateTarget);
            var main_invoice = $('#main_invoice').val();
            var sub_invoice = $('#sub_invoice').val();

            $modalDiv.addClass('modalloading');
            $.ajax({
                type: "post",
                url: '<?php echo site_url("studentfee/deleteFee") ?>',
                dataType: 'JSON',
                data: {'main_invoice': main_invoice, 'sub_invoice': sub_invoice},
                success: function (data) {
                    $modalDiv.modal('hide').removeClass('modalloading');
                    location.reload(true);
                }
            });


        });


        $('.detail_popover').popover({
            placement: 'right',
            title: '',
            trigger: 'hover',
            container: 'body',
            html: true,
            content: function () {
                return $(this).closest('td').find('.fee_detail_popover').html();
            }
        });
    });



</script>

<script type="text/javascript">


    $("#myFeesModal").on('shown.bs.modal', function (e) {
        console.log("sdrere");
        e.stopPropagation();
        var discount_group_dropdown = '<option value=""><?php echo $this->lang->line('select'); ?></option>';
        var data = $(e.relatedTarget).data();
        $('#discount_group').html("");
        $("span[id$='_error']").html("");
        $('.fees_title').html("");
        $('#amount').val("");
        $('#amount_discount').val("0");
        $('#amount_fine').val("0");
        var type = data.type;
        var amount = data.amount;
        var group = data.group;
        var fee_groups_feetype_id = data.fee_groups_feetype_id;
        var student_fees_master_id = data.student_fees_master_id;
        var student_session_id = data.student_session_id;

        $('.fees_title').html("<b>" + group + ":</b> " + type);
        $('#fee_groups_feetype_id').val(fee_groups_feetype_id);
        $('#student_fees_master_id').val(student_fees_master_id);



        $.ajax({
            type: "post",
            url: '<?php echo site_url("studentfee/geBalanceFee") ?>',
            dataType: 'JSON',
            data: {'fee_groups_feetype_id': fee_groups_feetype_id,
                'student_fees_master_id': student_fees_master_id,
                'student_session_id': student_session_id
            },
            success: function (data) {

                if (data.status == "success") {
                    fee_amount = data.balance;

                    $('#amount').val(data.balance);
                    console.log(data.discount_not_applied);

                    $.each(data.discount_not_applied, function (i, obj)
                    {
                        discount_group_dropdown += "<option value=" + obj.student_fees_discount_id + " data-disamount=" + obj.amount + ">" + obj.code + "</option>";
                    });
                    $('#discount_group').append(discount_group_dropdown);




                }
            }
        });


    });

</script>

<script type="text/javascript">
    $(document).ready(function () {
        $(document).on('click', '.printSelected', function () {
            var array_to_print = [];
            $.each($("input[name='fee_checkbox']:checked"), function () {
                var section_id = $(this).data('section_id');
                var class_id = $(this).data('class_id');
                var student_id = $(this).data('student_id');
                item = {}
                item ["section_id"] = section_id;
                item ["class_id"] = class_id;
                item ["student_id"] = student_id;

                array_to_print.push(item);
            });
            if (array_to_print.length == 0) {
                alert("no record selected");
            } else {
                $.ajax({
                    url: '<?php echo site_url("studentfee/printFeesStatementByGroup") ?>',
                    type: 'post',
                    data: {'data': JSON.stringify(array_to_print)},
                    success: function (response) {
                        Popup(response);
                    }
                });
            }
        });
    });


    $(function () {
        $(document).on('change', "#discount_group", function () {
            var amount = $('option:selected', this).data('disamount');

            var balance_amount = (parseFloat(fee_amount) - parseFloat(amount)).toFixed(2);
            if (typeof amount !== typeof undefined && amount !== false) {
                $('div#myFeesModal').find('input#amount_discount').prop('readonly', true).val(amount);
                $('div#myFeesModal').find('input#amount').val(balance_amount);

            } else {
                $('div#myFeesModal').find('input#amount').val(fee_amount);
                $('div#myFeesModal').find('input#amount_discount').prop('readonly', false).val(0);
            }

        });
    });
</script>

