<?php
  require_once "../magic/session_config.php";
  bdr_start_session();
      ob_start();
  $father = $_SESSION['user_id'];
   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");
    
    $currentCars   =  $carHistory->currentCars();
    $sanndy        =  $currentCars['sanndy'];
    $shop          =  $currentCars['shop'];
    $runn          =  $currentCars['runn'];
    $service       =  $currentCars['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  ment_request.done = 0");
      $want->execute();
      $wantedCars = $want->rowCount();
    ?>
<div  class="panel panel-primary">
  <style>
  .control-label , .table , .h6, h6 {
    color:#A9A9A7;
  }
  </style>
  <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 id="box_msg"></div>
          <div class="row">
              <div class="col-md-2"style="float:right;">
                <input type="text" class="form-control" style="float:right;" id="myday" name="myday"  value="<?php echo $today; ?>" readonly/>
              </div>
        <script>
              $(function() {
                var calendar = $.calendars.instance('');
                $('#myday').calendarsPicker({calendar: calendar});
                  });
        </script>
            <div class="col-md-2"style="float:left;">
              <button id="current_cars" type="button" style="font-weight:bold;float:right;" class="btn btn-success btn-block">سيارات الفروع<span class="glyphicon glyphicon-print" aria-hidden="true"></span></button>
            </div>
        </div>
    <div class="row" style="padding:3px;">
        <div class="col-xs-2 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-xs-2 col-md-2"style="float:right;">
    <button id="treasury" 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-xs-2 col-md-2"style="float:right;">
          <select  class="form-control" id="br" name="br">
         <option value= "" >الفرع</option>
       <?php
       $sql3 =$conn->prepare("SELECT branchID , b_name from branches where branchID > 1") ;
       $sql3->execute();
         if($sql3->rowCount()>0)
        {
           while($row = $sql3->fetch()){
                 $id     = $row["branchID"];
                 $label  = $row["b_name"];
       echo "<option value= $id >$label</option>";
             }} ?>
      </select>
        </div>
        <div class="col-xs-2 col-md-2"style="float:right;">
    <button id="br_day" 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-xs-2 col-md-2"style="float:left;">
      <button id="month_bank" type="button" style="font-weight:bold;float:right;" class="btn btn-success btn-block">المعاملات البنكية <span class="glyphicon glyphicon-print" aria-hidden="true"></span></button>
        </div>
        <div class="col-xs-2 col-md-2"style="float:left;">
      <button id="month_cash" type="button" style="font-weight:bold;float:right;" class="btn btn-success btn-block">الإيداعات النقدية<span class="glyphicon glyphicon-print" aria-hidden="true"></span></button>
        </div>
    </div>
  </form><!-- end of box form -->
</div>
<br /><br />
   <div class="container-fluid">
     <div class="row" style="padding:3px;">
       <div class="col-xs-12 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="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>
<?php
    $output = '';
     $bb = $conn->prepare("SELECT branchID , b_name  from branches  where branchID > 1 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 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);
              $sql->execute();
       if($sql->rowCount()> 0)
            { $dd = 1 ;
              
              while($row = $sql->fetch())
                  {
                    $style="";
              $mycar  = $row['carid'];
              $qty    = $carHistory->car_state($mycar);
            $area = $row['area'];
            if($area != $branch){$style="color:#DA2D07; background-color:#F0F7E4;font-weight:bold;";}

            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">' . $b_name. '</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="a/car_update.php?id=' . $row['carid'] . '"target="_blank"><button type="button" class="btn btn-success btn-xs ">تحديث</button></a>
                                </td>
                              </tr>  '; $dd = $dd + 1;
                }
             }
            else
            { $output .= '<tr><td colspan="9"> لا يوجد سيارات لفرع '.$b_name.'</td></tr>';  }
                                  }}
                                    echo $output;
                                    ?>
                                </tbody>
                              <tfoot></tfoot>
                            </table>
                       </form> <!-- /.form -->

                   </div>
            </div><!-- /standby -->
            <!-- // -->
    <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="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="10%">حركة</th>
                 </tr>
              </thead>
      <tbody>
      <?php
        $output2 = '';
        $bb =$conn->prepare("SELECT branchID , b_name  from branches  where branchID > 1 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 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");
        $sql->bindParam(1,$branch_d);
        $sql->execute();
        $row = $sql->rowCount();
          $sql->bindParam(1,$branch);
          $sql->execute();
      if($sql->rowCount()>0)
              {
            $dd = 1;
            $date1 = new DateTime(date("Y-m-d"));
          while($row = $sql->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");

              if($dd == 1 ){$output2 .= '<tr> <td style="color:#DA2D07; background-color:#F0F7E4;font-weight:bold;" >' . $dd. '</td>' ;}
                    else{ $output2 .= '<tr> <td>' . $dd. '</td>' ;}

                 $output2 .= '  <td class="text-center">' . $b_name. '</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 mycar=' . $mycar . '  myreq=' . $row['req_id'] . ' class="btn btn-success btn-xs btn-block car_want ">سبب الطلب</button>
                                </td>
                            </tr>';  $dd++ ;
                   }
                }
           else{   $output2 .= '<tr><td colspan="7"> لا يوجد سيارات لفرع '.$b_name.'</td></tr>';}
             }}
                  echo $output2;
             ?>
                          </tbody>
                        <tfoot></tfoot>
                      </table>
                 </form> <!-- /.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="15%">الفرع</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 = '';
                            $bb =$conn->prepare("SELECT branchID , b_name  from branches  where branchID > 1 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 .= '<tr>
                                                  <td class="text-center">' . $b_name. '</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="a/car_update.php?id=' . $row['carid'] . '"target="_blank"><button type="button" class="btn btn-success btn-xs ">تحديث</button></a>
                                                  </td>
                                               </tr>  ';
                                   }
                              }
                              else{   $output2 .= '<tr><td colspan="7"> لا يوجد سيارات لفرع '.$b_name.'</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="15%">الفرع</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 = '';
                      $bb =$conn->prepare("SELECT branchID , b_name  from branches  where branchID > 1 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 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 .= '   <tr>
                                            <td class="text-center">' . $b_name. '</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="a/car_update.php?id=' . $row['carid'] . '"target="_blank"><button type="button" class="btn btn-success btn-xs ">تحديث</button></a>
                                            </td>
                                         </tr>  ';
                             }
                        }
                        else
                        {
                             $output3 .= '<tr><td colspan="7"> لا يوجد سيارات لفرع '.$b_name.'</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="15%">الفرع</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 = '';
                $bb =$conn->prepare("SELECT branchID , b_name  from branches  where branchID > 1 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 .= ' <tr>
                                      <td class="text-center">' . $b_name. '</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="a/car_update.php?id=' . $row['carid'] . '"target="_blank"><button type="button" class="btn btn-success btn-xs ">تحديث</button></a>
                                      </td>
                                   </tr>  ';
                       }
                  }
                  else
                  {
                       $output4 .= '<tr>
                                       <td colspan="7"> لا يوجد سيارات لفرع '.$b_name.'</td>
                                   </tr>';
                  }}}
                  echo $output4;
                  ?>
              </tbody>
            <tfoot></tfoot>
          </table>
     </form> <!-- /.form -->

 </div>
