<style type="text/css">
    #searchdiv{
        margin-bottom: 10px;
    }
    .searchfieldslist{
        margin-top: 4px;
    }
</style>
<!-- Page Content -->
<div class="page-content">
    <!-- Page Header -->
    <div class="page-header position-relative">
        <div class="header-title">
            <h1>
                <?php echo $reporttitle; ?>
            </h1>
        </div>
    </div>
    <!-- /Page Header -->
    <!-- Page Body -->
    <div class="page-body">
        
        <div class="row">
            <div class="col-xs-12 col-md-12">
                <div class="widget">
                    <div class="widget-body">
        <div id="searchdiv">
            
            <form name="frmsearch" id="frmsearch" method="post">
                <div class="row">
                    <?php
                    if(isset($searchfields) && $searchfields){
                        foreach($searchfields as $searchfield){
                            echo $searchfield;
                        }
                    }
                    ?>
                    <div class="clearfix"></div>
                    <div class='col-md-3 searchfieldslist'>
                        <button type="submit" class="btn btn-primary shiny" name="allsearch"><span class="fa fa-search"></span>Execute</button>
                    </div>
                </div>
            </form>
            
        </div>
                        
                <form name="exportexcel" id="exportexcel" action="<?php echo base_url(); ?>reports/inventory/exportexceldata" method="post">
                    <?php
                    if(isset($_POST["allsearch"])){
                    ?>
                    <div style="float: right;">
                        <div class="btn-group">
                            <button type="submit" class="btn btn-success shiny" name="excelexport"><i class="fa fa-file-excel-o"></i> Excel</button>
                            <button type="submit" class="btn btn-danger shiny" name="pdfxport"><i class="fa fa-file-pdf-o"></i> PDF</button>
                        </div>
                    </div>
                    <?php
                    }
                    ?>
