<?php
require_once("../actions/dbPDO.php");
require_once('../op/paginator.php');
$query = "SELECT customers.cust_name ,vip.custid,vip.totmoney, vip.addition , vip.dayQty , vip.wanted , 
                vip.carskind , vip.model ,vip.cust_level , vip.cust_status
            from customers , vip where vip.custid = customers.cust_id AND cust_level = 6
           ORDER BY totmoney DESC" ;

//these variables are passed via URL
$limit = (isset($_REQUEST['limit']))? $_REQUEST['limit']: 15 ; // row per page
$page = (isset($_REQUEST['page']) ) ? $_REQUEST['page']: 1 ; // starting page

$paginator = new Paginator($db , $query); //_constructor is called
$results = $paginator->getData($limit, $page);
$links = 10 ;
$output = "";
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta name="robots" content="index,follow">
	<link rel="icon" href="../images/perfect.ico">
	<title>عملاء VIP</title>
	<link rel="stylesheet" href="../dist/css/bootstrap.min.css" />
	<link rel="stylesheet" href="../css/font-awesome.min.css">
	<link rel="stylesheet" href="../css/myCar.css" />
	<script src="../js/jquery.js"></script>
	<script src="../dist/js/umd/popper.min.js"></script>
	<script src="../dist/js/bootstrap.js"></script>
</head>
<style>
  .table th{
    background-attachment: fixed;
    background-color: #888888;
    color:#ffffff;
  }
  .table.sticky th{
    position: sticky;
    top: 30px;
    
  }
  .table.sticky th::after{
    content:'';
    width: 100%;
    height: 2px;
    position: absolute;
    bottom:0;
    right: 0;
    background:black;
  }
  /* pagination */
  .pagination > .active > a , .pagination > .active > a:hover, .pagination > .active > a:focus {
    background-color: greenyellow;
    border-color:green;
}
</style>
<body>
<div class="container-fluid">
  <?php
    $data = $results->data; 
  ?>
 
    <br /><br />
<div class="form-row" >
  <div class="col">
    <div class="table-responsive">
         <table class="table table-bordered table-hover sticky">
           <thead>
              <tr>
                   <th class="text-center" width="5%">م</th>
                   <th class="text-center" width="25%">الاسم</th>
                   <th class="text-center" width="8%">المدفوعات</th>
                   <th class="text-center" width="6%">إجمالي ايام</th>
                   <th class="text-center" width="8%">تعويضات</th>
                   <th class="text-center" width="6%">مديونية</th>
                   <th class="text-center" width="20%">السيارات</th>
                   <th class="text-center" width="15%">الموديلات</th>
                   <th class="text-center" width="8%">تصنيف</th>
                   <th class="text-center" width="6%">الحالة</th>
              </tr>
            </thead>
                <?php
      if ($results->total > 0 )
            {
            //   $members = $data['data'];
              $no = $results->row_start + 1;
              $master = 1 ;
              $output = "";
        foreach($data as $row)
              {
                $cust_status  = $row['cust_status'];
                $custid       = $row['custid'];
            switch ($cust_status) {
                  case 1:
                      $dd = '<button class="btn btn-success btn-sm btn-block active " act="' . $custid . '" style="font-weight:bold ;font-size:14px; padding:2;" >نشط</button>';
                    break;
                  case 2:
                      $dd = '<button class="btn btn-danger btn-sm btn-block cutt " cut="' . $custid . '" style="font-weight:bold ;font-size:14px; padding:2;" >معطل </button>';
                  default:
                      $dd = '<button class="btn btn-danger btn-sm btn-block cutt " cut="' . $custid . '" style="font-weight:bold ;font-size:14px; padding:2;" >معطل </button>';
                    break;
                }
                // customer level
                $level = $row['cust_level'];
                $options ='';
                $sql = $db->prepare("SELECT id , cust_level from levels");
                $sql->execute();
                if ($sql->rowCount() > 0) {
                  while ($ro = $sql->fetch()) {
                    $id           = $ro["id"];
                    $cust_level   = $ro["cust_level"];
                if ($id == $level) {
                    $options.='<option selected="selected" value="'. $id .'" >' . $cust_level . '</option>';
                    } else {
                      $options.= "<option value= $id >$cust_level</option>";
                    }
                  }
                } 
        if($master > 0){
          $output .= '<tr><td>
                            <a href="../acc/cust_update.php?id=' .  $custid . '"target="_blank"><button type="button" style="font-weight:bold ; font-size:14px; padding:2;" class="btn btn-success btn-xs btn-block ">'.$no.'</button></a>
                          </td>' ;
                         }else{
                  $output .= '
                            <tr><td>
                                <a href="../a/cust_update.php?id=' .  $custid . '"target="_blank"><button type="button" style="font-weight:bold ; font-size:14px; padding:2;" class="btn btn-success btn-xs btn-block ">'.$no.'</button></a>
                            </td>' ;
                         }
                  $output .= '
                              <td >' . $row['cust_name'] . '</td>
                              <td >' . $row['totmoney'] . '</td>
                              <td >' . $row['dayQty'] . '</td>
                              <td >' . $row['addition'] . '</td>
                              <td >' . $row['wanted'] . '</td>
                              <td class="text-center" >' . $row['carskind'] . '</td>
                              <td class="text-center">' . $row['model'] . '</td>
                              <td contenteditable="true"><select id="'.$custid.'" class="form-control changelevel" style="background-color:transparent;padding:3px;margine:0px;" >
							                ' . $options . '
							                  </select></td>
                              <td>' . $dd . '</td>
                          </tr>';
                 $no++ ;
              }
            }
        else
            { $output .= '<tr><td colspan="10"><h3>لا يوجد سجلات ...</h3></td></tr>';   }

        echo $output;
                  ?>
              </tbody>
						<tfoot></tfoot>
          </table>
</div>
</div>
</div>
<div class="form-row" style="width:100%; position: fixed; top: 0px;background:white;">
    <div class="col-10">
    <?php 
    echo $paginator->createLinks($links,'pagination page-link');
    ?>
    </div>
  <div class="col-2">
  <button type="button" style="margin-left:15px; color: #000 ;" id="new_members_btn" class="btn btn-success  float-left">تصفية جديدة</button>
  </div>
    </div>

</body>
<script>
  $(document).ready(function(){
          $("#new_members_btn").click(function(event){
          event.preventDefault();
          var action = "add";
     $.ajax({
            url     : "../customers/membership_action.php",
            method  : "POST",
             data    :{action:action},
            success : function(data){ $("#myTable").html(data); },
            error   :function(result){
              alert(result.statusText);
              }
          });
        });
      });

      $(".changelevel").change(function(event) {
				event.preventDefault();
				if (confirm("هل تريد تعديل مستوي المستأجر؟")) {
					var custid = $(this).attr('id');
					var level = $("#" + custid).val();

          var action = "changelevel";
					$.ajax({
						url: "../customers/membership_action.php",
						method: "POST",
						data: {
							custid: custid,
							level: level,
              action:action,
						},
						success: function(data) {
							location.reload();
						}
					});
				} else {
					return false;
				}
			}); 

</script>
</html>