<?php
/*================================================================+\
|| # PHPRetro - An extendable virtual hotel site and management
|+==================================================================
|| # Copyright (C) 2009 Yifan Lu. All rights reserved.
|| # http://www.yifanlu.com
|| # Parts Copyright (C) 2009 Meth0d. All rights reserved.
|| # http://www.meth0d.org
|| # All images, scripts, and layouts
|| # Copyright (C) 2009 Sulake Ltd. All rights reserved.
|+==================================================================
|| # PHPRetro is provided "as is" and comes without
|| # warrenty of any kind. PHPRetro is free software!
|| # License: GNU Public License 3.0
|| # http://opensource.org/licenses/gpl-license.php
\+================================================================*/

$page['allow_guests'] = true;
require_once('./includes/core.php');
require_once('./includes/session.php');
$lang->addLocale("credits.credits");

$page['id'] = "jobs_create";
$page['name'] = 'Create Business';
$page['bodyid'] = "home";
$page['cat'] = "bussiness";

require_once('./templates/community_header.php');
?>

<style type="text/css">
input[type="text"], input[type="password"] {
  background-color: #F1F1F1;
  border: 1px solid #999999;
  width: 175px;
  padding: 5px;
  font-family: verdana;
  font-size: 10px;
  color: #666666;
}
input[type="submit"] {
  background-color: #F1F1F1;
  border: 1px solid #999999;
  padding: 5px;
  font-family: verdana;
  font-size: 10px;
  color: #666666;
}
select {
  background-color: #F1F1F1;
  border: 1px solid #999999;
  padding: 5px;
  font-family: verdana;
  font-size: 10px;
  color: #666666;
}
</style>

<?php
if($user->name == "Guest"){
  header("Location: ".PATH);
}

$RankID = $input->FilterText($_GET['id']);
$getCorpID = mysql_query("SELECT corp_id FROM jobs_ranks WHERE id = '".$RankID."';");
$CorpID = mysql_fetch_assoc($getCorpID);
$checkUserQuery = mysql_query("SELECT * FROM jobs_corps WHERE owner = '".$user->id."' && id = '".$CorpID['corp_id']."';");
if(mysql_num_rows($checkUserQuery) == 0){
  header("Location: ".PATH."/jobs_create");
}

if(isset($_POST['rank_submit']))
{
  $rank_motto = strip_tags($input->FilterText($_POST['rank_motto']));
  $rank_payout = strip_tags($input->FilterText($_POST['rank_payout']));
  $rank_spots = strip_tags($input->FilterText($_POST['rank_spots']));
  $rank_status = strip_tags($input->FilterText($_POST['rank_status']));
  $rank_global = strip_tags($input->FilterText($_POST['rank_global']));
  if($rank_motto == NULL || $rank_payout == NULL || $rank_spots  == NULL || $rank_spots == NULL || $rank_status == NULL || $rank_global == NULL){
    $error_message = '<font color="red"><strong>You have left a field blank.</strong></font><br /><br />';
  }else{
    if(!is_numeric($rank_payout) || !is_numeric($rank_spots) || !is_numeric($rank_status) || !is_numeric($rank_global)){
      $error_message = '<font color="red"><strong>Please use numbers in the appropriate fields.</strong></font><br /><br />';
    }else{
      if($rank_payout < 0 || $rank_spots < 0 || $rank_status < 0 || $rank_global < 0){
        $error_message = '<font color="red"><strong>Numbers can not below the value of 0.</strong></font><br /><br />';
      }else{
        if($rank_payout > 100){
          $error_message = '<font color="red"><strong>You cannot make the payout more than 100 credits.</strong></font><br /><br />';
        }else{
          if($rank_spots > 500){
            $error_message = '<font color="red"><strong>You cannot open more than 500 spots.</strong></font><br /><br />';
          }else{
            if($rank_status > 2 || $rank_global > 1){
              $error_message = '<font color="red"><strong>What exactly are you trying to do?</strong></font><br /><br />';
            }else{
              mysql_query("UPDATE jobs_ranks SET motto = '".$rank_motto ."', manager_rank = '".$rank_status."', pay = '".$rank_payout."', open_spots = '".$rank_spots."', global = '".$rank_global."' WHERE id = '".$RankID."';");
              $error_message = '<font color="green"><strong>You have successfully edited this rank.</strong></font><br /><br />';
            }
          }
        }
      }
    }
  }
}

$getRankInfoQuery = mysql_query("SELECT * FROM jobs_ranks WHERE id = '".$RankID."';");
$rankInfo = mysql_fetch_array($getRankInfoQuery);

if(isset($_POST['rank_delete'])){
  $getHighestRank = mysql_query("SELECT * FROM jobs_ranks WHERE corp_id = '".$CorpID['corp_id']."'");
  if($rankInfo['job_rank'] != mysql_num_rows($getHighestRank)){
    $error_message = '<font color="red"><strong>To delete this rank, you must delete all ranks above it.</strong></font><br /><br />';
  }else{
    mysql_query("UPDATE users SET corp_id = '0', job_rank = '0', secure_id = '0' WHERE secure_id = '".$rankInfo['id']."'");
    mysql_query("DELETE FROM jobs_rooms WHERE rank_id = '".$rankInfo['id']."'");
    mysql_query("DELETE FROM jobs_ranks WHERE id = '".$rankInfo['id']."'");
    header("Location: ".PATH."/jobs_edit?id=".$CorpID['corp_id']."");
  }
}

