<?php
  ///////////////////////////////////////////////////
  //2007 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////

  $lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0,2);
  if(is_file ("../lang/".$lang.".php")) include "../lang/".$lang.".php";
     else include "../lang/ru.php";

  $title=$titlepage=$TEXT['support33'];

  require_once "../config.php";
  require_once "top.php";

  ?>
  <title><?php echo $title; ?></title>
  <link rel="StyleSheet" type="text/css" href="../utils/reader.css" />
  </head>
  <body leftmargin="0" marginheight="0" marginwidth="0" rightmargin="0" bottommargin="0" topmargin="0" >
  <?php

  if($result2['user'] == 1) puterror2($TEXT['error-supp11']);
  if($result2['master'] == 1) puterror2($TEXT['error-supp11']);

  if(empty($_POST['zay_id'])) puterror2($TEXT['error-supp6']);
  if(strlen($_POST['zay_id'])>11) puterror2($TEXT['error-supp7']);
  if(strlen($_POST['org_id'])>11) puterror2($TEXT['error-org']);
  if(strlen($_POST['tema_id'])>11) puterror2($TEXT['error-supp1']);
  if(strlen($_POST['old_tema_id'])>11) puterror2($TEXT['error-supp1']);
  if(strlen($_POST['man_id'])>11) puterror2($TEXT['error-supp13']);
  if(strlen($_POST['old_mast_id'])>11) puterror2($TEXT['error-supp12']);
  if(strlen($_POST['mast_id'])>11) puterror2($TEXT['error-supp12']);
  if(strlen($_POST['description'])>20000) puterror2($TEXT['error-supp3']);
  if(strlen($_POST['master'])>255) puterror2($TEXT['error-supp17']);
  if(strlen($_POST['manager'])>255) puterror2($TEXT['error-supp16']);
  if(strlen($_POST['user'])>255) puterror2($TEXT['error-supp18']);
  if(strlen($_POST['old_sost_id'])>11) puterror2($TEXT['error-supp21']);
  if(strlen($_POST['sost_id'])>11) puterror2($TEXT['error-supp21']);
  if(strlen($_POST['old_sroch_id'])>11) puterror2($TEXT['error-supp20']);
  if(strlen($_POST['sroch_id'])>11) puterror2($TEXT['error-supp20']);
  if(strlen($_POST['mar_id'])>11) puterror2($TEXT['error-mar1']);

  $_POST['zay_id'] = rep($_POST['zay_id']);
  $_POST['org_id'] = rep($_POST['org_id']);
  $_POST['tema_id'] = rep($_POST['tema_id']);
  $_POST['old_tema_id'] = rep($_POST['old_tema_id']);
  $_POST['man_id'] = rep($_POST['man_id']);
  $_POST['old_mast_id'] = rep($_POST['old_mast_id']);
  $_POST['mast_id'] = rep($_POST['mast_id']);
  $_POST['description'] = rep($_POST['description']);
  $_POST['master'] = rep($_POST['master']);
  $_POST['manager'] = rep($_POST['manager']);
  $_POST['user'] = rep($_POST['user']);
  $_POST['old_sost_id'] = rep($_POST['old_sost_id']);
  $_POST['sost_id'] = rep($_POST['sost_id']);
  $_POST['old_sroch_id'] = rep($_POST['old_sroch_id']);
  $_POST['sroch_id'] = rep($_POST['sroch_id']);

  $new_tema = $_POST['tema_id'];
  $new_mast = $_POST['mast_id'];
  $new_sost = $_POST['sost_id'];
  $new_sroch = $_POST['sroch_id'];
  $ob_type = "";
  $step = "";
  $max_step = "";

   $query_mast2 = "SELECT `fullname` FROM `tbl_logins` WHERE `login_id`='$new_mast' ";
     $mast_tmp = mysql_query($query_mast2);
       if(!$mast_tmp) puterror2($TEXT['error-tbl20']);
        $mast = mysql_fetch_array($mast_tmp,MYSQL_ASSOC);

   $query_tm = "SELECT `tema` FROM `tbl_supp_tema` WHERE `tema_id`='$new_tema' ";
     $tema_tmp = mysql_query($query_tm);
       if(!$tema_tmp) puterror2($TEXT['error-tbl22']);
         $tema = mysql_fetch_array($tema_tmp,MYSQL_ASSOC);

    $query_sr = "SELECT `srochnost` FROM `tbl_supp_srochnost` WHERE `sroch_id`='$new_sroch' ";
      $sroch_tmp = mysql_query($query_sr);
       if(!$sroch_tmp) puterror2($TEXT['error-tbl24']);
         $sroch = mysql_fetch_array($sroch_tmp,MYSQL_ASSOC);

    $query_st = "SELECT `sostoyanie` FROM `tbl_supp_sostoyanie` WHERE `sost_id`='$new_sost' ";
       $sost_tmp = mysql_query($query_st);
         if(!$sost_tmp) puterror2($TEXT['error-tbl23']);
           $sost = mysql_fetch_array($sost_tmp,MYSQL_ASSOC);

   if($_POST['mar_chb'] == "on")
  {
   $ob_type = ", `ob_type` = '5'";
   $step = ", `step` = 1 ";
   $data_k_isp = "";
   $time_isp = 0;
   $otv = 1;
   $ob_type_ins = 5;

   $query_iskh = "SELECT `step`, `usr_id`, `pic_tr` FROM `tbl_supp_zayavka` WHERE `zay_id`='".$_POST['zay_id']."'";
       $zay_iskh_tmp = mysql_query($query_iskh);
         if(!$zay_iskh_tmp) puterror2($TEXT['error-tbl26']);
            $zay_iskh = mysql_fetch_array($zay_iskh_tmp,MYSQL_ASSOC);

     $query_mar = "SELECT `mar_id`, `login_id`, (SELECT MAX(position) FROM `tbl_step` WHERE mar_id = ".$_POST['mar_id'].") AS max_step FROM `tbl_step` WHERE mar_id = ".$_POST['mar_id']." ";
      $mar_tmp = mysql_query($query_mar);
       if(!$mar_tmp) puterror2($TEXT['error-tbl42']);
        $mar = mysql_fetch_array($mar_tmp,MYSQL_ASSOC);
        $max_step =  ", `max_step` = ".$mar['max_step']." ";
        $max_step_ins =  $mar['max_step'];
        $step_ins =  $zay_iskh['step'] + 1;
        $mar_id =  $mar['mar_id'];

   $query_new = "INSERT INTO `tbl_supp_zayavka` (`zay_id` , `data_post` , `data_k_isp` , `data_close` , `man_id` , `mast_id` , `usr_id` , `sost_id` , `sroch_id` , `tema_id` , `problema` , `pic_tr` , `reshenie` , `kod` , `time_isp` , `org_id` , `ob_type`, `otv`, `mar_id`, `step`, `max_step`, `parent` )
   VALUES (NULL , NOW() , NOW() , '0000-00-00' , '".$_POST['man_id']."' , ".$mar['login_id']." , '".$zay_iskh['usr_id']."' , '$new_sost' , '$new_sroch' , '$new_tema' , '".$_POST['description']."' , '".$zay_iskh['pic_tr']."' , '".$TEXT['error-supp4']."' , '$passwd' , '$time_isp' , '".$_POST['org_id']."' , '$ob_type_ins' , '$otv', '$mar_id', '$step_ins', '$max_step_ins', '".$_POST['zay_id']."' )";
   $new_step = mysql_query($query_new);
   if(!$new_step) puterror2($TEXT['error-tbl26']);

   $query_mast3 = "SELECT `login_id` FROM `tbl_logins` WHERE `login`='robot' ";
     $mast_tmp2 = mysql_query($query_mast3);
       if(!$mast_tmp2) puterror2($TEXT['error-tbl20']);
        $mast_robot = mysql_fetch_array($mast_tmp2,MYSQL_ASSOC);

  $new_mast = $mast_robot['login_id'];
   }

  $query = "UPDATE `tbl_supp_zayavka` SET `data_k_isp` = NOW(), `man_id` = '".$_POST['man_id']."', `mast_id` = '$new_mast', `sost_id` = '$new_sost', `sroch_id` = '$new_sroch', `tema_id` = '$new_tema', `problema` = '".$_POST['description']."' ".$ob_type." ".$step." ".$max_step." WHERE `zay_id`='".$_POST['zay_id']."'";
  if(mysql_query($query))
  {
    //*************************************
    if($_POST['old_tema_id'] !== $_POST['tema_id']) $log_tema = $TEXT['support13']." ".$tema['tema'];
      else $log_tema = "";

    if($_POST['old_mast_id'] !== $_POST['mast_id']) $log_mast = $TEXT['support43']." ".$mast['fullname'];
      else $log_mast = "";

    if($_POST['old_sost_id'] !== $_POST['sost_id']) $log_sost = $TEXT['support40']." ".$sost['sostoyanie'];
      else $log_sost = "";

    if($_POST['old_sroch_id'] !== $_POST['sroch_id']) $log_sroch = $TEXT['support41']." ".$sroch['srochnost'];
      else $log_sroch = "";

    $ip = $_SERVER['REMOTE_ADDR'];
    $doing = $TEXT['error-supp19'];
    $what = $log_tema." ".$log_mast." ".$log_sost." ".$log_sroch." ".$TEXT['support1']." ".$_POST['zay_id'];
    $who = $_POST['manager'];
    //*************************************
    $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysql_query($query2))
           {
             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=manager.php' />
                   </head></html>";
           } else puterror2($TEXT['error-log']);
  } else puterror2($TEXT['error-tbl26']);
?>
</body>
</html>