<style type="text/css">
    .nav-tabs-custom>.nav-tabs>li.active {
        border-top-color: #faa21c;
    }
</style>
<?php
$currency_symbol = $this->customlib->getSchoolCurrencyFormat();
?>
<div class="content-wrapper" style="min-height: 946px;">
    <section class="content-header">
        <h1>
            <i class="fa fa-line-chart"></i> <?php echo $this->lang->line('financial_reports'); ?> <small> </small>   </h1>
											<a href="<?php  echo base_url(); ?>uniformreport" class="col-md pull-right">Uniform Report</a>
                                            <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>												 
                                            <a href="<?php  echo base_url(); ?>admin/payroll/payrollreport" class="col-md pull-right">Payroll Report</a> 
                                            <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>
                                            <a href="<?php  echo base_url(); ?>admin/transaction/searchtransaction" class="col-md pull-right">Transaction Report</a>	
                                            <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>
                                            <a href="<?php  echo base_url(); ?>admin/transaction/studentacademicreport" class="col-md pull-right">Balance Fees Report</a>	
                                            <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>
    </section>
    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title"><i class="fa fa-search"></i> Cash Flow Statement</h3>
                    </div>
                    <div class="box-body">
                        <div class="row">
                            <div class="col-md-12">
                                <form id="reportForm" role="form" action="<?php echo site_url('admin/cashflowstatement/searchcashflows'); ?>" method="post" class="form-horizontal">
                                    <?php echo $this->customlib->getCSRF(); ?> 
                                <!-- Year From / Year To -->
                                <div class="form-group">
                                    <div class="col-sm-6">
                                        <label>Year From:</label>
                                        <select id="year_from" name="year_from" class="form-control">
                                            <?php for($y = 2021; $y <= 2040; $y++): ?>
                                                <option value="<?php echo $y; ?>" <?php echo (isset($year_from) && $year_from == $y) ? 'selected' : ''; ?>>
                                                    <?php echo $y; ?>
                                                </option>
                                            <?php endfor; ?>
                                        </select>
                                    </div>
                                    <div class="col-sm-6">
                                        <label>Year To:</label>
                                        <select id="year_to" name="year_to" class="form-control">
                                            <?php for($y = 2021; $y <= 2040; $y++): ?>
                                                <option value="<?php echo $y; ?>" <?php echo (isset($year_to) && $year_to == $y) ? 'selected' : ''; ?>>
                                                    <?php echo $y; ?>
                                                </option>
                                            <?php endfor; ?>
                                        </select>
                                    </div>
                                </div>

                                <!-- Period (Month) From / To -->
                                <div class="form-group">
                                    <div class="col-sm-6">
                                        <label>Period From (Month):</label>
                                        <select id="month_from" name="month_from" class="form-control">
                                            <?php 
                                                $months = [
                                                    1 => 'January',
                                                    2 => 'February',
                                                    3 => 'March',
                                                    4 => 'April',
                                                    5 => 'May',
                                                    6 => 'June',
                                                    7 => 'July',
                                                    8 => 'August',
                                                    9 => 'September',
                                                    10 => 'October',
                                                    11 => 'November',
                                                    12 => 'December'
                                                ];
                                                foreach($months as $num => $name): 
                                            ?>
                                                <option value="<?php echo $num; ?>" <?php echo (isset($month_from) && $month_from == $num) ? 'selected' : ''; ?>>
                                                    <?php echo $name; ?>
                                                </option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>
                                    <div class="col-sm-6">
                                        <label>Period To (Month):</label>
                                        <select id="month_to" name="month_to" class="form-control">
                                            <?php foreach($months as $num => $name): ?>
                                                <option value="<?php echo $num; ?>" <?php echo (isset($month_to) && $month_to == $num) ? 'selected' : ''; ?>>
                                                    <?php echo $name; ?>
                                                </option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>
                                </div>

                                <!-- Date From / Date To -->
                                <div class="form-group">
                                    <div class="col-sm-6">
                                        <label><?php echo $this->lang->line('date_from'); ?></label>
                                        <input id="datefrom" name="date_from" type="text" class="form-control date" 
                                               value="<?php echo set_value('date_from', date($this->customlib->getSchoolDateFormat())); ?>" 
                                               readonly="readonly" />
                                        <span class="text-danger"><?php echo form_error('date_from'); ?></span>
                                    </div>
                                    <div class="col-sm-6">
                                        <label><?php echo $this->lang->line('date_to'); ?></label>
                                        <input id="dateto" name="date_to" type="text" class="form-control date" 
                                               value="<?php echo set_value('date_to', date($this->customlib->getSchoolDateFormat())); ?>" 
                                               readonly="readonly" />
                                        <span class="text-danger"><?php echo form_error('date_to'); ?></span>
                                    </div>
                                </div>

                                <!-- Search Button -->
                                <div class="form-group">
                                    <div class="col-sm-12">
                                        <button type="submit" name="search" value="search_filter" class="btn btn-primary btn-sm pull-right">
                                            <i class="fa fa-search"></i> <?php echo $this->lang->line('search'); ?>
                                        </button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Income Statement Table Container (initially hidden) -->
            <div id="incomeStatementContainer" class="box bozero" style="width: 100%; padding: 10px; display: none;">
                <h3 class="titless pull-left" style="margin-left: 0;">
                    <i class="fa fa-money"></i> Cash Flow Statement
                </h3>
                <!-- Trial Balance Table -->
                <table style="width: 100%; font-family: Arial, sans-serif; font-size: 16px; border-collapse: collapse;">
                <thead>
                    <tr>
                    <th colspan="4" style="text-align: left; font-size: 22px; padding: 12px;"><?php echo $company_name; ?> Melsoft</th>
                    <th colspan="2" style="text-align: right; font-size: 22px; color: #0056b3; padding: 12px;">Cash Flow Statement</th>
                    </tr>
                    <tr>
                    <th colspan="6" style="text-align: right; padding: 12px;">For the Year Ending: ____________</th>
                    </tr>
                    <tr>
                    <th colspan="6" style="text-align: right; padding: 12px;">Cash at Beginning of Year: $15,700</th>
                    </tr>
                </thead>

                <tbody>
                    <!-- Operations -->
                    <tr style="background-color: #003366; color: white;">
                    <td colspan="6" style="padding: 8px 12px;"><strong>Operations</strong></td>
                    </tr>
                    <tr style="background-color: #e9ecef;">
                    <td colspan="6" style="padding: 8px 12px;"><strong>Cash receipts from:</strong></td>
                    </tr>
                    <tr style="background-color: #f8f9fa;"><td colspan="5" style="padding: 8px 12px;">Customers</td><td style="text-align: right; padding: 8px 12px;">$693,200</td></tr>
                    <tr style="background-color: #ffffff;"><td colspan="5" style="padding: 8px 12px;">Other Operations</td><td style="text-align: right; padding: 8px 12px;">-</td></tr>

                    <tr style="background-color: #e9ecef;">
                    <td colspan="6" style="padding: 8px 12px;"><strong>Cash paid for:</strong></td>
                    </tr>
                    <tr style="background-color: #f8f9fa;"><td colspan="5" style="padding: 8px 12px;">Inventory purchases</td><td style="text-align: right; padding: 8px 12px;">($264,000)</td></tr>
                    <tr style="background-color: #ffffff;"><td colspan="5" style="padding: 8px 12px;">General operating and administrative expenses</td><td style="text-align: right; padding: 8px 12px;">($112,000)</td></tr>
                    <tr style="background-color: #f8f9fa;"><td colspan="5" style="padding: 8px 12px;">Wage expenses</td><td style="text-align: right; padding: 8px 12px;">($123,000)</td></tr>
                    <tr style="background-color: #ffffff;"><td colspan="5" style="padding: 8px 12px;">Interest</td><td style="text-align: right; padding: 8px 12px;">($13,500)</td></tr>
                    <tr style="background-color: #f8f9fa;"><td colspan="5" style="padding: 8px 12px;">Income taxes</td><td style="text-align: right; padding: 8px 12px;">($32,800)</td></tr>
                    <tr style="background-color: #d1ecf1;">
                    <td colspan="5" style="padding: 8px 12px;"><strong>Net Cash Flow from Operations</strong></td>
                    <td style="text-align: right; padding: 8px 12px;"><strong>$147,900</strong></td>
                    </tr>

                    <!-- Investing Activities -->
                    <tr style="background-color: #003366; color: white;">
                    <td colspan="6" style="padding: 8px 12px;"><strong>Investing Activities</strong></td>
                    </tr>
                    <tr style="background-color: #f8f9fa;"><td colspan="5" style="padding: 8px 12px;">Sale of property and equipment</td><td style="text-align: right; padding: 8px 12px;">$33,600</td></tr>
                    <tr style="background-color: #ffffff;"><td colspan="5" style="padding: 8px 12px;">Collection of principal on loans</td><td style="text-align: right; padding: 8px 12px;">-</td></tr>
                    <tr style="background-color: #f8f9fa;"><td colspan="5" style="padding: 8px 12px;">Purchase of property and equipment</td><td style="text-align: right; padding: 8px 12px;">($75,000)</td></tr>
                    <tr style="background-color: #d1ecf1;">
                    <td colspan="5" style="padding: 8px 12px;"><strong>Net Cash Flow from Investing Activities</strong></td>
                    <td style="text-align: right; padding: 8px 12px;"><strong>($41,400)</strong></td>
                    </tr>

                    <!-- Financing Activities -->
                    <tr style="background-color: #003366; color: white;">
                    <td colspan="6" style="padding: 8px 12px;"><strong>Financing Activities</strong></td>
                    </tr>
                    <tr style="background-color: #f8f9fa;"><td colspan="5" style="padding: 8px 12px;">Repurchase of stock (treasury stock)</td><td style="text-align: right; padding: 8px 12px;">($34,000)</td></tr>
                    <tr style="background-color: #ffffff;"><td colspan="5" style="padding: 8px 12px;">Dividends</td><td style="text-align: right; padding: 8px 12px;">($53,000)</td></tr>
                    <tr style="background-color: #d1ecf1;">
                    <td colspan="5" style="padding: 8px 12px;"><strong>Net Cash Flow from Financing Activities</strong></td>
                    <td style="text-align: right; padding: 8px 12px;"><strong>($87,000)</strong></td>
                    </tr>

                    <!-- Summary -->
                    <tr style="background-color: #c3e6cb;">
                    <td colspan="5" style="padding: 8px 12px;"><strong>Net Increase in Cash</strong></td>
                    <td style="text-align: right; padding: 8px 12px;"><strong>$19,500</strong></td>
                    </tr>
                    <tr style="background-color: #f1f1f1;">
                    <td colspan="5" style="padding: 8px 12px;"><strong>Cash at End of Year</strong></td>
                    <td style="text-align: right; padding: 8px 12px;"><strong>$35,200</strong></td>
                    </tr>
                </tbody>
                </table>


                                
                                <!-- Print Button -->
                                <div class="text-right" style="margin-top: 25px; width: 100%;">
                                    <button id="printIncomeStatementBtn" class="btn btn-info printInv" title="Print Income Statement" style="font-size: 16px; padding: 8px 16px;">
                                        <i class="fa fa-print"></i> Print Income Statement
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
</section>
<!-- jQuery to reveal the Income Statement table after form submission -->
<script type="text/javascript">
    $(document).ready(function () {
        var date_format = '<?php echo strtr($this->customlib->getSchoolDateFormat(), ['d' => 'dd', 'm' => 'mm', 'Y' => 'yyyy']); ?>';
        $(".date").datepicker({
            format: date_format,
            autoclose: true,
            todayHighlight: true
        });

        // When the search form is submitted, prevent default submission and show the income statement container
        $("#reportForm").on('submit', function(e){
            e.preventDefault();
            $("#incomeStatementContainer").show();
        });
    });
