<?php


require_once __DIR__ . '/../magic/auth.php';

$auth = bdr_require_privilege(2, '../logout.php');
$user = $auth['user_id'];
$father = $auth['user_id'];
$branch = $auth['branch'];
$conn = $auth['conn'];
$db = $auth['conn'];

//check if accountant of Oprearation manager
$sandy = $conn->prepare("SELECT count(carid) as sandy , (SELECT count(carid) FROM cars where carstatus = 3 AND branch > 1) as shop ,
    (SELECT count(carid) FROM cars where carstatus = 4 AND branch > 1) as runn ,(SELECT count(carid) FROM cars where carstatus = 5 AND branch > 1) as service
       FROM cars where carstatus = 1 AND branch > 1 ");
$sandy->execute();
if ($sandy->rowCount() > 0) {
  $wow = $sandy->fetch(PDO::FETCH_ASSOC);
  $sanndy = $wow['sandy'];
  $shop = $wow['shop'];
  $runn = $wow['runn'];
  $service = $wow['service'];
}
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="robots" content="index,follow">
  <meta name="author" content="سامح دياب">
  <link rel="icon" href="../images/perfect.ico">
  <title> بحث عن سيارة </title>
  <link rel="stylesheet" href="../dist/css/bootstrap.css" />
  <link rel="stylesheet" href="../css/font-awesome.min.css" />
  <link rel="stylesheet" href="myCar.css" />
  <script src="../js/jquery.js"></script>
  <script src="../dist/js/umd/popper.min.js"></script>
  <script src="../dist/js/bootstrap.js"></script>
</head>

<body style="background-color:#002933 ; color:#cce5ff;">
  <!-- <div class="navbar navbar-inverse bg-inverse bg-faded sticky-top"> -->
  <div class="container-fluid">
    <div class="row">
      <div class="col text-center">
        <div class="btn-group btn-group-lg" style="font-weight:bold;padding:5px;" role="group" aria-label="...">
          <button type="button" id="branch" style="font-weight:bold;padding:5px;font-size:2rem;" class="btn btn-outline-success">بحث</button>
          <button type="button" id="br_rent" style="font-weight:bold;padding:5px;font-size:2rem;" class="btn btn-info">ساري</button>
          <button type="button" id="br_debt" style="font-weight:bold;padding:5px;font-size:2rem;" class="btn btn-danger">مديون</button>
          <button type="button" id="check" style="font-weight:bold;padding:5px;font-size:2rem;" class="btn btn-secondary">السيارة</button>
        </div>
      </div>
    </div>
  </div>
  <!-- </div> -->
  <br />
  <div class="container-fluid">
    <form method="post" class="form-horizontal" id="car_form" name="car_form">
      <div class="row align-items-center">
        <div class="col-6">
          <p style="font-weight:bold;font-size:1.5rem;float:left;">بحث برقم السيارة </p>
        </div>
        <div class="col-3 text-center">
          <input type="number" id="car_no" name="car_no" placeholder="ارقام فقط" style="font-weight:bold;font-size:1.6rem;max-width:100px;" />
        </div>
        <div class="col-3 text-center">
          <button class="btn btn-success" id="car_sr" style="font-weight:bold;font-size:1.8rem;">بحث</button>
        </div>
      </div>
    </form>
  </div>
  <br />

  <div id="re_car"></div>
  <br />
  <div class="container-fluid">
    <div class="row align-items-center">
      <div class="col text-center">
        <ul class="nav nav-pills navbar-right" role="tablist">
          <li class="nav-item"><a class="nav-link active" href="#standby" aria-controls="profile" role="tab" data-toggle="tab">وقوف<span class="badge"><?php echo $sanndy; ?></span></a></li>
          <li class="nav-item"><a class="nav-link" href="#workshope" aria-controls="profile" role="tab" data-toggle="tab">ورشة<span class="badge"><?php echo $shop; ?></span></a></li>
          <li class="nav-item"><a class="nav-link" href="#service" aria-controls="profile" role="tab" data-toggle="tab">خدمة<span class="badge"><?php echo $service; ?></span></a></li>
          <li class="nav-item"><a class="nav-link" href="#runaway" aria-controls="profile" role="tab" data-toggle="tab">تويوتا شامل<span class="badge"><?php echo $runn; ?></span></a></li>
        </ul>
        <!-- Tab panes -->
        <div class="tab-content" style="width:100% ;">
          <div role="tabpanel" class="tab-pane fad active" id="standby">
            <form class="form-horizontal" id="form_standby" method="POST" class="form-horizontal">
              <br />
              <div id="form_standby_msg"></div>
              <div class="table-responsive">
                <table class="table table-bordered table-hover">
                  <thead>
                    <tr>
                      <th class="text-center" width="10%">م</th>
                      <th class="text-center" width="20%">طراز</th>
                      <th class="text-center" width="25%">لوحة</th>
                      <th class="text-center" width="15%">موديل</th>
                      <th class="text-center" width="13%">وقوف</th>
                      <th class="text-center" width="22%">الفرع</th>
                    </tr>
                  </thead>
                  <tbody id="tstandby">
                    <?php
                    include "../op/CarHistory.php";

                    $carHistory = new CarHistory($conn);
                    $output = '';
                    $bb = $conn->prepare("SELECT branchID , b_name  from branches  where branchID > 1 and branchID != 4  order by branchID ");
                    $bb->execute();
                    if ($bb->rowCount() > 0) {
                      while ($result = $bb->fetch()) {
                        $branch = $result["branchID"];
                        $b_name = $result["b_name"];

                        $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    = $carHistory->car_state($mycar);
                            if ($dd == 1) {
                              $output .= '<tr> <td style="color:#DA2D07; background-color:#F0F7E4;font-weight:bold;" >' . $dd . '</td>';
                            } else {
                              $output .= '<tr> <td>' . $dd . '</td>';
                            }
                            $output .= '
                                                        <td class="text-center">' . $row['kind'] . '</td>
                                                        <td>' . $row['numm'] . '</td>
                                                        <td>' . $row['model'] . '</td>
                                                        <td class="text-center" >' . $qty . '</td>
                                                        <td class="text-center">' . $b_name . '</td>
                                                     </tr>  ';
                            $dd = $dd + 1;
                          }
                        } else {
                          $output .= '<tr>
                                         <td colspan="6"> لا يوجد سيارات لفرع ' . $b_name . '</td>
                                               </tr>';
                        }
                      }
                    }
                    echo $output;
                    ?>
                  </tbody>
                  <tfoot></tfoot>
                </table>
            </form> <!-- /.form -->

          </div>
        </div><!-- /standby -->
        <div role="tabpanel" class="tab-pane fad" id="workshope">
          <form class="form-horizontal" id="form_workshope" method="POST" class="form-horizontal">
            <br />
            <table class="table table-bordered table-hover">
              <thead>
                <tr>
                  <th class="text-center" width="20%">طراز</th>
                  <th class="text-center" width="25%">لوحة</th>
                  <th class="text-center" width="15%">الموديل</th>
                  <th class="text-center" width="15%">وقوف</th>
                  <th class="text-center" width="25%">الفرع</th>
                </tr>
              </thead>
              <tbody id="tworkshope">
                <?php
                $output2 = '';
                $bb = $conn->prepare("SELECT branchID , b_name  from branches  where branchID > 1 and branchID != 4 order by branchID ");
                $bb->execute();
                if ($bb->rowCount() > 0) {
                  while ($result = $bb->fetch()) {
                    $branch = $result["branchID"];
                    $b_name = $result["b_name"];
                    $sql = $conn->prepare("select carid,kind,concat(letters,numbers) as numm, model,color
                                from cars  where carstatus = 3 and cars.branch = ? order by model DESC ");
                    $sql->bindParam(1, $branch);
                    $sql->execute();
                    if ($sql->rowCount() > 0) {
                      while ($row = $sql->fetch()) {
                        $mycar = $row['carid'];
                        $qty    = $carHistory->car_state($mycar);
                        $output2 .= '
                                                <td class="text-center">' . $row['kind'] . '</td>
                                                <td>' . $row['numm'] . '</td>
                                                <td>' . $row['model'] . '</td>
                                                <td class="text-center" >' . $qty . '</td>
                                                <td class="text-center">' . $b_name . '</td>
                                             </tr>  ';
                      }
                    } else {
                      $output2 .= '<tr><td colspan="5">لا يوجد سيارات لفرع ' . $b_name . '</td></tr>';
                    }
                  }
                }
                echo $output2;
                ?>
              </tbody>
              <tfoot></tfoot>
            </table>
          </form> <!-- /.form -->
        </div><!-- /workshope -->
        <div role="tabpanel" class="tab-pane fad" id="service">
          <form class="form-horizontal" id="form_service" method="POST" class="form-horizontal">
            <br />
            <table class="table table-bordered table-hover">
              <thead>
                <tr>
                  <th class="text-center" width="20%">طراز</th>
                  <th class="text-center" width="25%">لوحة</th>
                  <th class="text-center" width="15%">الموديل</th>
                  <th class="text-center" width="15%">وقوف</th>
                  <th class="text-center" width="25%">الفرع</th>
                </tr>
              </thead>
              <tbody id="tservice">
                <?php
                $output3 = '';
                $bb = $conn->prepare("SELECT branchID , b_name  from branches  where branchID > 1 and branchID != 4 order by branchID ");
                $bb->execute();
                if ($bb->rowCount() > 0) {
                  while ($result = $bb->fetch()) {
                    $branch = $result["branchID"];
                    $b_name = $result["b_name"];

                    $sql = $conn->prepare("SELECT carid,kind,concat(letters,numbers) as numm, model,color
                          from cars  where carstatus = 5 and cars.branch = ? order by model DESC ");
                    $sql->bindParam(1, $branch);
                    $sql->execute();
                    if ($sql->rowCount() > 0) {
                      while ($row = $sql->fetch()) {
                        $mycar = $row['carid'];
                        $qty    = $carHistory->car_state($mycar);
                        $output3 .= '
                                            <td class="text-center">' . $row['kind'] . '</td>
                                            <td>' . $row['numm'] . '</td>
                                            <td>' . $row['model'] . '</td>
                                            <td class="text-center" >' . $qty . '</td>
                                            <td class="text-center">' . $b_name . '</td>
                                         </tr>  ';
                      }
                    } else {
                      $output3 .= '<tr><td colspan="5"> لا يوجد سيارات لفرع ' . $b_name . '</td></tr>';
                    }
                  }
                }
                echo $output3;
                ?>
              </tbody>
              <tfoot></tfoot>
            </table>
          </form> <!-- /.form -->
        </div><!-- /service -->
        <div role="tabpanel" class="tab-pane fad" id="runaway">
          <form class="form-horizontal" id="form_runaway" method="POST" class="form-horizontal">
            <br />
            <table class="table table-bordered table-hover">
              <thead>
                <tr>
                  <th class="text-center" width="20%">طراز</th>
                  <th class="text-center" width="25%">لوحة</th>
                  <th class="text-center" width="15%">الموديل</th>
                  <th class="text-center" width="15%">وقوف</th>
                  <th class="text-center" width="25%">الفرع</th>
                </tr>
              </thead>
              <tbody id="trunaway">
                <?php
                $output4 = '';
                $bb = $conn->prepare("SELECT branchID , b_name  from branches  where branchID > 1 and branchID != 4 order by branchID ");
                $bb->execute();
                if ($bb->rowCount() > 0) {
                  while ($result = $bb->fetch()) {
                    $branch = $result["branchID"];
                    $b_name = $result["b_name"];

                    $sql = $conn->prepare("SELECT carid,kind,concat(letters,numbers) as numm, model,color
                    from cars  where carstatus = 4 and cars.branch = ? order by model DESC ");
                    $sql->bindParam(1, $branch);
                    $sql->execute();
                    if ($sql->rowCount() > 0) {
                      while ($row = $sql->fetch()) {
                        $mycar = $row['carid'];
                        $qty    = $carHistory->car_state($mycar);
                        $output4 .= '
                                      <td class="text-center">' . $row['kind'] . '</td>
                                      <td>' . $row['numm'] . '</td>
                                      <td>' . $row['model'] . '</td>
                                      <td class="text-center" >' . $qty . '</td>
                                      <td class="text-center">' . $b_name . '</td>
                                   </tr>  ';
                      }
                    } else {
                      $output4 .= '<tr><td colspan="5">لا يوجد سيارات لفرع ' . $b_name . '</td></tr>';
                    }
                  }
                }
                echo $output4;
                ?>
              </tbody>
              <tfoot></tfoot>
            </table>
          </form> <!-- /.form -->
        </div><!-- /runaway -->
      </div>
    </div>
  </div>
  </div>
  <script>
    $(document).ready(function() {
      $("#br_rent").click(function(event) {
        event.preventDefault();
        window.open("br_rent.php", '_self');
      });
      $("#br_debt").click(function(event) {
        event.preventDefault();
        window.open("br_debt.php", '_self');
      });

      $("#check").click(function(event) {
        event.preventDefault();
        window.open("check.php", '_self');
      });

      $("#car_sr").click(function(event) {
        event.preventDefault();
        $.ajax({
          url: "br_car.php",
          method: "POST",
          data: $("#car_form").serialize(),
          success: function(data) {
            $("#re_car").html(data);
          },
          error: function(result) {
            alert(result.statusText);
          }
        });
      });

    });
  </script>

</body>

</html
