<div  class="panel panel-primary" id="main_supp">
  <?php 
  require_once "../magic/DataBase.php";
 $database = new DataBase();
 $conn = $database->getConnection();
 ?>

  <div class="panel-heading clearfix"style="font-size:1.2em;">
  <div class="row" style="padding:4px;">
      <div class="col-md-3"style="float:right;">
        <label class="control-label"style="float:right;font-size: 16px">بحث و تعديل اسعار السيارات </label>
      </div>
      <div class="col-md-1"style="float:left;">
      <a id="nn_sr" href=""target="_blank"><button type="button" style="float:right;  margin-right:8px; color: #000 ;" class="btn btn-success">تحديث</button></a>
    </div>
    <div class="col-md-2"style="float:left;">
    <input type="text" id="nn" name="nn" class="form-control" name="nn" style="float:left; margin-right:8px; color: #000 ;" placeholder="ابحث بالرقم">
  </div>
    </div>
    <br />
<form id="priceForm" method="post"  class="form-horizontal" style="float:center;">
    <div class="row" style="padding:4px;">
        <div class="col-md-1"style="float:right;padding:2px;">
        </div>
        <div class="col-md-3"style="float:right;padding:2px;">
          <div class="input-group">
           <span class="input-group-addon" style="font-size: 14px;font-weight:bold;background:transparent;color:black;" >موديل</span>
           <select  class="form-control" id="model" name="model">
           <option value= "0" >جميع الموديلات</option>
           <?php
           $sql3 =$conn->prepare("SELECT DISTINCT model from cars where car_job_id < 2  ORDER BY model DESC") ;
           $sql3->execute();
           if($sql3->rowCount()>0)
           {
            while($row = $sql3->fetch()){
                  $id     = $row["model"];
                  $label  = $row["model"];
           echo "<option value= $id >$label</option>";
              }} ?>
           </select>
         </div>
        </div>
          <div class="col-md-3"style="float:right;padding:2px;">
            <div class="input-group">
             <span class="input-group-addon" style="font-size: 14px;font-weight:bold;background:transparent;color:black;" >نوع السيارة</span>
             <select  class="form-control" id="kind" name="kind" onchange="send()">
             <option value= "0" >جميع الأنواع</option>
             <?php
             $sql3 =$conn->prepare("SELECT DISTINCT kind from cars where car_job_id < 2 ORDER BY kind") ;
             $sql3->execute();
             if($sql3->rowCount()>0)
             {
              while($row = $sql3->fetch()){
                    $id     = $row["kind"];
                    $label  = $row["kind"];
             echo "<option value='$id' >$label</option>";
                }} ?>
             </select>
           </div>
          </div>
          <div class="col-md-2"style="float:right;padding:2px;margin-right:20px;">
            <button class="btn btn-danger btn-block" id="search">بحث </button>
          </div>
          <!-- <div class="col-md-1"style="float:right;padding:2px;margin-right:20px;">
            <button class="btn btn-success btn-block" id="rep">تقرير</button>
          </div> -->
        </div>
        <div class="row" style="padding:4px;">
            <div class="col-md-2"style="float:right;">
              <input type="number" id="costday" name="costday" class="form-control"style="float:right; margin-right:8px; color: #000 ;" placeholder="يومي ">
            </div>
            <div class="col-md-2"style="float:right;">
              <input type="number" id="monthly" name="monthly" class="form-control"style="float:right; margin-right:8px; color: #000 ;" placeholder="اسبوعي">
            </div>
            <div class="col-md-3"style="float:right;">
              <input type="number" id="month_cost" name="month_cost" class="form-control"style="float:right; margin-right:8px; color: #000 ;" placeholder="شهري شامل">
            </div>
            <div class="col-md-2"style="float:right;padding:2px;margin-right:20px;">
            <button class="btn btn-warning btn-block" id="updatePrices">تحديث أسعار </button>
          </div>
        </div>