</script>

<!-- Additional scripts for print functionality (unchanged) -->
<script type="text/javascript">
    $(document).ready(function () {
        $(document).on('click', '#printStatementBtwn', function () {
            var date_from = $(this).data('date_from');
            var date_to = $(this).data('date_to');
            var report_type = $(this).data('report_type');  // Get report type

            $.ajax({
                url: '<?php echo site_url("admin/outstanding/printTransactions") ?>',
                type: 'post',
                data: {
                    'date_from': date_from,
                    'date_to': date_to,
                    'report_type': report_type  // Send report type
                },
                success: function (response) {
                    Popup(response);
                }
            });
        });
    });
</script>
<script type="text/javascript">
    $(document).ready(function () {
        $(document).on('click', '#printStatementBtn', function () {
            var date_from = $(this).data('date_from');
            var date_to = $(this).data('date_to');
            var report_type = $(this).data('report_type');  // Get report type

            $.ajax({
                url: '<?php echo site_url("admin/outstanding/printTransaction") ?>',
                type: 'post',
                data: {
                    'date_from': date_from,
                    'date_to': date_to,
                    'report_type': report_type  // Send report type
                },
                success: function (response) {
                    Popup(response);
                }
            });
        });
    });
</script>

<script type="text/javascript">
    $(document).ready(function () {
        var date_format = '<?php echo strtr($this->customlib->getSchoolDateFormat(), ['d' => 'dd', 'm' => 'mm', 'Y' => 'yyyy']); ?>';
        $(".date").datepicker({
            format: date_format,
            autoclose: true,
            todayHighlight: true
        });
    });
</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 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);

                $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>
    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();
        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;
    }
</script>

<script>
    $(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>
    $(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');
                var 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>