<div  class="panel panel-danger" id="main_supp">
  <div class="panel-heading clearfix"style="font-size:1.2em;">
  <div class="row" style="padding:4px;">
      <div class="col-xs-4 col-md-3"style="float:right;">
        <label class="control-label"style="float:right;font-size: 15px"> فحص العقود </label>
      </div>
      <div class="col-md-3"style="float:left;">
        <div class="input-group">
         <span class="input-group-addon" style="font-size: 14px;font-weight:bold;background:transparent;color:black;" >عقد رقم</span>
         <input type="text" id="nn" name="nn" class="form-control" name="nn" style="float:left; margin-right:8px; color: #000 ;" placeholder="رقم العقد">
         <span class="input-group-btn">
         <a id="nn_sr" href=""target="_blank"><button  type="button" class="btn btn-primary btn-xs " style="float:right; color: #000 ;"><i class="fa fa-search" style="font-size: 22px;font-weight:bold;color:#ff9966; padding:3px; margin:0px;" aria-hidden="true"></i></button></a>
         </span>
       </div><!-- /input-group -->
  </div>
  </div>
  <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="br" name="br">
         <option value= "0" >جميع الفروع </option>
         <?php
         $end   = date("Y-m-d");
         $start = date('Y-m-d', strtotime($end. ' - 90 days'));
        require_once "../magic/DataBase.php";
        require_once "../op/wanted.php";

        $dataBase = new DataBase();
        $conn = $dataBase->getConnection();
        

         $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>
         <span class="input-group-btn">
           <button id="br_a"  type="button" class="btn btn-primary btn-xs "><i class="fa fa-search" style="font-size: 22px;font-weight:bold;color:#ff9966; padding:3px; margin:0px;" aria-hidden="true"></i></button>
         </span>
       </div><!-- /input-group -->
      </div>
      <div class="col-md-2"style="float:right;padding:2px;margin-right:10px;">
      <div class="input-group">
       <span class="input-group-addon" style="font-size: 14px;font-weight:bold;background:transparent;color:black;" >من</span>
       <input type="text" class="form-control" style="float:right;" id="start" name="start" placeholder="إقفال عقد " value="<?php echo $start ; ?>" readonly />
     </div><!-- /input-group -->
     <script>
       $(function() {
         var calendar = $.calendars.instance('');
         $('#start').calendarsPicker({calendar: calendar});
         $('#end').calendarsPicker({calendar: calendar});
         });
     </script>
   </div>
   <div class="col-md-3"style="float:right;padding:2px;">
     <div class="input-group">
      <span class="input-group-addon" style="font-size: 16px;font-weight:bold;background:transparent;color:black;" >إلي </span>
      <input type="text" class="form-control" style="float:right;" id="end" name="end" placeholder="إقفال عقد" value="<?php echo $end ; ?>" readonly />
      <span class="input-group-btn">
        <button id="datt_a" type="button" class="btn btn-primary btn-xs "><i class="fa fa-search" style="font-size: 22px;font-weight:bold;color:#ff9966; padding:3px; margin:0px;" aria-hidden="true"></i></button>
      </span>
    </div><!-- /input-group -->
  </div>
  <div class="col-md-2">
  <!-- <a href="./jaudge/checker_rep.php" target="_blank"><button type="button" class="btn btn-success btn-block">كشف حصر </button></a> -->
  </div>
    </div>
    <form id="update7" 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-4"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>
                 <input type="text" class="form-control" style="float:right;" id="namm" name="namm" placeholder="اسم او رقم هوية"/>
                 <span class="input-group-btn">
                   <button id="namm_a" type="button" class="btn btn-primary btn-xs "><i class="fa fa-search" style="font-size: 22px;font-weight:bold;color:#ff9966; padding:3px; margin:0px;" aria-hidden="true"></i></button>
                 </span>
               </div><!-- /input-group -->
              </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>
                 <input type="number" class="form-control" style="float:right;" id="model" name="model"  placeholder="السيارة"/>
                 <span class="input-group-btn">
                   <button id="model_btn" type="button" class="btn btn-primary btn-xs " ><i class="fa fa-search" style="font-size: 22px;font-weight:bold;color:#ff9966; padding:3px; margin:0px;" aria-hidden="true"></i></button>
                 </span>
               </div><!-- /input-group -->
              </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>
               <input type="text" class="form-control" style="float:right;" id="ad" name="ad"  placeholder="السيارة"/>
               <span class="input-group-btn">
                 <button id="ad_a" type="button" class="btn btn-primary btn-xs "><i class="fa fa-search" style="font-size: 22px;font-weight:bold;color:#ff9966; padding:3px; margin:0px;" aria-hidden="true"></i></button>
               </span>
             </div><!-- /input-group -->
            </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="8%">رقم العقد</th>
                   <th class="text-center" width="24%">الإسم</th>
                   <th class="text-center" width="8%">متأخرات</th>
                   <th class="text-center" width="10%">الجوال</th>
                   <th class="text-center" width="7%">ايام</th>
                   <th class="text-center" width="9%">فتح</th>
                   <th class="text-center" width="8%">طراز</th>
                   <th class="text-center" width="7%">رقم</th>
                   <th class="text-center" width="7%">موديل</th>
                   <th class="text-center" width="8%">إضافي</th>
              </tr>
            </thead>
              <tbody id="myData">
                <?php
                 
           $output = '';
           $sql =$conn->prepare("SELECT customers.cust_name ,customers.phone , cars.kind , cars.numbers , cars.model ,
                     contracts.contract_id , contracts.date_out , contracts.days_qty , contracts.addtion_cost , contracts.violations , contracts.ins_claim
                      from customers , contracts , cars
                     WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid AND contracts.cont_status = 1
                            order by contracts.date_out  DESC, contracts.contract_id ");
            $sql->execute();
              if($sql->rowCount()>0)
                  { $nn = 1;
                while($row = $sql->fetch())
                     {
                       $ss        = $row['contract_id'];
                      $wanted = new  Wanted($conn,$ss) ;
                      $pay    = $wanted->wanted() ;
                      $ww     = $pay["wanted"] ;

                       $tot_add   = $pay["tot_addtion"] ;
                       $days_qty  =  $pay["days_qty"] ;
                    if($ww < 200){ $output .='<tr class="bg-success" style="color:black;  border: 0.5px solid #000000;">'; } else { $output .= "<tr>" ;}
                          $output .= '
                                    <td>'.$nn.'</td>
                                    <td>
                                    <a href="ment/cont2collect.php?id=' . $ss . '"target="_blank"><button type="button" style="font-weight:bold ; font-size:14px; padding:2;" class="btn btn-danger btn-xs btn-block ">' . $row['contract_id'] . '</button></a>
                                    </td>
                                    <td>' . $row['cust_name'] . '</td>
                                    <td>' . $ww . '</td>
                                    <td>' . $row['phone'] . '</td>
                                    <td class="text-center" >' . $days_qty . '</td>
                                    <td class = "table-info">' . $row['date_out'] . '</td>
                                    <td class="text-center" >' . $row['kind'] . '</td>
                                    <td class="text-center" >' . $row['numbers'] . '</td>
                                    <td class="text-center" >' . $row['model'] . '</td>
                                    <td class="text-center" >' . $tot_add . '</td>
                                 </tr>  ';
                                 $nn++ ;
                     }}
                  else
                  {    $output .= '<tr><td colspan="11">لا يوجد سجلات ...</td></tr>';       }
                  echo $output;
                  ?>
              </tbody>
          </table>
    </div>
</div>
</div>
<?php
$outcont = array();
    $cont_sr = $conn->prepare("SELECT contracts.contract_id , concat(contracts.contract_id,' ', cars.numbers) as ko  
            from contracts , cars
             where contracts.carid = cars.carid AND contracts.date_out >'2025-07-29'");
    $cont_sr->execute();
    $i= 0;
    if($cont_sr->rowCount()>0)
      {
         while($row = $cont_sr->fetch())
         {
             $outcont[$i] = array(
                           "value" => $row["ko"],
                           "data" => $row["contract_id"]
                           );    $i++ ;
           }
        }
  else{$outcont[0]= 'لا يوجد '; }
?>
<script>
$(document).ready(function(){
  var arr=  JSON.parse('<?php echo json_encode($outcont) ; ?>');
  $('#nn').autocomplete({
   lookup: arr,
   maxHeight : 200,
   onSelect: function(suggestion) {
    $('#nn_sr').attr("href","ment/cont2collect.php?id=" + suggestion.data);
         },
    }) ;
  $("#nn_sr").click(function(event){
    event.preventDefault();
    var nn = $("#nn").val();
      window.open("ment/cont2collect.php?id="+nn, '_blank');
  });

    $("#br_a").click(function(event){
        event.preventDefault();
        var branch = $("#br").val();
        var start  = $("#start").val();
        var end    = $("#end").val();
    $.ajax({
          url     : "jaudge/action_checker.php",
          method  : "POST",
           data    :{branch:branch,start:start,end:end},
          success : function(data){ $("#myData").html(data); },
          error   :function(result){
            alert(result.statusText);
            }
        });
      });

    $("#ad_a").click(function(event){
        event.preventDefault();
        var ad_a  = $("#ad").val();
        var br    = $("#br").val();
        var start = $("#start").val();
        var end   = $("#end").val();
    $.ajax({
          url     : "jaudge/action_checker.php",
          method  : "POST",
           data    :{ad_a:ad_a,br:br,start:start,end:end},
          success : function(data){ $("#myData").html(data); },
          error   :function(result){
            alert(result.statusText);
            }
        });
      });

      $("#cont_debt_print").click(function(event){
          event.preventDefault();
          var start = $("#start").val();
          var end   = $("#end").val();
      window.open("acc/cont_debt_print.php?start="+start+"&end="+end, '_blank');
        });

      $("#namm_a").click(function(event){
          event.preventDefault();
          var name = $("#namm").val();
          var br    = $("#br").val();
          var start = $("#start").val();
          var end   = $("#end").val();
     $.ajax({
            url     : "jaudge/action_checker.php",
            method  : "POST",
             data    :{name:name,br:br,start:start,end:end},
            success : function(data){ $("#myData").html(data); },
            error   :function(result){
              alert(result.statusText);
              }
          });
        });
        $("#model_btn").click(function(event){
            event.preventDefault();
            var model = $("#model").val();
            var br    = $("#br").val();
            var start = $("#start").val();
            var end   = $("#end").val();
       $.ajax({
              url     : "jaudge/action_checker.php",
              method  : "POST",
               data    :{model:model,br:br,start:start,end:end},
              success : function(data){ $("#myData").html(data); },
              error   :function(result){
                alert(result.statusText);
                }
            });
          });

      $("#datt_a").click(function(event){
          event.preventDefault();
          var datt    = $("#br").val();
          var start = $("#start").val();
          var end   = $("#end").val();
     $.ajax({
            url     : "jaudge/action_checker.php",
            method  : "POST",
             data    :{datt:datt,start:start,end:end},
            success : function(data){ $("#myData").html(data); },
            error   :function(result){
              alert(result.statusText);
              }
          });
        });
});

</script>
