<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: 16px">بحث و تعديل صلاحيات الأجهزة</label>
      </div>
    </div>
    <br />
    <form id="auth_update" 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-2" 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="branch" name="branch">
              <option value="*">جميع الفروع </option>
              <?php
              include "../actions/dbPDO.php";
              $sql3 = $db->prepare("SELECT branchID , b_name from branches");
              $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>
        <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="*">جميع الأنواع</option>
              <?php
              $sql3 = $db->prepare("SELECT DISTINCT kind from cars where car_job_id < 6 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>
    </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="5%">كود</th>
            <th class="text-center" width="18%">الفرع</th>
            <th class="text-center" width="26%">المستخدم</th>
            <th class="text-center" width="3%">-</th>
            <th class="text-center" width="16%">نوع الجهاز</th>
            <th class="text-center" width="14%">اخر جلسة</th>
            <th class="text-center" width="10%">الحالة</th>
            <th class="text-center" width="8%">حذف</th>
          </tr>
        </thead>
        <tbody id="myData">
          <?php
          $output = '';
          $sql = $db->prepare("SELECT loginhistory.history_id,users.realName ,loginhistory.user_id , loginhistory.kind, loginhistory.active, loginhistory.default_branch ,
                           branches.b_name ,  loginhistory.last_login, loginhistory.default_user , loginhistory.staff_device
                  FROM loginhistory , users , branches WHERE loginhistory.user_id = users.user_Id and loginhistory.default_branch = branches.branchID ORDER BY default_branch , last_login DESC ");
          $sql->execute();
          if ($sql->rowCount() > 0) {
            while ($row = $sql->fetch()) {
              $active = $row['active'];
              $emp    = $row['default_user'];
              $staff_device = $row['staff_device'];
              $id = $row['history_id'];
              if ($staff_device == 1) {
                $dd = '<button class="btn btn-success btn-sm btn-block active " act="' . $id . '" style="font-weight:bold ;font-size:14px; padding:2;" >فعال </button>';
              } else {
                $dd = '<button class="btn btn-danger btn-sm btn-block cutt " cut="' . $id . '" style="font-weight:bold ;font-size:14px; padding:2;" >معطل </button>';
              }
              $kind = $row['kind'];
              if ($kind == 'كمبيوتر') {
                $pc_phone = '<button class="btn btn-success btn-sm btn-block pcc " pc="' . $id . '" style="font-weight:bold ;font-size:14px; padding:2;" >كمبيوتر</button>';
              } else {
                $pc_phone = '<button class="btn btn-primary btn-sm btn-block phonee" phone="' . $id . '" style="font-weight:bold ;font-size:14px; padding:2;" >جوال</button>';
              }
              $output .= '<tr> <td>' . $id . '</td>
                            <td class="text-center">' . $row['b_name'] . '</td>
                            <td class="text-center">' . $row['realName'] . '</td>
                            <td ><input class="emp" type="checkbox" name="emp[]" value="' . $emp . '"/> </td>
                            <td class="text-center">' . $pc_phone . '</td>
                            <td class="text-center" >' . $row['last_login'] . '</td>
                            <td class="text-center" >' . $dd . '</td>
                            <td><button del="' . $id . '" style="font-weight:bold ;font-size:14px; padding:2;" class="btn btn-danger btn-sm btn-block dell ">حذف</button></td>
                            </tr>';
            }
          } else {
            $output .= '<tr><td colspan="7">لا يوجد سجلات ...</td></tr>';
          }
          echo $output;
          ?>
        </tbody>
        <tfoot></tfoot>
      </table>
    </div>
    <hr />
    <form id="msg_form" method="post" class="form-horizontal" style="float:center;margin:10px;">
      <div class="row">
        <div class="col-md-10" style="float:right;">
          <input type="text" id="title" class="form-control" name="title" placeholder="عنوان الرسالة" />
        </div>
        <div class="col-md-1" style="float:right;">
          <input type="text" class="form-control" id="selection" name="selection" />
        </div>
        <div class="col-md-1" style="float:right;">
          <button class="btn btn-success btn-sm btn-block" id="take_selected">مستلم</button>
        </div>
      </div>
      <div class="row" style="margin-top:10px;margin-left:2px;margin-right:2px;">
        <textarea class="form-control" row="3" id="msg" name="msg" placeholder="نص الرسالة"></textarea>
      </div>
      <br />
      <div class="row">
        <div class="col-md-2" style="float:right;">
          <button class="btn btn-block btn-success" id="send_msg">إرسال الرسالة</button>
        </div>
      </div>
    </form>
  </div>
</div>
<!-- panel body style -->
<script>
  //  function mySelection(){
  //    var len = document.emp[].length;
  //    var result ="";
  //    for(i=0;i<len;i++){
  //      var checkedVAL = document.emp[i].checked;
  //      if(checkedVAL){
  //        result += document.emp[i].value + ", ";
  //      }
  //    }
  //    document.getElementById("selection").innerHTML = result ;
  //  }
</script>
<script>
  $(document).ready(function() {
    $("#take_selected").click(function(e) {
      e.preventDefault();
      var emps = "";
      $('.emp').each(function() {
        var count = 1;
        if ($(this).is(":checked")) {
          emps += $(this).val();
          emps += ", ";
        }
        count = count + 1;
      });
      $("#selection").val(emps);
    });

  $("#send_msg").click(function(event) {
      event.preventDefault();
      var emps = "";
      $('.emp').each(function() {
        var count = 1;
        if ($(this).is(":checked")) {
          emps += $(this).val();
          emps += ", ";
        }
        count = count + 1;
      });
     $("#selection").val(emps);
      $.ajax({
        url: "auth/auth_actions.php",
        method: "POST",
        data: $("#msg_form").serialize(),
       success: function(data) {
          $('#site').load("auth/auth_update.php");
        },
          error   :function(result){
          alert(result.statusText);
      }
      });
    });

    $(".active").click(function(event) {
      event.preventDefault();
      if (confirm("هل تريد تعديل صلاحية الجهاز الحالي ؟")) {
        var id = $(this).attr('act');
        var staff = 2;
        $.ajax({
          url: "auth/auth_actions.php",
          method: "POST",
          data: {
            id: id,
            staff: staff
          },
          success: function(data) {
            $('#site').load("auth/auth_update.php");
          }
        });
      } else {
        return false;
      }
    });
    //end of edit privileges
    $(".cutt").click(function(event) {
      event.preventDefault();
      if (confirm("هل تريد تعديل صلاحية الجهاز الحالي ؟")) {
        var id = $(this).attr('cut');
        var staff = 1;
        $.ajax({
          url: "auth/auth_actions.php",
          method: "POST",
          data: {
            id: id,
            staff: staff
          },
          success: function(data) {
            $('#site').load("auth/auth_update.php");
          }
        });
      } else {
        return false;
      }
    }); //end of edit privileges
    $(".pcc").click(function(event) {
      event.preventDefault();
      if (confirm("هل تريد تعديل نوع الجهاز الحالي ؟")) {
        var id = $(this).attr('pc');
        var kind = "جوال";
        $.ajax({
          url: "auth/auth_actions.php",
          method: "POST",
          data: {
            id: id,
            kind: kind
          },
          success: function(data) {
            $('#site').load("auth/auth_update.php");
          }
        });
      } else {
        return false;
      }
    }); //end of edit privileges
    $(".phonee").click(function(event) {
      event.preventDefault();
      if (confirm("هل تريد تعديل نوع الجهاز الحالي ؟")) {
        var id = $(this).attr('phone');
        var kind = "كمبيوتر";
        $.ajax({
          url: "auth/auth_actions.php",
          method: "POST",
          data: {
            id: id,
            kind: kind
          },
          success: function(data) {
            $('#site').load("auth/auth_update.php");
          }
        });
      } else {
        return false;
      }
    }); //end of edit privileges
    $(".dell").click(function(event) {
      event.preventDefault();
      if (confirm("هل تريد حذف الجهاز الحالي ؟")) {
        var dell = $(this).attr('del');
        $.ajax({
          url: "auth/auth_actions.php",
          method: "POST",
          data: {
            dell: dell
          },
          success: function(data) {
            $('#site').load("auth/auth_update.php");
          }
        });
      } else {
        return false;
      }
    }); //end of edit privileges

    // $("#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');
    //     });
    // $("#all").click(function(event){
    //   event.preventDefault();
    //   // var col   = $(".action").attr("id");
    //   // var val   = $(".action").val();
    //     window.open("acc/all_cars.php", '_blank');
    // });



  });
</script>