</form>
    </div>
  <div class="panel-body"  style="background-color:#00324d;color:#cce5ff;">
    <div class="table-responsive">
         <table class="table table-bordered table-hover">
           <thead>
              <tr>
                   <th class="text-center" width="4%">م</th>
                   <th class="text-center" width="6%">حركة</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="6%">الموديل</th>
                   <th class="text-center" width="6%">يومي</th>
                   <th class="text-center" width="6%">اسبوعي</th>
                   <th class="text-center" width="6%">الحد الأدني</th>
                   <th class="text-center" width="9%">الشهري شامل</th>
                   <th class="text-center" width="7%">التارجت</th>
              </tr>
        </thead>
        <tbody id="myData">
                <?php
                  $output = '';
                  $sql =$conn->prepare("SELECT carid,kind,concat(letters ,' ',numbers) as numbers,b_name,model,costday,monthly,minn,	month_cost ,target , carstatus
                  from cars , branches 
                  where cars.branch = branches.branchID 
                  AND cars.car_job_id < 2
                  order by model DESC ");
                  $sql->execute();
                   if($sql->rowCount()>0)
                  { $nn = 0 ;
                     while($row = $sql->fetch())
                       {
                         $style ="";
                        if($row['carstatus'] > 1){
                          $style = 'style="color:#420000; background-color:#c8e6c9;font-weight:bolder;"';
                        }
                         $nn++ ;
            $output .= '<tr> <td '.$style.' >' . $nn . '</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>
                            <td class="text-center">' . $row['kind'] . '</td>
                            <td>' . $row['numbers'] . '</td>
                            <td>' . $row['b_name'] . '</td>
                            <td>' . $row['model'] . '</td>
                            <td class="text-center" >' . $row['costday'] . '</td>
                            <td class="text-center" >' . $row['monthly'] . '</td>
                            <td class="text-center" >' . $row['minn'] . '</td>
                            <td class="text-center" >' . $row['month_cost'] . '</td>
                            <td class="text-center" >' . $row['target'] . '</td>
                          </tr>';
                       }
                  }
        else {  $output .= '<tr><td colspan="11">لا يوجد سجلات ...</td></tr>'; }
                  echo $output;
                  ?>
              </tbody>
						<tfoot></tfoot>
          </table>
    </div>
</div>
</div>
<!-- panel body style -->
<?php
$outcar = array();
    $cars_sr = $conn->prepare("SELECT carid , concat(letters,' ', numbers) as plate FROM cars WHERE model > 2019 ORDER by numbers ");
    $cars_sr->execute();
    $i=0;
    if($cars_sr->rowCount()>0)
      {
         while($row = $cars_sr->fetch())
         { $outcar[$i] = array(
                           "value" => $row["plate"],
                           "data" => $row["carid"]
                           );$i++ ;
           }  }
    else {  $outcar[0]= 'لا يوجد '; }
?>
<script>
$(document).ready(function(){

  $("#search").click(function (event) {
  event.preventDefault();

  // احصل على القيم بشكل صحيح
  let search_kind = document.getElementById('kind').value;
  let search_model = document.getElementById('model').value;

  $.ajax({
    url: "actions/cars24Update.php",
    method: "POST",
    data: {
      search_kind: search_kind,
      search_model: search_model
    },
    success: function (data) {
      $('#myData').html(data);
    },
    error: function (xhr, status, error) {
      console.error("حدث خطأ في الاتصال:", error);
      alert("حدث خطأ أثناء جلب البيانات ❌");
    }
  });
});



 $("#updatePrices").click(function(event){
          event.preventDefault();
          valid = true;
    const form = document.getElementById('priceForm');
    const inputs = form.querySelectorAll('input, select'); // كل الحقول داخل النموذج

    // إزالة التنسيقات القديمة إن وجدت
    inputs.forEach(el => {
      el.style.border = '';
    });

    // فحص الحقول
    inputs.forEach(el => {
      if (el.type !== 'button' && el.type !== 'submit' && el.value.trim()<1) {
        valid = false;
        el.style.border = '2px solid red';
      }
    });

    if (!valid) {
       event.preventDefault();
      alert('يجب ملء كل الحقول ❗');
      return
    }
      $.ajax({
      url       :"actions/cars24Update.php",
      method  	: "POST",
      data    	:	$("#priceForm").serialize(),
      success 	: function(data){
       $('#myData').html(data);
     }});
});
// $("#rep").click(function(event){
//           event.preventDefault();
//     var b = $("#branch").val();
//     var c = $("#car_job_id").val();
//     var m = $("#model").val();
//     var w = $("#woner").val();
//     var k = $("#kind").val();
//   window.open("acc/cars_rep.php?b="+b+"&c="+c+"&m="+m+"&w="+w+"&k="+k   , '_blank');
//     });

var arr=  JSON.parse('<?php echo json_encode($outcar) ; ?>');
$('#nn').autocomplete({
 lookup: arr,
 maxHeight : 200,
 onSelect: function(suggestion) {
  $('#nn_sr').attr("href","a/car_update.php?id="+suggestion.data);
      //  one_invoice(suggestion.data);
       },
  })

});

</script>
