<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="col-lg-12 col-sm-12 col-xs-12 no-padding">
        <div class="widget widgetsearch" style="margin-bottom: 0px;">
            <div class="widget-header bg-themeprimary" style="text-align:center;min-height: 25px;">
                <div class="widget-buttons" style="height: 25px;line-height: 24px;">
                    <a href="#" data-toggle="collapse">
                        <i class="fa fa-minus"></i>
                    </a>
                </div><!--Widget Buttons-->
            </div><!--Widget Header-->
            <div class="widget-body col-lg-12 col-md-12 col-sm-12 col-xs-12" style="display: block;float: left;padding: 0px;padding-bottom: 10px;margin-bottom: 9px;">
                <form name="frmsearch" id="frmsearch" method="post">
                    <div class="row">
                        <?php
                        if(isset($searchfields) && $searchfields){
                            foreach($searchfields as $searchfield){
                        ?>
                        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                        <?php
                            echo $searchfield;
                        ?>
                        </div>
                        <?php
                            }
                        }
                        echo $limittext;
                        ?>
                        <div class="clearfix"></div>
                            <div class='col-lg-2 col-md-12 col-sm-12 col-xs-12' style='text-align: left;padding-top: 12px;'>&nbsp;</div>
                            <div class='col-md-3 searchfieldslist' style="padding-left: 26px;">
                                <button type="submit" class="btn btn-primary shiny" name="allsearch"><span class="fa fa-search"></span>Execute</button>
                            </div>
                    </div>
                </form>
            </div><!--Widget Body-->
        </div><!--Widget-->
    </div>
                <div class="widget">
                    <div class="widget-body">
               
                <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;
                $flagexist      =   0;
                $arrFinal       =   array();
                    if(isset($groupArray) && $groupArray) {
                        foreach($dataresult as $datarow) {
                            foreach($groupArray as $groupArrData) {
                                $key = array_search($groupArrData, $groupArray);
                                if(!isset($prevFlddata["$groupArrData"]))
                                    $prevFlddata["$groupArrData"][] =   "";
                                if(!in_array($datarow->$groupArrData,$prevFlddata["$groupArrData"])) {
                                    $subcount  =   0;
                                    $prevFlddata["$groupArrData"][$datcount]   =   $datarow->$groupArrData;
                                    $arrFinal["GROUPCATEGORY"][$datcount]  =   $datarow->$groupArrData;
                                    $datcount++;
                                    $prevFlddata["$groupArrData"][$key]   =   $datarow->$groupArrData;
                                    if(isset($groupArrayCount)) {
                                        for($q=$key+1;$q<$groupArrayCount;$q++) {
                                            if(isset($prevFlddata["$groupArray[$q]"])) {
                                                unset($prevFlddata["$groupArray[$q]"]);
                                            }
                                        }
                                    }
                                }
                            }
                            
                            if($datatableVar["title"]) {
                                $k=0;
                                $tabdata    =   '';
                                $subtotalflag   =   0;
                                foreach($datatableVar["title"] as $title){
                                    $fieldname  =   $datatableVar["field"][$k];
                                    $arrFinal["SUBDATA"][$datcount][$k]["txtalign"]     =   $datatableVar["align"][$k];
                                    $arrFinal["SUBDATA"][$datcount][$k]["width"]        =   $datatableVar["width"][$k];
                                    $arrFinal["SUBDATA"][$datcount][$k]["type"]         =   $datatableVar["type"][$k];
                                    $arrFinal["SUBDATA"][$datcount][$k]["value"]        =   $datarow->$fieldname;
                                    if($datatableVar["subtotal"][$k]=="true")
                                        $subtotalflag   =   1;
                                    $arrFinal["SUBDATA"][$datcount][$k]["subtotal"]     =   $datatableVar["subtotal"][$k];
                                    if(isset($datatableVar["subtotal"][$k])) {
                                        if($datatableVar["subtotal"][$k]=='true')
                                            $arrSumkey[] =   $k;
                                    }
                                    $k++;
                                }
                            }
                            $datcount++;
                        }
                        
                        if($arrFinal) {
                            if(isset($arrSumkey)) {
                                $arrSumkey  =   array_unique($arrSumkey);
                                if($arrSumkey) {
                                    $totsumtmp["SUMTOTAL"]  =   array();
                                    foreach($arrSumkey as $arrSumrec) {
                                        $totsumtmp["SUM"]   =   array();
                                        $sum    =   0;
                                        for($k=0;$k<$datcount;$k++) {
                                            if(isset($arrFinal["GROUPCATEGORY"][$k])) {
                                                $k++;
                                                $totsumtmp["SUM"][]    =   $sum;
                                                $sum    =   0;
                                            }
                                            if(isset($arrFinal["SUBDATA"][$k])) {
                                                $sum    =   $sum+$arrFinal["SUBDATA"][$k][$arrSumrec]["value"];
                                            }
                                        }
                                        $totsumtmp["SUM"][] =   $sum;
                                        $tmtsumarr            =   $totsumtmp["SUM"];
                                        $totsumtmp["SUMTOTAL"][]    =   $tmtsumarr;
                                    }
                                }
                            }
                        }
                        
                        if($arrFinal) {
                            $subdataflag=0;
                            $totlflag=1;
//                            $countrows   =   count($arrFinal['SUBDATA']);
                            for($a=0;$a<$datcount;$a++) {
                                if(isset($arrFinal['SUBDATA'][$a])) {
                                    $tabledata.='<tr>';
                                    if($arrFinal['SUBDATA'][$a]) {
                                        $k=0;
                                        foreach($arrFinal['SUBDATA'][$a] as $title){
                                            $tabledata.='<td style="text-align:'.$title["txtalign"].';width:'.$title["width"].';">';
                                            if($title["type"]=='currency') {
                                                $tabledata.=$this->formatslist->formatCurrencywithHash($title["value"]);
                                            } else if($title["type"]=='double') {
                                                $tabledata.=$this->formatslist->formatDoubleNumber($title["value"]);
                                            } else if($title["type"]=='number') {
                                                $tabledata.=$this->formatslist->formatIntegerNumber($title["value"]);
                                            } else if($title["type"]=='date') {
                                                if($title["value"])
                                                    $tabledata.=$this->formatslist->dateFormat($title["value"]);
                                            } else if($title["type"]=='datetime') {
                                                if($title["value"])
                                                    $tabledata.=$this->formatslist->datetimeFormat($title["value"]);
                                            } else
                                                $tabledata.=$title["value"];
                                            $tabledata.='</td>';
                                            $k++;
                                        }
                                    }
                                    $tabledata.='</tr>';
                                    $subdataflag    =   1;
                                }
                                
                                if(isset($arrFinal['GROUPCATEGORY'][$a]) && $subdataflag==1) {
                                    $subdataflag    =   0;
                                    if(isset($datatableVar["subtotal"])) {
                                        if($subtotalflag==1)
                                            $tabledata.=    '<tr>';
                                        $sfalg      =   0;
                                        foreach($datatableVar["subtotal"] as $chksumtitle) {
                                            if($chksumtitle=='true') {
                                                if(isset($totsumtmp["SUMTOTAL"][$sfalg][$totlflag])) {
                                                    $tabledata.='<td style="text-align:right;font-weight:bold;">'.
                                                        $this->formatslist->formatCurrencywithHash($totsumtmp["SUMTOTAL"][$sfalg][$totlflag]).'</td>';
                                                }
                                                $sfalg++;
                                            } else {
                                                if($subtotalflag==1)
                                                    $tabledata.='<td>&nbsp;</td>';
                                            }
                                        }
                                        if($subtotalflag==1)
                                            $tabledata.='</tr>';
                                        $totlflag++;
                                    }
                                }
                                
                                if(isset($arrFinal['GROUPCATEGORY'][$a])) {
                                    $tabledata.='<tr>';
                                    $tabledata.='<td colspan="'.$totalcols.'" style="font-weight:bold;">'
                                        .$arrFinal['GROUPCATEGORY'][$a].'</td>';
                                    $tabledata.='</tr>';
                                }
                                
                            }
                            
                            if(isset($datatableVar["subtotal"])) {
                                if($subtotalflag==1)
                                    $tabledata.='<tr>';
                                $sfalg  =   0;
                                foreach($datatableVar["subtotal"] as $chksumtitle) {
                                    if($chksumtitle=='true') {
                                        if(isset($totsumtmp["SUMTOTAL"][$sfalg][$totlflag])) {
                                            $tabledata.='<td style="text-align:right;font-weight:bold;">'.
                                                $this->formatslist->formatCurrencywithHash($totsumtmp["SUMTOTAL"][$sfalg][$totlflag]).'</td>';
                                        }
                                        $sfalg++;
                                    } else {
                                        if($subtotalflag==1)
                                            $tabledata.='<td>&nbsp;</td>';
                                    }
                                }
                                if($subtotalflag==1)
                                    $tabledata.='</tr>';
                                $totlflag++;
                            }
                            
//                            $tabledata.='<td style="text-align:right;font-weight:bold;">'.
//                                                        $totsumtmp["SUMTOTAL"][$sfalg][$totlflag].'</td>';
                        }
                        
                    } else {
                        foreach($dataresult as $datarow) {
                            $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>