<?php
/**
 * Created by PhpStorm.
 * User: IOAN
 * Date: 9/29/2015
 * Time: 9:19 AM
 */
require_once(dirname(__FILE__) . "/../classes/Paginator.php");
// Breadcrumbs
$breadcrumbs [] = array(
    'link' => 'account/my_account.html',
    'title' => 'My Account'
);

$breadcrumbs [] = array(
    'link'  => '',
    'title' => "My Jobs"
);

$optres['css'][] = $path . "assets/css/bootstrap-dialog.min.css";
$optres['js'][] = $path . "assets/js/bootstrap-dialog.min.js";

$optres['css'][] = $module_css_path . "amodules/jobsresumes/css/paginator.css";
$optres['css'][] = $module_css_path . "amodules/jobsresumes/css/my_jobs.css";
$optres['js'][] = $path . "amodules/jobsresumes/js/my_jobs.js";

$member_id = $_SESSION['member_id'];
$my_jobs = array();
$job_categs = array();

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

$search = array (
    'job_jobs_id' => '',
    'member_id' => '',
    'job_cat_id' => '',
    'JobTitle' => '',
    'Location' => '',
    'CompanyName' => '',
    'email' => '',
    'phone' => '',
    'active' => '',
    'date_before' => '',
    'date_after' => '',
    'added_by' => '',
    'search' => 'no',
    'i_from' => 0,
    'i_limit' => 20
);

if(isset($_REQUEST['filter'])) {
    $filter = (trim($_REQUEST['filter']) != "") ? base64_decode($_REQUEST['filter']) : "";
    if($filter != "") {
        $qs = array();
        parse_str($filter, $qs);
        $search = array_merge($search, $qs);
    }
}

$num_jobs = 0;
$q = "SELECT * FROM job_jobs WHERE member_id='$member_id' ORDER BY updated_at DESC";
$rnum = @mysql_query($q);
if($rnum && mysql_num_rows($rnum) > 0) {
    $num_jobs = mysql_num_rows($rnum);

    $q .= " LIMIT  {$search['i_from']}, {$search['i_limit']} ";
    $r = @mysql_query($q);
    //echo $q;
    while($row = mysql_fetch_assoc($r)) {
        $job_jobs_id = intval($row['job_jobs_id']);
        $my_jobs[$job_jobs_id] = array_map('stripslashes', $row);
    }
}

// Jobs configs
$NeedActivation = 'yes';
$DaysToLive = 180;
$Expiration = 'yes';
$q = "SELECT * FROM job_config";
$r = @mysql_query($q);
if($r && mysql_num_rows($r) > 0) {
    $row = mysql_fetch_assoc($r);

    $NeedActivation = $row["NeedActivation"];
    $DaysToLive = $row["DaysToLive"];
    $Expiration = $row["Expiration"];
}

if(isset($_SESSION['flashm']) && trim($_SESSION['flashm']) != "") {
    ?>
    <div class="alert alert-info">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
        <p><i class="fa fa-info-circle"></i> <?=$_SESSION['flashm'];?></p>
    </div>
    <?php
    unset($_SESSION['flashm']);
}
?>

<a href="account/jobs/job.html" class="btn btn-success btn-sm"><i class="fa fa-plus"></i> Add New Job</a>

<?php


