<?php
class report_disk extends c_report
{
 var $title='Отчет по дисковому пространству' ;
 var $filter=array() ;
 public $use_fusioncharts=1 ;
 public $view_by_space_filter=1 ;
 var $step=0 ;

 function setting()
   {  $data_info=execSQL_van('select min(c_data) as min_data,max(c_data) as max_data from obj_site_disk') ;
      $t=$data_info['min_data'] ;
      if ($data_info['max_data']>$data_info['min_data']) while($t<$data_info['max_data'])
           {   $ts=getdate($t) ;
               $cur_month=mktime(0,0,0,$ts['mon'],1,$ts['year']) ;
               $arr_from[$cur_month]=get_month_year($cur_month,'month year') ;
               $cur_month=mktime(0,0,0,$ts['mon']+1,1,$ts['year'])-1 ;
               $arr_to[$cur_month]=get_month_year($cur_month,'month year') ;
               $t=mktime(0,0,0,$ts['mon']+1,1,$ts['year']) ;
           }

     ?><div id="panel_filter">
                <form id="sky-form" class="sky-form"><input type="hidden" name="filter_dir" value="<?echo _CUR_REPORT_DIR?>">
                     <fieldset>
                         <div class="row">
                             <section class="col col-2">
                                 <label class="label">Информация:</label>
                                 <label class="select">
                                     <select name="filter[mode]">
                                         <option value="0" <?if($this->filter['mode']==0) echo 'selected'?>>Размер файлов</option>
                                         <option value="1" <?if($this->filter['mode']==1) echo 'selected'?>>Количество файлов</option>
                                     </select>
                                 </label>
                             </section>
                             <section class="col col-2">
                                 <label class="label">С, месяц:</label>
                                 <label class="select">
                                     <select name="filter[time_from]"><option></option>
                                         <?foreach($arr_from as $t=>$month){?><option value="<?echo $t?>" <?if ($this->filter['time_from']==$t) echo 'selected'?>><?echo $month?></option><?}?>
                                     </select>
                                 </label>
                             </section>
                             <section class="col col-2">
                                 <label class="label">По, месяц</label>
                                 <label class="select">
                                     <select name="filter[time_to]"><option></option>
                                         <?foreach($arr_to as $t=>$month){?><option value="<?echo $t?>" <?if ($this->filter['time_to']==$t) echo 'selected' ?>><?echo $month?></option><?}?>
                                     </select>
                                 </label>
                             </section>
                         </div>
                     </fieldset>
                     <?$this->panel_buttons();?>
                 </form>
         </div>
     <?
   }

