<?php
//insert.php
require_once "../magic/session_config.php";
bdr_start_session();
ob_start();

$branch = $_SESSION["branch"];
$father = $_SESSION["user_id"];

if(isset($_POST["st"])){
    $st = $_POST["st"] ;
    $carid = $_POST["carid"] ;
    require_once "../magic/DataBase.php";
$database = new DataBase();

$conn = $database->getConnection();

require_once "../op/CarHistory.php";

// $carHistory = new CarHistory($conn);

      $car= $conn->prepare("UPDATE cars set carstatus = ? where carid = ?");
      $car->bindParam(1,$st);
      $car->bindParam(2,$carid);
      if($car->execute()){
        // echo "تم التحديث ";
        $data = array();
        // $error = array();
        $output = '';
          $sql =$conn->prepare("SELECT carid,kind,concat(letters,numbers) as numm, model,color
            from cars  where carstatus = 1 and cars.branch = ? order by model DESC ");
         $sql->bindParam(1,$branch);
          $sql->execute();
           if($sql->rowCount()>0)
           {$dd = 1 ;
              while($row = $sql->fetch())
                {
                  $mycar = $row['carid'];
                  $qty = car_state($mycar);
                     $output .= '<tr>
                               <td>' . $dd. '</td>
                               <td class="text-center">' . $row['kind'] . '</td>
                               <td>' . $row['numm'] . '</td>
                               <td>' . $row['model'] . '</td>
                               <td>' . $row['color'] . '</td>
                               <td class="text-center" >'.$qty.'</td>
                               <td>
                               <a href="#"<button type="button"  st="3" mycar='. $row['carid'] .' class="btn btn-danger btn-xs car_ment">إلي الورشة</button></a>
                               </td>
                            </tr>  '; $dd = $dd + 1;
                }
           }
           else
           {
                $output .= '<tr><td colspan="7">لا يوجد سجلات ...</td></tr>';
           }
          $data["standby"]= $output;

          $output2 = '';
            $sql2 =$conn->prepare("SELECT carid,kind,concat(letters,numbers) as numm, model,color
              from cars  where carstatus = 3 and cars.branch = ? order by model DESC ");
           $sql2->bindParam(1,$branch);
           $sql2->execute();
             if($sql2->rowCount()>0)
             {$dd = 1 ;
                while($row = $sql2->fetch())
                  {
                    $mycar = $row['carid'];
                   $qty = car_state($mycar);
                       $output2 .= '<tr>
                                 <td>' . $dd. '</td>
                                 <td class="text-center">' . $row['kind'] . '</td>
                                 <td>' . $row['numm'] . '</td>
                                 <td>' . $row['model'] . '</td>
                                 <td>' . $row['color'] . '</td>
                                 <td class="text-center" >'.$qty.'</td>
                                 <td>
                                 <a href="#"<button type="button" st="1"  mycar='. $row['carid'] .' class="btn btn-success btn-xs car_ment ">إستعداد</button></a>
                                 </td>
                              </tr>  '; $dd = $dd + 1;
                  }
             }
             else
             {
                  $output2 .= '<tr><td colspan="7">لا يوجد سجلات ...</td></tr>';
             }
          $data["workshope"]= $output2;
          $output3 = '';
            $sql3 =$conn->prepare("SELECT carid,kind,concat(letters,numbers) as numm, model,color
              from cars  where carstatus = 5 and cars.branch = ? order by model DESC ");
            $sql3->bindParam(1,$branch);
            $sql3->execute();
             if($sql3->rowCount()>0)
             {$dd = 1 ;
                while($row = $sql3->fetch())
                  {
                  $mycar = $row['carid'];
                   $qty = car_state($mycar);
                       $output3 .= '<tr>
                                 <td>' . $dd. '</td>
                                 <td class="text-center">' . $row['kind'] . '</td>
                                 <td>' . $row['numm'] . '</td>
                                 <td>' . $row['model'] . '</td>
                                 <td>' . $row['color'] . '</td>
                                 <td class="text-center" >'.$qty.'</td>
                                 <td>
                                 <a href="#"<button type="button"  st="3" mycar='. $row['carid'] .' class="btn btn-danger btn-xs car_ment">إلي الورشة</button></a>
                                 </td>
                              </tr>  '; $dd = $dd + 1;
                  }
             }
             else
             {
                  $output3 .= '<tr><td colspan="7">لا يوجد سجلات ...</td></tr>';
             }
          $data["service"]= $output3;

          $output4 = '';
            $sql4 =$conn->prepare("SELECT carid,kind,concat(letters,numbers) as numm, model,color
              from cars  where carstatus = 4 and cars.branch = ? order by model DESC ");
           $sql4->bindParam(1,$branch);
            $sql4->execute();
             if($sql4->rowCount()>0)
             {$dd = 1 ;
                while($row = $sql4->fetch())
                  {
                  $mycar = $row['carid'];
                   $qty = car_state($mycar);
                       $output4 .= '<tr>
                                 <td>' . $dd. '</td>
                                 <td class="text-center">' . $row['kind'] . '</td>
                                 <td>' . $row['numm'] . '</td>
                                 <td>' . $row['model'] . '</td>
                                 <td>' . $row['color'] . '</td>
                                 <td class="text-center" >'.$qty.'</td>
                                 <td>
                                 <a href="#"<button type="button"  st="3" mycar='. $row['carid'] .' class="btn btn-danger btn-xs car_ment">إلي الورشة</button></a>
                                 </td>
                              </tr>  '; $dd = $dd + 1;
                  }
             }
             else
             {
                  $output4 .= '<tr><td colspan="7">لا يوجد سجلات ...</td></tr>';
             }
            $data["runaway"] = $output4;
      }//after excute car update
      echo json_encode($data);
    }//if is set status an car id

?>
