<style>
    /* enable absolute positioning */
    .inner-addon { 
        position: relative; 
    }

    /* style icon */
    .inner-addon .glyphicon {
        position: absolute;
        padding: 10px;
        pointer-events: none;
    }

    /* align icon */
    .left-addon .glyphicon  { left:  0px;}
    .right-addon .glyphicon { right: 0px;}

    /* add padding  */
    .left-addon input  { padding-left:  30px; }
    .right-addon input { padding-right: 30px; }

    .loader {
        position: fixed;
        left: 0px;
        top: 0px;
        width: 100%;
        height: 100%;
        z-index: 9999;
        background: url('<?php echo base_url(); ?>assets/images/page-loader-1.gif') 50% 50% no-repeat ;
        display: none;
    }


    table.dataTable tr.group td{font-weight:bold;background-color:#e0e0e0}
    
     table.dataTable tr.group,
        tr.group:hover {
            background-color: #ddd !important;
        }
    
</style>
<?php
//    echo "<pre>";
//    print_r($balanceSheet);
//    exit();
const RETAINED_INCOME = 'Auto';
?>
<div class="wrapper">
    <div class="container">
        <!-- Page-Title -->
        <div class="row">
            <div class="col-sm-12">
                <div class="btn-group pull-right m-t-15">
                    
                </div>
                <h4 class="page-title">Balance Sheet (<?php echo $business[0]['business_name'];?>):</h4>
            </div>
        </div>
        
        <div class="row" id="divselection">
            <div class="col-sm-12">
                <div class="card-box ">
                    <h4 class="header-title m-t-0 m-b-10">Filters:</h4>
                    <div class="row m-b-10" id="div-filters">
                        <div class="col-lg-12">
                            <div class="row">
                                <div class="col-lg-3">
                                    <label class="control-label">As on :</label>
                                </div>
                                 <?php if($this->session->userdata('ho')=='Yes'){ ?>
                                <div class="col-lg-3">
                                    <label class="control-label">Branch</label>
                                </div>
                                <?php } ?>
<!--                                <div class="col-lg-4">
                                    <label class="control-label">Account Range</label>
                                </div>-->
                                <div class="col-lg-2"></div>    
                            </div>
                        </div>
                        <div class="row">
                            <form method="post" action="<?php echo base_url();?>balacne_sheet">
                                
                                <input type="hidden" name="csrf_test_name" id="accounting_voucher_csrf" value=""/>
                                <div class="col-lg-3">
                                    <input style="background-color: white;" required readonly class="form-control" type="text" name="startdate" id="startdate" value="<?php if(isset($startdate)){echo $startdate;}?>"/>                                
                                </div>
                                <?php if($this->session->userdata('ho')=='Yes'){ ?>
                                <div class="col-lg-3">
                                    <select style="background-color: white;" class="form-control" type="text" name="business_id" id="business_id">
                                        <!--<option value="">All</option>-->
                                        <?php foreach($businesses as $b){?>
                                        <option <?php if($businessselected==$b['id_business']){echo "selected='selected'";}?> value="<?php echo $b['id_business'];?>"><?php echo $b['business_name'];?></option>
                                        <?php } ?>
                                    </select>                                
                                </div>
                                <?php } ?>
                                <div class="col-lg-2"><button type="submit" onclick="$('#accounting_voucher_csrf').val($('#cook').val());" class="btn btn-pink btn-bordred waves-effect w-md waves-light m-b-5">Run</button></div>
                            </form>
                        </div>
                    </div>
                    
                    <div class="row">
                         <div class="col-md-12">
                         <?php if(DOWNLOAD == true || $this->session->userdata('role') == 'Admin' || $this->session->userdata('role')=='Super User'){ ?>
                             <a class="btn btn-warning m-l-10" onclick="tableToExcel('sheet1', 'Balance Sheet')" style="float:right">Export to Excel</a>
                        <?php } ?>
                        <table class="table table-bordered table-condensed" id="sheet1">
                            <thead>
                                <tr>
                                    <th class="text-center">ASSETS</th>
                                    <th class="text-center">EQUITIES</th>
                                </tr>
                            </thead>
                            
                            <tbody>
                                <tr>
                                    <td>
                                        <?php  $total_assets =0; foreach($balanceSheet['Assets'] as $ma_index=>$ma_value): ?> 
                                        <table class="table table-bordered table-condensed">
                                        <tbody>
                                            <tr><td colspan="3"><strong><i><?php echo  $ma_index ?></i></strong></td></tr>
                                            <?php $total=0;  foreach($ma_value as $index=>$ca_value): ?>  
                                                <?php foreach($ca_value as $index=>$value): ?> 
                                                <tr>
                                                    <td style="width:80%;"><?php echo  $index ?></td>
                                                    <td style="width:20%; text-align: right;"><?php echo  $value[0]['balance'] < 0 ? "(".abs($value[0]['balance']).")" : $value[0]['balance'] ?></td>
                                                    <?php $total = $total + $value[0]['balance']; ?>
                                                    <?php $total_assets += $value[0]['balance']; ?>
                                               </tr>
                                               <?php endforeach; ?>
                                            <?php endforeach; ?> 
                                            <tfoot>
                                            <th>Total:</th>
                                            <th  style="text-align: right;"><?php echo $total < 0 ?  "(".number_format(abs($total),2).")" :number_format(abs($total),2) ; ?></th>
                                            </tfoot> 
                                        </tbody>
                                        </table>
                                        <?php endforeach; ?>  
                                    </td>
                                    <td>
                                        <?php $total_equits=0; foreach($balanceSheet['Liabilities'] as $ma_index=>$ma_value): ?> 
                                        <table class="table table-bordered table-condensed">
                                            <tbody>
                                                <tr><td colspan="3"><strong><i><?php echo  $ma_index ?></i></strong></td></tr>
                                                <?php $total=0; foreach($ma_value as $index=>$ca_value): ?>
                                                
                                                     <?php  foreach($ca_value as $index=>$value): ?> 
                                                    <tr>
                                                        <td style="width:80%;"><?php echo  $index ?></td>
                                                        <td style="width:20%;  text-align: right;"><?php echo  $value[0]['balance'] < 0 ? "(".abs($value[0]['balance']).")" : $value[0]['balance'] ?></td>
                                                        <?php $total += $value[0]['balance']; ?>
                                                        <?php $total_equits += $value[0]['balance']; ?>
                                                   </tr>
                                                    <?php endforeach; ?> 
                                                <?php endforeach; ?> 
                                                <tfoot>
                                                <th>Total:</th>
                                                <th style=" text-align: right;"><?php if($total < 0){ echo "(".abs($total).")"; } else {echo number_format(abs($total),2);} ?></th>
                                                </tfoot> 
                                            </tbody>
                                        </table>
                                        <?php endforeach; ?>  
                                        
                                        
                                        <?php foreach($balanceSheet['Equity'] as $ma_index=>$ma_value): ?> 
                                        <table class="table table-bordered table-condensed">
                                            <tbody>
                                                <tr><td colspan="3"><strong><i><?php echo  $ma_index ?></i></strong></td></tr>
                                                <?php $total=0; foreach($ma_value as $index=>$ca_value): ?>
                                                    <?php  foreach($ca_value as $index=>$value): ?> 
                                                    <tr>
                                                        <td style="width:80%;"><?php echo  $index; ?></td>
                                                        <?php if(RETAINED_INCOME=='Auto'){?>
                                                            <?php if(substr($index,0,17) !== 'Retained Earnings'){?>
                                                            <td style="width:20%;  text-align: right;"><?php echo  $value[0]['balance'] < 0 ? "(".abs($value[0]['balance']).")" : $value[0]['balance'] ?></td>
                                                                <?php $total += $value[0]['balance']; ?>
                                                                <?php $total_equits += $value[0]['balance']; ?>
                                                            <?php } else {?>
                                                            <td style="width:20%;  text-align: right;"><?php echo  $retainedincome < 0 ? "(".abs($retainedincome).")" : $retainedincome; ?></td>
                                                                <?php $total += $retainedincome; ?>
                                                                <?php $total_equits += $retainedincome; ?>
                                                            <?php } ?>
                                                        <?php } else { ?>
                                                            <td style="width:20%;  text-align: right;"><?php echo  $value[0]['balance'] < 0 ? "(".abs($value[0]['balance']).")" : $value[0]['balance'] ?></td>
                                                                <?php $total += $value[0]['balance']; ?>
                                                                <?php $total_equits += $value[0]['balance']; ?>
                                                        <?php } ?>     
                                                   </tr>
                                                   <?php endforeach; ?> 
                                                <?php endforeach; ?> 
                                                <tfoot>
                                                <th>Total:</th>
                                                <th style=" text-align: right;"><?php echo $total < 0 ? "(". number_format(abs($total),2).")" : number_format($total,2); ?></th>
                                                </tfoot> 
                                            </tbody>
                                        </table>
                                        <?php endforeach; ?>  
                                        
                                   </td>
                                </tr>
                            </tbody>
                            <tfoot>
                                <tr>
                                    <th>
                                        <div class="pull-left">Total Assets</div>
                                        <div class="pull-right"><?php echo $total_assets < 0 ? "(". number_format(abs($total_assets), 2).")" :  number_format(abs($total_assets), 2); ?></div>
                                    </th>
                                    <th>
                                        <div class="pull-left">Total Equities</div>
                                        <div class="pull-right" id="total_equities"><?php echo $total_equits < 0 ? "(". number_format(abs($total_equits), 2).")" :  number_format(abs($total_equits), 2); ?></div>
                                    </th>
                                </tr>
                            </tfoot>
                        </table>
                       
                    </div>
                        
                        
                        
                        
                        
                    </div>
                    
                </div>
            </div>
        </div>    
    </div>
</div>

<script>
    $(document).ready(function() {
        $("#fromAccount").select2();
        $("#toAccount").select2();
        
        $("#startdate").datepicker({autoclose:true, format:'yyyy-mm-dd'});
        $("#enddate").datepicker({autoclose:true, format:'yyyy-mm-dd'});

        toastr.options = {
            "closeButton": true,
            "debug": true,
            "newestOnTop": true,
            "progressBar": true,
            "positionClass": "toast-bottom-center",
            "preventDuplicates": true,
            "onclick": null,
            "showDuration": "300",
            "hideDuration": "1000",
            "timeOut": "5000",
            "extendedTimeOut": "1000",
            "showEasing": "swing",
            "hideEasing": "linear",
            "showMethod": "fadeIn",
            "hideMethod": "fadeOut"
        }

        $(".numeric").keypress(function(e) {
            //if the letter is not digit then display error and don't type anything
            if (e.which != 13 && e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
                return false;
            }
        });
    });


var tableToExcel = (function () {
          //  application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
            var uri = 'data:application/vnd.ms-excel;base64,'
              , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{table}</table></body></html>'
              , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
              , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
            return function (table, name) {
                if (!table.nodeType) table = document.getElementById(table)
                var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
                var link = document.createElement("a");
                
                var d = new Date();
                var datestring = ("0" + d.getDate()).slice(-2) + "-" + ("0"+(d.getMonth()+1)).slice(-2) + "-" +
                d.getFullYear() + " " + ("0" + d.getHours()).slice(-2) + ":" + ("0" + d.getMinutes()).slice(-2);
    
                link.download = ctx.worksheet + ' ' + datestring;
                link.href = uri + base64(format(template, ctx));
                link.click();
                //window.location.href = uri + base64(format(template, ctx))
            }
        })()
 



</script>