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

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

    $searchfilter      = $array['searchfilter'] ;
    $searchfilter      = ( $searchfilter != '' ? $searchfilter : 'current' ) ;

    $join_filter       = '' ;
    $join_query        = '' ;
    $query_sortable    = 'ORDER BY a.sortable ASC, a.created_at DESC' ;

    $search_query      = '' ;
    if ( $array['search'] != '' ){
        $search_query .= " AND b.title LIKE '%".$array['search']."%'" ;
    }
    if ( $array['category_id'] != '' ){
        $search_query .= " AND a.category_id = '".$array['category_id']."'" ;
    }

    if ( $searchfilter != '' ){
        switch ( $searchfilter ){
            case 'current' :
                $search_query  .= " AND a.branch LIKE '%/".$array['branch_id']."/%' AND a.status = 'active' AND ( ( a.redeem_type = 'all' ) OR ( a.redeem_type = 'date' AND a.date_start <= '".TODAYDATE."' AND a.date_end >= '".TODAYDATE."' ) )" ;
            break ; 
            case 'previous' : // my redeem
                $join_filter    = ", c.view_id, c.status as redeem_status" ;
                $join_query     = "LEFT JOIN staff_redeem c ON ( a.redeem_id = c.redeem_id )" ;
                $search_query  .= " AND c.branch_id = '".$array['branch_id']."' AND c.staff_id = '".$staff_info['staff_id']."'" ;
                $query_sortable = "ORDER BY c.view_id DESC" ;
            break ; 
            case 'expired' :  // temporary close
                $search_query  .= " AND a.branch LIKE '%/".$array['branch_id']."/%' AND ( a.status = 'inactive' OR ( a.redeem_type = 'date' AND a.date_start <= '".TODAYDATE."' AND NOT ( a.date_end >= '".TODAYDATE."' ) ) )" ;
            break ; 
            case 'history' :
                $join_filter    = ", c.view_id, c.point as redeem_point, c.status as redeem_status, c.created_at as redeem_created, d.staff_idno, d.staff_shortname" ;
                $join_query    .= " LEFT JOIN staff_redeem c ON ( a.redeem_id = c.redeem_id )" ;
                $join_query    .= " LEFT JOIN staff d ON ( c.staff_id = d.staff_id )" ;
                $search_query  .= " AND c.branch_id = '".$array['branch_id']."' AND c.status = 'confirmed'" ;
                $query_sortable = "ORDER BY c.view_id DESC" ;
            break ; 
        }
    }

    $query             = "SELECT a.redeem_id, a.redeem_type, a.date_start, a.date_end, a.point, a.redeem_quantity, a.file, a.created_at, b.title ".$join_filter." FROM redeem a
                          LEFT JOIN redeem_translation b ON ( a.redeem_id = b.redeem_id )
                          ".$join_query."
                          WHERE a.deleted_at IS NULL AND b.lang = '".$array['lang']."' " . $search_query ;
    $mysqli_query      = $mysqli->query( $query . " ".$query_sortable." LIMIT " . getLimit( $current ) ) ;

    if ( $mysqli_query->num_rows > 0 ){

        $status        = '200' ;

        $list          = [] ;
        while ( $row   = $mysqli_query->fetch_assoc() ){
            $row['id']            = dataFilter( $row['redeem_id'] ) ;
            $row['title']         = dataFilter( $row['title'] ) ;
            $row['date_start']    = resetDateFormat( $row['date_start'] ) ;
            $row['date_end']      = resetDateFormat( $row['date_end'] ) ;
            $row['created_at']    = resetDateFormat( $row['created_at'] ) ;
            $row['redeem_status'] = ( checkExists( $row['redeem_status'] ) != '' ? $row['redeem_status'] : '' ) ;
            $row['file']          = ( $row['file'] != '' ? PATH.'uploads/Redeem/b/'.$row['file'] : '' ) ;

            $row['redeem_left']   = 0 ;
            switch ( $searchfilter ){
                case 'current' :
                    $get_staffredeem       = $mysqli->query( "SELECT view_id FROM staff_redeem 
                                                              WHERE deleted_at IS NULL AND redeem_id = '".$row['redeem_id']."' AND status != 'rejected'" ) ;
                    $total_redeem          = $get_staffredeem->num_rows ;
                    $row['redeem_left']    = ( $row['redeem_quantity'] - $total_redeem ) ;
                break ;
                case 'history' :
                    $row['redeem_created'] = resetDateFormat( $row['redeem_created'] ) ;
                break ;
            }

            $list[]               = $row ;
        }

        $data['list']  = $list ;

    }
}

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