<?php

    global $db , $error , $page , $tpl , $c;

    // Wyświetlenie zakladki logowania
    if( isset( $_REQUEST['login'] ) )
    {
        page::$i->val( js::InitClick('#InitLogin') , false );
    }
    
    /** OLD **/

    $orderby = " order by ";

    switch( $_REQUEST['o'] ){
    
        case 'profit_win':
            $orderby .= "u.profit_win desc";
            $ordervar = $_REQUEST['o'];
            break;
    
        case 'yield':
            $orderby .= "u.yield desc";
            $ordervar = $_REQUEST['o'];
        break;
    
        default:
            $orderby .= "u.stars desc"; // , u.yield desc
            $ordervar = 'default';
        break;
    
    }

    switch( $_REQUEST['w'] ){
            
        case 'over0':
            $whereby = " and u.picks>0";
            $wherevar = $_REQUEST['w'];
            break;
        case 'over100':
            $whereby = " and u.picks>100";
            $wherevar = $_REQUEST['w'];
            break;
        case 'over250':
            $whereby = " and u.picks>250";
            $wherevar = $_REQUEST['w'];
            break;
        case 'over500':
            $whereby = " and u.picks>500";
            $wherevar = $_REQUEST['w'];
            break;
        case 'over1000':
            $whereby = " and u.picks>1000";
            $wherevar = $_REQUEST['w'];
            break;
        default:
                $whereby="";
                $wherevar="default";
            break;
    }


    //$query = db::$i->q("select a.uid,u.profit,u.yield,u.win_count,u.lose_count from abonament a left join user u on u.id=a.uid where a.status=1{$whereby} group by uid".$orderby,false,true);
    $query = db::$i->q("select a.status as astatus,u.profit,u.* from user u left join abonament a on a.uid=u.id where a.status=1 and u.hidden_list is null{$whereby}".$orderby,false,true);
    // DEBUG SQL and HTTP QUERY
    //echo '<div>'.db::$i->query.'</div>';
    //echo '<pre>';
    //print_r($_GET);
    //echo '</pre>';

    

    if( $abonament_count = db::$i->rows ){

        while( $item = db::$i->f( $query ) ){
            
        unset($myData);
        unset( $chart_data );
        
            // UPDATE 29.09.2015 / Synchronizacja do starej wersji listowania 
            $item['uid'] = $item['id'];
            
            list( $userrates ) = db::$i->row("select count(1) as userrates from rate where uid='".$item['uid']."'");

            $UserStat = statistics::user( $item['uid'] );

        // Typy u poszczegolnych bukmacherow
        $query_string = "
            SELECT e.bukmacher, b.name, (
            SELECT COUNT( 1 )
            FROM `event_selecting`
            WHERE `uid` ='".$item['uid']."'
            AND `bukmacher` = e.bukmacher
            ) AS bukmachercount
            FROM `event_selecting` e LEFT JOIN `bukmacher` b ON b.id=e.bukmacher WHERE e.uid='".$item['uid']."' GROUP BY e.bukmacher";

            $UserBukmachers = db::$i->converted( $query_string );

        /**
        $chart = db::$i->q("select e.* from event_selecting e where e.uid='" . $item['uid'] . "' group by e.hash ORDER BY e.crt DESC, e.result ASC", false, true);
        if (db::$i->rows) {
            unset($chart_data);
            while ($item_e = db::$i->f($chart)) {
                $chart_data[] = event::parse($item_e);
            }
        }

        $myData = array_reverse($chart_data);

        foreach ($myData as $key => $row) {
            $course = 1;
            foreach ($row['events'] as $event) {
                switch ($event['result']) {
                    case '1':
                        $course = $event['course_one'] * $course;
                        break;
                    case '2':
                        $course = 0;
                        break;
                    case null:
                        if ($course > 0) {
                            $course = -1;
                        }
                        break;
                }
            }

            if ($course >= 0) {
                $value = $value + (($row['amount'] * $course) - $row['amount']);
                $coupons[$key]['date'] = substr($row['crt'], 0, -3);
                $coupons[$key]['value'] = $value;
            }
        }
        **/
        
        // STATYSTYKI - GRAF 
        
        $start = time::getModify( "-12 MONTHS" , date("Y-m-d H:i:s") , "Y-m");
        
        
        for( $i=0; $i!=13 ; $i++ ){
            
            unset( $thismonth );
            unset( $data_thismonth );
            unset( $months_data );
            
            $nowy = new DateTime( $start );
            $nowy->modify("+" . $i ." MONTHS");
            
            
            // DANE DO STAT.
            $thismonth = db::$i->q("SELECT * FROM event_selecting WHERE uid='".$item['uid']."' and crt LIKE('".$nowy->format("Y-m")."%') and `result` is not null ORDER BY `crt` ASC",true);
            
            if( db::$i->rows ){
                
  
                
                while( $item_month = mysql_fetch_assoc( $thismonth ) ){
                    $data_thismonth[] = $item_month;
                }

                          
                $months_data = statistics::events( $data_thismonth );
                
                $coupons[$nowy->format("Y-m")]['date'] = l('month2_'.$nowy->format("m"));
                $coupons[$nowy->format("Y-m")]['value'] = $months_data['profit_win'];
            
            } else {
                $coupons[$nowy->format("Y-m")]['date'] = l('month2_'.$nowy->format("m"));
                $coupons[$nowy->format("Y-m")]['value'] = 0;
            }
          
         }  
        // END: STATYSTYKI - GRAF 
         
         
        // Pobranie abonamentów usera
        $UserData = db::$i->row("select * from user where id='" . $item['uid'] . "'");

        $query_user = db::$i->q("select a.* from abonament a where a.uid='" . $item['uid'] . "' and a.status=1 order by a.days", false, true);
        if (db::$i->rows) {

            while ($item_abonament = db::$i->f($query_user)) {

                $abonament_group[] = abonament::parse($item_abonament);
            }
        }
        // END: pobranie abonamentów usera

            if( count($abonament_group)>0 ){
            $data[$item['uid']] = array(
                'UserData' => user::parseInfo( $UserData ),
                'UserOffers' => $abonament_group,
                'UserStat' => $UserStat,
                'UserBukmachers' => $UserBukmachers,
                'UserChart' => $coupons,
                'UserRates' => $userrates
            );
        }
            unset( $abonament_group );
    }

}

db::$i->q("SELECT 1 FROM `abonament` GROUP BY `uid`",true);
$abonament_count = db::$i->rows;

$tpl->assign(array(
    'abonament' => $data,
    'abonament_count' => $abonament_count,
    'o' => $_REQUEST['o'],
    'w' => $_REQUEST['w']
));


module::$instance->on();

?>