if(isset($_POST['hire_submit'])){
  $hire_user = strip_tags($input->FilterText($_POST['hire_user']));
  $getUsersInfo = mysql_query("SELECT * FROM users WHERE name = '".$hire_user."'");
  if(mysql_num_rows($getUsersInfo) == 0){
    $error_message_one = '<font color="red"><strong>'.$hire_user.' does not exsist!</strong></font><br /><br />';
  }else{
    $UsersInfo = mysql_fetch_array($getUsersInfo);
    if($UsersInfo['corp_id'] != 0 || $UsersInfo['job_rank'] != 0 || $UsersInfo['secure_id'] != 0){
      $error_message_one = '<font color="red"><strong>'.$UsersInfo['name'].' already has a job.</strong></font><br /><br />';
    }else{
      $error_message_one = '<font color="green"><strong>'.$UsersInfo['name'].' has recieved the job.</strong></font><br /><br />';
      mysql_query("UPDATE users SET corp_id = '".$rankInfo['corp_id']."', job_rank = '".$rankInfo['job_rank']."', secure_id = '".$rankInfo['id']."' WHERE name = '".$UsersInfo['name']."'");
    }
  }
}

$getUserSex = mysql_query("SELECT sex FROM users WHERE id = '".$user->id."';");
$userSex = mysql_fetch_assoc($getUserSex);
if(isset($_POST['rank_uniform'])){
  $getFigureData = mysql_query("SELECT figure FROM users WHERE id = '".$user->id."';");
  $FigureData = mysql_fetch_assoc($getFigureData);
  mysql_query("UPDATE jobs_ranks SET figure_data_".$userSex['sex']." = '".$FigureData['figure']."'WHERE id = '".$RankID."';");
  $error_message_two = '<font color="green"><strong>You have set this ranks uniform to what you are currently wearing.</strong></font><br /><br />';
}

if(isset($_POST['rooms_submit'])){
  $rooms_room = strip_tags($input->FilterText($_POST['rooms_room']));
  $getRooms = mysql_query("SELECT * FROM rooms WHERE id = '".$rooms_room."';");
  if(!is_numeric($rooms_room)){
    $error_message_three = '<font color="red"><strong>The workroom must be a room ID.</strong></font><br /><br />';
  }else{
    if(mysql_num_rows($getRooms) == 0){
      $error_message_three = '<font color="red"><strong>Sorry, but that room does not exsist.</strong></font><br /><br />';
    }else{
      $getRoomCheck = mysql_query("SELECT * FROM rooms WHERE owner = '".$user->name."' && id = '".$rooms_room."'");
      if(mysql_num_rows($getRoomCheck) < 1){
        $error_message_three = '<font color="red"><strong>Sorry, but you do not own that room.</strong></font><br /><br />';
      }else{
        mysql_query("INSERT into jobs_rooms (corp_id, rank_id, room_id) VALUES ('".$rankInfo['corp_id']."', '".$rankInfo['id']."', '".$rooms_room."');");
        $error_message_three = '<font color="green"><strong>You have successfully made room '.$rooms_room.' a workroom.</strong></font><br /><br />';
      }
    }
  }
}
?>
<div id="container">
	<div id="content" style="position: relative" class="clearfix">
    <div id="column1" class="column">

				<div class="habblet-container ">
						<div class="cbb clearfix blue ">

							<h2 class="title"><?php echo $rankInfo['motto']; ?>
							</h2>
<p class="credits-countries-select">
<center>
<?php echo $error_message; ?>
<table width="180px">
<tr>
<td>
<form name="post" action="" method="post">
Rank Motto:<br />
<input type="text" name="rank_motto" value="<?php if(isset($_POST['rank_submit'])){echo $rank_motto;}else{echo $rankInfo['motto'];} ?>" /><br />
<br />
Payout:<br />
<input type="text" name="rank_payout" value="<?php if(isset($_POST['rank_submit'])){echo $rank_payout;}else{echo $rankInfo['pay'];} ?>" /><br />
<br />
Open Positions:<br />
<input type="text" name="rank_spots" value="<?php if(isset($_POST['rank_submit'])){echo $rank_spots;}else{echo $rankInfo['open_spots'];} ?>" /><br />
<br />
Rank Status:<br />
<select name="rank_status">
  <option value="0">Normal</option>
  <option value="1"<?php if(isset($_POST['rank_submit'])){if($rank_status == 1){echo ' selected="selected"';}}else{if($rankInfo['manager_rank'] == 1){echo ' selected="selected"';}} ?>>Supervisor</option>
  <option value="2"<?php if(isset($_POST['rank_submit'])){if($rank_status == 2){echo ' selected="selected"';}}else{if($rankInfo['manager_rank'] == 2){echo ' selected="selected"';}} ?>>Manager</option>
