<?php require_once "../magic/session_config.php";
bdr_start_session();
// ob_start();
$branch = $_SESSION['branch'];
// include "../actions/dbPDO.php";
include "../op/wanted.php";
require_once "../magic/DataBase.php";
$dataBase = new DataBase();
$conn = $dataBase->getConnection();

$output = '';
$nn = 0;
$sql = $conn->prepare("SELECT customers.cust_name ,customers.phone,cars.kind , cars.numbers , cars.model , contracts.day_cost , contracts.tamm_expire ,
         contracts.contract_id , contracts.date_out , contracts.carcheck
         from customers , contracts , cars
         WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
          AND contracts.cont_status = 1 order by contracts.contract_id");
$sql->execute();
if ($sql->rowCount() > 0) {
  $tot_wanted = 0;
  $backRed = "color:#DA2D07; background-color:#F0F7E4;font-weight:bold;";
  while ($row = $sql->fetch()) {
    $nn++;
    $contract_id = $row['contract_id'];
    $wanted = new  Wanted($conn, $contract_id);
    $pay = $wanted->wanted();
    $ww  = $pay["wanted"];
    $late = $pay["late"];
    $date1 = new DateTime($row['tamm_expire']);
    $date2 = new DateTime(date("Y-m-d"));
    $qty = $date2->diff($date1)->format("%a");
    $carcheck = $row['carcheck'];
    $style_late = '' ;
    if ($late > 40) {
      $style = 'style="color:#DA2D07; background-color:#d0d0d0;font-weight:bold;"';
      $style_late = $style ;
    } else if (($late < 40) && ($late > 6)) {
      $style = 'style="color:#420000; background-color:#F6E9CA;font-weight:bold;"';
    } else {
      $style = '';
    }

    $output .= '<tr>';
    if (!empty($carcheck)) {
      $output .= '<td>' . $nn . '</td>';
    } else {
      $output .= '<td style="color:#DA2D07; background-color:#F0F7E4;font-weight:bold;">' . $nn . '</td>';
    }

    $output .= '<td>
               <a href="acc/cont2close.php?id=' . $contract_id . '"target="_blank"><button type="button" style="font-weight:bold ; font-size:14px; padding:2;" class="btn btn-danger btn-xs btn-block ">' . $contract_id . '</button></a>
               </td> <td>' . $row['cust_name'] . '</td>
               <td ' . $style . '>' . $ww . '</td>
               <td>' . $row['phone'] . '</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 ' . $style_late . ' >' . $late . '</td>
                <td class="text-center" >' . $row['day_cost'] . '</td>';

    if (($qty < 8) || ($date1 < $date2)) {
      $output .= '    <td class="text-center" style="color:#DA2D07;background-color:#F0F7E4;" >' . $row['tamm_expire'] . '</td>   </tr>';
    } else {
      $output .= '    <td class="text-center">' . $row['tamm_expire'] . '</td>  </tr>';
    }
    if ($ww > 0) {
      $tot_wanted += $ww;
    }
  }
} else {
  $output .= '<tr><td colspan="12">لا يوجد سجلات ...</td></tr>';
}
?>
<div class="panel panel-primary" id="main_supp">
  <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: 15px"> متابعة العقود المفتوحة </label>
      </div>
      <div class="col-md-3" style="float:right;">
        <label class="control-label" style="float:right;font-size: 15px"> إجمالي مستحق <span class="badge"><?php echo $tot_wanted; ?></span></label>
      </div>
      <div class="col-xs-2 col-md-2" 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-xs-2 col-md-2" style="float:left;">
        <input type="search" id="nn" name="nn" class="form-control" name="nn" style="float:left; margin-right:8px; color: #000 ;" placeholder="ابحث بالرقم">
      </div>
    </div>
    <form id="update7" method="post" class="form-horizontal" style="float:center;">
      <div class="row" style="padding:4px;">
        <div class="col-xs-2 col-md-2" style="float:right;padding:2px;">
          <label for="br" class="control-label" style="float:right;margin:5px;">فرع<span style="float:right;margin-left:10px;"><button id="br_a" type="button" class="btn btn-primary btn-xs "><i class="fa fa-search" style="font-size: 14px;font-weight:bold;color:#ff9966;" aria-hidden="true"></i></button></span></label>
          <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-4 col-md-3" style="float:right;padding:2px;">
          <label for="namm" class="control-label" style="float:right;margin:5px;"> باسم المستاجر <span style="float:right;margin-left:10px;"><button id="namm_a" type="button" class="btn btn-primary btn-xs "><i class="fa fa-search" style="font-size: 14px;font-weight:bold;color:#ff9966;" aria-hidden="true"></i></button></span></label>
          <input type="text" class="form-control" style="float:right;" id="namm" name="namm" placeholder="اسم المستأجر" />
        </div>
        <div class="col-xs-1 col-md-1" style="float:right;padding:2px;">
          <label for="numb" class="control-label" style="float:right;margin:5px;">برقم <span style="float:right;margin-left:10px;"><button id="numb_a" type="button" class="btn btn-primary btn-xs "><i class="fa fa-search" style="font-size: 14px;font-weight:bold;color:#ff9966;" aria-hidden="true"></i></button></span></label>
          <input type="number" class="form-control" style="float:right;" id="numb" name="numb" placeholder="الموديل" />
        </div>
        <div class="col-xs-2 col-md-2" style="float:right;padding:2px;">
          <label for="datt" class="control-label" style="float:right;margin:5px;">من تاريخ<span style="float:right;margin-left:10px;"><button id="datt_a" type="button" class="btn btn-primary btn-xs "><i class="fa fa-search" style="font-size: 14px;font-weight:bold;color:#ff9966;" aria-hidden="true"></i></button></span></label>
          <input type="text" class="form-control" style="float:right;" id="datt" name="datt" placeholder="فتح عقد" readonly />
          <script>
            $(function() {
              var calendar = $.calendars.instance('');
              $('#datt').calendarsPicker({
                calendar: calendar
              });
            });
          </script>
        </div>
        <div class="col-xs-2 col-md-2" style="float:right;padding:2px;">
          <label for="ad" class="control-label" style="float:right;margin:5px;">نوع السيارة <span style="float:right;margin-left:10px;"><button id="ad_a" type="button" class="btn btn-primary btn-xs "><i class="fa fa-search" style="font-size: 14px;font-weight:bold;color:#ff9966;" aria-hidden="true"></i></button></span></label>
          <input type="text" class="form-control" style="float:right;" id="ad" name="ad" placeholder="جاري التحديث" />
        </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="7%">رقم العقد</th>
            <th class="text-center" width="28%">الإسم</th>
            <th class="text-center" width="5%">مستحق</th>
            <th class="text-center" width="8%">الجوال</th>
            <th class="text-center" width="9%">فتح العقد</th>
            <th class="text-center" width="8%">طراز</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="4%">يومي</th>
            <th class="text-center" width="9%">التفويض</th>
          </tr>
        </thead>
        <tbody id="myData">
          <?php
          echo $output;
          ?>
        </tbody>
        <tfoot></tfoot>
      </table>
    </div>
  </div>
</div>
<!-- panel body style -->
<?php
//  include "../actions/dbPDO.php";
$outcont = array();
$cont_sr = $conn->prepare("SELECT contracts.contract_id , concat(contracts.contract_id,' ', cars.numbers) as ko  from cars , contracts
                            where contracts.carid = cars.carid AND contracts.cont_status = 1 ");
$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", "acc/cont2close.php?id=" + suggestion.data);
        //  one_invoice(suggestion.data);
      },
    });
    //branches
    $("#br_a").click(function(event) {
      event.preventDefault();
      var br = $("#br").val();
      $.ajax({
        url: "acc/action_flow.php",
        method: "POST",
        data: {
          br: br
        },
        success: function(data) {
          $("#myData").html(data);
        },
        error: function(result) {
          alert(result.statusText);
        }
      });
    });

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

    $("#namm_a").click(function(event) {
      event.preventDefault();
      var name = $("#namm").val();
      $.ajax({
        url: "acc/action_flow.php",
        method: "POST",
        data: {
          name: name
        },
        success: function(data) {
          $("#myData").html(data);
        },
        error: function(result) {
          alert(result.statusText);
        }
      });
    });
    $("#numb_a").click(function(event) {
      event.preventDefault();
      var numb = $("#numb").val();
      $.ajax({
        url: "acc/action_flow.php",
        method: "POST",
        data: {
          numb: numb
        },
        success: function(data) {
          $("#myData").html(data);
        },
        error: function(result) {
          alert(result.statusText);
        }
      });
    });

    $("#datt_a").click(function(event) {
      event.preventDefault();
      var datt = $("#datt").val();
      if (datt.length = 0) {
        var d = new Date();
      }
      $.ajax({
        url: "acc/action_flow.php",
        method: "POST",
        data: {
          datt: datt
        },
        success: function(data) {
          $("#myData").html(data);
        },
        error: function(result) {
          alert(result.statusText);
        }
      });
    });
  });
</script>