<?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 = 'setting' ;
$active_sub_menu = 'setting-hr' ;
$active_menu = 'setting-hr-position' ;


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


	// edit position
	case 'new' :
	case 'edit' :

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

			if ( $page == '' ){
				$mysqli->query("INSERT INTO setting_job_position (created_at) VALUES ('".TODAYDATE."')") ;
				$page = $mysqli->insert_id ;	
			}

			// update database
			$mysqli->query("UPDATE setting_job_position SET
							job_position_code     = '".escapeString($_POST['code'])."',
							job_position_show_al  = '".escapeString($_POST['show_al'])."',
							updated_at            = '".TODAYDATE."'
							WHERE job_position_id = '".$page."'") ;

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

				checkLangUpdate( 'setting_job_position_translation', 'job_position_id', $page, $klang, [ 
					'job_position_desc' => [ 'type' => 'input', 'value' => $job_position_desc   ]
				] ) ;
			}

			// refresh page
			$_SESSION['system_result'] = 'success-updated' ;
			echo '<script>window.open("setting-position.php?page_mode=edit&page='.$page.'&type=edit&success=1", "_self");</script>';

			exit ;
		}

		if ( ( $page_mode == 'new' && !permissionCheck($row_user, 'hr-position-list-new') ) ){
			header('Location: index.php') ;
			exit ;
		}

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


		<style>
		#map-container,#canvas,#existMap {
		  height: 400px;
		}
		.list_first, .list_second, .list_third, .list_four, .list_five {
		    border: 1px solid #ccc;
		    border-top: 0;
		    padding: 0;
		    width: 20%;
		    text-align: center;
		}
		.list_border {
		    border-top: 1px solid #ccc;
		}
		.bold{
			font-weight: bold;
		}
		</style>


		<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['Designation']?> <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_your_position_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="setting-position.php?page_mode=edit&page=<?= $page ?>&type=edit" novalidate="novalidate">
                        
                    	<?php 
                		echo showTabs( 'setting_job_position_translation', 'job_position_id', $page, [ 
                			'job_position_desc' => [
                				'type'    => 'input', 
                				'title'   => $lang['name']
                			]
                		]) ; 
                    	?>

                        <div class="form-group">
                            <div class="col-sm-2 control-label"><?= $lang['Code']?></div>
                            <div class="col-sm-9">
                                <input type="text" name="code" class="form-control ui-search-input" value="<?= dataFilter($row_page['job_position_code']) ?>" placeholder="<?= $lang['Code']?>" >
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label"><?= $lang['Show in Application List']?></div>
                            <div class="col-sm-9">                            	
                            	<select name="show_al" class="form-control ui-search-input" required="required">
                            		<option value=""><?= $lang['select'] ?></option>
                            		<option value="Show" <?= ($row_page['job_position_show_al'] == 'Show' ? 'selected' : '') ?> ><?= $lang['Show'] ?></option>
                            		<option value="Hide" <?= ($row_page['job_position_show_al'] == 'Hide' ? 'selected' : '') ?> ><?= $lang['Hide'] ?></option>
                            	</select>
                            </div>
                        </div>

                        <?php if (  $page_mode == 'new'  || ( $page_mode == 'edit' && permissionCheck($row_user, 'hr-position-list-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" value="1">
                                <input type="hidden" name="page_status" value="<?= $submit_type ?>">
                            </div>
                        </div>   
                        <?php } ?> 
                    </form>
                </div>
            </div>
        </div>
		</div>
		<?php
	break ;
	

	// all position list
	case 'all' :
	default :

		// check permission
		if ( !permissionCheck($row_user, 'hr-position-list-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_title  != ''){
			$search_query  .= " AND b.job_position_desc 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 " . setting_job_position . " SET deleted_at = '".TODAYDATE."' WHERE job_position_id = " ;
					$trash_page 	= trashPage('position', $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.job_position_id, a.job_position_code, a.created_at, b.job_position_desc FROM setting_job_position a
						 LEFT JOIN setting_job_position_translation b ON ( a.job_position_id = b.job_position_id )
						 WHERE a.deleted_at IS NULL AND b.lang = 'en' " . $search_query ;


		// export excel
		if ( $_GET['export'] == 'yes' ){
		    include 'PhpExcel/PHPExcel.php' ;

		    $page_filename = 'Position-'.date( 'Ymd', time() ) ;
		    $objPHPExcel   = new PHPExcel() ;
		    $objPHPExcel->getProperties()
		                ->setCreator(COMPANY)
		                ->setTitle(COMPANY)
		                ->setSubject(COMPANY)
		                ->setDescription(COMPANY)
		                ->setKeywords(COMPANY)
		                ->setCategory(COMPANY) ;

		    $objPHPExcel->getActiveSheet()->setTitle( $page_filename ) ;
		    $objPHPExcel->setActiveSheetIndex(0);
		    $objWriter = PHPExcel_IOFactory::createWriter( $objPHPExcel, 'Excel5' ) ;

		    $styleArrayTitle = array(
		        'font'  => array(
		            'bold'  => true ,
		            'color' => array( 'rgb' => '000000' ) ,
		            'size'  => 15
		        ),
		        'alignment' => array(
		            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
		        )
		    );

		    $styleArrayDay = array(
		        'font'  => array(
		            'bold'  => true
		        ),
		        'borders' => array(
		            'outline' => array(
		                'style' => PHPExcel_Style_Border::BORDER_THIN
		            )
		        ),
		        'alignment' => array(
		            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
		        )
		    ) ;

		    $styleArrayBg1 = array(
		        'fill' => array(
		            'type' => PHPExcel_Style_Fill::FILL_SOLID,
		            'color' => array('rgb' => 'AFABAB')
		        ),
		        'borders' => array(
		            'outline' => array(
		                'style' => PHPExcel_Style_Border::BORDER_THIN
		            )
		        ),
		        'alignment' => array(
		            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
		        )
		    ) ;

		    $styleArrayBg2 = array(
		        'fill' => array(
		            'type' => PHPExcel_Style_Fill::FILL_SOLID,
		            'color' => array('rgb' => 'FFDA65')
		        ),
		        'borders' => array(
		            'outline' => array(
		                'style' => PHPExcel_Style_Border::BORDER_THIN
		            )
		        ),
		        'alignment' => array(
		            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
		        )
		    ) ;

		    $styleArray = array(
		        'borders' => array(
		            'outline' => array(
		                'style' => PHPExcel_Style_Border::BORDER_THIN
		            )
		        ),
		        'alignment' => array(
		            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
		        )
		    ) ;

		    $styleArrayLeft = array(
		        'borders' => array(
		            'outline' => array(
		                'style' => PHPExcel_Style_Border::BORDER_THIN
		            )
		        ),
		        'alignment' => array(
		            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
		        )
		    ) ;

		    $styleArray2 = array(
		        'alignment' => array(
		            //'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
		            'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
		        )
		    ) ;


		    $count 	   = 1 ;
		    $firstChar = 'A' ;
		    $lastChar  = 'E' ;

		    // title name
		    $objPHPExcel->getActiveSheet()->mergeCells( $firstChar.$count.':'.$lastChar.$count ) ;
		    $objPHPExcel->getActiveSheet()->getStyle( $firstChar.$count.':'.$lastChar.$count )->applyFromArray( $styleArrayTitle ) ;
		    $objPHPExcel->setActiveSheetIndex(0)->setCellValue( $firstChar.$count, 'Position List' ) ;
		    $count++ ;


		    $setChar = $firstChar ;
		    $objPHPExcel->getActiveSheet()->getStyle($setChar.$count)->applyFromArray( $styleArray ) ;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue( $setChar.$count, 'No' ) ;
            $setChar++ ;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue( $setChar.$count, 'Title' ) ;
            $setChar++ ;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue( $setChar.$count, 'Date' ) ;


		    // loop all attendance record

		    $mysqli_page = $mysqli->query($mysqli_query." ORDER BY job_position_id") ;
		    if ( $mysqli_page->num_rows > 0 ){

		    	$count_no = 0 ;
		    	while ( $row_page = $mysqli_page->fetch_assoc() ){

		            $count++ ;
		            $count_no++ ;
		            $setChar = $firstChar ;

		            $objPHPExcel->getActiveSheet()->getStyle($setChar.$count)->applyFromArray( $styleArray ) ;
		            $objPHPExcel->setActiveSheetIndex(0)->setCellValue( $setChar.$count, $count_no ) ;
		            $setChar++ ;
		            $objPHPExcel->setActiveSheetIndex(0)->setCellValue( $setChar.$count, dataFilter($row_page['job_position_desc']) ) ;
		            $setChar++ ;
		            $objPHPExcel->setActiveSheetIndex(0)->setCellValue( $setChar.$count, dataFilter($row_page['created_at']) ) ;
		        }
		    }

		    // Submission from
		    header( 'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ) ; 
		    header( 'Content-Disposition: attachment;filename="'.$page_filename.'.xls"' ) ;
		    header( 'Cache-Control: max-age=0' ) ;
		    $objWriter->save('php://output') ;
		    header( "Refresh: 0" ) ;
		    exit ;
		}

		// load pagination
		$mysqli_page = $mysqli->query($mysqli_query." ORDER BY a.job_position_id LIMIT $start_from, " . LIMIT) ;
		$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><?= $lang['Designation']?> <small><?= $lang['list']?></small></h1>
					</div>
					<div class="pull-right col">
					<?php if ( permissionCheck($row_user, 'hr-position-list-new') ){ ?>
						<a href="setting-position.php?page_mode=new" class="btn" style="color:white;background-color: #5e5bd0;" target="_blank"><?= $lang['add_new']?></a>
					<?php } ?>
	        		<a href="setting-position.php?<?= $search_url ?>&export=yes" target="_blank" class="btn" style="color:white;background-color: #5e5bd0;"><?= $lang['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['title'] ?></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, 'hr-position-list-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['Code']?></th>
									<th><?= $lang['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)){

										echo '

										 <tr class="odd gradeX">
											<td><a href="setting-position.php?page_mode=edit&page='.$row_page['job_position_id'].'">'.dataFilter($row_page['job_position_desc']).'</a></td>
											<td>'.dataFilter($row_page['job_position_code']).'</td>
											<td>'.resetDateFormat($row_page['created_at']).'</td>
											<td>
												<div class="checkbox multiple_trash">
													<input type="checkbox" name="multiple_trash['.$row_page['job_position_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' ;
?>