</select><br />
<br />
Global Status:<br />
<select name="rank_global">
  <option value="0">No</option>
  <option value="1"<?php if(isset($_POST['rank_submit'])){if($rank_global == 1){echo ' selected="selected"';}}else{if($rankInfo['global'] == 1){echo ' selected="selected"';}} ?>>Yes</option>
</select><br />
<br />
<input type="submit" name="rank_submit" value="Edit Rank" /> <input type="submit" name="rank_delete" value="Delete Rank" /><br /><br />
<center>[<a href="<?php echo PATH.'/jobs_edit?id='.$CorpID['corp_id']; ?>">Go Back</a>]</center>
</form>
</td>
</tr>
</table>
</center>
</p>
					</div>
											<div class="cbb clearfix blue ">
							<h2 class="title">Uniform
							</h2>
<p class="credits-countries-select">
<center>
<?php
echo $error_message_two;
echo '<img src="http://www.habbo.co.uk/habbo-imaging/avatarimage?figure='.$rankInfo['figure_data_M'].'&size=b&direction=2&head_direction=3&gesture=sml&size=m"> <img src="http://www.habbo.co.uk/habbo-imaging/avatarimage?figure='.$rankInfo['figure_data_F'].'&size=b&direction=2&head_direction=3&gesture=sml&size=m">';
?>
<br /><br />
<form action="" method="post">
<input type="submit" name="rank_uniform" value="Set Uniform (<?php echo $userSex['sex']; ?>)" /></center>
</form>
</center>
</p>

					</div>
							<div class="cbb clearfix blue ">
							<h2 class="title">Workrooms
							</h2>
<p class="credits-countries-select">
<center>
<?php echo $error_message_three; ?>
<table width="180px" border="0">
<tr>
<td>
<strong>Workrooms:</strong><br />
<?php
$getRoomsQuery = mysql_query("SELECT * FROM jobs_rooms WHERE rank_id = '".$rankInfo['id']."';");
while($Rooms = mysql_fetch_array($getRoomsQuery)) {
  echo $Rooms['room_id'].', ';
}
?>
<br /><br />
<form action="" method="post">
Workroom:
<input type="text" name="rooms_room" value="<?php echo $rooms_room; ?>" /><br /><br />
<input type="submit" name="rooms_submit" value="Create Workroom" /></center>
</form>
</td>
</tr>
</table>
</center>
</p>

					</div>
						<div class="cbb clearfix blue ">
							<h2 class="title">Hire User
							</h2>
<p class="credits-countries-select">
<center>
<?php
$ranks_num = mysql_query("SELECT * FROM jobs_ranks WHERE corp_id = '".$CorpID."';");
$new_rank = mysql_num_rows($ranks_num) + 1;
echo $error_message_one;
?>

<table width="180px" border="0">
<tr>
<td>
<form action="" method="post">
User:<br />
<input type="text" name="hire_user" value="<?php echo $hire_user; ?>" /><br /><br />
<center><input type="submit" name="hire_submit" value="Hire User" /></center>
</form>
</td>
</tr>
</table>
</center>
</p>

					</div>
<?php
$getUsers = mysql_query("SELECT * FROM users WHERE secure_id = '".$rankInfo['id']."'");
?>
						<div class="cbb clearfix blue ">
							<h2 class="title">Employees (<?php echo mysql_num_rows($getUsers); ?>)
							</h2>
<p class="credits-countries-select">
<?php
while($Users = mysql_fetch_array($getUsers)) {
  echo '<img src="http://www.habbo.co.uk/habbo-imaging/avatarimage?figure='.$Users['figure'].'&size=s&direction=2&head_direction=3&gesture=sml&size=m" align="left">
        <strong><a href="#">'.$Users['name'].'</a></strong><br />'.$Users['mission'].'<br /><br />Last visit: '.$Users['lastvisit'].'<br /><a href="#">Promote</a> | <a href="#">Demote</a> | <a href="#">Fire</a><br /><br />';
}
?>
</p>

					</div>
				</div>
</div>
<div id="column2" class="column">
				<div class="habblet-container ">
						<div class="cbb clearfix blue ">

							<h2 class="title">My Business
							</h2>
						<div id="purse-habblet">

<div class="box-content">
<img src="http://www.habbosolutions.org/resources/images/habbocharacters/frank_15.gif" align="right">

<?php
$getinfo = mysql_query("SELECT * FROM users WHERE name = '".$user->name."'");
$info = mysql_fetch_array($getinfo);
$getevenmoreinfo = mysql_query("SELECT * FROM jobs_corps WHERE owner = '".$info['id']."'");
$corpcount = mysql_num_rows($getevenmoreinfo);
if($corpcount > 0) {
      while($s = mysql_fetch_array($getevenmoreinfo)) {
            echo '<a href="jobs_edit?id='.$s['id'].'">'.$s['corp_name'].'</a><br />';
      }
}else{
      echo 'Sorry, but you do not have any corps.<br />';
}
?>
<br />
<a href="<?php echo PATH; ?>/jobs_create">Create Business</a>
</div>
</div>


					</div>
				</div
</div>

<?php require_once('./templates/community_footer.php'); ?>