<?php
  ///////////////////////////////////////////////////
  //2010 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////
  require_once "../utils/chlang.php";

  $title = $TEXT['menu-btn27-title'];

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

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

  $button = $TEXT['btn_filter'];
  $action = "statistic.php";

  if(empty($_POST['rows'])) {$_POST['rows'] = "100";}

  if(strlen($_POST['user_id'])>15) {puterror($result2['skin'],$TEXT['error-login14']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['obr_id'])>5) {puterror($result2['skin'],$TEXT['error-obr6']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['zad_ot'])>8) {puterror($result2['skin'],$TEXT['book-zad-ot-err']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['zad_do'])>8) {puterror($result2['skin'],$TEXT['book-zad-do-err']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['rows'])>12) {puterror($result2['skin'],$TEXT['error-log10']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_GET['book_id'])>12) {puterror($result2['skin'],$TEXT['error-book6']); require_once "../utils/bottom_adm_film.php"; exit();}

  $_POST['bname'] = rep($_POST['bname']);
  $_POST['user_id'] = intval($_POST['user_id']);
  $_POST['zad_ot'] = intval($_POST['zad_ot']);
  $_POST['zad_do'] = intval($_POST['zad_do']);
  $_POST['obr_id'] = intval($_POST['obr_id']);
  $_POST['rows'] = intval($_POST['rows']);

  $query2 = mysqli_query($dbcnx,"SELECT COUNT(stat_id) AS c_stat_id FROM tbl_statistik");
    $coun = mysqli_fetch_array($query2);

  if($_POST['login_ch'] == "on")
   {
    $login_id="AND tbl_statistik.user_id = '".$_POST['user_id']."' ";
    $login_ch="checked";
   }
   else
   {
    $login_id="";
    $login_ch="";
   }

  if($_POST['obr_ch'] == "on")
   {
    $obr_id="AND tbl_statistik.obr_id = '".$_POST['obr_id']."' ";
    $obr_ch="checked";
   }
   else
   {
    $obr_id="";
    $obr_ch="";
   }

?>
<form enctype='multipart/form-data' action=<?php echo $action; ?> method=post >
<table align="center" border="0" cellpadding="0" cellspacing="0" width="90%">
<tr><td>
<fieldset>
<legend align="left" title="<?php echo $TEXT['log-tit7']; ?>"><?php echo $TEXT['btn_filter']; ?></legend>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr class=button2>
 <td><?php echo $TEXT['book-name']; ?></td>
 <td><?php echo $TEXT['login-user']; ?><input type="checkbox" name="login_ch" <?php echo $login_ch; ?>/></td>
 <td><?php echo $TEXT['book-zad-ot']; ?></td>
 <td><?php echo $TEXT['book-zad-do']; ?></td>
 <td><?php echo $TEXT['tbl-obr']; ?><input type="checkbox" name="obr_ch" <?php echo $obr_ch; ?>/></td>
 <td><?php echo $TEXT['size-count']; ?></td>