if($num_jobs > 0) {
    $paginator = new Paginator(array(
        "base_url"  => "account/jobs/my-jobs.html?",
        "request_parameter_name" => "filter",
        "records_per_page" => 20
    ));

    $search = array_map('stripslashes', $search);
    $pagination = $paginator->paginate($num_jobs, $search);
    echo $pagination;


    foreach ($my_jobs as $job_jobs_id => $job) {
        switch($job['active']) {
            case "yes":
                $status = 'Approved';
                $status_tip = 'Approved Listing';
                $status_class = 'label-success';
                break;
            case "new":
                $status = 'Pending';
                $status_tip = 'Pending approval by Administrator';
                $status_class = 'label-warning';
                break;
            case "no":
                $status = 'Rejected';
                $status_tip = 'Rejected by Administrator';
                $status_class = 'label-danger';
                break;

        }

        if(intval($job['updated']) == 1) {
            $status = 'Pending';
            $status_tip = 'Pending approval by Administrator';
            $status_class = 'label-warning';
        }

        $job_cat_id = $job['job_cat_id'];

        $created = strtotime($job['added_on']);
        $updated_at = strtotime($job['updated_at']);
        $expires = $updated_at + $DaysToLive * 3600 * 24;
        $now = time();
        $expired = ($now > $expires) ? "Expired" : "Available";
        $expired_class = ($now > $expires) ? 'label-danger' : 'label-success';
        $expired_tip = ($now > $expires) ? 'This Job is expired' : 'This Job is available';

        $job_status = intval($job['job_status']);
        $job_status_status = $job_status == 1 ? "Active" : "Inactive";
        $job_status_text = $job_status == 1 ? "Inactivate" : "Activate";
        $job_status_label = $job_status == 1 ? "label-success" : "label-danger";
        $job_status_text = 'This Job is <span class="job-stat label '.$job_status_label.'">'.$job_status_status.'</span>. <span class="status-action hidden">Click to <a href="#" class="job-status btn btn-xs btn-default" data-job_status="'.$job_status.'" data-jid="'.$job_jobs_id.'">' . $job_status_text . '</a></span>';

        $jcategs = [];
        $categs = [];
        $q = "SELECT * FROM job_categs WHERE job_id='$job_jobs_id' ORDER BY id ASC";
        $r = @mysql_query($q);
        if($r && mysql_num_rows($r) > 0) {
            while($row = mysql_fetch_assoc($r)) {
                $categs [] = $row['categ_id'];
            }
        }
        if(count($categs) > 0) {
            foreach($categs as $c_id) {
                $jcategs [] = $job_categs[$c_id];
            }
        }


        ?>
        <div class="box box-info job-box">
            <div class="container-fluid">
                <div class="row job-row">
                    <div class="col-xs-10">
                        <div class="row job-header">
                            <div class="col-xs-7"><span class="job-title"><?= $job['JobTitle']; ?></span></div>
                            <div class="col-xs-3 text-right">
                                <a href="account/jobs/job.html?jid=<?= $job_jobs_id; ?>" class="btn btn-info btn-xs"
                                   title="Edit"><i class="fa fa-pencil"></i> </a>
                                <a href="#" class="btn btn-danger btn-xs delete-job" data-jid="<?= $job_jobs_id;?>" title="Delete"><i
                                        class="fa fa-times"></i> </a>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-10">
                        <div class="row job-body">
                            <div class="col-xs-5 text-left">Updated: <i class="fa fa-calendar-o fa-fw"></i> <span
                                    class="text-bold"><?= date('d/m/Y', strtotime($job['updated_at'])); ?></span>
                            </div>
                            <div class="col-xs-3 text-left">Clicks: <span
                                    class="text-bold"><?= $job['clicks']; ?></span></div>
                            <div class="col-xs-2 text-right"><div class="label <?= $status_class; ?>" data-toggle="tooltip" data-container="body" data-placement="left" title="<?=$status_tip;?>"><?= $status; ?></div></div>

                            <div class="col-xs-8 text-left">Expires: <i class="fa fa-hourglass-end fa-fw"></i> <span
                                    class="text-bold"><?= date('d/m/Y', $expires); ?></span>
                            </div>
                            <div class="col-xs-2 text-right"><div class="label <?= $expired_class; ?>"><?= $expired; ?></div></div>

                            <div class="col-sm-10 col-xs-10 text-left">Category: <i class="fa fa-folder fa-fw"></i>
                                <span class="text-bold"><?= implode(', <i class="fa fa-folder fa-fw"></i>', $jcategs); ?></span>
                            </div>

                            <div class="col-sm-8 col-xs-8 text-left">Location:
                                <?php
                                $locs = explode(";", $job['Location']);
                                // get the empty array keys using array_keys
                                $locs_keys = array_keys($locs,"");

                                // foreach empty key, we unset that entry
                                foreach ($locs_keys as $lk) unset($locs[$lk]);

                                foreach($locs as $loc) {
                                    echo '<span class="text-bold"><i class="fa fa-map-marker text-info"></i> '.$loc.'</span> ';
                                }
                                ?>

                            </div>
                            <div class="col-xs-2 text-right">ID# <span class="text-bold"><?= $job_jobs_id; ?></span></div>
                            <div class="col-sm-10 col-xs-10 text-left job-footer"><?= $job_status_text; ?></div>
                        </div>
                    </div>


                </div>
            </div>
        </div>
        <?php
    }
    echo $pagination;
    ?>

    <?php
}
?>

