public function Top_Department()
    {
        $bl = new BLogger();
        $beginDate = addslashes($_GET['begindate']);
        if ($beginDate == "") {
            $month = date("n");
            $day = 1;
            $year = date("Y");
            $timeAwal = mktime(0, 0, 0, $month, $day, $year);
            $timeAkhir = mktime(0, 0, 0, $month + 1, $day, $year);
            $mon = $month;
            $y = $year;
            $timeAwalText = date("Y") . "-" . date("m") . "-01 00:00:00";
            $ts = mktime(0, 0, 0, date("n") + 1, 1, date("Y"));
            $timeAkhirText = date("Y-m-d H:i:s", $ts);
        } else {
            $timeAwalText = $beginDate . " 00:00:00";
            $bgindatestr = strtotime($beginDate);
            $mon = date("n", $bgindatestr);
            $y = date("Y", $bgindatestr);
            $ts = mktime(0, 0, 0, $mon + 1, 1, $y);
            $timeAkhirText = date("Y-m-d H:i:s", $ts);
            $year = $y;
        }
        /*echo $timeAwal;
          echo "<br>";
          echo $timeAkhir;
          */
        ?>
        <h1>Top Department</h1>    
         <?php 
        $text = "(b_log_time BETWEEN '{$timeAwalText}' AND '{$timeAkhirText}')";
        $textAs = "b_timestamp >= {$timeAwal} AND b_timestamp < {$timeAkhir}";
        // echo "<br>";
        // echo $text;
        $arrLogs = $bl->getWhere("{$text} ORDER BY b_timestamp ASC");
        //pr($arrLogs);
        foreach ($arrLogs as $h) {
            if ($h->log_user_org == "" || $h->log_user_org == 0) {
                continue;
            }
            $newArr["org_" . $h->log_user_org][] = $h;
        }
        foreach ($newArr as $url => $n) {
            $newArrs[$url] = count($n);
        }
        asort($newArrs);
        $jadi = array_reverse($newArrs);
        // pr($newArr);
        $t = time();
        //pr($jadi);
        ?>
<div class="table-responsive">
    <form class="form-inline">
  <div class="form-group">
    <label for="monselection_<?php 
        echo $t;
        ?>
">Select Month</label>
    <select class="form-control" id="monselection_<?php 
        echo $t;
        ?>
"><?php 
        $beginyear = date("Y") - 5;
        $endyear = date("Y");
        $beginyear = 2015;
        $mons = array(1 => "Jan", 2 => "Feb", 3 => "Mar", 4 => "Apr", 5 => "May", 6 => "Jun", 7 => "Jul", 8 => "Aug", 9 => "Sep", 10 => "Oct", 11 => "Nov", 12 => "Dec");
        for ($x = $beginyear; $x <= $endyear; $x++) {
            for ($m = 1; $m < 13; $m++) {
                if ($m == $mon && $x == $year) {
                    $select = "selected";
                    echo "in";
                } else {
                    $select = "";
                }
                if ($x == date("Y")) {
                    if ($m > date("n")) {
                        continue;
                    }
                }
                ?>
                <option <?php 
                echo $select;
                ?>
 value="<?php 
                echo $x;
                ?>
-<?php 
                echo $m;
                ?>
-01"><?php 
                echo $x;
                ?>
 <?php 
                echo $mons[$m];
                ?>
 </option>   
                 <?php 
            }
        }
        ?>
    </select> 
  </div>
 <button class="btn btn-default" onclick="window.open('<?php 
        echo _SPPATH . _PHOTOURL . "logs/TopDepartment_" . $year . "_" . $mon;
        ?>
.xls', '_blank');event.preventDefault();">Download</button>
</form>
    <script>
        $('#monselection_<?php 
        echo $t;
        ?>
').change(function(){
            var slc = $('#monselection_<?php 
        echo $t;
        ?>
').val();
            openLw('Top_Department','<?php 
        echo _SPPATH;
        ?>
BLoggerWeb/Top_Department?begindate='+slc,'fade'); 
        });
        </script>
    <div style="padding: 10px;"></div>
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>Rank</th>
                
                <th>Department</th>
                <th>URL</th>
                <th>No.Visits</th>
            </tr>
        </thead>
        <?php 
        $num = 0;
        $arrExcel = array();
        $arrExcel2 = array();
        $arrExcel[] = "Rank;Department;No.Visits";
        foreach ($jadi as $url => $n) {
            $alog = $newArr[$url];
            $num++;
            $dp = new RoleOrganization();
            $id = explode("_", $url);
            //pr($id);
            $dp->getByID($id[1]);
            $arrExcel[] = "{$num};{$dp->organization_name};{$n}";
            ?>
        <tr>
            <td><?php 
            echo $num;
            ?>
</td>
            <td><span style="cursor:pointer; text-decoration: underline;" onclick="$('.detail_<?php 
            echo $url;
            ?>
').toggle();"><?php 
            echo $dp->organization_name;
            ?>
</span></td>    
            <td></td>
            <td><?php 
            echo $n;
            ?>
</td>
        </tr>
        <?php 
            $obje = array();
            $obje["Rank"] = $num;
            $obje["Department"] = $dp->organization_name;
            $obje["URL"] = "";
            $obje["No Visits"] = $n;
            $arrExcel2[] = $obje;
            $key = $url;
            $newArr_dalam = array();
            foreach ($alog as $h) {
                $newArr_dalam[$h->b_log_url][] = $h;
            }
            $newArrs_dalam = array();
            foreach ($newArr_dalam as $url => $n) {
                $newArrs_dalam[$url] = count($n);
            }
            asort($newArrs_dalam);
            $jadi_dalam = array_reverse($newArrs_dalam);
            ?>
        <?php 
            $num_dalam = 0;
            foreach ($jadi_dalam as $url_dalam => $n_dalam) {
                $num_dalam++;
                ?>
        <tr  class="detail_<?php 
                echo $key;
                ?>
" style="display:none;">
            <td></td>
            <td></td>
            <td><?php 
                echo $url_dalam;
                ?>
 <a href="<?php 
                echo $url_dalam;
                ?>
" target="_blank">link</a></td>           
            <td><?php 
                echo $n_dalam;
                ?>
</td>
        </tr>
        <?php 
                $obje2 = array();
                $obje2["Rank"] = " ";
                $obje2["Department"] = " ";
                $obje2["URL"] = $url_dalam;
                $obje2["No Visits"] = $n_dalam;
                $arrExcel2[] = $obje2;
            }
            ?>
        
        <?php 
        }
        ?>
    </table>
</div>    
         <?php 
        /*
                $fname = "TopDepartment_".$year."_".$mon. ".csv";
                $file = fopen(_PHOTOPATH . "logs/" . $fname, "w+");
        
                foreach ($arrExcel as $line) {
                        fputcsv($file, explode(';', $line));
                }
        
                fclose($file);*/
        $fname = _PHOTOPATH . "logs/" . "TopDepartment_" . $year . "_" . $mon . ".xls";
        ExcelExporter::saveIt($arrExcel2, $fname);
    }