<?php
$must_login          = true ;
$require_path        = '../../../' ;
$require_sub         = '../../' ;
require( $require_sub.'header.php' ) ;

$html                = '' ;
$list                = [] ;

$total_accumulated   = 0 ;
$total_redeem        = 0 ;

if ( $boolean_login ){
    $status          = '201' ;


    if ( $array['iswebview'] == 'no' ){


        // get point list
        $query           = "SELECT a.point_id, a.file, a.created_at, b.title, b.content FROM profile_point a
                            LEFT JOIN profile_point_translation b ON ( a.point_id = b.point_id )
                            WHERE a.deleted_at IS NULL AND b.lang = '".$array['lang']."'" ;
        $mysqli_query    = $mysqli->query( $query ) ;

        if ( $mysqli_query->num_rows > 0 ){
            $status             = '200' ;

            $row                = $mysqli_query->fetch_assoc() ;
            $temp               = [] ;
            $temp['title']      = dataFilter( $row['title'] ) ;
            $temp['content']    = dataFilter( $row['content'] ) ;
            $temp['file']       = ( $row['file'] != '' ? PATH.'uploads/ProfilePoint/b/'.$row['file'] : '' ) ;
            $temp['created_at'] = resetDateFormat( $row['created_at'] ) ;
            $list               = $temp ;
        }


        // get total accumulated point
        // 'system','staff_point_movement_cutoff','task','redeem','adjustment','training'
        $select_movement = $mysqli->query( "SELECT SUM( amount ) as total FROM staff_point_movement
                                            WHERE staff_id = '".$staff_info['staff_id']."' AND from_table IN ( 'system','task','adjustment','training' ) AND created_at LIKE '%".date('Y', time())."-%'" ) ;
        if ( $select_movement->num_rows > 0 ){
            $data_movement     = $select_movement->fetch_assoc() ;
            $total_accumulated = $data_movement['total'] ;
        }

        // get total redeem
        $select_movement = $mysqli->query( "SELECT SUM( amount ) as total FROM staff_point_movement
                                            WHERE staff_id = '".$staff_info['staff_id']."' AND from_table IN ( 'redeem' ) AND created_at LIKE '%".date('Y', time())."-%'" ) ;
        if ( $select_movement->num_rows > 0 ){
            $data_movement     = $select_movement->fetch_assoc() ;
            $total_redeem      = $data_movement['total'] ;
            $total_redeem      = numberFormat( $total_redeem < 0 ? -( $total_redeem ) : 0 ) ;
        }

        $data['list']              = $list ;
        $data['total_accumulated'] = $total_accumulated ;
        $data['total_redeem']      = $total_redeem ;

    }



    // direct echo
    if ( $array['iswebview'] == 'yes' ){
        require( $require_path.'languages/'.$array['lang'].'.php' ) ;

        $search_year           = ( $array['search_year'] != '' ? $array['search_year'] : date('Y') ) ;
        $search_type           = ( $array['search_type'] != '' ? $array['search_type'] : 'personal' ) ;

        $staff_settings        = $staff_info['staff_settings'] ;

        $months                 = [] ;
        $months_name            = [ $lang['Jan'], $lang['Feb'], $lang['Mar'], $lang['Apr'], $lang['May'], $lang['Jun'], $lang['Jul'], $lang['Aug'], $lang['Sep'], $lang['Oct'], $lang['Nov'], $lang['Dec'] ] ;
        for ( $a = 1 ; $a <= 12 ; $a++ ){ $months[$a] = 0 ; }

        // monthly report = by personal or whole branch
        $array_report           = [
            'personal'          => [
                'yes'           => $months
            ],
        ] ;

        $filtertype             = [] ;
        foreach ( $array_report as $k => $v ){
            if ( $k == $search_type ){
                $filtertype[]   = $k ;
            }
        }

        foreach ( $filtertype as $k => $v ){

            $search_query       =  " AND ( a.staff_id = '".$staff_info['staff_id']."' )" ;

            $select_task        = $mysqli->query( "SELECT SUM(a.staff_point_achievement) as total, MONTH(a.reported_at) as month FROM staff_monthly_achievement a
                                                   WHERE a.deleted_at IS NULL AND a.reported_at LIKE '%".$search_year."%' ".$search_query."
                                                   GROUP BY MONTH(a.reported_at)" ) ;
            if ( $select_task->num_rows > 0 ){
                while ( $row_report = $select_task->fetch_assoc() ){
                    $array_report[$v]['yes'][$row_report['month']] = $row_report['total'] ;
                }
            }
        }


        $html         .= '
        <!DOCTYPE html>
        <html lang="en">
            <head>
                <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
                <meta http-equiv="X-UA-Compatible" content="IE=edge" />
                <meta name="viewport" content="width=device-width, initial-scale=1" />
                <link rel="stylesheet" href="'.PATH.'css/bootstrap.css?v='.filemtime($require_path.'css/bootstrap.css').'" />
                <script src="'.PATH.'scripts/chart.js"></script>
            </head>
            <body>
                <div class="report_chart_main">

                    <div class="report_chart_filter_year">
                        <a href="?search_year='.( $search_year - 1 ).resetGetParams($array, [ 'search_year' ]).'" class="report_chart_filter_year_button">
                            <i class="fa fa-chevron-left" aria-hidden="true"></i>
                        </a>
                        <span class="report_chart_filter_year_span">'. $search_year .'</span>
                        <a href="?search_year='.( $search_year + 1 ).resetGetParams($array, [ 'search_year' ]).'" class="report_chart_filter_year_button">
                            <i class="fa fa-chevron-right" aria-hidden="true"></i>
                        </a>
                    </div>' ;

                    foreach ( $array_report as $kreport => $vreport ){

                        if ( $kreport == $search_type ){

                            $title     = $lang['By '.ucwords($kreport).' Report'] ;
                            $titlename = $title.' ( '.$lang['Year'].' '.$search_year.' )' ;

                            $html .= '
                            <div class="report_chart_line">
                                <canvas id="myChart'.$kreport.'"></canvas>
                            </div>
                            <script>
                                const ctx'.$kreport.'    = document.getElementById("myChart'.$kreport.'") ;
                                const config'.$kreport.' = {
                                    type : "line",
                                    data : {
                                        datasets : [
                                            {
                                                label       : "'.$lang['Completed Task'].'",
                                                data        : [ "'.implode( '", "', $vreport['yes'] ).'" ]
                                            }
                                        ],
                                        labels   : [ "'.implode( '", "', $months_name ).'" ]
                                    },
                                    options: {
                                        responsive          : true,
                                        maintainAspectRatio : false,
                                        plugins             : {
                                            legend          : {
                                                position    : "top",
                                            },
                                            title           : {
                                                display     : true,
                                                text        : "'.$titlename.'"
                                            }
                                        }
                                    }
                                } ;
                                new Chart( ctx'.$kreport.', config'.$kreport.' ) ;
                            </script>' ;
                            
                        }
                    }

                $html .= '
                </div>
            </body>
        </html>' ;
    }

}

if ( $array['iswebview'] == 'yes' ){
    echo $html ;
    exit ;
}

require( $require_sub.'footer.php' ) ;
?>