<div id="tablecontent" class="table-scrollable">
<?php
    $tabledata  =   '<table class="table table-striped table-hover table-bordered" id="reportdatatable">
    <thead>
        <tr role="row">';
            if($datatableVar["title"]){
                $k=0;
                $sumArray=array();
                foreach($datatableVar["title"] as $title){
                    $tabledata.=   '<th style="text-align:'.$datatableVar["align"][$k].';width:'.$datatableVar["width"][$k].'">'.$title;
                    if($datatableVar["sum"][$k]=='true'){
                        array_push($sumArray,'sum('.$datatableVar["field"][$k].') as '.$datatableVar["field"][$k]);
                    }
                    $tabledata.=   '</th>';
                    $k++;
                }
                $totalcols  =   $k;
            }
    $tabledata.=   '</tr>
    </thead>
    <tfoot>
        <tr role="row">';
        if(isset($sumArray) && $sumArray){
            if(isset($sqlquery) && $sqlquery){
                $sumField    =   implode(",",$sumArray);
                $qry =   "SELECT ".$sumField." from (".$sqlquery.") mytable";
                $sumdataresult = $this->common->customquery($qry);
                if($datatableVar["title"]){
                    $k=0;
                    foreach($datatableVar["title"] as $title) {
                        $tabledata.=   '<th style="text-align:'.$datatableVar["align"][$k].';width:'.$datatableVar["width"][$k].';"><div style="font-size:20px;font-weight: bold;">';
                        if(trim($datatableVar["sum"][$k])=='true'){
                            $fieldname  =   $datatableVar["field"][$k];
                            if(trim($datatableVar["type"][$k])=='currency'){
                                $tabledata.=$this->formatslist->formatCurrencywithHash($sumdataresult[0]->$fieldname);
                            }else if(trim($datatableVar["type"][$k])=='double'){
                                $tabledata.=$this->formatslist->formatDoubleNumber($sumdataresult[0]->$fieldname);
                            }else if(trim($datatableVar["type"][$k])=='number'){
                                $tabledata.=$this->formatslist->formatIntegerNumber($sumdataresult[0]->$fieldname);
                            }else{
                                $tabledata.=$sumdataresult[0]->$fieldname;
                            }
                        }
                        $tabledata.='</div>';
                        $k++;
                    }
                    $tabledata.='</th>';
                }
            }
        }
        $tabledata.='</tr>';
        $tabledata.='</tfoot><tbody>';
            if(isset($dataresult) && $dataresult){
                $prevFlddata[]   =   '';
                $datcount        =   0;
                foreach($dataresult as $datarow){
                    if(isset($groupArray) && $groupArray){
                        foreach($groupArray as $groupArrData){
                            $key = array_search($groupArrData, $groupArray);
                            if(!isset($prevFlddata["$groupArrData"]))
                                $prevFlddata["$groupArrData"][] =   "";
                            if(!in_array($datarow->$groupArrData,$prevFlddata["$groupArrData"])) {
                                $prevFlddata["$groupArrData"][$datcount]   =   $datarow->$groupArrData;
                                $tabledata.='<tr>';
                                $tabledata.='<td colspan="'.$totalcols.'" style="font-weight:bold;">'.$datarow->$groupArrData.'</td>';
                                $tabledata.='</tr>';
                                $prevFlddata["$groupArrData"][$key]   =   $datarow->$groupArrData;
                                if(isset($groupArrayCount)) {
                                    for($q=$key+1;$q<$groupArrayCount;$q++){
                                        if(isset($prevFlddata["$groupArray[$q]"]))
                                            unset($prevFlddata["$groupArray[$q]"]);
                                    }
                                }
                            }
                        }
                    }

                    
                    
                    $tabledata.='<tr>';
                    if($datatableVar["title"]){
                        $k=0;
                        foreach($datatableVar["title"] as $title){
                            $fieldname  =   $datatableVar["field"][$k];
                            $tabledata.='<td style="text-align:'.$datatableVar["align"][$k].';width:'.$datatableVar["width"][$k].';">';
                            if($datatableVar["type"][$k]=='currency'){
                                $tabledata.=$this->formatslist->formatCurrencywithHash($datarow->$fieldname);
                            }else if($datatableVar["type"][$k]=='double'){
                                $tabledata.=$this->formatslist->formatDoubleNumber($datarow->$fieldname);
                            }else if($datatableVar["type"][$k]=='number'){
                                $tabledata.=$this->formatslist->formatIntegerNumber($datarow->$fieldname);
                            }else if($datatableVar["type"][$k]=='date'){
                                if($datarow->$fieldname)
                                    $tabledata.=$this->formatslist->dateFormat($datarow->$fieldname);
                            }else if($datatableVar["type"][$k]=='datetime'){
                                if($datarow->$fieldname)
                                    $tabledata.=$this->formatslist->datetimeFormat($datarow->$fieldname);
                            }else
                                $tabledata.=$datarow->$fieldname;
                            $tabledata.='</td>';
                        $k++;
                        }
                    }
                    $tabledata.='</tr>';
                    
                    
                    $datcount++;
                }
            }
        $tabledata.='</tbody></table>';
        echo $tabledata;
        ?>
    </div>
<?php

//echo $sqlquery;

if(isset($sqlquery) && $sqlquery){
?>
                    <input type="hidden" name="hiddenqueryvalue" value="<?php echo $sqlquery; ?>" />
<?php
}
if(isset($qry) && $qry){
?>
<input type="hidden" name="hiddensumqueryvalue" value="<?php echo $qry; ?>" />
<?php
}
?>
<?php
if(isset($reportid) && $reportid){
?>
<input type="hidden" name="reportid" value="<?php echo $reportid; ?>" />
<?php
}
?>
                    </form>
                    </div>
                </div>
            </div>
        </div>
        <!-- /Page Body -->
    </div>
    <!-- /Page Content -->
</div>
<!-- /Page Container -->
<!-- Main Container -->
</div>
<script type="text/javascript">
    function fnExcelReport() {
        
    }
</script>