<?php
/**
 * Created by PhpStorm.
 * User: IOAN
 * Date: 9/29/2015
 * Time: 10:32 AM
 */

/*$optres['css'][] = $path . "assets/css/bootstrap3-wysihtml5.min.css";
$optres['js'][] = $path . "assets/js/bootstrap3-wysihtml5.all.min.js";*/

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

$optres['js'][] = $path . "assets/plugins/tinymce/js/tinymce/tinymce.min.js";
$optres['css'][] = $module_css_path . "amodules/jobsresumes/css/job.css";
$optres['js'][] = $path . "amodules/jobsresumes/js/job_v1.js";

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


$member_id = intval($_SESSION['member_id']);

// Get member trust
$trusted = 0;
$q = "SELECT * FROM job_members WHERE member_id='$member_id'";
$r = @mysql_query($q);
if($r && mysql_num_rows($r) == 1) {
    $row = mysql_fetch_assoc($r);
    $trusted = intval($row['trusted']);
}

$jid = isset($_REQUEST['jid']) ? intval($_REQUEST['jid']) : 0;
$job_jobs_id = $jid;

$show_form = true;
$err = array();

$new_date = time();

$j = array(
    //"job_cat_id" => "",
    "catdom_id" => $domain_id,
    "CompanyName" => "",
    "Address" => "",
    "Town" => "",
    "OtherTown" => "",
    "Phone" => "",
    "Mobile" => "",
    "Fax" => "",
    "ContactPerson" => "",
    "EmailAdd" => "",
    "JobTitle" => "",
    "JobDescription" => "",
    "salary_min" => null,
    "salary_max" => null,
    "NumberOfPersons" => 1,
    "Qualifications" => "",
    "Other" => "",
    "Location" => "",
    "Conditions" => "",
    "added_on" => date("Y-m-d", $new_date),
    "added_on_time" => date("H:i:s", $new_date),
    "created_at" => date("Y-m-d H:i:s", $new_date),
    "updated" => 0,
    "updated_at" => date("Y-m-d H:i:s", $new_date),
    "member_id" => $_SESSION['member_id'],
    "active" => "new",
    "job_status" => 1,
    "clicks" => 0,
    "jversion" => 1,
    "categs" => []
);

if($job_jobs_id > 0) {
    $q = "SELECT * FROM job_jobs WHERE job_jobs_id='$jid' AND member_id='$member_id';";
    $r = @mysql_query($q);
    if($r && mysql_num_rows($r) == 1) {
        $show_form = true;
        $row = mysql_fetch_assoc($r);
        $tmp = array_map('stripslashes', $row);
        $j = array_merge($j, $tmp);

        $categs = [];
        $q = "SELECT * FROM job_categs WHERE job_id='$jid' 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) {
            $j['categs'] = $categs;
        }


    }
    else {
        $show_form = false;
        $err [] = "The job you are trying to Edit was not found in our database.";
    }
}

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']);
}