</tr>
<tr>
 <td><input size=20 maxlength=255 type=text name=bname title='<?php echo $TEXT['book-name-tit']; ?>' value='<?php echo $_POST['bname']; ?>'></td>
 <td>
  <select name="user_id">
    <?php
        $query11 = "SELECT login_id,  name, familiya, otchestvo FROM `tbl_logins` ORDER BY familiya ";
          $result11 = mysqli_query($dbcnx,$query11);
            if(!$result11) {puterror2($result2['skin'],$TEXT['error-tbl7']); require_once "utils/bottom_adm_film.php"; exit();}
              while($result_login = mysqli_fetch_array($result11))
                {
                 if($result_login['login_id'] == $_POST['user_id']) {$chk_login = "<option selected value=".$result_login['login_id'].">".$result_login['familiya']."&nbsp;".$result_login['name']."&nbsp;".$result_login['otchestvo']."";}
                   else {$chk_login = "<option value=".$result_login['login_id'].">".$result_login['familiya']."&nbsp;".$result_login['name']."&nbsp;".$result_login['otchestvo']."";}
                 echo $chk_login;
                }
    ?>
    </select>
 </td>
 <td><input size=8 maxlength=8 type=text name=zad_ot title='<?php echo $TEXT['book-zad-ot-tit']; ?>' value='<?php echo $_POST['zad_ot']; ?>'></td>
 <td><input size=8 maxlength=8 type=text name=zad_do title='<?php echo $TEXT['book-zad-do-tit']; ?>' value='<?php echo $_POST['zad_do']; ?>'></td>
 <td>
   <select name="obr_id">
    <?php
        $query10 = "SELECT * FROM `tbl_obraschenie` ORDER BY kachestvo ";
          $result10 = mysqli_query($dbcnx,$query10);
            if(!$result10) {puterror2($result2['skin'],$TEXT['error-tbl18']); require_once "utils/bottom_adm_film.php"; exit();}
              while($result_obr = mysqli_fetch_array($result10))
               {
                if($result_obr['obr_id'] == $_POST['obr_id']) {$chk_obr = "<option selected value=".$result_obr['obr_id'].">".$result_obr['kachestvo']."";}
                 else {$chk_obr = "<option value=".$result_obr['obr_id'].">".$result_obr['kachestvo']."";}
                 echo $chk_obr;
                }
    ?>
    </select>
 </td>
 <td><input size=11 maxlength=11 type=text name=rows title='<?php echo $TEXT['log-tit12']; ?>' value='<?php echo $_POST['rows']; ?>'></td>
 <td><input class="button2" type=submit value='<?php echo $button; ?>'></td>
</tr>
</table>
</fieldset>
</td></tr>
</table>
</form>
<table align="center" border="0" cellpadding="0" cellspacing="0" width="95%">
<tr>
  <td align="right" width="18"><a href="statistic_sm.php" title="<?php echo $TEXT['book-stat-sm-tit']; ?>" ><img alt="<?php echo $TEXT['book-stat-sm']; ?>" src='../skins/<?php echo $result2['skin']; ?>/minus.png' /></a></td>
  <td class="informssilka"><?php echo $TEXT['book-stat-sm']; ?></td>
  <td align="right" class="bold_it"><?php echo $TEXT['log-tit3']; ?><?php echo $coun['c_stat_id']; ?></td>
</tr>
</table>
<table align="center" class="table" border="1" cellpadding="6" cellspacing="0" width="95%">
 <thead>
  <tr class='tableheadercat'>
   <td width="10%"><?php echo $TEXT['book-name']; ?></td>
   <td><?php echo $TEXT['login-user']; ?></td>
   <td><?php echo $TEXT['book-data3']; ?></td>
   <td><?php echo $TEXT['book-data4']; ?></td>
   <td><?php echo $TEXT['book-zad']; ?></td>
   <td><?php echo $TEXT['tbl-obr']; ?></td>
  </tr>
 </thead>
 <tbody id="tab">
