<?php
$must_login                = true ;
$require_path              = '../../../' ;
$require_sub               = '../../' ;
require( $require_sub.'header.php' ) ;
require( $require_path.'languages/'.$array['lang'].'.php' ) ;

$html                      = '' ;

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

    $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'          => [
            'plus'          => $months,
            'minus'         => $months
        ],
    ] ;
    if ( $staff_settings['reportadjustmentbranch'] == 'yes' ){
        $array_report['branch'] = [
            'plus'          => $months,
            'minus'         => $months
        ] ;
    }

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

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

        $search_query       =  " AND ( b.created_branch_id = '".$array['branch_id']."' XXXXXX )" ;
        switch ( $v ){
            case 'personal' :
                $search_query = str_replace( 'XXXXXX', " AND b.created_by = '".$staff_info['staff_id']."'", $search_query ) ;
            break ;
            case 'branch'   :
                $search_query = str_replace( 'XXXXXX', '', $search_query ) ;
            break ;
        }

        $select_report     = $mysqli->query( "SELECT SUM(a.point) as total, b.adjustment_type, MONTH(b.created_at) as month FROM staff_adjustment_point a
                                              LEFT JOIN staff_adjustment b ON ( a.adjustment_id = b.adjustment_id )
                                              WHERE a.deleted_at IS NULL AND b.deleted_at IS NULL AND b.created_at LIKE '%".$search_year."%' ".$search_query."
                                              GROUP BY b.adjustment_type, MONTH(b.created_at)" ) ;

        if ( $select_report->num_rows > 0 ){
            while ( $row_report = $select_report->fetch_assoc() ){
                $array_report[$v][$row_report['adjustment_type']][$row_report['month']] = $row_report['total'] ;
            }
        }

    }


    // direct echo
    if ( $array['iswebview'] == 'yes' ){

        $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>' ;

                    if ( count($array_report) > 1 ){
                        $html .= '
                        <div class="report_chart_filter_type">
                            <a href="?search_type=personal'.resetGetParams($array, [ 'search_type' ]).'" class="report_chart_filter_type_button '.( $search_type == 'personal' ? 'active' : '' ).'">
                                '.$lang['By Personal'].'
                            </a>
                            <a href="?search_type=branch'.resetGetParams($array, [ 'search_type' ]).'" class="report_chart_filter_type_button '.( $search_type == 'branch' ? 'active' : '' ).'">
                                '.$lang['By Branch'].'
                            </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['Give Point'].'",
                                                data        : [ "'.implode( '", "', $vreport['plus'] ).'" ]
                                            },
                                            {
                                                label       : "'.$lang['Minus Point'].'",
                                                data        : [ "'.implode( '", "', $vreport['minus'] ).'" ]
                                            }
                                        ],
                                        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' ) ;
?>