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

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

//check password
if ($_POST['password_inserted']!='') {
	$password_inserted = escapeString($_POST['password_inserted']) ;
	$_SESSION['nomination_password'] = $password_inserted;
}

$mysqli_ck_password = $mysqli->query("SELECT a.password_id, a.content FROM app_password a LEFT JOIN app_password_translation b ON ( a.password_id = b.password_id ) WHERE a.deleted_at IS NULL AND a.password_type = 'nomination' AND b.lang = 'en'");
if ($mysqli_ck_password->num_rows > 0) {
	$row_ck_password = $mysqli_ck_password->fetch_array();
}
if ($_SESSION['nomination_password'] == '' ){
	echo '<script>
			alert("You need password to access this page.");
			window.open("check_password.php?type=nomination&pu='.base64_encode($_SERVER['REQUEST_URI']).'","_self");
		</script>';
}
if($_SESSION['nomination_password'] != $row_ck_password['content']) {
	unset($_SESSION['nomination_password']);
	echo '<script>
			alert("Sorry, password inserted is wrong!");
			var boolean_confirm = confirm("Do you want to retry?");
			if(boolean_confirm == true){
				window.open("check_password.php?type=nomination&pu='.base64_encode($_SERVER['REQUEST_URI']).'","_self");
			}else{
				window.open("index.php","_self");
			}
		</script>';
}

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

