<?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']) ;

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

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

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

		// check query exsits
		$submit_type = 'new' ;
		$mysqli_page = $mysqli->query("SELECT * FROM suggestion
									   WHERE suggestion_id = '".$page."' 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 ){

			// update database
			$mysqli->query( "UPDATE suggestion SET
							 comment 	    	  = '".escapeString($_POST['comment'])."',
							 status 	    	  = '".escapeString($_POST['status'])."'
							 WHERE suggestion_id  = '".$page."'" ) ;

			if ( $row_page['status'] != $_POST['status'] ){
				pushToUserCron( 'suggestion', $page, $row_page['staff_id'], 'Suggestion', 'Suggestion has been update.' ) ;
			}

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

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

		// get all media
		$media_list   = [] ;
		$mysqli_media = $mysqli->query( "SELECT file, filetype FROM suggestion_media
										 WHERE deleted_at IS NULL AND suggestion_id = '".$page."'" ) ;
		if ( $mysqli_media->num_rows > 0 ){
			while ( $row_media = $mysqli_media->fetch_assoc() ){
				$media_list[]  = $row_media ;
			}
		}

		// get all staff
		$staff_list   = [] ;
		$mysqli_staff = $mysqli->query( "SELECT staff_id, staff_name, staff_idno FROM staff
										WHERE deleted_at IS NULL ".$user_branch_permission_sql) ;
		if ( $mysqli_staff->num_rows > 0 ){
			while ( $row_staff = $mysqli_staff->fetch_assoc() ){
				$staff_list[$row_staff['staff_id']] = dataFilter($row_staff['staff_name']) . ' ( ' . dataFilter($row_staff['staff_idno']) . ' )' ;
			}
		}

		// 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>Suggestion <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-suggestion.php?page_mode=edit&page=<?= $page ?>&type=edit" novalidate="novalidate">

						<div class="form-group">
                            <div class="col-sm-2 control-label">Staff</div>
                            <div class="col-sm-9">
                                <select name="staff_id" id="staff_id" class="chosen-select form-control tab_index ui-dropdown-select ui-search-input select2-basic-single">
                                	<option value="">Select</option>
									<?php
									if ( count($staff_list) > 0 ){
										foreach ( $staff_list as $k => $v ){
											echo '<option value="'.$k.'" '.( $k == $row_page['staff_id'] ? 'selected' : '').'>'.$v.'</option>' ;
										}
									}
									?>
								</select>
                            </div>
                        </div>

                        <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>
                        <div class="form-group">
                            <div class="col-sm-2 control-label">Content</div>
                            <div class="col-sm-9">
                            	<textarea name="content" class="form-control ui-search-input" readonly rows="10" cols="80"><?= dataFilter($row_page['content']) ?></textarea>
                            </div>
                        </div>

						<div class="form-group">
                            <div class="col-sm-2 control-label">Type</div>
                            <div class="col-sm-9">
                                <select name="type_select" id="staff_id" class="chosen-select form-control tab_index ui-dropdown-select ui-search-input select2-basic-single">
                                	<option value="">Select</option>
									<option value="suggestion"       <?= ( $row_page['type'] == 'suggestion' ? 'selected' : '') ?> >Suggestion</option>
									<option value="improvement-plan" <?= ( $row_page['type'] == 'improvement-plan' ? 'selected' : '') ?> >Improvement Plan</option>
								</select>
                            </div>
                        </div>

                        <?php if ( count($media_list) ){ ?>
                        	<div class="form-group form-group-pdf">
	                            <div class="col-sm-2 control-label"><?= $lang['preview']?></div>
	                            <div class="col-sm-9">
		                        	<?php foreach ( $media_list as $k => $row_image ){ ?>
		                        		<?php 
		                        		switch ( $row_image['filetype'] ){
											case 'jpg'  :
											case 'jpeg' :
											case 'png'  :
												echo '
						                        <a href="'.PATH.'uploads/Suggestion/b/'.dataFilter($row_image['file']).'" class="fancybox fancybox_iframe fancybox_iframe_background" style="background-image:url('.PATH.'uploads/Suggestion/'.dataFilter($row_image['file']).')" >
						                        </a>' ;
											break ;
											default     :
												echo '
						                        <a href="'.PATH.'uploads/Suggestion/'.dataFilter($row_image['file']).'" target="_blank" class="fancybox_iframe_background" style="background-image:url(images/filetype/'.$row_image['filetype'].'.png)" >
						                        </a>' ;
										}
		                        		?>
		                        	<?php } ?>
		                        </div>
	                        </div>
                        <?php } ?>

                        <hr />

                        <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="pending"   <?= ( $row_page['status'] == 'pending' ? 'selected' : '' ) ?> >Pending</option>
                                	<option value="confirmed" <?= ( $row_page['status'] == 'confirmed' ? 'selected' : '' ) ?> >Confirmed</option>
                                	<option value="rejected"  <?= ( $row_page['status'] == 'rejected' ? 'selected' : '' ) ?> >Rejected</option>
								</select>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-sm-2 control-label">Comment</div>
                            <div class="col-sm-9">
                            	<textarea name="comment" class="content" id="editor2" rows="10" cols="80" <?=$row_user['user_permission'] != 'admin' ? 'readonly' : '' ?>> <?= dataFilter($row_page['comment']) ?>  </textarea>
		                        <script>
		                        CKEDITOR.replace('editor2') ;
		                        </script>
                            </div>
                        </div>

                        <?php if ( $row_page['status'] != 'confirmed' && $row_page['status'] != 'rejected' ){ ?>
	                        <div class="form-group">
	                            <div class="col-sm-2 control-label"></div>
	                            <div class="col-sm-9">
	                                <button type="submit" class="btn btn-purple" style="float:right"><?= $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 ;
	
	// all announcement list
	case 'all' :
	default :
		
		$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 = escapeString($_GET['search_type']) ;
		$export_excel = escapeString($_GET['export-excel']) ;

		// query type
		$search_query = '' ;
		
		// search query
		if ($search != ''){
			$search_query 	.= " AND ( title LIKE '%".$search."%' )" ;
		}
		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."%' " ;
		}
		if( $search_type 	!= ''){
			$search_query 	.= " AND a.status = '".$search_type."'" ;
		}

		$get_user_tier     	 = userTierQuery( $row_user ) ;
		$search_query 	  	.= ( $get_user_tier['check'] ? " AND b.staff_tier IN ( ".implode(', ', $get_user_tier['tiers'])." )" : '' ) ;
		
		// form submit
		if ( $_POST['hide'] == '1' && $_POST['hide_status'] == 'action' ){
			switch($_POST['page_action']){
				case 'trash':
					$mysqli_query = "UPDATE suggestion SET
									deleted_at = '".TODAYDATE."'
									WHERE suggestion_id = " ;
					$trash_page = trashPage('suggestion', $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_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.'&search_type='.$search_type ;
		
		// page query
		$mysqli_query = "SELECT a.*, b.staff_idno, b.staff_name FROM suggestion a
						 LEFT JOIN staff b ON ( a.staff_id = b.staff_id )
						 WHERE a.deleted_at IS NULL " . $search_query.$user_branch_permission_sql_b ;

		$mysqli_page = $mysqli->query( $mysqli_query." ORDER BY a.suggestion_id DESC LIMIT $start_from, " . LIMIT ) ;

		// load pagination
		$page_pagination = nextPrevious($product_page, LIMIT, $search_url, $mysqli_query) ;
		

		if ( $export_excel == 'export_eae' ){

		    $page_export_file_name = 'Suggestion Report-';

		    $array_header_excel =   array( 
		                                'No.',
		                                'ID', 
		                                'Name', 
		                                'Subject', 
		                                'Content',
		                                'Comment',
		                                'Status',
		                                'Created Date',
		                                'Updated Date'
		                            ) ;

		    $mysqli_export = $mysqli->query( $mysqli_query ) ;

		    if ( $mysqli_export->num_rows > 0 ){
		    	$count_mysqli_export_page = 0;
		        while ( $mysqli_export_page = $mysqli_export->fetch_assoc() ){
		        	$count_mysqli_export_page ++;
		            $array_body_excel[] = array( 
		                                        $count_mysqli_export_page,
		                                        $mysqli_export_page['staff_idno'], 
		                                        $mysqli_export_page['staff_name'], 
		                                        $mysqli_export_page['title'], 
		                                        dataFilter($mysqli_export_page['content']), 
		                                        strip_tags(dataFilter($mysqli_export_page['comment'])), 
		                                        $mysqli_export_page['status'], 
		                                        date('Y-m-d', strtotime($mysqli_export_page['created_at'])),
		                                        date('Y-m-d', strtotime($mysqli_export_page['updated_at']))
		                                    ) ;
		        }
		        $count_mysqli_export_page = 0;
		    }

		    include 'export_excel_default.php';

		}


		// 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>Suggestion <small><?= $lang['list']?></small></h1>
					<?php if ( permissionCheck($row_user, 'our-suggestion-new') ){ ?>
						<!-- <a href="app-suggestion.php?page_mode=new" class="btn btn-purple" target="_blank"><?= $lang['add_new']?></a> -->
					<?php } ?>
					</div>
					<div class="pull-right col">
						<a class="btn" style="color:white;margin:5px;background-color: #5e5bd0;margin-top: 5px;" href="?search_name=<?= $search_name ?>&search_idno=<?= $search_idno ?>&search_mobile=<?= $search_mobile ?>&search_mail=<?= $search_mail ?>&search_date=<?= $search_date ?>&search_update_date=<?= $search_update_date ?>&search_type=<?= $search_type ?>&page_mode=list&search=<?= $search ?>&export-excel=export_eae">Export As Excel</a>
					</div>
				</div>
			</div>
			
		    <div class="panel panel-default" id="basic-table-title">
		        <div class="panel-heading">search</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 Modified">
		                    </div>
		                </div>
	                    <div class="form-group">
		                    <label class="col-sm-2 control-label"><?= $lang['status'] ?></label>
		                    <div class="col-sm-9">
		                        <select class="form-control" name="search_type">
		                        	<option value=""><?= $lang['All'] ?></option>
		                        	<option value="pending" <?= ($search_type == 'pending' ? 'selected' : '') ?>>Pending</option>
		                        	<option value="confirmed" <?= ($search_type == 'confirmed' ? 'selected' : '') ?>>Confirmed</option>
		                        	<option value="rejected" <?= ($search_type == 'rejected' ? '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="margin-top:5px;float:right;width:100px;color:white;background-color: #5e5bd0;"><?= $lang['submit'] ?></button>
		                    </div>
		                </div>
		            </form>
		        </div>
		    </div>


            <form method="post">
                
                <?php if ( permissionCheck($row_user, 'our-suggestion-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">listing</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['Staff']?></th>
									<th><?= $lang['Subject']?></th>
									<th><?= $lang['Created At']?></th>
									<th><?= $lang['Updated At']?></th>
									<th><?= $lang['status']?></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['suggestion_id'] ;
		
										echo '
										 <tr class="odd gradeX">
										 	<td class="align_center">' ;
												if ( permissionCheck($row_user, 'our-suggestion-edit') ){
													echo '
										 			<a title="Edit Current Suggestion" href="app-suggestion.php?page_mode=edit&page='.$id.'"><i class="fa fa-edit"></i></a>' ;
												}else{
													echo '-' ;
												}
											echo '
										 	</td>
											<td class="text-center">'.dataFilter($row_page['staff_name']).' ( '.dataFilter($row_page['staff_idno']).' )</td>
											<td>'.dataFilter($row_page['title']).'</td>
											<td class="text-center">'.resetDateFormat($row_page['created_at']).'</td>
											<td class="text-center">'.resetDateFormat($row_page['updated_at']).'</td>
											<td class="text-center">'.resetStatus($row_page['status']).'</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>
		</div>
		<?php
	break ;
}
// footer
include 'requires/page_footer.php' ;
?>