if($show_form) {

    //echo '<pre>', var_export($j, true), '</pre>';
    // Breadcrumbs
    $breadcrumbs [] = array(
        'link' => 'account/my_account.html',
        'title' => 'My Account'
    );

    $breadcrumbs [] = array(
        'link'  => 'account/jobs/my-jobs.html',
        'title' => "My Jobs"
    );

    $breadcrumbs [] = array(
        'link'  => '',
        'title' => $job_jobs_id > 0 ? "Edit Job Id# $job_jobs_id" : "Add New Job"
    );

    if($job_jobs_id > 0) {

        switch($j['active']) {
            case "yes":
                $status = 'Approved';
                $status_tip = 'Approved Listing';
                $status_class = 'fa fa-lg fa-check text-success';
                $text_class = "text-success";
                break;
            case "new":
                $status = 'Pending';
                $status_tip = 'Pending approval by Administrator';
                $status_class = 'fa fa-lg fa-hourglass-half text-warning';
                $text_class = "text-warning";
                break;
            case "no":
                $status = 'Rejected';
                $status_tip = 'Rejected by Administrator';
                $status_class = 'fa fa-lg fa-ban text-danger';
                $text_class = "text-danger";
                break;

        }

        if(intval($j['updated']) == 1) {
            $status = 'Pending';
            $status_tip = 'Pending approval by Administrator';
            $status_class = 'fa fa-lg fa-hourglass-half text-warning';
            $text_class = "text-warning";
        }


        $job_status = intval($j['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>';

        ?>
        <div class="box">
            <fieldset class="box-body form-horizontal">
                <legend>Approval Status</legend>
                <div class="alert">
                    <p><span class="text-bold <?=$text_class;?>" data-toggle="tooltip" title="<?=$status_tip;?>" data-placement="bottom" data-container="body"><i class="<?=$status_class;?>"></i> <?=$status_tip;?></span></p>
                </div>

                <legend>Activation Status</legend>

                <div class="alert job-footer">
                    <p><?=$job_status_text;?></p>
                </div>

            </fieldset>
        </div>
        <?php

    }

    ?>
    <form action="account/jobs/job.html" method="post" id="job-form" class="form-horizontal" role="form">
        <div class="box box-default box-solid">
            <div class="box-header with-border">
                <?php
                if ($job_jobs_id > 0) {
                    echo '<b><i class="fa fa-pencil"></i> Edit Job ID# ' . $job_jobs_id . '</b>';
                }
                else {
                    echo '<b><i class="fa fa-plus"></i> Add New Job</b>';
                }
                ?>
            </div>

            <fieldset class="box-body">
                <?php

                ?>

                <?php
                if($job_jobs_id == 0) {
                    ?>
                    <legend>Info</legend>
                    <div class="alert alert-info">
                        <p><i class="fa fa-info-circle"></i> <b>Attention!</b> Your Job Listing will not be displayed online for a few days until the Administrator of the site will approve and activate it.</p>
                    </div>
                    <?php
                }
                ?>

                <legend>Contact Details</legend>

                <div class="form-group">
                    <label class="control-label col-sm-3">Company name <span class="req">*</span></label>
                    <div class="col-sm-7">
                        <input name="CompanyName" type="text" class="form-control input-sm" id="CompanyName" value="<?php echo $j["CompanyName"]; ?>" maxlength="100" required="required">
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label col-sm-3">Address <span class="req">*</span></label>
                    <div class="col-sm-7">
                        <textarea name="Address" rows="2" wrap="soft" class="form-control" id="Address" required="required"><?php echo $j["Address"]; ?></textarea>
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-3">Town: <span class="req">*</span></label>
                    <div class="col-sm-7">
                            <?php
                            $querydistricts = "SELECT * FROM  districts WHERE active='yes' ORDER BY order_seq ASC";
                            $resultdistricts = mysql_query($querydistricts);
                            if (mysql_num_rows($resultdistricts) <> 0) {
                                ?>
                                <select class="form-control input-sm" name="Town" id="Town" required="required">
                                    <option value="" selected>Select</option>
                                    <?php
                                    $TownFound = 0;
                                    while ($linedistricts = mysql_fetch_assoc($resultdistricts)) {
                                        ?>
                                        <option value="<?php echo $linedistricts["title_en"]; ?>"<?php if ($linedistricts["title_en"] == $j["Town"]) {
                                            echo ' selected="selected"';
                                            $TownFound++;
                                        } ?>>
                                            <?php print $linedistricts["title_en"]; ?>
                                        </option>
                                        <?php
                                    }
                                    ?>
                                    <option value="other" <?php if (isset($j["Town"]) && $TownFound < 1) echo ' selected="selected"'; ?>>
                                        Other
                                    </option>
                                </select>
                                <?php
                            } else {
                                ?>
                                No districts added yet.
                                <?php
                            }
                            ?>
                            <input class="form-control input-sm" name="OtherTown" type="text" id="OtherTown" <?php echo (isset($j["Town"]) && $j['Town'] != "other") ? 'disabled="disabled"' : '';?> value="<?php if (isset($j["Town"]) && $TownFound < 1) { echo $j["OtherTown"]; } ?>" placeholder="Other Town" maxlength="50">

                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-3">Phone <span class="req">*</span></label>
                    <div class="col-sm-7">
                        <input name="Phone" type="text" class="form-control input-sm" value="<?php echo $j["Phone"]; ?>" maxlength="15" required="required">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-3">Mobile</label>
                    <div class="col-sm-7">
                        <input name="Mobile" type="text" class="form-control input-sm" value="<?php echo $j["Mobile"]; ?>" maxlength="15">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-3">Fax</label>
                    <div class="col-sm-7">
                        <input name="Fax" type="text" class="form-control input-sm" id="Fax" value="<?php echo $j["Fax"]; ?>" maxlength="15">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-3">Contact person <span class="req">*</span></label>
                    <div class="col-sm-7">
                        <input name="ContactPerson" type="text" class="form-control input-sm" id="ContactPerson" value="<?php echo $j["ContactPerson"]; ?>" maxlength="100" required="required">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-3">E-mail address</label>
                    <div class="col-sm-7">
                        <input name="EmailAdd" type="text" class="form-control input-sm"  id="EmailAdd" value="<?php echo $j["EmailAdd"]; ?>" maxlength="100">
                    </div>
                </div>

                <legend>Position Details</legend>

                <?php
                for($i=0; $i<3; $i++) {
                    $cat_id = isset($j['categs'][$i]) ? intval($j['categs'][$i]) : null;

                    ?>
                    <div class="form-group">
                        <label class="control-label col-sm-3">Job category <?=($i+1);?> <?php echo ($i==0) ? '<span class="req">*</span>' : ''; ?></label>
                        <div class="col-sm-7">
                            <select name="job_cat_id[]" class="form-control input-sm" id="job_cat_id_<?=$i;?>" required="required">
                                <option value="" <?php echo ($cat_id == null) ? 'selected="selected"' : '';?>>CHOOSE CATEGORY</option>
                                <?php
                                foreach($categories as $job_cat_id=>$c_title) {
                                    $selected = "";
                                    if($job_cat_id == $cat_id) {
                                        $selected = 'selected="selected"';
                                    }
                                    ?>
                                    <option value="<?=$job_cat_id;?>" <?=$selected;?>><?php echo $c_title; ?></option>
                                    <?php
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <?php
                }
                ?>

                <div class="form-group">
                    <label class="control-label col-sm-3">Job title <span class="req">*</span></label>
                    <div class="col-sm-7">
                        <input name="JobTitle" type="text" class="form-control input-sm" id="JobTitle" value="<?php echo $j["JobTitle"]; ?>" maxlength="100" required="required">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-3">Number of persons required</label>
                    <div class="col-sm-3">
                        <input name="NumberOfPersons" type="text" class="form-control input-sm" id="NumberOfPersons" value="<?php echo $j["NumberOfPersons"]; ?>" maxlength="3">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-3">Salary</label>
                    <div class="col-sm-7">
                        <div class="row">
                            <div class="col-sm-5">
                                <div class="input-group">
                                    <span class="input-group-addon">&euro;</span>
                                    <input name="salary_min" type="number" class="form-control input-sm" id="salary_min" value="<?php echo intval($j["salary_min"]) > 0 ? intval($j["salary_min"]) : ''; ?>" max="9999999" placeholder="Base Salary">
                                </div>
                            </div>
                            <div class="col-sm-5">
                                <div class="input-group">
                                    <span class="input-group-addon">&euro;</span>
                                    <input name="salary_max" type="number" class="form-control input-sm" id="salary_max" value="<?php echo intval($j["salary_max"]) > 0 ? intval($j["salary_max"]) : ''; ?>" max="9999999" placeholder="Max Salary">
                                </div>
                            </div>
                        </div>

                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-xs-10">Job description <span class="req">*</span></label>
                    <div class="col-xs-10">
                        <textarea name="JobDescription" rows="16" wrap="soft" readonly class="wysihtml5 form-control input-sm" style="width: 100%;" id="JobDescription" required="required"><?php echo ($j['jversion'] == 0) ? nl2br($j["JobDescription"]) : $j["JobDescription"]; ?></textarea>
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label col-xs-10">Qualifications required</label>
                    <div class="col-xs-10">
                        <textarea name="Qualifications" rows="10" wrap="soft" readonly class="wysihtml5 form-control input-sm" style="width: 100%;" id="Qualifications"><?php echo $j["Qualifications"]; ?></textarea>
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-xs-10">Other requirements</label>
                    <div class="col-xs-10">
                        <textarea name="Other" rows="10" wrap="soft" readonly class="wysihtml5 form-control input-sm" style="width: 100%;" id="Other"><?php echo $j["Other"]; ?></textarea>
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-3">Location(s) <span class="req">*</span></label>
                    <div class="col-sm-7 checkbox">
                        <?php
                        $querydistricts = "SELECT * FROM districts WHERE active='yes' ORDER BY order_seq ASC";
                        $resultdistricts = mysql_query($querydistricts);
                        if ($resultdistricts && mysql_num_rows($resultdistricts) > 0) {
                            while ($linedistricts = mysql_fetch_assoc($resultdistricts)) {
                                $checked = '';
                                if (strstr($j["Location"], $linedistricts["title_en"])) {
                                    $checked = 'checked="checked"';
                                }
                                ?>
                                <label>
                                <input type="checkbox" name="Location[]" value="<?php echo $linedistricts["title_en"]; ?>" <?=$checked?>>
                                <?php echo $linedistricts["title_en"]; ?>
                                </label>
                                <?php
                            }
                        } else {
                            ?>
                            No districts added yet.
                            <?php
                        }
                        ?>
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-3">Employment conditions</label>
                    <div class="col-sm-7">
                        <select name="Conditions" class="form-control input-sm">
                            <option
                                value="full-time" <?php if ($j["Conditions"] == "full-time") {
                                echo " selected";
                            } ?>>Full Time
                            </option>
                            <option
                                value="part-time" <?php if ($j["Conditions"] == "part-time") {
                                echo " selected";
                            } ?>>Part Time
                            </option>
                            <option
                                value="contract" <?php if ($j["Conditions"] == "contract") {
                                echo " selected";
                            } ?>>Contract
                            </option>
                            <option
                                value="temp" <?php if ($j["Conditions"] == "temp") {
                                echo " selected";
                            } ?>>Temporary
                            </option>
                            <option
                                value="internship" <?php if ($j["Conditions"] == "internship") {
                                echo " selected";
                            } ?>>Internship
                            </option>
                            <option
                                value="other" <?php if ($j["Conditions"] == "other") {
                                echo " selected";
                            } ?>>Other
                            </option>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-3">Operation</label>
                    <div class="col-sm-7 radio">
                        <input name="jversion" type="hidden" value="1">
                        <input name="module_page" type="hidden" id="module_page" value="jobsresumes">
                        <input type="hidden" name="jobs_url" value="jobs_add">
                        <input name="job_jobs_id" type="hidden" id="job_jobs_id" value="<?php echo $job_jobs_id; ?>">
                        <?php
                        if ($job_jobs_id > 0) {
                            ?>
                            <label class="radio"><input type="radio" name="chcAction" id="radio-edit" value="Edit" checked> <i class="fa fa-pencil"></i> Edit</label>
                            <label class="radio"><input type="radio" name="chcAction" value="Add"> <i class="fa fa-plus"></i> Save As New Job</label>
                            <label class="radio"><input type="radio" name="chcAction" id="radio-delete" value="Delete"> <i class="fa fa-times text-danger"></i> Delete</label>

                            <?php
                        } else {
                            ?>
                            <label class="radio"><input type="radio" name="chcAction" value="Add" checked> <i class="fa fa-plus"></i> Add New Job</label>
                            <?php
                        }
                        ?>
                    </div>
                </div>

            </fieldset>

            <div class="box-footer text-center">
                <button type="button" name="Submit2" id="form-submit" class="btn btn-primary btn-sm">Submit</button>
            </div>
        </div>
    </form>
    <?php
}

?>
<script type="text/javascript">
    var full_uri = '<?=$full_uri;?>';
</script>