// active menu bar
$active_main_menu 	= 'service' ;
$active_sub_menu 	= 'form-submission-question' ;
$active_menu 		= 'form-nomination-question-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, 'form-nomination-question-view') ){
	header('Location: index.php') ;
	exit ;
}

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

		// check query exsits
		$submit_type = 'new' ;
		$mysqli_page = $mysqli->query("SELECT * FROM formnomination_question
									   WHERE question_id = '".$question_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 ( $question_id == '' ){
				$mysqli->query( "INSERT INTO formnomination_question 
							    ( created_at ) VALUES 
							    ( '".TODAYDATE."' )" ) ;
				$question_id = $mysqli->insert_id ;	
			}

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

			// update database
			$mysqli->query( "UPDATE formnomination_question SET
							".$image_query."
							branch                = '/".implode('/', $array_branch)."/',
							nomination_type       = '".escapeString($_POST['nomination_type'])."',
							question_type         = '".escapeString($_POST['question_type'])."',
							sortable   			  = '".escapeString($_POST['sortable'])."'
							WHERE question_id     = '".$question_id."'" ) ;

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

				checkLangUpdate( 'formnomination_question_translation', 'question_id', $question_id, $klang, [ 
					'title' 	=> [ 'type' => 'input',    'value' => $title   ],
					'content' 	=> [ 'type' => 'textarea', 'value' => escapeString( $_POST['content_'.$klang] )   ],
					'questions' => [ 'type' => 'input',    'value' => escapeString( $_POST['questions_'.$klang] )   ]
				] ) ;
			}

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

		if ( ( $page_mode == 'new' && !permissionCheck($row_user, 'form-nomination-question-new') ) ||
			 ( $page_mode == 'edit' && !permissionCheck($row_user, 'form-nomination-question-edit') ) ){
			header('Location: app-form-nomination-question.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>Nomination Question <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-form-nomination-question.php?page_mode=edit&question_id=<?= $question_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( 'formnomination_question_translation', 'question_id', $question_id, [ 
                			'title'       => [
                				'type'    => 'input', 
                				'title'   => $lang['title']
                			],
                			'content'     => [
                				'type'    => 'input', 
                				'title'   => $lang['Content']
                			],
                			'questions'   => [
                				'type'    => 'input', 
                				'title'   => $lang['Questions']
                			]
                		]) ; 
                    	?>

                        <div class="form-group">
                            <div class="col-sm-2 control-label">Nomination Type</div>
                            <div class="col-sm-9">
                                <select name="nomination_type" id="nomination_type" class="form-control ui-search-input">
                                	<option value="" >Select</option>
                                	<option value="promotion" <?= ( $row_page['nomination_type'] == 'promotion' ? 'selected' : '' ) ?> >Promotion</option>
                                	<option value="demotion"  <?= ( $row_page['nomination_type'] == 'demotion' ? 'selected' : '' ) ?> >Demotion</option>
								</select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label">Type</div>
                            <div class="col-sm-9">
                                <select name="question_type" id="question_type" class="form-control ui-search-input">
                                	<option value="" >Select</option>
                                	<option value="question" <?= ( $row_page['question_type'] == 'question' ? 'selected' : '' ) ?> >Question</option>
                                	<option value="form"  <?= ( $row_page['question_type'] == 'form' ? 'selected' : '' ) ?> >Form</option>
								</select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label">Sortable</div>
                            <div class="col-sm-9">
                            	<input type="text" name="sortable" value="<?= $row_page['sortable'] ?>" class="form-control ui-search-input" />
                            </div>
                        </div>
                        <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>   
                    </form>
                </div>
            </div>
		</div>

		<?php
		
	break ;


	
	// all nomination question list
	case 'all' :
	default :
		
		$search_title 		= escapeString($_GET['search_title']) ;
		$search_type 		= escapeString($_GET['search_type']) ;
		$search_date 		= ( $_GET['search_date']!= '' ? date('Y-m-d', strtotime($_GET['search_date'])) : '' ) ;
		
		// query type
		$search_query 		= '' ;
		
		// search query
		if( $search_title  != ''){
			$search_query  .= " AND b.title LIKE '%".$search_title."%'" ;
		}
		if ( $search_type  != '' ){
		    $search_query  .= " AND a.question_type = '".$search_type."' " ;
		}
		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 formnomination_question SET deleted_at = '".TODAYDATE."' WHERE question_id = " ;
					$trash_page 	= trashPage('formnomination_question', $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_type='.$search_type.'&search_date='.$search_date.'&page_mode='.$page_mode ;
		
		// page query
		$mysqli_query 	= "SELECT a.question_id, a.nomination_type, a.question_type, a.created_at, b.title FROM formnomination_question a
						   LEFT JOIN formnomination_question_translation b ON ( a.question_id = b.question_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.question_id ASC 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="page-header">
				<h1>Nomination Question <small><?= $lang['list']?></small></h1>
				<?php if ( permissionCheck($row_user, 'form-nomination-question-new') ){ ?>
					<a href="app-form-nomination-question.php?page_mode=new" class="btn btn-purple" target="_blank"><?= $lang['add_new']?></a>
				<?php } ?>
			</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" style="max-width:600px;">
		                <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">
                            <div class="col-sm-2 control-label">Type</div>
                            <div class="col-sm-9">
                                <select name="search_type" id="search_type" class="form-control ui-search-input">
                                	<option value="">All</option>
                                	<option value="question" <?= ( $search_type == 'question' ? 'selected' : '' ) ?> >Question</option>
                                	<option value="form"  <?= ( $search_type == 'form' ? 'selected' : '' ) ?> >Form</option>
								</select>
                            </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 btn-purple" style="float:right"><?= $lang['submit'] ?></button>
		                    </div>
		                </div>
		            </form>
		        </div>
		    </div>

            <form method="post">
                
                <?php if ( permissionCheck($row_user, 'form-nomination-question-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 btn-purple" 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="responsive table table-striped table-bordered" id="basic-datatable">
							<thead>
								<tr>
									<th><?= $lang['Action']?></th>
									<th>Nomination Type</th>
									<th><?= $lang['type']?></th>
									<th><?= $lang['Subject']?></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['question_id'] ;
		
										echo '
										 <tr class="odd gradeX">
										 	<td class="align_center">' ;
												if ( permissionCheck($row_user, 'form-nomination-question-edit') ){
													echo '
										 			<a title="Edit Current Nomination Question" href="app-form-nomination-question.php?page_mode=edit&question_id='.$id.'"><i class="fa fa-edit"></i></a>' ;
												}else{
													echo '-' ;
												}
											echo '
										 	</td>
											<td>'.ucwords($row_page['nomination_type']).'</td>
											<td>'.ucwords($row_page['question_type']).'</td>
											<td>'.dataFilter($row_page['title']).'</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 ;

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