</div><!-- /runaway -->
</div>
</div>
</div>
</div>
</div><!-- panel body style -->
</div>
<?php
    $master = $conn->prepare('SELECT role from users where user_id = ? ');
    $master->bindParam(1, $father);
    $master->execute();
    $ro = $master->fetch(PDO::FETCH_ASSOC);
    $role		  = $ro["role"];

    if($role < 3){
        require_once "branches_money.php" ;
    }else{
        null ;
    }
?>
<script>
$(document).ready(function(){
  $("#current_cars").click(function(event){
    event.preventDefault();
    var myday = $("#myday").val();
      window.open("acc/current_cars.php?dd="+myday, '_blank');
  });
$("#trans").click(function(event){
  event.preventDefault();
  var myday = $("#myday").val();
    window.open("rent/acc_daytrans.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');
    });
$("#treasury").click(function(event){
  event.preventDefault();
  var myday = $("#myday").val();
    window.open("rent/treasury.php?dd="+myday, '_blank');
});
$("#br_day").click(function(event){
  event.preventDefault();
  var myday = $("#myday").val();
  var br    = $("#br").val();
    window.open("rent/day_transaction.php?dd="+myday+"&br="+br, '_blank');
});

$("#month_bank").click(function(event){
  event.preventDefault();
  var myday = $("#myday").val();
  var br    = $("#br").val();
    window.open("rent/month_bank.php?dd="+myday+"&br="+br, '_blank');
});
$("#month_cash").click(function(event){
  event.preventDefault();
  var myday = $("#myday").val();
  var br    = $("#br").val();
    window.open("rent/month_cash.php?dd="+myday+"&br="+br, '_blank');
});

// $(".virtual_btn").click(function(event) {
// 				event.preventDefault();
//         let msg ;
//         var kind = $(this).attr('data');
//         if(kind == 1){
//           msg = "هل تريد نقل السيارة إلي الفرع الإفتراضي؟";
//         }else{
//           msg = "هل تريد نقل السيارة لفرعها الاصلي";
//         }
// 				if (confirm(msg)) {
// 					var id = $(this).attr('id');
//           var job = $(this).attr('job');
// 					$.ajax({
// 						url: "actions/car_update.php",
// 						method: "POST",
// 						data: {
// 							virtual: id,
// 							job: job
// 						},
// 						success: function(data) {
//               $('#site').load("includes/day_acc.php");
// 						}
// 					});
// 				} else {
// 					return false;
// 				}
// 			});
})//end off page code
</script>
