<?php

include 'connect/cms-config.php' ;
include 'requires/function.php' ;
include 'extensions/mailer.php' ;

// include the class
include 'requires/class_resize.php' ;

if ( !permissionCheck($row_user, 'year-end-cut-off') ){
    echo '<script>alert("Sorry You Don\'t Have The Permission.")</script>';

    header('Location: index.php') ;
    exit ;
}

//nav menu
$active_main_menu = 'report' ;
$active_sub_menu = 'year-end-cut-off' ;


// keep parameter in value

$page = escapeString($_GET['page']) ;

$page_mode = escapeString($_GET['page_mode']) ;

$type = escapeString($_GET['type']) ;

$search = escapeString($_GET['search']) ;

$search_name = escapeString($_GET['search_name']) ;

$search_idno = escapeString($_GET['search_idno']) ;

$search_year = ( ($_GET['search_year'] !='' && preg_match("/[2][0][0-9][0-9]/u", $_GET['search_year'])) ? escapeString($_GET['search_year']) : date('Y'));

$export_excel = escapeString($_GET['export-excel']);

$show_another_script = true ;

$hide_title = false ;

// page header
$letter_head = getOwnerCompanyLetterHead($_SESSION['url_get_branch_admin']) ;

$search_query = '' ;

if( $search_name != ''){
	$search_query .= " AND b.staff_name LIKE '%".$search_name."%'" ;
}

if( $search_idno != ''){
	$search_query .= " AND b.staff_idno LIKE '%".$search_idno."%'" ;
}

if ( $search_year != '' && is_numeric($search_year) ) {
	$search_query .= " AND Year(a.created_at) = '".$search_year."'" ;
}else{
	$search_query .= " AND Year(a.created_at) = Year(Now())" ;
}


//pagination
if (isset($page) && !empty($page)) { $product_page = $page ; } else { $product_page = 1 ; } // next and prev page (5 thing need to change)


$start_from = ($product_page - 1) * LIMIT ; //end next and prev page


$mysqli_cutoff = $mysqli->query("SELECT a.*, b.staff_name, b.staff_idno FROM staff_point_movement_cutoff a LEFT JOIN staff b ON ( a.staff_id = b.staff_id ) WHERE a.staff_id != '' AND a.deleted_at IS NULL ".$search_query." ".$user_branch_permission_sql_b." ORDER BY a.amount DESC LIMIT $start_from, ".LIMIT) ; 



$mysqli_cutoff_query = "SELECT a.*, b.staff_name, b.staff_idno FROM staff_point_movement_cutoff a LEFT JOIN staff b ON ( a.staff_id = b.staff_id ) WHERE a.staff_id != '' AND a.deleted_at IS NULL ".$search_query." ".$user_branch_permission_sql_b." ORDER BY a.amount DESC";

$search_url = 'page_mode=all&search_name='.$search_name.'&search_idno='.$search_idno.'&search_year='.$search_year ;

$page_pagination = nextPrevious($product_page, LIMIT, $search_url, $mysqli_cutoff_query) ;

if ($export_excel != '' && $export_excel == 'export_eae') {
	$page_export_file_name = 'Year End Cut Off Report-';

    $t_title_header_excel = 'Year End Cut Off Report ('.$search_year.')';

    $array_header_excel =   array( 
                                'Year', 
                                'ID', 
                                'Name', 
                                'Point'
                            ) ;

    $mysqli_export = $mysqli->query( $mysqli_cutoff_query ) ;
 
    if ( $mysqli_export->num_rows > 0 ){
        while ( $mysqli_export_page = $mysqli_export->fetch_assoc() ){

            $array_body_excel[] = array( 
                                        $mysqli_export_page['cutoff_year']-1, 
                                        $mysqli_export_page['staff_idno'], 
                                        $mysqli_export_page['staff_name'], 
                                        $mysqli_export_page['amount']
                                    ) ;
        }
    }

    include 'export_excel_default.php';
}

include 'requires/page_header.php';

include 'requires/page_top.php';
?>

<?php  ?>
<div class="warper container-fluid">   	
    <div class="page-header"><h1>YEAR END CUT OFF<small><?= $lang['list']?></small></h1></div>

        <div class="panel panel-default" id="basic-table-title">
            <div class="panel-heading">
            	<a href="?page_mode=all&search_name=<?= $search_name?>&search_idno=<?= $search_idno?>&search_year=<?= $search_year - 1?>" class="btn btn-purple" style="margin-right: 20px ;">Prev</a>
                <?= $search_year ?>
                <a href="?page_mode=all&search_name=<?= $search_name?>&search_idno=<?= $search_idno?>&search_year=<?= $search_year + 1?>" class="btn btn-purple" style="margin-left: 20px ;">Next</a>
            	<a href="?page_mode=all&search_name=<?= $search_name?>&search_idno=<?= $search_idno?>&search_year=<?= $search_year ?>&export-excel=export_eae" style="float:right">Export As Excel</a>
            </div>
            <div class="panel-body">
                <form method="get" class="form-horizontal" style="max-width:600px;">
                    <div class="form-group">
                        <label class="col-sm-2 control-label"><?= $lang['Name'] ?></label>
                        <div class="col-sm-9">
                            <input type="text" name="search_name" value="<?= $search_name ?>" class="form-control" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label"><?= $lang['ID'] ?></label>
                        <div class="col-sm-9">
                            <input type="text" name="search_idno" value="<?= $search_idno ?>" class="form-control" />
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-11">
                            <input type="hidden" name="page_mode" value="<?= $page_mode ?>" />
                            <input type="hidden" name="search_year" value="<?= $search_year ?>" />
                            <button type="submit" class="btn btn-purple" style="float:right"><?= $lang['submit'] ?></button>
                        </div>
                    </div>
                </form>
            </div>
        </div>

    <div class="panel panel-default">
        <div class="panel-heading"></div>
        <div class="panel-body">
            <table cellpadding="0" cellspacing="0" border="0" class="responsive table table-striped table-bordered" id="basic-datatable">
                <thead>
                    <tr>
                        <th><?= $lang['Year']?></th>
                        <th><?= $lang['ID']?></th>
                        <th><?= $lang['Name']?></th>
                        <th><?= $lang['Point']?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php

                    if ($mysqli_cutoff->num_rows > 0){
                        while ($row_cutoff = $mysqli_cutoff->fetch_assoc()){
                            
                            echo '
                             <tr class="odd gradeX">
                                <td class="text_center">'.dataFilter($row_cutoff['cutoff_year']-1).'</td>
                                <td class="text_center">'.dataFilter($row_cutoff['staff_idno']).'</td>
                                <td class="text_center">'.dataFilter($row_cutoff['staff_name']).'</td>
                                <td class="text_center">'.dataFilter($row_cutoff['amount']).'</td> ' ;
                            echo '
                            </tr>' ;
                        }
                    }else{
                        echo '
                        <tr class="odd gradeX">
                            <td class="border_none">'.$lang['no_data'].'</td>
                            <td class="border_none"></td>
                            <td class="border_none"></td>
                            <td class="border_none"></td>
                        </tr>' ;
                    }
                    ?>
                </tbody>
            </table>
            <?= $page_pagination['page_pagination'] ?>
        </div>
    </div>
</div>

<?php

// footer

include 'requires/page_footer.php' ;

include 'requires/session.php' ;

?>