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

$auth = bdr_require_privilege([20, 36, 30], 'branch.php', 'branch.php');
$user = $auth['user_id'];
$father = $auth['user_id'];
$branch = $auth['branch'];
$conn = $auth['conn'];
$db = $auth['conn'];
$role = 9 ;
require_once "../op/wanted.php";

//check if accountant of Oprearation manager
$roleQuery = $conn->prepare('SELECT role FROM users WHERE user_id = ? LIMIT 1');
$roleQuery->execute([$father]);
if ($row = $roleQuery->fetch(PDO::FETCH_ASSOC)) {
  $role = $row['role'];
}
if ($father == 99) {
  $style = "style='display:none;'";
} else {
  $style = "";
}
?>
<!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:#00324d ; color:#cce5ff;">
  <div class="navbar navbar-inverse bg-inverse bg-faded sticky-top">
    <div class="container-fluid">
      <div class="row">
        <div class="col">
          <div class="btn-group btn-group-lg" style="font-weight:bold;padding:5px;" role="group" aria-label="...">
            <button type="button" style="font-weight:bold;padding:5px;font-size:2rem;" class="btn btn-outline-success">اليوم</button>
            <button type="button" id="at_rent" style="font-weight:bold;padding:5px;font-size:2rem;" class="btn btn-info">ساري</button>
            <button type="button" id="debt_contracts" style="font-weight:bold;padding:5px;font-size:2rem;" class="btn btn-danger">مديون</button>
            <button type="button" id="car_search" style="font-weight:bold;padding:5px;font-size:2rem;" class="btn btn-secondary">بحث سيارة </button>
            <?php
            if ($role == 1) {
              echo '<button type="button" id="log" style="font-weight:bold;padding:5px;font-size:2rem;" class="btn btn-success">سجلات</button>';
            }
            ?>
          </div>
        </div>
      </div>
    </div>
  </div>
  <br />
  <div id="main">
    <?php
    $today = date("Y-m-d");
    $first_day = date('Y-m-01', strtotime($today));
    $last_day   = date('Y-m-t', strtotime($today));
    // box remain money
    $money_b = 0;
    $sql = $conn->prepare("SELECT sum(money_in) as money_in FROM invoice  WHERE payMethod = 1  AND sanad_date between  ? AND ? ");
    $sql->bindParam(1, $first_day);
    $sql->bindParam(2, $last_day);
    $sql->execute();
    if ($sql->rowCount() > 0) {
      $row = $sql->fetch(PDO::FETCH_ASSOC);
      $money_in       = round(($row["money_in"]), 2);
    }

    $sql2 = $conn->prepare("SELECT sum(money_out) as money_out FROM outvoice  WHERE sanad_out between  ? AND ? ");
    $sql2->bindParam(1, $first_day);
    $sql2->bindParam(2, $last_day);
    $sql2->execute();
    if ($sql2->rowCount() > 0) {
      $row = $sql2->fetch(PDO::FETCH_ASSOC);
      $money_out       = round(($row["money_out"]), 2);
    }

    $sql3 = $conn->prepare("SELECT sum(money_box) as tot_box FROM boxmoney  WHERE month_date between  ? AND ? ");
    $sql3->bindParam(1, $first_day);
    $sql3->bindParam(2, $last_day);
    $sql3->execute();
    if ($sql3->rowCount() > 0) {
      $row = $sql3->fetch(PDO::FETCH_ASSOC);
      $box       = round(($row["tot_box"]), 2);
    }
    $money_b = round(($money_in - ($money_out + $box)), 2);

    $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'];
    }
    $inn = 0;
    $outt = 0;
    $bank = 0;
    $buy_points = 0;
    $money = $conn->prepare("SELECT sum(money_in) as inn  , (select sum(money_out) from outvoice where sanad_out = CURDATE()) as outt ,
     (SELECT sum(money_in) from invoice where paymethod = 2 AND sanad_date = CURDATE()) as bank ,(SELECT sum(money_in) from invoice where paymethod = 5 AND sanad_date = CURDATE()) as bank2 ,(select sum(money_in) from invoice where paymethod = 3 AND sanad_date = CURDATE()) as buy_points
      from invoice where sanad_date = CURDATE() ");
    $money->execute();
    if ($money->rowCount() > 0) {
      $now = $money->fetch(PDO::FETCH_ASSOC);
      // !== null
      // $inn        = round(($now['inn']),2);
      // $outt       = $now['outt'] !== null ? round(($now['outt']),2):0;
      // $bank       = round(($now['bank']),2);
      // $bank2       = round(($now['bank2']),2);
      // $buy_points = round(($now['buy_points']),2);

      $inn        = $now['inn'] !== null ? round(($now['inn']), 2) : 0;
      $outt       = $now['outt'] !== null ? round(($now['outt']), 2) : 0;
      $bank       = $now['bank'] !== null ? round(($now['bank']), 2) : 0;
      $bank2       = $now['bank2'] !== null ? round(($now['bank2']), 2) : 0;
      $buy_points = $now['buy_points'] !== null ? round(($now['buy_points']), 2) : 0;
    }
    $bank = $bank + $bank2;
    ?>
    <div class="container-fluid">
      <div class="row align-items-center" <?php echo $style; ?>>
        <div class="col-3" style="float:right;padding:3px; margin: 2px;">
          <label class="control-label" style="float:left;font-size:1.1em;">إيرادات اليوم</label>
        </div>
        <div class="col-2" style="float:right;padding:3px; margin: 2px;">
          <input type="text" class="form-control" style="float:right;font-size:1.2em;" value="<?php echo $inn - $outt; ?>" readonly />
        </div>
        <div class="col-3" style="float:right;padding:3px; margin: 2px;">
          <label class="control-label" style="float:left;font-size:1.1em;">رصيد الصندوق</label>
        </div>
        <div class="col-3" style="float:right;padding:3px; margin: 2px;">
          <input type="text" class="form-control" style="float:right;font-size:1.2em;" value="<?php echo $money_b; ?>" readonly />
        </div>
      </div>
      <div class="row align-items-center" <?php echo $style; ?>>
        <div class="col-3" style="float:right;">
          <label class="control-label" style="float:left;font-size:1.1em;">تحويل بنكي</label>
        </div>
        <div class="col-2" style="float:right;padding:3px; margin: 2px;">
          <input type="text" class="form-control" style="float:right;font-size:1.2em;" value="<?php echo $bank; ?>" readonly />
        </div>
        <div class="col-3" style="float:right;padding:3px; margin: 2px;">
          <label class="control-label" style="float:left;font-size:1.1em;">نقاط بيع</label>
        </div>
        <div class="col-3" style="float:right;padding:3px; margin: 2px;">
          <input type="text" class="form-control" style="float:right;font-size:1.2em;" value="<?php echo $buy_points; ?>" readonly />
        </div>
      </div>
    </div>
  </div>
  <br /><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
      require_once "../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"];

