<?php
require_once "../magic/session_config.php";
bdr_start_session();
ob_start();
$branch_d = $_SESSION['branch'];
require_once "../magic/DataBase.php";
 $database = new DataBase();

$conn = $database->getConnection();

require_once "../op/CarHistory.php";
$carHistory = new CarHistory($conn);

$today = date("Y-m-d");
$sandy = $conn->prepare("SELECT count(carid) as sandy , (SELECT count(carid) FROM cars where carstatus = 3 AND branch = ?) as shop ,
  (SELECT count(carid) FROM cars where carstatus = 4 AND branch = ?) as runn ,(SELECT count(carid) FROM cars where carstatus = 5 AND branch = ?) as service
   FROM cars where carstatus = 1 AND branch = ? ");

$sandy->bindParam(1, $branch_d);
$sandy->bindParam(2, $branch_d);
$sandy->bindParam(3, $branch_d);
$sandy->bindParam(4, $branch_d);
$sandy->execute();
if ($sandy->rowCount() > 0) {
  $wow = $sandy->fetch(PDO::FETCH_ASSOC);
  $sanndy = $wow['sandy'];
  $shop = $wow['shop'];
  $runn = $wow['runn'];
  $service = $wow['service'];
}
$want = $conn->prepare("SELECT cars.carid, cars.kind,concat(cars.letters,cars.numbers) as numm, cars.model,cars.color , 
        ment_request.ask_date , ment_request.req_id from cars , ment_request 
    where cars.carid = ment_request.car_id AND cars.branch = ? AND 
    ment_request.done = 0");
$want->bindParam(1,$branch_d);
$want->execute();
$wantedCars = $want->rowCount();
?>
<div class="panel panel-primary">
  <div class="panel-heading" style="font-size:1.2em;">اليومية</div>
  <div class="panel-body" style="background-color:#00324d;color:#f9feff;">
    <div class="container-fluid">
      <form method="post" class="form-horizontal" id="form_box" style="float:center;">
        <div class="row" style="padding:3px;">
          <div class="col-md-12" style="float:right;">
            <div id="box_msg"></div>
            <input type="hidden" id="box_id" name="box_id" />
          </div>
          <br/>
          <div class="row" style="padding:3px;">
            <div class="col-md-2" style="float:right;">
              <input type="datepicker" class="form-control" style="float:right;" id="myday" name="myday" value="<?php echo $today; ?>" readonly />
            </div>
            <div class="col-md-1" style="float:right;">
              <label for="money_box" class="control-label" style="float:left;">المبلغ <span class="text-danger">*</span></label>
            </div>
            <div class="col-md-2" style="float:right;">
              <input type="number" class="form-control" style="float:right;" id="money_box" name="money_box" placeholder="بالريال" />
              <div id="money_box_msg" class="text-danger" style="font-size:12px;"></div>
            </div>
            <!-- <div class="col-md-4" style="float:right;">
            </div> -->
            <div class="col-md-2" style="float:right;">
              <select class="form-control" id="payMethod2" name="payMethod2">
                <option selected value="1">كاش</option>
                <option value="4">فواتير</option>
              </select>
            </div>
            <script>
              $(function() {
                var calendar = $.calendars.instance('');
                $('#myday').calendarsPicker({
                  calendar: calendar,
                  minDate : -1.03
                });
              });
            </script>
          </div>
          <div class="row" style="padding:3px;">
            <div class="col-md-2" style="float:right;">
              <button id="trans" type="button" style="font-weight:bold;" class="btn btn-success btn-block"> طباعة اليومية <span class="glyphicon glyphicon-print" aria-hidden="true"></span></button>
            </div>
            <div class="col-md-1" style="float:right;">
              <label for="receipt" class="control-label" style="float:left;padding-left:2px;padding-right:2px;">المستلم</label>
            </div>
            <div class="col-md-2" style="float:right;">
              <input type="text" style="float:right;padding-left:2px;padding-right:2px;" class="form-control" id="receipt" name="receipt" placeholder="للإدارة" />
            </div>
            <div class="col-md-7" style="float:right;">
              <input type="text" class="form-control" style="float:right;padding-left:2px;padding-right:2px;" id="comm3" name="comm3" placeholder="ملاحظات" />
            </div>
          </div>
      </form><!-- end of box form -->
      <br/>
      <div class="modal-footer form-group">
        <div class="row" style="padding:3px;">
          <div class="col-md-2" style="float:right;">
            <button id="box_trns" type="button" style="font-weight:bold;" class="btn btn-success btn-block"> طباعة الصندوق <span class="glyphicon glyphicon-print" aria-hidden="true"></span></button>
          </div>
          <div class="col-md-1" style="float:right;">
          </div>
          <div class="col-md-2" style="float:right;">
            <input type="button" id="sanad_box" name="sanad_in_add" style="font-weight:bold;" class="btn btn-primary btn-block" value=" حفظ " />
            <a id="box_print" href="" target="_blank" style="font-weight:bold;display: none;"><button type="button" class="btn btn-success btn-block">طباعة السند <span class="glyphicon glyphicon-print" aria-hidden="true"></span></button></a>
          </div>
          <div class="col-md-2" style="float:left;">
            <button id="month_bank" type="button" style="font-weight:bold;" class="btn btn-success btn-block">المعاملات البنكية <span class="glyphicon glyphicon-print" aria-hidden="true"></span></button>
          </div>
          <div class="col-md-2" style="float:left;">
            <button id="month_cash" type="button" style="font-weight:bold;" class="btn btn-success btn-block">الإيداعات النقدية<span class="glyphicon glyphicon-print" aria-hidden="true"></span></button>
          </div>
        </div>
      </div>
    </div><br />
    <div class="container-fluid">
      <div class="row" style="padding:3px;">
        <div class="col-md-12">
          <ul class="nav nav-tabs navbar-right" role="tablist">
            <li role="presentation" class="active" style="float:right;"><a href="#standby" aria-controls="home" role="tab" data-toggle="tab"> سيارات وقوف <span class="badge"><?php echo $sanndy; ?></span></a></li>
            <li role="presentation" style="float:right;"><a href="#want" aria-controls="home" role="tab" data-toggle="tab">مطلوبة <span class="badge"><?php echo $wantedCars; ?></span> </a></li>
            <li role="presentation" style="float:right;"><a href="#workshope" aria-controls="home" role="tab" data-toggle="tab">ورشة <span class="badge"><?php echo $shop; ?></span> </a></li>
            <li role="presentation" style="float:right;"><a href="#service" aria-controls="home" role="tab" data-toggle="tab">خدمة <span class="badge"><?php echo $service; ?></span></a></li>
            <li role="presentation" style="float:right;"><a href="#runaway" aria-controls="home" 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" style="float:center;">
                <br /><br /><br /><br />
                <div class="table-responsive">
                  <table class="table table-bordered table-hover">
                    <thead>
                      <tr>
                        <th class="text-center" width="5%">م</th>
                        <th class="text-center" width="15%">طراز</th>
                        <th class="text-center" width="10%">لوحة</th>
                        <th class="text-center" width="10%">الموديل</th>
                        <th class="text-center" width="10%">اللون</th>
                        <th class="text-center" width="10%">موقع السيارة</th>
                        <th class="text-center" width="15%">عدد ايام</th>
                        <th class="text-center" width="10%">حركة</th>
                      </tr>
                    </thead>
                    <tbody id="tstandby">
                      <?php

                      $output = '';
                      $sql = $conn->prepare("SELECT cars.carid, cars.kind,concat( cars.letters, cars.numbers) as numm, cars.model,cars.color ,branches.b_name ,cars.area
                                  from cars , branches 
                                  where cars.area = branches.branchID AND
                                  carstatus = 1 and cars.branch = ? order by model DESC");
                      $sql->bindParam(1, $branch_d);
                      $sql->execute();
                      if ($sql->rowCount() > 0) {
                        $nn = 1;
                        while ($row = $sql->fetch()) {
                          $mycar = $row['carid'];
                          $style = "";
                          $area = $row['area'];
                      if($area != $branch_d){$style="color:#DA2D07; background-color:#F0F7E4;font-weight:bold;";}
                          $qty    = $carHistory->car_state($mycar);
                          $output .= '<tr><td>' . $nn . '</td>
                              <td class="text-center">' . $row['kind'] . '</td>
                                          <td>' . $row['numm'] . '</td>
                                          <td>' . $row['model'] . '</td>
                                          <td>' . $row['color'] . '</td>
                                          <td style="'.$style.'">' . $row['b_name'] . '</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>  ';
                          $nn = $nn + 1;
                        }
                      } else {
                        $output .= '<tr><td colspan="8">لا يوجد سجلات ...</td></tr>';
                      }
                      echo $output;
                      ?>
                    </tbody>
                    <tfoot></tfoot>
                  </table>
              </form> <!-- /.form -->

            </div><!-- /standby -->
          </div>

          <div role="tabpanel" class="tab-pane fad" id="want">
            <form class="form-horizontal" id="form_want" method="POST" class="form-horizontal" style="float:center;">
              <br /><br /><br /><br />
              <div id="form_want_msg"></div>
              <div class="table-responsive">
                <table class="table table-bordered table-hover">
                  <thead>
                    <tr>
                      <th class="text-center" width="5%">م</th>
                      <th class="text-center" width="15%">طراز</th>
                      <th class="text-center" width="10%">لوحة</th>
                      <th class="text-center" width="10%">الموديل</th>
                      <th class="text-center" width="10%">اللون</th>
                      <th class="text-center" width="15%">عدد ايام</th>
                      <th class="text-center" width="15%">حركة</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php
                    $output2 = '';
          if ($wantedCars > 0) {
                      $dd = 1;
                      $date1 = new DateTime(date("Y-m-d"));
              while ($row = $want->fetch()) {
                        $mycar = $row['carid'];
                        $req_id = $row['req_id'] ;
                        $ask_date = $row['ask_date'];
                        $date2 = new DateTime($ask_date);
                        $qty = $date2->diff($date1)->format("%a");;
                        $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>
                                         <button type="button" mycar=' . $mycar . '  myreq=' . $row['req_id'] . ' class="btn btn-success btn-xs btn-block car_want ">سبب الطلب</button>
                                        </td>
                                    </tr>  ';
                        $dd = $dd + 1;
                      }
                    } else {
                      $output2 .= '<tr><td colspan="7">لا يوجد سجلات ...</td></tr>';
                    }
                    echo $output2;
                    ?>
                  </tbody>
                  <tfoot></tfoot>
                </table>
            </form>
          </div>
          </div>

          <div role="tabpanel" class="tab-pane fad" id="workshope">
            <form class="form-horizontal" id="form_workshope" method="POST" class="form-horizontal" style="float:center;">
              <br /><br /><br /><br />
              <div id="form_workshope_msg"></div>
              <div class="table-responsive">
                <table class="table table-bordered table-hover">
                  <thead>
                    <tr>
                      <th class="text-center" width="5%">م</th>
                      <th class="text-center" width="15%">طراز</th>
                      <th class="text-center" width="10%">لوحة</th>
                      <th class="text-center" width="10%">الموديل</th>
                      <th class="text-center" width="10%">اللون</th>
                      <th class="text-center" width="15%">عدد ايام</th>
                      <th class="text-center" width="15%">حركة</th>
                    </tr>
                  </thead>
                  <tbody id="tworkshope">
                    <?php
                    $output2 = '';
                    $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_d);
                    $sql->execute();
                    if ($sql->rowCount() > 0) {
                      $dd = 1;
                      while ($row = $sql->fetch()) {
                        $mycar = $row['carid'];
                        $qty    = $carHistory->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>';
                    }
                    echo $output2;
                    ?>
                  </tbody>
                  <tfoot></tfoot>
                </table>
            </form> <!-- /.form -->

          </div>
        </div><!-- /workshope -->
        <div role="tabpanel" class="tab-pane fad" id="service">
          <form class="form-horizontal" id="form_service" method="POST" class="form-horizontal" style="float:center;">
            <br /><br /><br /><br />
            <div id="form_service_msg"></div>
            <div class="table-responsive">
              <table class="table table-bordered table-hover">
                <thead>
                  <tr>
                    <th class="text-center" width="5%">م</th>
                    <th class="text-center" width="15%">طراز</th>
                    <th class="text-center" width="10%">لوحة</th>
                    <th class="text-center" width="10%">الموديل</th>
                    <th class="text-center" width="10%">اللون</th>
                    <th class="text-center" width="15%">عدد ايام</th>
                    <th class="text-center" width="15%">حركة</th>
                  </tr>
                </thead>
                <tbody id="tservice">
                  <?php
                  $output3 = '';
                  $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_d);
                  $sql->execute();
                  if ($sql->rowCount() > 0) {
                    $dd = 1;
                    while ($row = $sql->fetch()) {
                      $mycar = $row['carid'];
                      $qty    = $carHistory->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>';
                  }
                  echo $output3;
                  ?>
                </tbody>
                <tfoot></tfoot>
              </table>
          </form> <!-- /.form -->

        </div>
      </div><!-- /service -->
      <div role="tabpanel" class="tab-pane fad" id="runaway">
        <form class="form-horizontal" id="form_runaway" method="POST" class="form-horizontal" style="float:center;">
          <br /><br /><br /><br />
          <div id="form_runaway"></div>
          <div class="table-responsive">
            <table class="table table-bordered table-hover">
              <thead>
                <tr>
                  <th class="text-center" width="5%">م</th>
                  <th class="text-center" width="15%">طراز</th>
                  <th class="text-center" width="10%">لوحة</th>
                  <th class="text-center" width="10%">الموديل</th>
                  <th class="text-center" width="10%">اللون</th>
                  <th class="text-center" width="15%">عدد ايام</th>
                  <th class="text-center" width="15%">حركة</th>
                </tr>
              </thead>
              <tbody id="trunaway">
                <?php
                $output4 = '';
                $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_d);
                $sql->execute();
                if ($sql->rowCount() > 0) {
                  $dd = 1;
                  while ($row = $sql->fetch()) {
                    $mycar = $row['carid'];
                    $qty    = $carHistory->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>';
                }
                echo $output4;
                ?>
              </tbody>
            </table>
        </form> <!-- /.form -->
        </div> <!-- runaway-->
      </div> <!-- runaway-->
    </div><!-- main tabpage -->
  </div>
</div>
</div>
</div>
  </div>
</div>
</div>
</div>
</div>

<!-- panel body style -->
<script>
  $(document).ready(function() {

    $(".car_ment").click(function(event) {
      event.preventDefault();
      var st = $(this).attr("st");
      var carid = $(this).attr("mycar");
      // alert(st+" "+carid);
      $.ajax({
        url: "actions/day_action.php",
        method: "POST",
        data: {
          st: st,
          carid: carid
        },
        dataType: "json",
        success: function(data) {
          // alert(data);
          $("#tstandby").html(data.standby);
          $("#tworkshope").html(data.workshope);
          $("#tservice").html(data.service);
          $("#trunaway").html(data.runaway);
        },
        error: function(result) {
          // alert(result.statusText);
        }
      });
    }) //end of car monvement  ajax code

    $("#contt_btn").click(function() {
      event.preventDefault();
      var contract_id = $("#id2").val();
      if (contract_id != "") {
        $.ajax({
          url: "rent/contt_info.php",
          method: "POST",
          data: {
            contract_id: contract_id
          },
          dataType: "json",
          success: function(data) {
            $('#contt_info').css('display', 'block');
            $("#cust_name").val(data.cust_name);
            $("#phone").val(data.phone);
            $("#phone2").val(data.phone2);
            $("#kind").val(data.kind);
            $("#numbers").val(data.numbers);
            $("#model").val(data.model);
          },
          error: function(result) {
            alert(result.statusText);
          }
        });
      }
    }); 
    $("#sanad_box").click(function(event) {
      event.preventDefault();
      $("#money_box_msg").html("");
      $.ajax({
        url: "rent/money_box.php",
        method: "POST",
        data: $("#form_box").serialize(),
        dataType: "json",
        success: function(data) {
          if (data.error) {
            $("#money_box_msg").html(data.error.money_box);
          } else {
            $('#sanad_box').attr("type", "hidden");
            $('#box_print').css("display", "block");
            $("#box_msg").html(data.msg);
            $("#box_id").val(data.box_id);
            $("#box_print").attr("href", "rent/print/boxPrint.php?box=" + data.box_id);
            $("#box_msg").delay(2500).slideUp(function() {
              $("#box_msg").html('');
            });
          }
        }
      });
    }); //end of boxmoney button add sanad box

    $("#trans").click(function(event) {
      event.preventDefault();
      var myday = $("#myday").val();
      window.open("rent/day_transaction.php?dd=" + myday, '_blank');
    });
    $("#box_trns").click(function(event) {
      event.preventDefault();
      var myday = $("#myday").val();
      window.open("rent/box_statement.php?dd=" + myday, '_blank');
    });

    
    $(".car_want").click(function(event) {
      event.preventDefault();
      var myreq = $(this).attr("myreq");
      var mycar = $(this).attr("mycar");
      window.open("a/ment_b_r.php?id=" + mycar +"&req="+myreq, '_blank');
    });
   
    $("#month_bank").click(function(event) {
      event.preventDefault();
      var myday = $("#myday").val();
      window.open("rent/month_bank.php?dd=" + myday, '_blank');
    });
    $("#month_cash").click(function(event) {
      event.preventDefault();
      var myday = $("#myday").val();
      window.open("rent/month_cash.php?dd=" + myday, '_blank');
    });

  }) //end off page code
</script>