 function prepare_result_data()
 {  $data_info=execSQL_van('select min(c_data) as min_data,max(c_data) as max_data from obj_site_disk where clss=280') ;
    //$recs=execSQL('select pkey,c_data,status,obj_name,size,cnt from obj_site_disk where c_data>='.$data_from.' and c_data<='.$data_to.' order by c_data,obj_name',1) ;
    //$recs2=group_by_field() ;
    /*$result=array() ;
    if (_sizeof($recs)) foreach($recs as $rec)
     { $result[date('d.m.Y',$rec['c_data'])][$rec['status']]['size']+=$rec['size'] ;
       $result[date('d.m.Y',$rec['c_data'])][$rec['status']]['cnt']+=$rec['cnt'] ;
     }*/

    if (!$this->filter['time_from'])
    { $ts=getdate($data_info['min_data']) ;
      $this->filter['time_from']=mktime(0,0,0,$ts['mon'],1,$ts['year']) ;;
    }

    if (!$this->filter['time_to'])
    { $ts=getdate($data_info['max_data']) ;
      $this->filter['time_to']=mktime(0,0,0,$ts['mon']+1,1,$ts['year'])-1 ;;
    }

    $result2=array() ;
    $tf=getdate($this->filter['time_from']) ;
    $tt=getdate($this->filter['time_to']) ;
    $this->step=($tf['mon']==$tt['mon'])? '1d':'1m' ;
    damp_array($this->filter) ;
    echo 'step='.$this->step.'<br>' ;
    $t=$this->filter['time_from'] ;
    while($t<=$this->filter['time_to'])
        {   $ts=getdate($t) ;   $t1=$t ;
            $cur_month=mktime(0,0,0,$ts['mon'],1,$ts['year']) ;
            $arr_month[$cur_month]=get_month_year($cur_month,'month year') ;
            switch($this->step)
            { case '1d': $t=mktime(0,0,0,$ts['mon'],$ts['mday']+1,$ts['year']) ; $t2=$t-1;
                         $result2[date('d.m.Y',$t1)]['size']=execSQL_row('select status,round(sum(size)/(1024*1024)) from obj_site_disk where c_data='.$t1.' group by status',0,1) ;
                         $result2[date('d.m.Y',$t1)]['cnt']=execSQL_row('select status,sum(cnt) from obj_site_disk where c_data='.$t1.' group by status',0,1) ;
                         break  ;
              case '1m': $t=mktime(0,0,0,$ts['mon']+1,1,$ts['year']) ; $t2=$t-1;
                         // необходимо получить данные по последнему дню месяца.
                         // Но в текущем месяце надо использовать текущий день
                         $last_month_data=execSQL_value('select max(c_data) from obj_site_disk where clss=280 and  size>0 and c_data>='.$t1.' and c_data<'.$t2,2) ;
                         $result2[date('d.m.Y',$t1)]['size']=execSQL_row('select status,round(sum(size)/(1024*1024)) from obj_site_disk where c_data='.$last_month_data.' group by status',0,1) ;
                         $result2[date('d.m.Y',$t1)]['cnt']=execSQL_row('select status,sum(cnt) from obj_site_disk where c_data='.$last_month_data.' group by status',0,1) ;
                         break  ;
              default:   $t=$this->filter['time_to'] ;$t2=$t ;
            }
        }
    return($result2) ;

}

function HTML_report_body($options=array())
{   $result=$this->prepare_result_data() ;
    //print_2x_arr($result) ;
    switch($this->filter['mode'])
    { case 1:  $caption='Количество файлов системы ЭСМО' ;
               $pYAxisName='Количество файлов, шт.' ;
               $fname='cnt' ;
               break ;
      default: $caption='Размер занимаемого дискового пространства системы ЭСМО' ;
               $pYAxisName='Суммарный размер файлов, МБ' ;
               $fname='size' ;


    }
    switch($this->step)
    { case '1d': $yAxisName=get_month_year(($this->filter['time_from']),'month year') ; break ;
      case '1m': $yAxisName='Месяцы' ; break ;

    }

    $arr_month=array_keys($result) ;

    ?><div id="chart-container">Ожидайте, идет построение графика</div>
      <script type="text/javascript">
          $j(document).ready(function(){
              FusionCharts.ready(function(){


             var revenueChart2 = new FusionCharts({
                   type: 'mscombi2d',
                   //type: 'mscombidy2d',
                   //type: 'LogMSColumn2D',
                   renderAt: 'chart-container',
                   width: '1100',
                   height: '400',
                   dataFormat: 'json',
                   dataSource: {
                       "chart": {
                           "caption": "<?echo $caption?>",
                           "subCaption": "от <? echo date('d.m.Y',$this->filter['time_from']).' до '.date('d.m.Y',$this->filter['time_to'])?>",
                           "xAxisname": "<?echo $yAxisName?>",
                           "pYAxisName": "<?echo $pYAxisName?>",
                           "sYAxisName": "Число МО",
                           "numberPrefix": "",
                           //"sNumberSuffix" : "МБ",
                           //"sYAxisMaxValue" : "50",
                           //"sYAxisMaxValue" : "15",

                           //Cosmetics
                           "paletteColors" : "#f2c500,#1aaf5d,#2196F3,#FF0000,#8BC34A",
                           "baseFontColor" : "#333333",
                           "baseFont" : "Helvetica Neue,Arial",
                           "captionFontSize" : "14",
                           "subcaptionFontSize" : "14",
                           "subcaptionFontBold" : "0",
                           "showBorder" : "0",
                           "bgColor" : "#ffffff",
                           "showShadow" : "0",
                           "canvasBgColor" : "#ffffff",
                           "canvasBorderAlpha" : "0",
                           "divlineAlpha" : "100",
                           "divlineColor" : "#999999",
                           "divlineThickness" : "1",
                           "divLineIsDashed" : "1",
                           "divLineDashLen" : "1",
                           "divLineGapLen" : "1",
                           "usePlotGradientColor" : "0",
                           "showplotborder" : "0",
                           "showXAxisLine" : "1",
                           "xAxisLineThickness" : "1",
                           "xAxisLineColor" : "#999999",
                           "showAlternateHGridColor" : "0",
                           "showAlternateVGridColor" : "0",
                           "legendBgAlpha" : "0",
                           "legendBorderAlpha" : "0",
                           "legendShadow" : "0",
                           "legendItemFontSize" : "10",
                           "legendItemFontColor" : "#666666"
                       },
                       "categories":
                       [{
                           "category": [ <?foreach($arr_month as $t=>$date)
                           {  $time_s=strtotime($date) ;
                              $ee=($this->step=='1d')? $date:get_month_year($time_s,'month year') ;
                              echo '{ "label": "'.$ee.'" },' ;
                           }

                           ?>]
                       }],
                       "dataset":
                       [   {
                               "seriesName": "Общий размер файлов системы ЭСМО",
                               "data": [ <?foreach($result as $t=>$res) echo '{ "value" : "'.$res[$fname][0].'" },'?>]
                           },
                           {
                               "seriesName": "Программный код ЭСМО",
                               "renderAs": "line",
                               "showValues": "0",
                               "divlineThickness" : "1",
                               "data": [ <?foreach($result as $t=>$res) echo '{ "value" : "'.$res[$fname][1].'" },'?>]
                           },
                           {
                               "seriesName": "Данные ЭСМО",
                               "renderAs": "line",
                               "showValues": "0",
                               "divlineThickness" : "1",
                               "data": [ <?foreach($result as $t=>$res) echo '{ "value" : "'.$res[$fname][2].'" },'?>]
                           },
                           {
                               "seriesName": "Временные файлы",
                               "renderAs": "line",
                               "showValues": "0",
                               "divlineThickness" : "1",
                               "data": [ <?foreach($result as $t=>$res) echo '{ "value" : "'.$res[$fname][3].'" },'?>]
                           }
                       ]
                   },
                   events:
                   { dataLabelClick: function (eventObj, argsObj)
                     {
                       /*console.log('Chart clicked at ' + argsObj.chartX + ','
                                   + argsObj.chartY+ ','
                                   + argsObj.text+ ','
                                   + argsObj.dataIndex+ ','
                                   + argsObj.link+ ','
                                   + argsObj.id);
                       */
                       send_ajax_request({cmd:'log/get_disk_info',data:argsObj.text}) ;
                     }
                    }
               });

               revenueChart2.render();
           });
          });
                </script>
      <style type="text/css">#chart-container{margin:0 auto;width:1100px;}</style>
      <div id="disk_info"></div>
    <?


}



}

?>
