<?php
$boolean_terminate  = ($type == 'terminate' ? true : false) ;
$boolean_reject     = ($type == 'reject' ? true : false) ;
?>

<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[$select_worker] ?><?= $lang['Employment']?> (<?= $lang[$employment_status] ?>) <small><?= $lang['list']?></small></h1>

            <?php
            if ($_SESSION['system_result'] != ''){
                switch($_SESSION['system_result']){
                    case 'failed-exists' :
                        echo '<div class="result_error">Sorry, email exists</div>' ;    
                    break ;
                }
                unset($_SESSION['system_result']) ;
            }
            ?>
        </div>

        <div class="panel panel-default" id="basic-table-title">
            <div class="panel-heading">
                <?=$lang['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['IC'] ?></label>
                        <div class="col-sm-9">
                            <input type="text" name="search_ic" value="<?= $search_ic ?>" class="form-control" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">POSITION</label>
                        <div class="col-sm-9">
                            <?php
                            // page query
                            $mysqli_query = "SELECT a.job_position_id, 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' AND a.job_position_show_al = 'Show' ORDER BY b.job_position_desc ASC" ;
                            $mysqli_position = $mysqli->query($mysqli_query) ;
                            if ($mysqli_position->num_rows > 0){
                                echo '
                                <select name="search_designation" class="form-control">
                                    <option value="">'.$lang['select'].'</option>' ;
                                    while($row_position = $mysqli_position->fetch_array(MYSQLI_ASSOC)){
                                        echo '
                                        <option value="'.$row_position['job_position_id'].'" '.($search_designation == $row_position['job_position_id'] ? 'selected' : '').'>'.dataFilter($row_position['job_position_desc']).'</option>' ;
                                    }
                                echo '
                                </select>' ;
                            }
                            ?>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">Qualification</label>
                        <div class="col-sm-9">
                            <select name="search_qualification" class="form-control">
                                <option value=""><?= $lang['select'] ?></option>
                                <option value="qua_below_pmr" <?= ($search_qualification == 'qua_below_pmr' ? 'selected' : '') ?>>Below PMR</option>
                                <option value="qua_pmr_pt3" <?= ($search_qualification == 'qua_pmr_pt3' ? 'selected' : '') ?>>PMR / PT3</option>
                                <option value="qua_spm" <?= ($search_qualification == 'qua_spm' ? 'selected' : '') ?>>SPM</option>
                                <option value="qua_certificate_skm_svm" <?= ($search_qualification == 'qua_certificate_skm_svm' ? 'selected' : '') ?>>Certificate/ SKM / SVM</option>
                                <option value="qua_alevel_uec" <?= ($search_qualification == 'qua_alevel_uec' ? 'selected' : '') ?>>A-Level / UEC</option>
                                <option value="qua_diploma" <?= ($search_qualification == 'qua_diploma' ? 'selected' : '') ?>>Diploma</option>
                                <option value="qua_degree" <?= ($search_qualification == 'qua_degree' ? 'selected' : '') ?>>Degree</option>
                                <option value="qua_master_degree" <?= ($search_qualification == 'qua_master_degree' ? 'selected' : '') ?>>Master Degree</option>
                                <option value="qua_phd" <?= ($search_qualification == 'qua_phd' ? 'selected' : '') ?>>PHD</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">Spoken EN</label>
                        <div class="col-sm-9">
                            <select name="search_spoke_en" class="form-control">
                                <option value=""><?= $lang['select'] ?></option>
                                <option value="lsen_not_applicable" <?= ($search_spoke_en == 'lsen_not_applicable' ? 'selected' : '') ?>>Not Applicable</option>
                                <option value="lsen_good" <?= ($search_spoke_en == 'lsen_good' ? 'selected' : '') ?>>Good</option>
                                <option value="lsen_excellent" <?= ($search_spoke_en == 'lsen_excellent' ? 'selected' : '') ?>>Excellent</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">Spoken BM</label>
                        <div class="col-sm-9">
                            <select name="search_spoke_bm" class="form-control">
                                <option value=""><?= $lang['select'] ?></option>
                                <option value="lsbm_not_applicable" <?= ($search_spoke_bm == 'lsbm_not_applicable' ? 'selected' : '') ?>>Not Applicable</option>
                                <option value="lsbm_good" <?= ($search_spoke_bm == 'lsbm_good' ? 'selected' : '') ?>>Good</option>
                                <option value="lsbm_excellent" <?= ($search_spoke_bm == 'lsbm_excellent' ? 'selected' : '') ?>>Excellent</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">Spoken CN</label>
                        <div class="col-sm-9">
                            <select name="search_spoke_cn" class="form-control">
                                <option value=""><?= $lang['select'] ?></option>
                                <option value="lscn_not_applicable" <?= ($search_spoke_cn == 'lscn_not_applicable' ? 'selected' : '') ?>>Not Applicable</option>
                                <option value="lscn_good" <?= ($search_spoke_cn == 'lscn_good' ? 'selected' : '') ?>>Good</option>
                                <option value="lscn_excellent" <?= ($search_spoke_cn == 'lscn_excellent' ? 'selected' : '') ?>>Excellent</option>
                            </select>
                        </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['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="select_worker" value="<?= $select_worker ?>" />
                            <input type="hidden" name="type" value="<?= $type ?>" />
                            <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>

        <div class="panel panel-default">
            <div class="panel-heading"><?=$lang['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['Name']?></th>
                            <th>NRIC</th>
                            <th>POSITION</th>
                            <th><?= $lang['Sex']?></th>
                            <th><?= $lang['Nationality']?></th>
                            <th><?= $lang['Mobile']?></th>
                            <th><?= $lang['status']?></th>
                            <?= ($boolean_terminate  ? '' : '<th>'.$lang['Update Profile'].'</th>') ?>
                            <?php
                            if ( $type == 'processing' || $type == 'interview' || $type == 'offer' ) {
                               echo'<th>'.$lang['Update'].'<br>'.$lang['Interview'].'</th>';
                            }
                            ?>
                            <th><?= $lang['Print']?> <?= $lang['Profile']?></th>
                            <?php
                            if ( $type == 'pending' || $type == 'processing' || $type == 'interview' || $type == 'reschedule' ){}else{
                                echo '
                                '.($boolean_terminate  ? '' : '<th>'.$lang['Update'].'<br />'.$lang['status'].'</th>').'
                                <th>'.$lang['Print'].'<br />'.$lang['Letter'].'</th>' ; 
                            }
                            ?>
                        </tr>
                    </thead>
                    <tbody>
                        <?php

                        // get the position
                        $position        = [] ;
                        $mysqli_ic = $mysqli->query("SELECT employment_nric FROM staff_employment GROUP BY employment_nric");
                        $all_ic = $mysqli_ic->fetch_all(MYSQLI_ASSOC);
                        $mysqli_position = $mysqli->query("SELECT a.job_position_id, 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'") ; 
                        if ( $mysqli_position->num_rows > 0 ){
                            while ( $row_position = $mysqli_position->fetch_assoc() ){
                                $position[$row_position['job_position_id']]  = dataFilter($row_position['job_position_desc']) ;  
                            }
                        }

                        if ($mysqli_page->num_rows > 0){
                            while ($row_page = $mysqli_page->fetch_assoc()){    
                            
                                $employment_id = $row_page['employment_id'] ;
                            
                                // check page type
                                $array_update_status = array() ;
                                switch($type){
                                    case 'pending' :
                                        $array_update_status = array('Pending', 'Offer', 'Reject') ;
                                        $link_update = 'hr-employment.php?page_mode=edit&page='.$employment_id ;
                                    break ;
                                    case 'offer' :
                                        $array_update_status = array('Offer', 'Confirmation', 'Terminate') ;
                                        $link_update = 'hr-employment.php?page_mode=offer&page='.$employment_id ;
                                    break ;
                                    case 'confirmation' :
                                        $array_update_status = array('Confirmation', 'Terminate') ;
                                    break ;
                                    case 'terminate' :
                                        $array_update_status = array('Terminate') ;
                                    break ;
                                }
                                
                                $link_print_jpeg = 'hr-employment-pdf.php?page='.$employment_id.'&type=changeType&page_type=jpeg' ;
                                $link_print_pdf = 'hr-employment-pdf.php?page='.$employment_id.'&type=changeType&page_type=pdf' ;
                                
                                echo '
                                 <tr class="odd gradeX">
                                    <td>'.dataFilter($row_page['employment_name']).'</a></td>
                                    <td>' . dataFilter($row_page['employment_nric']) .'<br/>'.
                                         (in_array($row_page['employment_nric'], $ic_list) ? ' ic duplicated' : ' ic unique') . 
                                         '</td>
                                    <td class="text_center">'.$position[$row_page['employment_position']].'</td>
                                    <td class="text_center">'.dataFilter($row_page['employment_sex']).'</td>
                                    <td class="text_center">'.dataFilter($row_page['employment_nationality']).'</td>
                                    <td class="text_center">'.dataFilter($row_page['employment_mobile']).'</td>
                                    '.($boolean_terminate  ? '<td class="text_center">'.$row_page['employment_status'].'</td>' : '<td class="text_center">'.$row_page['employment_status'].' | '.(!permissionCheck($row_user, 'application-list-edit') ? '' : ''.($row_page['employment_status'] == 'Reschedule' ? '<a href="hr-employment.php?page_mode=edit_status&page='.$employment_id.'&select_worker='.$select_worker.'"><i class="fa fa-warning" style="color:orange;"></i></a>' : '<a href="hr-employment.php?page_mode=edit_status&page='.$employment_id.'&select_worker='.$select_worker.'"><i class="fa fa-edit"></i></a>').'').'</td>').' ' ;
                                    echo 
                                    ($boolean_terminate  ? '' : '<td class="text_center">'.(!permissionCheck($row_user, 'application-list-edit') ? '' : '<a href="hr-employment.php?page_mode=edit&page='.$employment_id.'&select_worker='.$select_worker.'"><i class="fa fa-edit"></i></a>').'</td>');
                                    if ( $type == 'processing' || $type == 'interview' || $type == 'offer' ){
                                        echo'
                                            <td class="align_center">
                                                <a href="hr-employment.php?page_mode=edit_interview_det&page='.$employment_id.'&select_worker='.$select_worker.'" target="_blank" style="color:#9D9EA5"><i class="fa fa-edit"></i></a>
                                            </td>
                                        ';
                                    }
                                    echo'
                                    <td class="align_center">
                                        <a href="'.str_replace('changeType', 'pending', $link_print_jpeg).'" target="_blank" style="color:#9D9EA5"><i class="fa fa-file-image-o"></i></a>
                                        <span class="order_print_span">|</span>
                                        <a href="'.str_replace('changeType', 'pending', $link_print_pdf).'" target="_blank" style="color:#9D9EA5"><i class="fa fa-print"></i></a>
                                    </td>' ;
                                    if ( $type == 'pending' || $type == 'processing' || $type == 'interview' || $type == 'reschedule' ){}else{
                                        echo '
                                        '.($boolean_terminate  ? '' : '<td class="text_center">'.(!permissionCheck($row_user, 'application-list-update') ? '' : '<a href="hr-employment.php?page_mode=offer&page='.$employment_id.'"><i class="fa fa-edit"></i></a>').'</td>').'
                                        <td class="align_center">
                                            '.($boolean_offer ? 'Offer Letter : ' : '').'
                                            <a href="'.str_replace('changeType', $type, $link_print_jpeg).'" target="_blank" style="color:#9D9EA5"><i class="fa fa-file-image-o"></i></a>
                                            <span class="order_print_span">|</span>
                                            <a href="'.str_replace('changeType', $type, $link_print_pdf).'" target="_blank" style="color:#9D9EA5"><i class="fa fa-print"></i></a>' ;
                                            if ($boolean_offer){
                                                echo '
                                                <br />
                                                I.E.A Letter : 
                                                <a href="'.str_replace('changeType', 'iea', $link_print_jpeg).'" target="_blank" style="color:#9D9EA5"><i class="fa fa-file-image-o"></i></a>
                                                <span class="order_print_span">|</span>
                                                <a href="'.str_replace('changeType', 'iea', $link_print_pdf).'" target="_blank" style="color:#9D9EA5"><i class="fa fa-print"></i></a>' ;
                                            }
                                        echo '
                                        </td>' ;
                                    }
                                echo '
                                </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>
                                <td class="border_none"></td>
                                <td class="border_none"></td>
                                '.($boolean_terminate  ? '' : '<td class="border_none"></td>').'
                                '.($boolean_terminate  ? '' : '<td class="border_none"></td>').'
                                '.($type == 'pending' ? '' : ($boolean_terminate  ? '' : '<td class="border_none"></td>').'<td class="border_none"></td>').'
                            </tr>' ;
                        }
                        ?>
                    </tbody>
                </table>
                <?= $page_pagination['page_pagination'] ?>
            </div>
        </div>
    </div>
</div>
<span class="display_none" id="click_warning_message"><?= $lang['Confirm to change this employment status to'] ?></span>
