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

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

    $search_query      = '' ;
    $search_query     .= " AND a.redeem_id = '".$array['id']."'" ;

    $join_filter       = "" ;
    $join_query        = "" ;
    if ( $array['mode'] == 'view' ){
        $join_filter  .= ", c.status as redeem_status, c.remark as redeem_remark" ;
        $join_query   .= " LEFT JOIN staff_redeem c ON ( a.redeem_id = c.redeem_id )" ;
        $search_query .= " AND c.view_id = '".$array['view_id']."' AND c.staff_id = '".$staff_info['staff_id']."'" ;
    }

    $query             = "SELECT a.redeem_id, a.category_id, a.redeem_type, a.date_start, a.date_end, a.point, a.file, a.status, a.created_at, b.title, b.content ".$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 " . $search_query ;
    $mysqli_query      = $mysqli->query( $query ) ;

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

        $row                  = $mysqli_query->fetch_assoc() ; 
        $row['id']            = dataFilter( $row['redeem_id'] ) ;
        $row['title']         = dataFilter( $row['title'] ) ;
        $row['content']       = dataFilter( $row['content'] ) ;
        $row['file']          = ( $row['file'] != '' ? PATH.'uploads/Redeem/b/'.$row['file'] : '' ) ;

        $is_redeem            = 'no' ;
        $is_custom            = 'no' ;
        if ( $row['status']  == 'active' ){
            if ( $row['redeem_type'] == 'all' || ( $row['redeem_type'] == 'date' && $row['date_start'] <= TODAYDATE && $row['date_end'] >= TODAYDATE ) ){
                $is_redeem    = 'yes' ;

                $select_category  = $mysqli->query( "SELECT category_mode FROM redeem_category
                                                     WHERE deleted_at IS NULL AND category_id = '".$row['category_id']."' LIMIT 1" ) ;
                if ( $select_category->num_rows > 0 ){
                    $row_category = $select_category->fetch_assoc() ;
                    $is_custom    = ( $row_category['category_mode'] == 'custom' ? 'yes' : 'no' ) ;
                }
            }
        }
        $row['is_redeem']     = $is_redeem ;
        $row['is_custom']     = $is_custom ;
        if ( $is_custom      == 'yes' ){
            $row['point']     = $staff_info['staff_point'] ;
        }

        $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['redeem_remark'] = dataFilter( checkExists( $row['redeem_remark'] ) != '' ? $row['redeem_remark'] : '') ;

        $data['list']         = $row ;
    }
}

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