$output .= '<tr><td colspan="6">' .$b_name . '</td></tr>';
          $sql = $conn->prepare("SELECT cars.carid, cars.branch ,cars.kind,concat( cars.letters, cars.numbers) as numm, cars.model,cars.color ,branches.b_name ,cars.area
          from cars , branches 
          where cars.branch = branches.branchID AND
          carstatus = 1 and ( cars.branch = ? OR cars.area = ?) order by model DESC");
            $sql->bindParam(1, $branch);
            $sql->bindParam(2, $branch);
                        $sql->execute();
                        if ($sql->rowCount() > 0) {
                          $dd = 1;
               while ($row = $sql->fetch()) {
                 $mycar = $row['carid'];
                  $style = "";
                  $area = $row['area'];
                  $branch = $row['branch'];
                if($area !== $branch){
                        $style ="color:#DA2D07; background-color:#F0F7E4;font-weight:bold;";
                      }
                            $qty   = $carHistory->car_state($mycar);
                             $branch_name = $row["b_name"];
                            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 style="'.$style.'">' . $branch_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() {
      $("#at_rent").click(function(event) {
        event.preventDefault();
        window.open("at_rent.php", '_self');
      });

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

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

      var s = window.matchMedia("(min-width:0px) and (max-width:550px)");
      isMobile(s);
      s.addListener(isMobile);
    }); //last

    function isMobile(mq) {
      const mego = document.getElementById("mego");
      if (!mq.matches) {
        window.location.href = '../profile.php';
      }
    }
  </script>


</body>

</html
