<?php

require_once('conf/dbc.php');
require_once('conf/session_start.php');
require_once('conf/ban.php');
if ((!isset($_SESSION['id'])) or (!isset($_SESSION['nick'])))  {
  ?>
  <script type="text/javascript">
  document.location.href = "reg.php?err_login=1";
  </script>
  <?php
  exit();
}
$user_id = abs(intval($_SESSION['id']));
$query = "Select clan, clan_rang from users where id = '$user_id'";
$result = mysqli_query($dbc, $query) or die ('Ошибка передачи запроса к БД');
$row = mysqli_fetch_array($result);
$clan = $row['clan'];
$clan_rang = $row['clan_rang'];
if($clan_rang == 9) {
 header ('Location: settings.php?error=8');
  exit();
}
///Если клана не существует
if ($clan == 0) {
?>
  <script type="text/javascript">
  document.location.href = "index.php";
  </script>
  <?php
}
else {
    $query = "select people from clans where clan_id = '$clan'";
    $result = mysqli_query($dbc, $query) or die ('Ошибка передачи запроса к БД');
    $row=mysqli_fetch_array($result);
	$count_people = $row['people'];
  if ($clan_rang == 9){///Если лидер и пользователей больше 1 то ошибка.
    if ($count_people > 1) {
    ?>
     <script type="text/javascript">
     document.location.href = "company.php?company_id=<?php echo "$clan";?>&err=out";
     </script>
    <?php
	exit();
	}
  }
  $people = ($count_people - 1);
$user=$dbc->query("SELECT * FROM`users` WHERE`id`='$set_id' LIMIT 1")->fetch_assoc();
$time=time();
$pdo->exec("INSERT INTO `clan_log` SET `user_id` = '$user_id', `user` = '".$user['nick']."', `tip`  = '6', `time` = '$time',`clan_id` = '$clan'");
  $query = "update clans set people = '$people' where clan_id = '$clan'";
$clan = $pdo->query("SELECT * FROM clans WHERE clan_id='".$user['clan']."'")->fetch();
$mhp = ($clan['hp'] * 250);
$mbron = ($clan['bron'] * 200);
  $result = mysqli_query($dbc, $query) or die ('Ошибка передачи запроса к БД');
  $query = "update users set clan = 0, clan_rang = 0, ko=0, mentor_time = NOW() - (60*60*3) where id = '$user_id'";
  $result = mysqli_query($dbc, $query) or die ('Ошибка передачи запроса к БД');
  $query = "DELETE FROM  in_clan WHERE id_in = '$user_id'";
  $result = mysqli_query($dbc, $query) or die ('Ошибка передачи запроса к БД');
} 
///////////////////////////////

mysqli_close($dbc);
?>
<script type="text/javascript">
document.location.href = "profile.php?id=<?php echo "$user_id";?>";
</script>