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

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

		// check permission
		if ( !permissionCheck($row_user, 'user-notification-new') ){
			header('Location: index.php') ;
			exit ;
		}
		
		$active_main_menu = 'setting' ;
		$active_sub_menu = 'setting-notification' ;
		$active_menu = 'page-notification-new' ;

		// 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;">
				<div class="row">
                	<div class="pull-left col">
                		<h1><?= $lang['Notification'] ?> <small><?= $lang['new'] ?></small></h1>
                	</div>
                </div>
            </div>
			<div class="row">
				<div class="col-md-12">
					<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="page-notification.php?page_mode=edit&page=<?= $last_id ?>&type=edit" novalidate="novalidate">
								<div class="form-group">
									<div class="col-sm-2 control-label"><?= $lang['Notification'] ?></div>
									<div class="col-sm-9">
										<input type="text" name="title" class="form-control ui-search-input" placeholder="<?= $lang['Notification'] ?>" >
									</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_status" />
										<input type="hidden" name="hide" value="1">
                                        <input type="hidden" name="page_status" value="new">
									</div>
								</div>   
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>
		</div>
		<?php
		
	break ;
	
	// edit category
	case 'edit' :
		
		// active page
		$active_main_menu = 'setting' ;
		$active_sub_menu = 'setting-notification' ;
		$active_menu = 'page-notification' ;
		
		if ($_POST['page_status'] == 'new'){
			$mysqli->query("INSERT INTO system_post (post_type, post_categories, post_date, post_modified) VALUES ('page-notification', 'page-notification', '".TODAYDATE."', '".TODAYDATE."')") ;
			$page = $mysqli->insert_id ;
		}
		
		// check query exsits
		$mysqli_page = $mysqli->query("SELECT * FROM system_post
										WHERE post_id = '".$page."' AND post_type = 'page-notification' LIMIT 1");
		if ($mysqli_page->num_rows > 0){
			
			// update database
			if (isset($type) && $type == 'edit' && $_POST['hide'] == 1){
				
				// keep value in variable
				$page_title = escapeString($_POST['title']) ;
				$page_title = ($page_title != '' ? $page_title : 'No Title') ;
				
				// check if name already exists.
				$check_status = checkTitle($page_title, $page) ;
				$title = $check_status['title'] ;
				$status = $check_status['status'] ;
				
				// set image in variable
				$image = $_FILES["image"]["name"] ;
				
				// remove photo
				$remove_photo = $_POST['remove_photo'] ;
				if ($remove_photo == 1){
					$image = '' ;	
					$image_query = "post_file = ''," ;
				}
				
				// update database
				$mysqli->query("UPDATE system_post SET
								post_title = '".$page_title."',
								post_status = '".$status."',
								post_link = '".$title."',
								".$image_query."
								post_modified = '".TODAYDATE."',
								post_trash = '0'
								WHERE post_id = '".$page."'") ;
								
				// resize image
				$create_image = reCreateImage('Product', $page, $page, '', $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	
					}
					// update database
					$mysqli->query("UPDATE system_post SET
									post_file = '".$create_image['image']."'
									WHERE post_id = '".$page."'");
				}
				
				// refresh page
				header("Location:page-notification.php?page_mode=edit&page=".$page."&success=1") ;
				$_SESSION['system_result'] = 'success-updated' ;
				exit ;
				
			}
			
			// start header here
			include 'requires/page_header.php' ;
			include 'requires/page_top.php' ;
			
			// keep query value in array
			$row_page = $mysqli_page->fetch_array(MYSQLI_ASSOC) ;
			
			?>
			<div class="warper container-fluid">   	
				<div class="page-header"><h1><?= $lang['Notification'] ?> <small><?= $lang['edit'] ?></small></h1></div>
				<div class="row">
					<div class="col-md-12">
                    	<?php
						if ($_SESSION['system_result'] != ''){
							switch($_SESSION['system_result']){
								case 'success-updated' :
									echo '
									<div class="result_success">
										'. $lang['thank_you_your_notification_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="page-notification.php?page_mode=edit&page=<?= $page ?>&type=edit" novalidate="novalidate">
									<div class="form-group">
										<div class="col-sm-2 control-label"><?= $lang['Notification'] ?></div>
										<div class="col-sm-9">
											<input type="text" name="title" class="form-control ui-search-input" value="<?= dataFilter($row_page['post_title']) ?>" placeholder="<?= $lang['Notification'] ?>" >
										</div>
									</div>

									<?php if ( permissionCheck($row_user, 'user-notification-edit') ){ ?>
										<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_status" value="<?= $row_page['post_status'] ?>" />
												<input type="hidden" name="hide" value="1">
	                                            <input type="hidden" name="page_status" value="edit">
											</div>
										</div> 
									<?php } ?>  
								</form>
							</div>
						</div>
					</div>
				</div>
			</div>
			<?php
		}else{
			header("Location: page-notification.php?page_mode=all") ;
			exit ;	
		}
	break ;
	
	// all category list
	case 'all' :
	default :

		// check permission
		if ( !permissionCheck($row_user, 'user-notification-view') ){
			header('Location: index.php') ;
			exit ;
		}
		
		$search_title = escapeString($_GET['search_title']) ;
		$search_date = ( $_GET['search_date']!= '' ? date('Y-m-d', strtotime($_GET['search_date'])) : '' ) ;
		
		// query type
		$search_query = '' ;
		
		// search query
		if ($search != ''){
			$search_query .= " AND (post_title LIKE '%".$search."%')" ;
		}
		if( $search_title != ''){
			$search_query .= " AND post_title LIKE '%".$search_title."%'" ;
		}
		if ( $search_date != '' ){
		    $search_query .= " AND post_date like '%".$search_date."%' " ;
		}

		// active page
		$active_main_menu = 'setting' ;
		$active_sub_menu = 'setting-notification' ;
		$active_menu = 'page-notification' ;
		
		// form submit
		if ($_POST['hide'] == '1' && $_POST['hide_status'] == 'action'){
			// sortable first
			$sortable = $_POST['sortable'] ;
            if (is_array($sortable) && count($sortable) > 0){
                foreach($sortable as $key => $value){
                    $mysqli->query("UPDATE system_post SET
                                    post_order = '".$value."'
                                    WHERE post_id = '".$key."'") ;
                }
            }
			// trash item
			switch($_POST['page_action']){
				case 'trash':
					$mysqli_query = "UPDATE " . system_post . " SET
									post_trash = '1'
									WHERE post_id = " ;
					$trash_page = trashPage('post', $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 * FROM system_post
						WHERE post_title != '' AND post_type = 'page-notification' AND post_trash = '0'".$search_query ;
		$mysqli_page = $mysqli->query($mysqli_query." ORDER BY post_order 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;">
                <h1><?= $lang['Notification'] ?> <small><?= $lang['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['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="color:white;background-color: #5e5bd0;float:right; margin-top: 5px;width: 100px;"><?= $lang['submit'] ?></button>
		                    </div>
		                </div>
		            </form>
		        </div>
		    </div>

            <form method="post">
                <?php if ( permissionCheck($row_user, 'user-notification-trash') ){ ?>
	                <!-- 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>
	                <!-- end trash -->
                <?php } ?>
                <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['title'] ?></th>
									<th><?= $lang['date'] ?></th>
                                    <th width="100"><?= $lang['sortable'] ?></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)){
										// title
										$title = dataFilter($row_page['post_title']) ;
										echo '
										 <tr class="odd gradeX">
											<td><a href="page-notification.php?page_mode=edit&page='.$row_page['post_id'].'">'.$title.'</a></td>
											<td>'.resetDateFormat($row_page['post_date']).'</td>
											<td><input type="text" name="sortable['.$row_page['post_id'].']" value="'.$row_page['post_order'].'" class="responsive_width text_center" /></td>
											<td>
												<div class="checkbox multiple_trash">
													<input type="checkbox" name="multiple_trash['.$row_page['post_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>
									</tr>' ;
								}
								?>
							</tbody>
						</table>
						<?= $page_pagination['page_pagination'] ?>
                    </div>
                </div>
			</form>
		</div>
		</div>
		<?php
	break ;
}
// footer
include 'requires/page_footer.php' ;
?>