<?php
   if(!empty($_POST['bname'])) {$bname = "AND LOWER(tbl_book.book_name) LIKE LOWER('%".$_POST['bname']."%') ";}
    else {$bname = "";}

   if(!empty($_GET['book_id'])) {$book_id = "AND tbl_statistik.book_id = ".$_GET['book_id']." ";}
    else {$book_id = "";}

   if($_POST['zad_ot'] !== 0 && $_POST['zad_do'] !== 0) {$zad = " AND tbl_statistik.zad_days BETWEEN ".$_POST['zad_ot']." AND ".$_POST['zad_do']." ";}
    elseif($_POST['zad_ot'] !== 0 && $_POST['zad_do'] == 0) {$zad = " AND tbl_statistik.zad_days > ".$_POST['zad_ot']." ";}
     elseif($_POST['zad_ot'] == 0 && $_POST['zad_do'] !== 0) {$zad = " AND tbl_statistik.zad_days < ".$_POST['zad_do']." ";}
    else {$zad = "";}

     $query = "SELECT 
     tbl_statistik.*,
     tbl_statistik.book_id AS stat_book_id,
     UNIX_TIMESTAMP(tbl_statistik.data_vidachi) AS data_vidachi,
     UNIX_TIMESTAMP(tbl_statistik.data_sdachi) AS data_sdachi,
     tbl_book.book_id,
     tbl_book.book_name,
     tbl_logins.login_id,
     tbl_logins.familiya,
     tbl_logins.name,
     tbl_logins.otchestvo,
     tbl_obraschenie.*
     FROM tbl_statistik
     JOIN tbl_book ON tbl_statistik.book_id=tbl_book.book_id
     JOIN tbl_logins ON tbl_statistik.user_id=tbl_logins.login_id
     JOIN tbl_obraschenie ON tbl_statistik.obr_id=tbl_obraschenie.obr_id
     WHERE tbl_statistik.stat_id != 0 ".$book_id." ".$bname." ".$obr_id." ".$login_id." ".$zad."
     ORDER BY tbl_statistik.data_vidachi DESC LIMIT ".$_POST['rows']."";
     $result = mysqli_query($dbcnx,$query);
     if(!$result) {puterror($result2['skin'],$TEXT['error-tbl4']); require_once "../utils/bottom_adm_film.php"; exit();}
       $result_rows = mysqli_num_rows($result);
        if($result_rows >0)
         {
          while($result_stat = mysqli_fetch_array($result))
           {
           if($result_stat['zad_days'] == 0) {$zad = $TEXT['book-zad-no'];}
            else {$zad = $result_stat['zad_days']."&nbsp;".$TEXT['book-zad-days'];}

           $month["01"] = $TEXT['month01'];
           $month["02"] = $TEXT['month02'];
           $month["03"] = $TEXT['month03'];
           $month["04"] = $TEXT['month04'];
           $month["05"] = $TEXT['month05'];
           $month["06"] = $TEXT['month06'];
           $month["07"] = $TEXT['month07'];
           $month["08"] = $TEXT['month08'];
           $month["09"] = $TEXT['month09'];
           $month["10"] = $TEXT['month10'];
           $month["11"] = $TEXT['month11'];
           $month["12"] = $TEXT['month12'];

           $date_day = date("d",$result_stat['data_sdachi']);
           $date_month = date("m",$result_stat['data_sdachi']);
           $date_year = date("Y",$result_stat['data_sdachi']);
           $year = $TEXT['year2'];
           if($result_stat['data_sdachi'] == "0") {$data_sd = "---";}
            else {$data_sd = (date("$date_day $month[$date_month] $date_year $year"));}

           $date_day2 = date("d",$result_stat['data_vidachi']);
           $date_month2 = date("m",$result_stat['data_vidachi']);
           $date_year2 = date("Y",$result_stat['data_vidachi']);
           if($result_stat['data_vidachi'] == "0") {$data_vid = "---";}
            else {$data_vid = (date("$date_day2 $month[$date_month2] $date_year2 $year"));}

           ?>
           <tr>
              <td><a href='../info.php?book_id=<?php echo $result_stat['book_id']; ?>' target=_blank title='<?php echo $TEXT['aut-viewtit']; ?>'><?php echo $result_stat['book_name']; ?></a></td>
              <td><?php echo $result_stat['familiya']; ?> <?php echo $result_stat['name']; ?> <?php echo $result_stat['otchestvo']; ?></td>
              <td><?php echo $data_vid; ?></td>
              <td><?php echo $data_sd; ?></td>
              <td><?php echo $zad; ?></td>
              <td><?php echo $result_stat['kachestvo']; ?></td>
           </tr>
           <?php
               }
         }
?>
</tbody>
<tfoot>
 <tr class='tableheadercat'>
 <td colspan=6 align="right"><?php echo $TEXT['size-count2']; ?> = <?php echo $result_rows; ?></td>
 </tr>
</tfoot>
</table>
<?php
  require_once "../utils/bottom_adm_film.php";
?>