<?php
$optres['css'][] = $module_css_path . "/amodules/jobs_ext/css/jobs_ext.css";
$optres['js'][] = $path . "/amodules/jobs_ext/js/jobs_main.js";

$jobs = array(
    'job_cat_id' => "",
    'Location' => $def_location,
    'Conditions' => "",
    'posted' => "all",
    'i_limit' => "20",
    'display' => "no"
);

$cv = array(
    'job_cat_id' => "",
    'JobLocation' => $def_location,
    'CountryOfOrigin' => "",
    'posted' => "all",
    'i_limit' => "20",
    'display' => "no"
);

if(isset($_REQUEST['jobs'])) {
	foreach($_REQUEST['jobs'] as $f=>$v) {
        if(isset($jobs[$f])) $jobs[$f] = $v;
    }
}

if(isset($_REQUEST['cv'])) {
    foreach($_REQUEST['cv'] as $f=>$v) {
        if(isset($cv[$f])) $cv[$f] = $v;
    }
}

$locations = array("Nicosia", "Limassol", "Larnaca", "Paphos", "Famagusta");

?>
<div class="row">
    <div class="col-sm-10">
        <div class="tabs-container">
            <ul class="nav nav-tabs" role="tablist">
                <li role="presentation" class="active"><a href="#jobs" aria-controls="jobs" role="tab" data-toggle="tab"><i class="fa fa-search fa-lg"></i> Jobs</a></li>
                <li role="presentation"><a href="#resumes" aria-controls="resumes" role="tab" data-toggle="tab"><i class="fa fa-search fa-lg"></i> Resumes</a></li>
            </ul>

            <div class="tab-content form-compact">
                <div role="tabpanel" class="tab-pane active" id="jobs">
                    <section class="jsrch_container box-body">
                            <form method="post" action="jobs/" name="jobs_search_frm" id="jobs_search_frm" class="form-horizontal" role="form">
                                <div class="container-fluid">

                                    <div class="form-group">
                                        <div class="row">
                                            <div class="col-sm-5">
                                                <label class="control-label sr-only" for="jobs_job_cat_id"><?php echo 'Job Category';?></label>
                                                <select name="jobs[job_cat_id]" id="jobs_job_cat_id" class="form-control input-sm">
                                                    <option value=""><?php echo 'Job Category';?></option>
                                                    <?php
                                                    $all_job_categs = $pfcache->get('all_job_categs');
                                                    if($all_job_categs == null) {
                                                        $all_job_categs = array();
                                                        $qexl = "select * from job_cat where active='yes' order by order_seq asc;";
                                                        $rexl = @mysql_query($qexl);
                                                        if($rexl && mysql_num_rows($rexl) > 0) {
                                                            while ($rowexl = mysql_fetch_assoc($rexl)) {
                                                                $all_job_categs [] = array_map('stripslashes', $rowexl);
                                                            }
                                                        }
                                                        $pfcache->set('all_job_categs', $all_job_categs, 1200);

                                                    }

                                                    foreach ($all_job_categs as $rowexl){
                                                        $selected = '';
                                                        if($jobs['job_cat_id'] == $rowexl['job_cat_id']) $selected = ' selected="selected"';
                                                        echo '<option value="',trim($rowexl['job_cat_id']),'" ',$selected,'>', stripslashes($rowexl['title_en']), '</option>';
                                                    }

                                                    ?>
                                                </select>
                                            </div>
                                            <div class="col-sm-5">
                                                    <label class="control-label sr-only" for="jobs_Location"><?php echo 'Job Location';?></label>
                                                    <select name="jobs[Location]" id="jobs_Location" class="form-control input-sm">
                                                        <option value=""><?php echo 'Job Location'?></option>
                                                        <?php
                                                        foreach ($locations as $loc) {
                                                            $selected = '';
                                                            if($jobs['Location'] == $loc) $selected = 'selected="selected"';
                                                            echo '<option value="',$loc,'" ',$selected,'>', $loc, '</option>';
                                                        }
                                                        ?>
                                                    </select>
                                            </div>
                                        </div>
                                    </div>



                                    <div class="form-group">
                                        <div class="row">
                                            <div class="col-sm-5">
                                                <label class="control-label sr-only" for="jobs_Conditions"><?php echo 'Job Type';?></label>
                                                <?php
                                                $all_commitment = array(
                                                    "full-time" 	=> "Full Time",
                                                    "part-time"		=> "Part Time",
                                                    "contract"		=> "Contract",
                                                    "temp"			=> "Temporary",
                                                    "internship"	=> "Internship",
                                                    "other"			=> "Other"
                                                );
                                                ?>
                                                <select name="jobs[Conditions]" id="jobs_Conditions" class="form-control input-sm">
                                                    <option value=""><?php echo 'Job Type';?></option>
                                                    <?php
                                                    foreach ($all_commitment as $ctype=>$ctype_title) {
                                                        $selected = '';
                                                        if($jobs['Conditions'] == $ctype) $selected = 'selected="selected"';
                                                        echo '<option value="',$ctype,'" ',$selected,'>', $ctype_title, '</option>';
                                                    }
                                                    ?>
                                                </select>
                                            </div>

                                            <div class="col-sm-5">
                                                <label class="control-label sr-only" for="jobs_posted"><?php echo 'Posted';?></label>
                                                <select name="jobs[posted]" id="jobs_posted" class="form-control input-sm">
                                                    <option value="all" selected="selected">Date Posted</option>
                                                    <?php
                                                    $posted_vals = array(
                                                        '0' => 'Today',
                                                        '1' => 'Yesterday',
                                                        '7'	=> 'Last 7 Days',
                                                        '30'=> 'Last 30 Days'

                                                    );

                                                    foreach ($posted_vals as $pv=>$pv_title) {
                                                        $selected = '';
                                                        if(isset($jobs['posted']) && $jobs['posted'] != "all" && $jobs['posted'] == $pv) $selected = 'selected="selected"';
                                                        echo '<option value="',$pv,'" ',$selected,'>', $pv_title, '</option>';
                                                    }
                                                    ?>
                                                </select>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="form-group" style="margin-bottom: 0!important;">
                                        <div class="row">
                                            <div class="col-sm-5 form-inline">
                                                <?php
                                                echo 'Show', ' ';
                                                echo '<select name="jobs[i_limit]" style="width: 70px;" class="form-control input-sm">';
                                                $limit_ar = array('10', '15', '20', '25', '30', '50');
                                                foreach ($limit_ar as $lt) {
                                                    $selected = '';
                                                    if(intval($jobs['i_limit']) == intval($lt)) $selected = 'selected="selected"';
                                                    echo '<option value="',$lt,'" ',$selected,'>',$lt,'</option>';
                                                }
                                                echo '</select> ', 'results per page';
                                                ?>
                                            </div>
                                            <div class="col-sm-5 tab-actions text-center">
                                                <button type="submit" class="load-disabled btn btn-primary btn-sm pull-left" disabled="disabled"><i class="fa fa-search fa-lg fa-spin"></i> Search Jobs</button>
                                                <a href="account/jobs/job" class="btn btn-darkred btn-sm pull-right"><b>POST FREE</b> Jobs!</a>
                                            </div>
                                        </div>
                                    </div>

                                </div>

                                <input type="hidden" name="jobs[i_from]" value="0">
                                <input type="hidden" name="module_page" value="jobs_ext">
                                <input type="hidden" name="jobs_url" value="jobs"/>
                                <input type="hidden" name="jobs[display]" value="yes"/>
                            </form>
                    </section>
                </div>
                <div role="tabpanel" class="tab-pane" id="resumes">
                    <section  class="jsrch_container box-body">
                            <form method="post" action="resumes/" name="resumes_search_frm" id="resumes_search_frm" class="form-horizontal" role="form">
                                <div class="container-fluid">
                                    <div class="form-group">
                                        <div class="row">
                                            <div class="col-sm-5">
                                                <label for="cv_job_cat_id" class="control-label sr-only"><?php echo 'Job Category';?></label>
                                                <select name="cv[job_cat_id]" id="cv_job_cat_id" class="form-control input-sm">
                                                    <option value=""><?php echo 'Job Category';?></option>
                                                    <?php

                                                    foreach ($all_job_categs as $rowexl){
                                                        $selected = '';
                                                        if($cv['job_cat_id'] == $rowexl['job_cat_id']) $selected = ' selected="selected"';
                                                        echo '<option value="',trim($rowexl['job_cat_id']),'" ',$selected,'>', stripslashes($rowexl['title_en']), '</option>';
                                                    }

                                                    ?>
                                                </select>
                                            </div>

                                            <div class="col-sm-5">
                                                <label for="cv_JobLocation" class="control-label sr-only"><?php echo 'Location';?></label>
                                                <select name="cv[JobLocation]" id="cv_JobLocation" class="form-control input-sm">
                                                    <option value=""><?php echo 'Job Location'?></option>
                                                    <?php
                                                    foreach ($locations as $loc) {
                                                        $selected = '';
                                                        if($cv['JobLocation'] == $loc) $selected = 'selected="selected"';
                                                        echo '<option value="',$loc,'" ',$selected,'>', $loc, '</option>';
                                                    }
                                                    ?>
                                                </select>
                                            </div>

                                        </div>
                                    </div>


                                    <div class="form-group">
                                        <div class="row">
                                            <div class="col-sm-5">
                                                <label for="cv_CountryOfOrigin" class="control-label sr-only"><?php echo 'Country Of Origin';?></label>
                                                <select name="cv[CountryOfOrigin]" id="cv_CountryOfOrigin" class="form-control input-sm">
                                                    <option value=""><?php echo 'Country Of Origin';?></option>
                                                    <?php
                                                    $countries = $pfcache->get('current_CountryOfOrigin');
                                                    if($countries == null) {
                                                        $countries = array();
                                                        $qexl = "SELECT DISTINCT CountryOfOrigin FROM job_cvs WHERE active='yes' AND resume_status='1' ";
                                                        if($rdl > 0) {
                                                            $daystolive = date("Y-m-d 00:00:00", strtotime("-$rdl days", time()));
                                                            $qexl .= " AND updated_at>='$daystolive' ";
                                                        }
                                                        $qexl .= " ORDER BY CountryOfOrigin ASC;";
                                                        $rexl = @mysql_query($qexl);
                                                        if($rexl && mysql_num_rows($rexl) > 0) {
                                                            while ($rowexl = mysql_fetch_assoc($rexl)){
                                                                $countries[] = stripslashes($rowexl['CountryOfOrigin']);
                                                            }
                                                        }
                                                        $pfcache->set('current_CountryOfOrigin', $countries, 1200);

                                                    }
                                                    if($countries != null) {


                                                        foreach ($countries as $index=>$coo){
                                                            $selected = '';
                                                            if( strcmp($cv['CountryOfOrigin'], $coo) === 0) $selected = ' selected="selected"';
                                                            echo '<option value="',$coo,'" ',$selected,'>', stripslashes($coo), '</option>';
                                                        }
                                                    }
                                                    ?>
                                                </select>
                                            </div>
                                            <?php
                                            //echo '<pre>', var_export($countries, true), '</pre>';
                                            ?>
                                            <div class="col-sm-5">
                                                <label for="cv_posted" class="control-label sr-only"><?php echo 'Posted';?></label>
                                                <select name="cv[posted]" id="cv_posted" class="form-control input-sm">
                                                    <option value="all" selected="selected">Date Posted</option>
                                                    <?php
                                                    $posted_vals = array(
                                                        '0' => 'Today',
                                                        '1' => 'Yesterday',
                                                        '7'	=> 'Last 7 Days',
                                                        '30'=> 'Last 30 Days'

                                                    );
                                                    foreach ($posted_vals as $pv=>$pv_title) {
                                                        $selected = '';
                                                        if($cv['posted'] != "all" && $cv['posted'] == $pv) $selected = 'selected="selected"';
                                                        echo '<option value="',$pv,'" ',$selected,'>', $pv_title, '</option>';
                                                    }
                                                    ?>
                                                </select>
                                            </div>
                                        </div>

                                    </div>

                                    <div class="form-group" style="margin-bottom: 0!important;">
                                        <div class="row">
                                            <div class="col-sm-5 form-inline">
                                                <?php
                                                echo 'Show', ' ';
                                                echo '<select name="cv[i_limit]"  style="width: 70px;" class="form-control input-sm">';
                                                $limit_ar = array('10', '15', '20', '25', '30', '50');
                                                foreach ($limit_ar as $lt) {
                                                    $selected = '';
                                                    if(intval($cv['i_limit']) == intval($lt)) $selected = 'selected="selected"';
                                                    echo '<option value="',$lt,'" ',$selected,'>',$lt,'</option>';
                                                }
                                                echo '</select> ', 'results per page';
                                                ?>
                                            </div>
                                            <div class="col-sm-5 tab-actions">
                                                <button type="submit" class="load-disabled btn btn-primary btn-sm pull-left" disabled="disabled"><i class="fa fa-search fa-lg fa-spin"></i> Search Resumes</button>
                                                <a href="account/resume/my-resume" class="btn btn-darkred btn-sm pull-right"><b>POST FREE</b> Resume!</a>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <input type="hidden" name="cv[i_from]" value="0">
                                <input type="hidden" name="module_page" value="jobs_ext">
                                <input type="hidden" name="jobs_url" value=""/>
                                <input type="hidden" name="cv[display]" value="yes"/>
                            </form>





                    </section>
                </div>
            </div>
        </div>

    </div>


