<?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']) ;
$formsubmissionid   = escapeString($_GET['formsubmissionid']) ;

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

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

		// check query exsits
		$submit_type = 'new' ;
		$mysqli_page = $mysqli->query("SELECT * FROM formsubmission
									   WHERE formsubmissionid = '".$formsubmissionid."' 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 ( $formsubmissionid == '' ){
				$mysqli->query( "INSERT INTO formsubmission 
							    ( created_at ) VALUES 
							    ( '".TODAYDATE."' )" ) ;
				$formsubmissionid = $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('FormSubmissionCategory', $formsubmissionid, $formsubmissionid, '', $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 ) ;
			}
			// update database
			$mysqli->query( "UPDATE formsubmission SET
							".$image_query."
							submission_type   	= '".escapeString($_POST['submission_type'])."',
							updated_at   		= NOW()
							WHERE formsubmissionid     = '".$formsubmissionid."'" ) ;

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

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

				checkLangUpdate( 'formsubmission_translation', 'formsubmissionid', $formsubmissionid, $klang, [ 
					'title' 	=> [ 'type' => 'input', 'value' => $title   ]
					//, 
					// 'content' 	=> [ 'type' => 'input', 'value' => $content ]
				] ) ;
			}

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

		if ( ( $page_mode == 'new' && !permissionCheck($row_user, 'form-submission-category-new') ) ||
			 ( $page_mode == 'edit' && !permissionCheck($row_user, 'form-submission-category-edit') ) ){
			header('Location: app-form-submission-category.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>Category <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-submission-category.php?page_mode=edit&formsubmissionid=<?= $formsubmissionid ?>&type=edit" novalidate="novalidate">

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

                        <div class="form-group display_none">
                            <div class="col-sm-2 control-label">Type</div>
                            <div class="col-sm-9">
                                <select name="submission_type" class="form-control ui-search-input">
                                	<option value="headcount" <?= ( $row_page['submission_type'] == 'headcount' ? 'selected' : '' ) ?> >Headcount Requisition </option>
                                	<option value="nomination" <?= ( $row_page['submission_type'] == 'nomination' ? 'selected' : '' ) ?> >Nomination</option>
                                	<option value="resignation" <?= ( $row_page['submission_type'] == 'resignation' ? 'selected' : '' ) ?> >Resignation Letter</option>
                                </select>
                            </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/FormSubmissionCategory/'.dataFilter($row_page['file']) ?>" target="_blank">
	                            		<img src="<?= PATH.'uploads/FormSubmissionCategory/'.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"></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 announcement list
	case 'all' :
	default :
		
		$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_title  != ''){
			$search_query  .= " AND b.title LIKE '%".$search_title."%'" ;
		}
		
		// form submit
		if ( $_POST['hide'] == '1' && $_POST['hide_status'] == 'action' ){
			switch($_POST['page_action']){
				case 'trash':
					$mysqli_query 	= "UPDATE formsubmission SET deleted_at = '".TODAYDATE."' WHERE formsubmissionid = " ;
					$trash_page 	= trashPage('formsubmission', $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.formsubmissionid, a.created_at, a.submission_type, b.title FROM formsubmission a
						   LEFT JOIN formsubmission_translation b ON ( a.formsubmissionid = b.formsubmissionid )
						   WHERE a.deleted_at IS NULL AND b.lang = 'en' " . $search_query ;
		$mysqli_page 	= $mysqli->query( $mysqli_query." ORDER BY a.formsubmissionid 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="page-header">
				<h1>Category <small><?= $lang['list']?></small></h1>
				<?php if ( permissionCheck($row_user, 'form-submission-category-new') ){ ?>
					<a href="app-form-submission-category.php?page_mode=new" class="btn btn-purple display_none" target="_blank"><?= $lang['add_new']?></a>
				<?php } ?>
			</div>

            <form method="post">

                <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><?= $lang['Subject']?></th>
									<th>Type</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_assoc() ){	  

										echo '
										 <tr class="odd gradeX">
										 	<td class="align_center">' ;
												if ( permissionCheck($row_user, 'form-submission-category-edit') ){
													echo '
										 			<a title="Edit Current Category" href="app-form-submission-category.php?page_mode=edit&formsubmissionid='.$row_page['formsubmissionid'].'"><i class="fa fa-edit"></i></a>' ;
												}else{
													echo '-' ;
												}
												echo '
										 	</td>
											<td>'.dataFilter($row_page['title']).'</td>
											<td>'.dataFilter($row_page['submission_type']).'</td>
											<td class="text-center">'.resetDateFormat($row_page['created_at']).'</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>
		<?php
	break ;

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