<?php
  ///////////////////////////////////////////////////
  //Вернуть книгу
  //2010 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////

 require_once "utils/chlang_osn.php";

  $title = $TEXT['book-nal-tit2'];

  require_once "config_a.php";
  require_once "utils/functions.php";
  require_once "utils/top.php";

  if($result2['administrator'] == 0) {puterror2($result2['skin'],$TEXT['error-loginform2']); require_once "utils/bottom.php"; exit();}

  if(empty($_POST['book_id'])) {puterror2($result2['skin'],$TEXT['error-book5']); require_once "utils/bottom.php"; exit();}
  if(strlen($_POST['book_id'])>20) {puterror2($result2['skin'],$TEXT['error-book6']); require_once "utils/bottom.php"; exit();}
  $_POST['book_id'] = rep($_POST['book_id']);
  if(empty($_POST['obr_id'])) {puterror2($result2['skin'],$TEXT['error-obr5']); require_once "utils/bottom.php"; exit();}
  if(strlen($_POST['obr_id'])>5) {puterror2($result2['skin'],$TEXT['error-obr6']); require_once "utils/bottom.php"; exit();}
  $_POST['obr_id'] = rep($_POST['obr_id']);

  $query_iskh = "SELECT
book_name,
data_vidachi,
data_sdachi,
user_id,
DATEDIFF(CURRENT_DATE(),data_sdachi) AS zad_date
FROM tbl_book
WHERE book_id = '".$_POST['book_id']."' ";
  $result_iskh = mysqli_query($dbcnx,$query_iskh);
  if(!$result_iskh) {puterror2($result2['skin'],$TEXT['error-tbl3']); require_once "utils/bottom.php"; exit();}
      $result_iskh2 = mysqli_fetch_array($result_iskh);

  if($result_iskh2['zad_date'] < 1) {$zad_date = 0;}
   else {$zad_date = $result_iskh2['zad_date'];}

  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-book5'];
  $what = $result_iskh2['book_name'].", ".$TEXT['log-id']." ".$_POST['book_id'];
  $who = $result2['familiya']." ".$result2['name'];
  //*************************************
  $query = "UPDATE `tbl_book` SET `napominalka`='0', `data_vidachi`='1970-01-01', `data_sdachi`='1970-01-01',`user_id`='0', `vidano`='0' WHERE `book_id`='".$_POST['book_id']."' ";
  if(mysqli_query($dbcnx,$query))
  {
      $queryst = "INSERT INTO tbl_statistik ( `stat_id` , `book_id` , `user_id` , `data_vidachi` , `data_sdachi` , `r_data_sdachi`, `obr_id`, `zad_days` )
                         VALUES ( NULL , '".$_POST['book_id']."' , '".$result_iskh2['user_id']."' , '".$result_iskh2['data_vidachi']."' , '".$result_iskh2['data_sdachi']."' , CURRENT_DATE(), '".$_POST['obr_id']."', ".$zad_date.")";
        if(mysqli_query($dbcnx,$queryst))
           {
             $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
             if(mysqli_query($dbcnx,$query2))
              {
                echo "<html><head>
                <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=info.php?book_id=".$_POST['book_id']."' />
                </head></html>";
              } else {puterror2($result2['skin'],$TEXT['error-log']); require_once "utils/bottom.php"; exit();}
           } else {puterror2($result2['skin'],$TEXT['error-tbl4']); require_once "utils/bottom.php"; exit();}
  } else {puterror2($result2['skin'],$TEXT['error-tbl3']); require_once "utils/bottom.php"; exit();}
  require_once "utils/bottom.php";
?>