</div>

<?php
if($gads_show) {
    if ($def_ads == "yes" && $def_ad_id != "") {
        include(dirname(__FILE__) . "/../gads_content/gads_content_default.php");
    }
}


if($def_category_list == "yes") {
// Get the category jobs
    $jobs_by_categs_key = 'jobs_by_categs';
    if(isset($def_location) && $def_location != "") {
        $jobs_by_categs_key .= "_" . $def_location;
    }
    $categs = $pfcache->get($jobs_by_categs_key);
    if($categs == null) {

        $jcategs = [];
        $q = "SELECT * FROM job_cat WHERE active='yes' ORDER BY order_seq ASC";
        $r = @mysql_query($q);
        if($r && mysql_num_rows($r) > 0) {
            while($row = mysql_fetch_assoc($r)) {
                $jcategs[$row['job_cat_id']] = stripslashes($row['title_en']);
            }
        }

        /*$q = "SELECT j.job_cat_id, COUNT(j.job_cat_id) AS num_jobs, c.title_en FROM job_jobs j LEFT JOIN job_cat c ON j.job_cat_id=c.job_cat_id WHERE j.active='yes' AND j.job_status='1' ";
        if ($jdl > 0) {
            $daystolive = date("Y-m-d 00:00:00", strtotime("-$jdl days", time()));
            $q .= " AND j.updated_at>='$daystolive' ";
        }

        if(isset($def_location) && $def_location != "") {
            $q .= " AND j.Location like '%$def_location%' ";
        }

        $q .= " GROUP BY j.job_cat_id ORDER BY c.title_en ASC;";*/

        /*---------------*/

        $q = "SELECT
                c.categ_id,
                count(c.categ_id) as num_jobs
              FROM
                job_categs c,
                job_jobs j
              WHERE
                c.job_id=j.job_jobs_id
                AND j.active='yes' AND j.job_status='1'
                ";
        if ($jdl > 0) {
            $daystolive = date("Y-m-d 00:00:00", strtotime("-$jdl days", time()));
            $q .= " AND j.updated_at>='$daystolive' ";
        }

        if(isset($def_location) && $def_location != "") {
            $q .= " AND j.Location like '%$def_location%' ";
        }

        $q .= " GROUP BY c.categ_id;";
        //echo $q;

        $tmp_array = [];
        $r = @mysql_query($q);
        if ($r && mysql_num_rows($r) > 0) {
            while ($row = mysql_fetch_assoc($r)) {
                $num_jobs = intval($row['num_jobs']);
                $job_cat_id = intval($row['categ_id']);
                $title_en = $jcategs[$job_cat_id];

                $link = "jobs/category/" . $job_cat_id . "/" . url_slug($title_en);
                if(isset($def_location) && $def_location != "") {
                    $link = "jobs/" . url_slug("in $def_location") . "/category/" . $job_cat_id . "/" . url_slug($title_en);
                }

                $tmp_array [$job_cat_id] = array(
                    "title" => $title_en . ' (' . $num_jobs . ')',
                    "link" => $link
                );
            }
        }

        foreach($jcategs as $cid=>$cdata) {
            if(isset($tmp_array[$cid])) $categs [] = $tmp_array[$cid];
        }



        $pfcache->set($jobs_by_categs_key, $categs, 1200);
    }



    $def_title_location = (isset($def_location) && $def_location != "") ? $def_location : "Cyprus";

    if (count($categs) > 0) {
        ?>
        <div class="box box-primary category-jobs">
            <div class="box-header with-border">
                <h2><?=$def_title_location;?> Jobs By Category</h2>
            </div>
            <div class="box-body">
                <div class="container-fluid">
                    <div class="row">
                        <?php
                        foreach ($categs as $cat) {
                            ?>
                            <div class="col-sm-5 jcateg">
                                <a href="<?= $cat['link']; ?>"><?= $cat['title']; ?></a>
                            </div>
                            <?php
                        }
                        ?>
                    </div>
                </div>
            </div>
        </div>
        <?php
    }

}
