<?php
include 'connect/cms-config.php' ;
include 'requires/function.php' ;
include 'requires/session.php' ;

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

// keep parameter in value
$page 				= escapeString($_GET['page']) ;
$page_mode 			= escapeString($_GET['page_mode']) ;
$type 				= escapeString($_GET['type']) ;
$search 			= escapeString($_GET['search']) ;
$redeem_id		    = escapeString($_GET['redeem_id']) ;
$view_id		    = escapeString($_GET['view_id']) ;

// active menu bar
$active_main_menu   = 'service' ;
$active_sub_menu    = 'redeem' ;
$active_menu        = 'redeem-list-list' ;

// get all branch
$branch_all     = [] ;
$get_branch = $mysqli->query( "SELECT * FROM branch
                               WHERE deleted_at IS NULL " . $user_branch_permission_sql_123 ) ;
if ( $get_branch->num_rows > 0 ){
    while ( $row_branch = $get_branch->fetch_assoc() ){
        $branch_all[$row_branch['branch_id']] = $row_branch['branch_name'] ;
    }
}

// check permission
if ( !permissionCheck($row_user, 'redeem-list-view') ){
	header('Location: index.php') ;
	exit ;
}

// mode type | all list | new | edit
switch($page_mode){
	
	// edit redeem
	case 'new' :
	case 'edit' :

		$active_menu  = 'redeem-list-category' ;

		// check query exsits
		$submit_type = 'new' ;
		$mysqli_page = $mysqli->query("SELECT * FROM redeem
									   WHERE redeem_id = '".$redeem_id."' LIMIT 1");
		if ($mysqli_page->num_rows > 0){
			// keep query value in array
			$row_page    = $mysqli_page->fetch_array(MYSQLI_ASSOC) ;
			$submit_type = 'edit' ;
		}

		// update database
		if ( isset($type) && ( $type == 'new' || $type == 'edit' ) && $_POST['hide'] == 1 ){

			// keep value in variable
			$page_title      = escapeString($_POST['title']) ;
			$page_title      = ($page_title != '' ? $page_title : 'No Title') ;
			if ( $redeem_id == '' ){
				$mysqli->query( "INSERT INTO redeem ( user_id, created_at ) VALUES ( '".$_SESSION['system_id']."', '".TODAYDATE."' )" ) ;
				$redeem_id   = $mysqli->insert_id ;	
			}

			// resize image
	        // set image in variable
			$image 		  = $_FILES["image"]["name"] ;
			$image_query  = '' ;
			$remove_photo = $_POST['remove_photo'] ;
			if ($remove_photo == 1){
				$image = '' ;	
				$image_query = "file = ''," ;
			}

			if ( $image != '' ){
				$get_image = pathinfo($image) ;

				$create_image = reCreateImage('Redeem', $redeem_id, $redeem_id, '', $image, $_FILES["image"]["type"], $_FILES['image']['tmp_name']) ;
				// Image uploads when exists
				if ($create_image['result'] && is_array($create_image['crop']) && count($create_image['result']) > 0){
					$resizeObj = new resize($create_image['original']) ; // Initialise load image
					foreach($create_image['crop'] as $value){
						// Resize image (options: exact, portrait, landscape, auto, crop)
						$resizeObj -> resizeImage($value['width'], $value['height'], $value['type']) ;
						$resizeObj -> saveImage($value['source'], 70) ; // Save image	
					}
					$get_image   = pathinfo($create_image['image']) ;
					$image_query = "file = '".$create_image['image']."'," ;
				}
			}

			$array_branch = [] ;
			foreach ( $_POST['branch'] as $k_branch => $v_branch ){
				$array_branch[] = escapeString( $v_branch ) ;
			}

			// get total redeem
			$redeem_quantity  = escapeString($_POST['redeem_quantity']) ;

			// update database
			$mysqli->query( "UPDATE redeem SET
							".$image_query."
							branch          	  = '/".implode('/', $array_branch)."/',
							category_id 		  = '".escapeString($_POST['category_id'])."',
							redeem_type   	      = '".escapeString($_POST['redeem_type'])."',
							date_start   		  = '".escapeString($_POST['date_start'])."',
							date_end    		  = '".escapeString($_POST['date_end'])."',
							point 	    	      = '".escapeString($_POST['point'])."',
							redeem_quantity       = '".$redeem_quantity."',
							status   			  = '".escapeString($_POST['status'])."'
							WHERE redeem_id       = '".$redeem_id."'" ) ;

			$title_en = '' ;
			foreach ( $LANGS as $klang => $vlang ){
				$title        = escapeString( $_POST['title_'.$klang] ) ;
				$content      = escapeString( $_POST['content_'.$klang] ) ;

				if ( $klang  == 'en' ){ $title_en = $title ; }

				checkLangUpdate( 'redeem_translation', 'redeem_id', $redeem_id, $klang, [ 
					'title' 	=> [ 'type' => 'input', 'value' => $title   ], 
					'content' 	=> [ 'type' => 'input', 'value' => $content ]
				] ) ;
			}

			if ( $submit_type == 'new' ){
				// pushToBranchUser( $array_branch, [], 'redeem', $redeem_id, 'New Redeem', ( $title != '' ? $title : 'New redeem has been submitted.' ) ) ;
			}

			// refresh page
			header("Location:app-redeem.php?page_mode=edit&redeem_id=".$redeem_id."&success=1") ;
			$_SESSION['system_result'] = 'success-updated' ;
			exit ;
		}

		if ( ( $page_mode == 'new' && !permissionCheck($row_user, 'redeem-list-new') ) ||
			 ( $page_mode == 'edit' && !permissionCheck($row_user, 'redeem-list-edit') ) ){
			header('Location: app-redeem.php') ;
			exit ;
		}

		// start header here
		include 'requires/page_header.php';
		include 'requires/page_top.php';
		?>

		<div class="warper container-fluid">   	
			<div class="page-header"><h1>Redeem <small><?= $lang[$page_mode] ?></small></h1></div>
			<?php
            if ($_SESSION['system_result'] != ''){
                switch($_SESSION['system_result']){
                    case 'success-updated' :
                        echo '<div class="result_success">'.$lang['Thank you details has been updated'].'</div>' ;	
                    break ;
                }
                unset($_SESSION['system_result']) ;
            }
            ?>
            <div class="panel panel-default">
                <div class="panel-heading"></div>
                <div class="panel-body">
                    <form method="post" class="form-horizontal" id="quotationForm" enctype="multipart/form-data" action="app-redeem.php?page_mode=edit&redeem_id=<?= $redeem_id ?>&type=edit" novalidate="novalidate">


                        <div class="form-group show_branch_action">
                            <div class="col-sm-2 control-label"><?= $lang['branch']?></div>
                            <div class="col-sm-9">
                                <select name="branch[]" class="form-control ui-search-input chosen-select select2-basic-single" multiple required>
                                	<?php
                            		foreach ( $branch_all as $key => $value ) {
                            			echo '<option value="'.$key.'" '. ( strpos( $row_page['branch'], '/'.$key.'/' ) !== false ? 'selected' : '' ) .' >'.$value.'</option>';
                            		}
                                	?>
                                </select>
                            </div>
                        </div>

                    	<?php 
                		echo showTabs( 'redeem_translation', 'redeem_id', $redeem_id, [ 
                			'title'   => [
                				'type'    => 'input', 
                				'title'   => $lang['title']
                			], 
                			'content' => [
                				'type'    => 'textarea', 
                				'title'   => $lang['Content']
                			]
                		]) ; 
                    	?>

                    	<div class="form-group">
                            <div class="col-sm-2 control-label">Category</div>
                            <div class="col-sm-9">
                                <select name="category_id" class="form-control ui-search-input">
		                            <?php
		                            $mysqli_category = $mysqli->query("SELECT a.category_id, b.title FROM redeem_category a
						   											   LEFT JOIN redeem_category_translation b ON ( a.category_id = b.category_id )
		                                                               WHERE a.deleted_at IS NULL AND b.lang = 'en' ORDER BY a.sortable") ;
		                            if ( $mysqli_category->num_rows > 0 ){
		                                while ( $row_category = $mysqli_category->fetch_assoc() ){
		                                    echo '<option '.( $row_category['category_id'] == $row_page['category_id'] ? 'selected' : '' ).' value="'.$row_category['category_id'].'">'.dataFilter($row_category['title']).'</option>' ;

		                                }
		                            }
		                            ?>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label">Redeem Type</div>
                            <div class="col-sm-9">
                                <select name="redeem_type" class="form-control ui-search-input">
                                	<option value="all" <?= ( $row_page['redeem_type'] == 'all' ? 'selected' : '' ) ?> >All</option>
                                	<option value="date" <?= ( $row_page['redeem_type'] == 'date' ? 'selected' : '' ) ?> >Date</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label">Date Start</div>
                            <div class="col-sm-9">
                                <input type="datetime-local" name="date_start" class="form-control ui-search-input" value="<?= dataFilter($row_page['date_start']) ?>" placeholder="Date Start" />
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label">Date End</div>
                            <div class="col-sm-9">
                                <input type="datetime-local" name="date_end" class="form-control ui-search-input" value="<?= dataFilter($row_page['date_end']) ?>" placeholder="Date End" />
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label">Point</div>
                            <div class="col-sm-9">
                                <input type="text" name="point" class="form-control ui-search-input" value="<?= dataFilter($row_page['point']) ?>" placeholder="Point" />
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label">Quantity</div>
                            <div class="col-sm-9">
                                <input type="text" name="redeem_quantity" class="form-control ui-search-input" value="<?= dataFilter($row_page['redeem_quantity']) ?>" placeholder="Quantity" />
                            </div>
                        </div>
                        <!-- <div class="form-group">
                            <div class="col-sm-2 control-label">Left Quantity</div>
                            <div class="col-sm-9">
                                <input type="text" name="redeem_left" class="form-control ui-search-input" value="<?= dataFilter($row_page['redeem_left']) ?>" readonly placeholder="Left Quantity" />
                            </div>
                        </div> -->

                        <div class="form-group">
                            <div class="col-sm-2 control-label"><?= $lang['photo']?></div>
                            <div class="col-sm-9">
                                <div class="file_upload">
                                    <div class="file_form">
                                        <input type="hidden" name="hide_image" value="<?= dataFilter($row_page['file']) ?>" />
                                        <input type="hidden" name="remove_photo" value="">
                                        <input type="file" name="image" class="file_button control-label" <?= $input_block ?> />
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php if ($row_page['file'] != ''){ ?>
	                        <div class="form-group form-group-pdf">
	                            <div class="col-sm-2 control-label"><?= $lang['preview']?></div>
	                            <div class="col-sm-9">
	                            	<label class="remove_photo"><input type="checkbox" name="remove_photo" class="ui-checkbox tick" value="1">&nbsp;<?= $lang['Remove File']?></label>
	                            	<a href="<?= PATH.'uploads/Redeem/'.dataFilter($row_page['file']) ?>" target="_blank">
	                            		<img src="<?= PATH.'uploads/Redeem/'.dataFilter($row_page['file']) ?>" style="width:100px;" />
	                            	</a>
		                        </div>
	                        </div>
                        <?php }else{ ?>
                            <input type="hidden" name="remove_photo" value="" />
                        <?php } ?>

                        <div class="form-group">
                            <div class="col-sm-2 control-label">Status</div>
                            <div class="col-sm-9">
                                <select name="status" id="status" class="form-control ui-search-input">
                                	<option value="inactive" <?= ( $row_page['status'] == 'inactive' ? 'selected' : '' ) ?> >Inactive</option>
                                	<option value="active"  <?= ( $row_page['status'] == 'active' ? 'selected' : '' ) ?> >Active</option>
								</select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label"></div>
                            <div class="col-sm-9">
                                <button type="submit" class="btn" style="color:white;background-color: #5e5bd0;float:right; margin-top: 5px;width: 100px;"><?= $lang['submit']?></button>
                                <input type="hidden" name="hide" value="1">
                                <input type="hidden" name="page_status" value="<?= $submit_type ?>">
                            </div>
                        </div>   
                    </form>
                </div>
            </div>
		</div>

		<?php
		
	break ;



	// edit redeem
	case 'view' :

		// check query exsits
		$submit_type = 'new' ;
		$mysqli_page = $mysqli->query("SELECT a.redeem_so, a.point, a.remark, a.status as redeem_status, a.created_at, b.staff_id, b.staff_image, b.staff_idno, b.staff_name, c.file as item_file, d.title FROM staff_redeem a
									   LEFT JOIN staff b ON ( a.staff_id = b.staff_id )
									   LEFT JOIN redeem c ON ( a.redeem_id = c.redeem_id )
									   LEFT JOIN redeem_translation d ON ( a.redeem_id = d.redeem_id )
									   WHERE a.deleted_at IS NULL AND d.lang = 'en' AND a.redeem_id = '".$redeem_id."' AND a.view_id = '".$view_id."' ". $user_branch_permission_sql_b." LIMIT 1") ;
		if ( $mysqli_page->num_rows == 0 ){
			exit ;
		}

		$row_page    = $mysqli_page->fetch_array(MYSQLI_ASSOC) ;

		$boolean_submit = false ;
		if ( $row_page['redeem_status'] != 'confirmed' && $row_page['redeem_status'] != 'rejected' ){
			$boolean_submit = true ;
		}


		// update database
		if ( $_POST['hide'] == 1 && $boolean_submit ){

			$redeem_status = escapeString($_POST['redeem_status']) ;

			if ( $row_page['redeem_status'] != $redeem_status ){

				$boolean_update     = false ;
				if ( $redeem_status == 'rejected' ){
					$remark         = 'Refund point from redeem ' . $row_page['redeem_so'] ;
                    pointMovement( 'redeem', $view_id, 'exchange-refund', 'normal', $row_page['staff_id'], $row_page['point'], $remark ) ;
                    
					$boolean_update = true ;
				}else{
					$boolean_update = true ;
				}

				if ( $boolean_update ){
					$mysqli->query( "UPDATE staff_redeem SET
									remark 		    = '".escapeString($_POST['remark'])."',
									status          = '".escapeString($redeem_status)."'
									WHERE redeem_id = '".$redeem_id."' AND view_id = '".$view_id."'" ) ;

					if ( $redeem_status == 'rejected' ){
						pushToUserCron( 'staff_redeem', $view_id, $row_page['staff_id'], 'Redeem', 'Redeem has been reject.' ) ;
					}else{
						pushToUserCron( 'staff_redeem', $view_id, $row_page['staff_id'], 'Redeem', 'Redeem has been update.' ) ;
					}

				}

			}

			// refresh page
			header("Location:app-redeem.php?page_mode=view&redeem_id=".$redeem_id."&view_id=".$view_id."&success=1") ;
			$_SESSION['system_result'] = 'success-updated' ;
			exit ;
		}

		if ( ( $page_mode == 'new' && !permissionCheck($row_user, 'redeem-list-new') ) ||
			 ( $page_mode == 'edit' && !permissionCheck($row_user, 'redeem-list-edit') ) ){
			header('Location: app-redeem.php') ;
			exit ;
		}

		// start header here
		include 'requires/page_header.php';
		include 'requires/page_top.php';
		?>

		<div class="warper container-fluid">
		<div class='container' style="background-color: white; border-radius: 10px;">
            <div class="page-header" style="margin: 30px 0px 0px 0px;padding: 0px;">
	            <h1>Redeem <small><?= $lang[$page_mode] ?></small></h1>
	        </div>
			<?php
            if ($_SESSION['system_result'] != ''){
                switch($_SESSION['system_result']){
                    case 'success-updated' :
                        echo '<div class="result_success">'.$lang['Thank you details has been updated'].'</div>' ;	
                    break ;
                }
                unset($_SESSION['system_result']) ;
            }
            ?>
            <div class="panel panel-default">
                <div class="panel-heading"><?= dataFilter($row_page['redeem_so']) ?></div>
                <div class="panel-body">
                    <form method="post" class="form-horizontal" id="quotationForm" enctype="multipart/form-data" action="app-redeem.php?page_mode=view&redeem_id=<?= $redeem_id ?>&view_id=<?= $view_id ?>&type=edit" novalidate="novalidate">

                    	<div class="form-group">
                            <div class="col-sm-2 control-label">Staff ID</div>
                            <div class="col-sm-9">
                                <input type="text" name="staff_idno" class="form-control ui-search-input" readonly value="<?= ( dataFilter($row_page['staff_idno']) ) ?>" placeholder="Staff ID" />
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label">Staff Name</div>
                            <div class="col-sm-9">
                                <input type="text" name="staff_name" class="form-control ui-search-input" readonly value="<?= ( dataFilter($row_page['staff_name']) ) ?>" placeholder="Staff Name" />
                            </div>
                        </div>
                        <?php if ($row_page['staff_image'] != ''){ ?>
	                        <div class="form-group form-group-pdf">
	                            <div class="col-sm-2 control-label"><?= $lang['preview']?></div>
	                            <div class="col-sm-9">
	                            	<a href="<?= PATH.'uploads/Staff/'.dataFilter($row_page['staff_image']) ?>" target="_blank">
	                            		<img src="<?= PATH.'uploads/Staff/'.dataFilter($row_page['staff_image']) ?>" style="width:100px;" />
	                            	</a>
		                        </div>
	                        </div>
                        <?php } ?>

                    	<hr />

                        <div class="form-group">
                            <div class="col-sm-2 control-label">Title</div>
                            <div class="col-sm-9">
                                <input type="text" name="title" class="form-control ui-search-input" readonly value="<?= dataFilter($row_page['title']) ?>" placeholder="Title" />
                            </div>
                        </div>
                        <?php if ($row_page['item_file'] != ''){ ?>
	                        <div class="form-group form-group-pdf">
	                            <div class="col-sm-2 control-label"><?= $lang['preview']?></div>
	                            <div class="col-sm-9">
	                            	<a href="<?= PATH.'uploads/Redeem/'.dataFilter($row_page['item_file']) ?>" target="_blank">
	                            		<img src="<?= PATH.'uploads/Redeem/'.dataFilter($row_page['item_file']) ?>" style="width:100px;" />
	                            	</a>
		                        </div>
	                        </div>
                        <?php } ?>

                    	<hr />

                    	<div class="form-group">
                            <div class="col-sm-2 control-label">Point</div>
                            <div class="col-sm-9">
                                <input type="text" name="point" class="form-control ui-search-input" readonly value="<?= dataFilter($row_page['point']) ?>" placeholder="Point" />
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label">Created At</div>
                            <div class="col-sm-9">
                                <input type="text" name="created_at" class="form-control ui-search-input" readonly value="<?= resetDateTimeFormat($row_page['created_at']) ?>" placeholder="Created At" />
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label">Status</div>
                            <div class="col-sm-9">
                                <select name="redeem_status" class="form-control ui-search-input" <?= ( $boolean_submit ? '' : 'readonly' ) ?> >
                                	<option value="pending" <?= ( $row_page['redeem_status'] == 'pending' ? 'selected' : '' ) ?> >Pending Review</option>
                                	<option value="awaiting-arrival" <?= ( $row_page['redeem_status'] == 'awaiting-arrival' ? 'selected' : '' ) ?> >Item Request - Awaiting Item Arrival</option>
                                	<option value="awaiting-collection" <?= ( $row_page['redeem_status'] == 'awaiting-collection' ? 'selected' : '' ) ?> >Item Request - Awaiting Staff Collection</option>
                                	<option value="confirmed" <?= ( $row_page['redeem_status'] == 'confirmed' ? 'selected' : '' ) ?> >Request Resolved</option>
                                	<option value="rejected" <?= ( $row_page['redeem_status'] == 'rejected' ? 'selected' : '' ) ?> >Item Request -  Rejected</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group form-group-message">
                            <div class="col-sm-2 control-label"><?= $lang['Message']?></div>
                            <div class="col-sm-9">
                            	<textarea name="remark" class="form-control ui-search-input" id="editor1" rows="10" cols="80"><?= dataFilter($row_page['remark']) ?></textarea>
		                        <script>
		                        CKEDITOR.replace('editor1') ;
		                        </script>
                            </div>
                        </div>

                        <?php if ( $boolean_submit ){ ?>
	                        <div class="form-group">
	                            <div class="col-sm-2 control-label"></div>
	                            <div class="col-sm-9">
	                                <button type="submit" class="btn" style="color:white;background-color: #5e5bd0;float:right; margin-top: 5px;width: 100px;"><?= $lang['submit']?></button>
	                                <input type="hidden" name="hide" value="1">
	                                <input type="hidden" name="page_status" value="<?= $submit_type ?>">
	                            </div>
	                        </div>   
	                    <?php } ?>

                    </form>
                </div>
            </div>
        </div>
		</div>

		<?php
		
	break ;




	case 'category' :

		$active_menu  = 'redeem-list-category' ;
		//$active_menu  		= 'redeem-list-list' ;
		
		$search_title 		= escapeString($_GET['search_title']) ;
		$search_date 		= ( $_GET['search_date']!= '' ? date('Y-m-d', strtotime($_GET['search_date'])) : '' ) ;

		$staff_redeem_array = [];

		// page query
		$mysqli_query_staff = "SELECT a.redeem_id, a.staff_id, a.created_at, b.staff_name FROM staff_redeem a
							   LEFT JOIN staff b ON ( a.staff_id = b.staff_id )
							   WHERE a.deleted_at IS NULL " . $user_branch_permission_sql_b ;
		$mysqli_staff 		= $mysqli->query( $mysqli_query_staff." ORDER BY a.redeem_id DESC") ;

		if ($mysqli_staff->num_rows > 0){
			while ( $row_staff = $mysqli_staff->fetch_array(MYSQLI_ASSOC) ){
				$staff_redeem_array[$row_staff['redeem_id']][] = $row_staff;
			}
		}
		
		// query type
		$search_query 		= '' ;
		
		// search query
		if( $search_title  != ''){
			$search_query  .= " AND b.title LIKE '%".$search_title."%'" ;
		}
		if ( $search_date  != '' ){
		    $search_query  .= " AND a.created_at like '%".$search_date."%' " ;
		}
		
		// form submit
		if ( $_POST['hide'] == '1' && $_POST['hide_status'] == 'action' ){
			switch($_POST['page_action']){
				case 'trash':
					$mysqli_query 	= "UPDATE redeem SET deleted_at = '".TODAYDATE."' WHERE redeem_id = " ;
					$trash_page 	= trashPage('redeem', $mysqli, $mysqli_query, $_POST['multiple_trash']) ;
				break;
			}
		}
		
		// 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
		
		// set search url
		$search_url 	= 'search='.$search.'&search_title='.$search_title.'&search_date='.$search_date.'&page_mode='.$page_mode ;
		
		// page query
		$mysqli_query 	= "SELECT a.redeem_id, a.point, a.status, a.created_at, b.title FROM redeem a
						   LEFT JOIN redeem_translation b ON ( a.redeem_id = b.redeem_id )
						   WHERE a.deleted_at IS NULL AND b.lang = 'en' " . $search_query . $user_branch_permission_sql_symbol ;
		$mysqli_page 	= $mysqli->query( $mysqli_query." ORDER BY a.redeem_id DESC LIMIT $start_from, " . LIMIT ) ;

		// load pagination
		$page_pagination = nextPrevious($product_page, LIMIT, $search_url, $mysqli_query) ;
		
		// start header here
		include 'requires/page_header.php' ;
		include 'requires/page_top.php' ;

		?>
		<!-- Header Ends -->
		<div class="warper container-fluid">   	
			<div class='container' style="background-color: white; border-radius: 10px;">
            <div class="page-header" style="margin: 30px 0px 0px 0px;padding: 0px;">
            	<div class="row">
                    <div class="pull-left col">
                    	<h1>Redeem <small>category</small></h1>
					</div>
					<div class="pull-right col">
						<?php if ( permissionCheck($row_user, 'redeem-list-new') ){ ?>
						<a href="app-redeem.php?page_mode=new" class="btn" style="color:white;margin:5px;background-color: #5e5bd0;margin-top: 5px;" target="_blank"><?= $lang['add_new']?></a>
						<?php } ?>
						<a href="app-redeem-category.php?page_mode=all" class="btn" style="color:white;margin:5px;background-color: #5e5bd0;margin-top: 5px;" target="_blank"><?= $lang['Category']?></a>
						<a href="app-redeem.php?page_mode=all" class="btn" style="color:white;margin:5px;background-color: #5e5bd0;margin-top: 5px;" target="_blank"><?= $lang['View All']?></a>
					</div>
				</div>
			</div>
			
		    <div class="panel panel-default" id="basic-table-title">
		        <div class="panel-heading"></div>
		        <div class="panel-body">
		            <form method="get" class="form-horizontal">
		                <div class="form-group">
		                    <label class="col-sm-2 control-label"><?= $lang['Subject'] ?></label>
		                    <div class="col-sm-9">
		                        <input type="text" name="search_title" value="<?= $search_title ?>" class="form-control" />
		                    </div>
		                </div>
	                    <div class="form-group">
		                    <label class="col-sm-2 control-label"><?= $lang['date'] ?></label>
		                    <div class="col-sm-9">
		                        <input class="form-control" name="search_date" type="date" value="<?= $search_date ?>" placeholder="Date Resigned">
		                    </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" value="<?= $search ?>" />
		                        <button type="submit" class="btn" style="float:right;color:white;margin:5px;background-color: #5e5bd0; width:100px;"><?= $lang['submit'] ?></button>
		                    </div>
		                </div>
		            </form>
		        </div>
		    </div>

            <form method="post">
                
                <?php if ( permissionCheck($row_user, 'redeem-list-trash') ){ ?>
                <div class="panel panel-default">
                    <div class="panel-body">
                        <select name="page_action" class="form-control selectpicker">
                            <option value=""><?= $lang['select']?></option>
                            <option value="trash"><?= $lang['move_to_trash']?></option>
                        </select>
                        <input type="hidden" name="hide" value="1" />
                        <input type="hidden" name="hide_status" value="action" />
                        <input type="submit" class="btn" style="color:white;background-color: #5e5bd0; width:100px;" value="<?= $lang['submit']?>" />
                    </div>
                </div>
                <?php } ?>

                <div class="panel panel-default">
                    <div class="panel-heading"></div>
                    <div class="panel-body">
						<table cellpadding="0" cellspacing="0" border="0" class="listing-table responsive table table-striped table-bordered" id="basic-datatable">
							<thead>
								<tr>
									<th><?= $lang['Action']?></th>
									<th><?= $lang['Subject']?></th>
									<th>Point</th>
									<th><?= $lang['status']?></th>
									<th>Created Date</th>
                                    <th width="50"><?= $lang['trash']?></th>
								</tr>
							</thead>
							<tbody>
								<?php
								if ($mysqli_page->num_rows > 0){
									while ( $row_page = $mysqli_page->fetch_array(MYSQLI_ASSOC) ){

										// default variable
										$id = $row_page['redeem_id'] ;
										$redeem_staff = 0;

										$blink_css = '';

										foreach ($staff_redeem_array[$id] as $key => $value) {
											// $redeem_staff .= $value['staff_name'].'<br/>';
											$redeem_staff ++;

											if(date( 'Y-m-d', strtotime( $value['created_at'] ) ) == date( 'Y-m-d' )){
												$blink_css = 'blink_css_cms';
											}
										}
		
										echo '
										 <tr class="odd gradeX">
										 	<td class="align_center">' ;
												if ( permissionCheck($row_user, 'redeem-list-edit') ){
													echo '
										 			<a title="Edit Current redeem" href="app-redeem.php?page_mode=edit&redeem_id='.$id.'"><i class="fa fa-edit"></i></a>
										 			<span class="order_print_span">|</span>' ;
												}else{
													echo '-' ;
												}
											echo '
										 		<a class="'.$blink_css.'" title="View Redeem Staff" href="app-redeem.php?page_mode=all&staff_redeem_id='.$id.'"><i class="fa fa-eye" aria-hidden="true"></i> ('.$redeem_staff.')</a>
										 	</td>
											<td>'.dataFilter($row_page['title']).'</td>
											<td class="text-center">'.dataFilter($row_page['point']).'</td>
											<td class="text-center">'.resetStatus($row_page['status']).'</td>
											<td class="text-center">'.resetDateFormat($row_page['created_at']).'</td>
											<td>
												<div class="checkbox multiple_trash">
													<input type="checkbox" name="multiple_trash['.$id.']" class="trash_button" value="1">
													<label for="checkbox1"></label>
												</div>
											</td>
										</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>
										<td class="border_none"></td>
										<td class="border_none"></td>
										<td class="border_none"></td>
									</tr>' ;
								}
								?>
							</tbody>
						</table>
						<?= $page_pagination['page_pagination'] ?>
                    </div>
                </div>
			</form>
		</div>
		<?php
	break ;




	case 'all' :
	default :
		
		// query type
		$search_query 		= '' ;
		$staff_redeem_id 	= $_GET['staff_redeem_id'];
		$search_name 		= escapeString($_GET['search_name']) ;
		$search_idno 		= escapeString($_GET['search_idno']) ;
		$search_mobile 		= escapeString($_GET['search_mobile']) ;
		$search_mail 		= escapeString($_GET['search_mail']) ;
		$search_date 		= ( $_GET['search_date']!= '' ? date('Y-m-d', strtotime($_GET['search_date'])) : '' ) ;
		$search_update_date 		= ( $_GET['search_update_date']!= '' ? date('Y-m-d', strtotime($_GET['search_update_date'])) : '' ) ;
		$search_type 		= $_GET['search_type'] ;
		
		$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_mobile != ''){
			$search_query  .= " AND b.staff_mobileno LIKE '%".$search_mobile."%'" ;
		}
		if( $search_mail   != ''){
			$search_query  .= " AND b.staff_email LIKE '%".$search_mail."%'" ;
		}
		if ( $search_date  != '' ){
		    $search_query  .= " AND a.created_at like '%".$search_date."%' " ;
		}
		if ( $search_update_date  != '' ){
		    $search_query  .= " AND a.updated_at like '%".$search_update_date."%' " ;
		}
		// search query
		if ($search  	   != ''){
			$search_query  .= " AND ( d.title LIKE '%".$search."%' )" ;
		}
		// search query
		if ($staff_redeem_id  != ''){
			$search_query  .= " AND ( a.redeem_id = '".$staff_redeem_id."' )" ;
		}
		if( $search_type   != ''){
			$search_query  .= " AND a.status IN ('".implode("', '",$search_type)."') " ;
		}
		
		// 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
		
		// set search url
		$search_url = 'search='.$search.'&search_name='.$search_name.'&search_date='.$search_date.'&search_update_date='.$search_update_date.'&page_mode='.$page_mode.'&search_idno='.$search_idno.'&search_mobile='.$search_mobile.'&search_mail='.$search_mail.'&staff_redeem_id='.$staff_redeem_id ;

		$mysqli_query = "SELECT a.view_id, a.redeem_id, a.redeem_so, a.point, a.remark, a.status as redeem_status, a.created_at, a.updated_at, b.staff_image, b.staff_idno, b.staff_name, c.file as item_file, d.title FROM staff_redeem a
						 LEFT JOIN staff b ON ( a.staff_id = b.staff_id )
						 LEFT JOIN redeem c ON ( a.redeem_id = c.redeem_id )
						 LEFT JOIN redeem_translation d ON ( c.redeem_id = d.redeem_id )
						 WHERE a.deleted_at IS NULL AND d.lang = 'en' " . $search_query . $user_branch_permission_sql_b ;
		$mysqli_page  = $mysqli->query( $mysqli_query." ORDER BY a.view_id DESC LIMIT $start_from, " . LIMIT ) ;


		if ($_POST['hide'] == '1' && $_POST['hide_status'] == 'action'){
			 switch($_POST['page_action']){
			 	case 'export-excel' :
		            include 'PhpExcel/PHPExcel.php' ;
		            // // Create new PHPExcel object
		            $objPHPExcel = new PHPExcel();

		            // set letter
		            $letters = array();
		            $letter = 'A';
		            while ($letter !== 'AAA') {
		                $letters[] = $letter++;
		            }

		            //  // get array header
		            $HeaderArray = array(
		                                'SO',
		                                'Item',
		                                'Name',
		                                'Point',
		                                'Remark',
		                                'Status',
		                                'Created At',
		                                'Updated At'
		                                );
		            // Set document properties
		            $objPHPExcel->getProperties()->setCreator("IPS")
		                                         ->setLastModifiedBy("CMS")
		                                         ->setTitle("System Export Excel")
		                                         ->setSubject("System Export Excel")
		                                         ->setDescription("System Export Excel")
		                                         ->setKeywords("System Excel")
		                                         ->setCategory("System Excel");

		            // Add some data
		            if (arrayCheck($HeaderArray)){
		                $cound_header = 1;
		                $count = 0;
		                foreach($HeaderArray as $key => $header_name){              
		                    // if sub exist
		                    if (arrayCheck($header_name)){
		                        
		                        $objPHPExcel->setActiveSheetIndex(0)->setCellValue($letters[$count].$cound_header, $key);
		                        $count_sub_header = $cound_header;
		                        $sub_count = $count;
		                        $count_sub_header++;
		                        foreach($header_name as $header_name_sub){
		                            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($letters[$sub_count].$count_sub_header, $header_name_sub);
		                            // continue first layer
		                            $count = $sub_count;
		                            // add second layer
		                            $sub_count++;                       
		                        }
		                    }else{
		                        $objPHPExcel->setActiveSheetIndex(0)->setCellValue($letters[$count].$cound_header, $header_name);
		                    }
		                    // merge value 
		                    $begin = $count;
		                    //$end = $count+15;
		                    $end = $count;
		                    
		                    $count++;
		                }
		            }

		            $mysqli_page = $mysqli->query($mysqli_query."  ORDER BY a.view_id ") ;
		            if ($mysqli_page->num_rows > 0){
		                
		                $array_customer = array() ;
		                $count = 2 ;
		                while ($row_page = $mysqli_page->fetch_array(MYSQLI_ASSOC)){
                          $objPHPExcel->setActiveSheetIndex(0)
                                        ->setCellValue('A'.$count, dataFilterDash($row_page['redeem_so']))
                                        ->setCellValue('B'.$count, dataFilterDash($row_page['title']))
                                        ->setCellValue('C'.$count, dataFilterDash(dataFilter($row_page['staff_name']).' ( '.$row_page['staff_idno']).' )')
                                        ->setCellValue('D'.$count, dataFilterDash($row_page['point']))
                                        ->setCellValue('E'.$count, dataFilterDash($row_page['remark']))
                                        ->setCellValue('F'.$count, dataFilterDash($row_page['redeem_status']))
                                        ->setCellValue('G'.$count, dataFilterDash($row_page['created_at']))
                                        ->setCellValue('H'.$count, dataFilterDash($row_page['updated_at']));
                                        $count++;
		                }   
		                               
		            }
		            // file name
		            $fileName = "Redeem_" .time();       

		            // Rename worksheet
		            $objPHPExcel->getActiveSheet()->setTitle($fileName);

		            // Set active sheet index to the first sheet, so Excel opens this as the first sheet
		            $objPHPExcel->setActiveSheetIndex(0);

		            // Save Excel 2007 file
		            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

		            //Setting the header type
		            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		            header('Content-Disposition: attachment;filename="'.$fileName.'.xlsx"');
		            header('Cache-Control: max-age=0');

		            // save to pc
		            $objWriter->save('php://output');
		            header("Refresh: 0") ;
		            exit ;
		        break ;
			 }
		}

		// load pagination
		$page_pagination = nextPrevious($product_page, LIMIT, $search_url, $mysqli_query) ;
		
		// start header here
		include 'requires/page_header.php' ;
		include 'requires/page_top.php' ;

		?>
		<div class="warper container-fluid">
		<div class='container' style="background-color: white; border-radius: 10px;">
            <div class="page-header" style="margin: 30px 0px 0px 0px;padding: 0px;">	
				<h1>Redeem <small>List</small></h1>
			</div>

		    <div class="panel panel-default" id="basic-table-title">
		        <div class="panel-heading"></div>
		        <div class="panel-body">
		            <form method="get" class="form-horizontal">
		                <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 No'] ?></label>
		                    <div class="col-sm-9">
		                        <input type="text" name="search_idno" value="<?= $search_idno ?>" class="form-control" />
		                    </div>
		                </div>
		                <div class="form-group">
		                    <label class="col-sm-2 control-label"><?= $lang['Mobile'] ?></label>
		                    <div class="col-sm-9">
		                        <input type="text" name="search_mobile" value="<?= $search_mobile ?>" class="form-control" />
		                    </div>
		                </div>
		                <div class="form-group">
		                    <label class="col-sm-2 control-label"><?= $lang['email'] ?></label>
		                    <div class="col-sm-9">
		                        <input type="text" name="search_mail" value="<?= $search_mail ?>" class="form-control" />
		                    </div>
		                </div>
	                    <div class="form-group">
		                    <label class="col-sm-2 control-label"><?= $lang['Created At'] ?></label>
		                    <div class="col-sm-9">
		                        <input class="form-control" name="search_date" type="date" value="<?= $search_date ?>" placeholder="Date Resigned">
		                    </div>
		                </div>
		                <div class="form-group">
		                    <label class="col-sm-2 control-label"><?= $lang['Updated At'] ?></label>
		                    <div class="col-sm-9">
		                        <input class="form-control" name="search_update_date" type="date" value="<?= $search_update_date ?>" placeholder="Date Updated">
		                    </div>
		                </div>
	                    <div class="form-group">
		                    <label class="col-sm-2 control-label"><?= $lang['status'] ?></label>
		                    <div class="col-sm-9">
		                        <select name="search_type[]" class="form-control ui-search-input chosen-select select2-basic-single" id="" multiple>
		                        	<option value=""><?= $lang['All'] ?></option>
		                        	<option value="pending" <?= (in_array("pending", $search_type) ? 'selected' : '') ?>>Pending</option>
		                        	<option value="awaiting-arrival" <?= (in_array("awaiting-arrival", $search_type) ? 'selected' : '') ?>>Awaiting Arrival</option>
		                        	<option value="awaiting-collection" <?= (in_array("awaiting-collection", $search_type) ? 'selected' : '') ?>>Awaiting Collection</option>
		                        	<option value="confirmed" <?= (in_array("confirmed", $search_type) ? 'selected' : '') ?>>Confirmed</option>
		                        	<option value="rejected" <?= (in_array("rejected", $search_type) ? 'selected' : '') ?>>Rejected</option>
		                        </select>
		                    </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" value="<?= $search ?>" />
		                        <button type="submit" class="btn" style="color:white;background-color: #5e5bd0;float:right; margin-top: 5px;width: 100px;"><?= $lang['submit'] ?></button>
		                    </div>
		                </div>
		            </form>
		        </div>
		    </div>

			<form method="post" action="app-redeem.php?page_mode=all&staff_redeem_id=<?=$staff_redeem_id?>">
	            <div class="panel panel-default">
	                <div class="panel-body">
	                    <select name="page_action" class="form-control selectpicker">
	                        <option value=""><?= $lang['select']?></option>
	                        <option value="export-excel"><?= $lang['export']?></option>
	                    </select>
	                    <input type="hidden" name="hide" value="1" />
	                    <input type="hidden" name="hide_status" value="action" />
	                    <input type="submit" class="btn" style="color:white;background-color: #5e5bd0;" value="<?= $lang['submit']?>" />
	                </div>
	            </div>
	        </form>

			<div class="panel panel-default">
                <div class="panel-heading">Staff Redeem</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['Action']?></th>
								<th>SO</th>
								<th>Item</th>
								<th>Name</th>
								<th>Point</th>
								<th>Remark</th>
								<th>Status</th>
								<th>Created At</th>
								<th>Updated At</th>
							</tr>
						</thead>
						<tbody>
							<?php
							if ( $mysqli_page->num_rows > 0 ){
								while ( $row_page = $mysqli_page->fetch_array(MYSQLI_ASSOC) ){
									$item_image   = ( $row_page['item_file'] != '' ? PATH.'uploads/Redeem/'.dataFilter($row_page['item_file']) : '' ) ;
									$staff_image  = ( $row_page['staff_image'] != '' ? PATH.'uploads/Staff/'.dataFilter($row_page['staff_image']) : '' ) ;

									echo '
									<tr class="odd gradeX">
										<td class="align_center">
											<a title="Edit Current redeem" href="app-redeem.php?page_mode=view&redeem_id='.$row_page['redeem_id'].'&view_id='.$row_page['view_id'].'" target="_blank"><i class="fa fa-eye"></i></a>
									 	</td>
										<td class="text-center">'.dataFilter($row_page['redeem_so']).'</td>
										<td>
											'.dataFilter($row_page['title']).'<br />
											'.( $item_image != '' ? '<a href="'.$item_image.'" target="_blank"><img src="'.$item_image.'" style="width:40px;" /></a>' : '' ).'
										</td>
										<td>
											'.dataFilter($row_page['staff_name']).' ( '.dataFilter($row_page['staff_idno']).' )<br />
											'.( $staff_image != '' ? '<a href="'.$staff_image.'" target="_blank"><img src="'.$staff_image.'" style="width:40px;" /></a>' : '' ).'
										</td>
										<td class="text-center">'.dataFilter($row_page['point']).'</td>
										<td class="text-center">'.dataFilter($row_page['remark']).'</td>
										<td class="text-center">'.resetStatus($row_page['redeem_status']).'</td>
										<td class="text-center">'.resetDateFormat($row_page['created_at']).'</td>
										<td class="text-center">'.resetDateFormat($row_page['updated_at']).'</td>
									</tr>';
								}
							}
							?>
						</tbody>
					</table>
					<?= $page_pagination['page_pagination'] ?>
                </div>
            </div>
        </div>
		</div>
		<?php
	break ;



}
// footer
include 'requires/page_footer.php' ;
?>