<?php $__env->startSection("content"); ?>
<?php
function DateThai($strDate)
{
  $strYear = date("Y",strtotime($strDate))+543;
  $strMonth= date("n",strtotime($strDate));
  $strWeek= date("w",strtotime($strDate));
  $strDay= date("j",strtotime($strDate));
  $strMonthCut = Array("","ม.ค.","ก.พ.","มี.ค.","เม.ย.","พ.ค.","มิ.ย.","ก.ค.","ส.ค.","ก.ย.","ต.ค.","พ.ย.","ธ.ค.");
  $thai_day_arr = Array("อา","จ","อ","พ","พฤ","ศ","ส");
  $strMonthThai=$strMonthCut[$strMonth];
  $strWeekThai=$thai_day_arr[$strWeek];
  return "$strWeekThai $strDay $strMonthThai $strYear";
}
?>
<nav aria-label="breadcrumb">
  <ol class="breadcrumb">
    <li class="breadcrumb-item"><a href="#">Setting</a></li>
    <li class="breadcrumb-item active" aria-current="page">การตั้งค่า - ตั้งค่าสิทธิ์</li>
  </ol>
</nav>
<div class="row">
  <div class="col-lg-6">
    <form action="<?php echo url('srtpermis2group'); ?>" method="post" id="frmPermissionGroup" class="col-lg-12 mb-12">
      <?php echo csrf_field(); ?>
      <div class="card">
        <div class="card-header">ตั้งค่าสิทธิ์</div>
        <div class="card-body">                
          <p style="font-size: 16px; color: #8B4513; font-weight: bold; margin-bottom: 3px"> กลุ่ม</p>
          <div class="table-responsive" style="margin-top: 10px;">
            <table class="table" style="border: 1px solid #ddd; ">
              <tbody>
                <tr>
                  <th style="font-size: 14px; color: #996666;"><i class="fab fa-elementor"></i> เลือกกลุ่ม :</th>
                  <td id="nameFarme"style="font-size: 14px; text-align: right;"> 
                    <select class="form-control form-control-sm" id="name_group" name="name_group" onchange="myGroupname()">
                      <option value="0">กรุณาเลือกประเภทผู้ใช้งาน</option>
                      <?php $__currentLoopData = $Gp; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $x): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                      <option value="<?php echo e($x->idGpPremi); ?>"><?php echo e($x->nameGp); ?></option>
                      <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </select>
                  </td>
                </tr>
              </tbody>
            </table>
          </div>
          <div class="table-responsive">
            <p style="font-size: 16px; color: #8B4513; font-weight: bold; margin-bottom: 2px;"> รายชื่อ </p>
            <table class="table table-sm table-bordered">
              <thead>
                <tr>
                  <th scope="col" class="text-center" width="50px;">#</th>
                  <th scope="col" class="text-center" width="650px;">รายชื่อ</th>
                  <th scope="col" class="text-center" width="150px;">แผนก</th>
                  <th scope="col" class="text-center" width="100px;">ตัวเลือก</th>
                </tr>
              </thead>
              <tbody id="tbody_namegroup">
              </tbody>
            </table>
            <div style="text-align: right;"><button type="button" id="bt_finedperson" class="btn btn-info btn-sm"><i class="fas fa-user-plus"></i> เพิ่มผู้ใช้</button></div>
          </div>
          <p style="font-size: 16px; color: #8B4513; font-weight: bold;"> เมนูการเข้าถึง</p>
          <div class="col-lg-12">
            <div class="form-group">
              <div class="card">
                <div class="card-body" style="overflow: auto;">
                  <div class="table-responsive" style="min-width: 300px;" id="jtMain">
                    <div id="jstree-proton-3" class="proton-demo"></div>
                  </div>
                </div>
              </div>
            </div>
          </div>      
        </div>
        <div class="card-footer" style="text-align: right;">
          <button type="button" class="btn btn-primary" id="confirm_menu">บันทึก</button>
        </div>
      </div>
    </form>

  </div>
</div>

<!-- ModalPerson -->
<div class="modal fade" id="modal_adduser" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">รายชื่อ</h5>
        <button type="button btn-sm" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <label>ค้นหารายชื่อ <span style="color: #DD2C00;">*</span> :</label>

        <div class="table-responsive">
          <table id="datatable_user" class="table table-bordered table-sm">
            <thead style="background-color:#778899 ;color: #FFF;">
              <tr>
                <th class="text-center"  style="width:20px !important;">เลือก</th>
                <th class="text-center text-nowrap">บริษัท</th>
                <th class="text-center text-nowrap" >รายชื่อ</th>
                <th class="text-center text-nowrap">ชื่อ</th>
                <th class="text-center text-nowrap">สกุล</th>
                <th class="text-center text-nowrap">แผนก</th>
              </tr>
            </thead>
<!--                         <tbody id="tbody_user">
</tbody> -->
</table>
</div>
</div>
</div>
</div>
</div>

<?php $__env->stopSection(); ?>
<?php $__env->startSection("script"); ?>
<script type="text/javascript">

  var _retData=null;
  var _rows = 0;
  function myGroupname(){

    $.ajax({
      url:'<?php echo e(url("findmenugrope")); ?>',
      data:{idGrp:$("#name_group").val()},
      type:'GET',
      dataType:'json',
      cache:false,
      success:function(ret){
        _rows =1;
        items = ret['ueser2grp'];
        imenu = ret["mn"];
        $("#tbody_namegroup").empty();
        $("#jtMain").empty().append("<div id=\"jstree-proton-4\" class=\"proton-demo\"></div>");
        var rowsq = 1;
        for(x in items){
          str = '<tr>'
          +'<td class="text-center">'+_rows+'</td>'
          +'<td>'+items[x].PsNnameF+'</td>'
          +'<td class="text-center">'+items[x].nameDep+'</td>'
          +'<td class="text-center" style="width:8%;"><a onclick="del_idPs('+items[x].idPs+','+items[x].idGpPremi+',this)" style="cursor:pointer;"><i class="fas fa-trash-alt" style="color:#D50000;"></i></a></td>'
          '</tr>';
          $("#tbody_namegroup").append(str);
          _rows++;
        }
        $("#jstree-proton-4").jstree(imenu);
      },
      error:function(err){
        alert('Error');
                //console.log(err.responseText);
              }
            });
  }


  $(function(){

    $("#bt_finedperson").click(function(){
      if($("#name_group").val() == 0 ){
        swal ( " " ,  "กรุณาเลือกกลุ่มผู้ใช้งาน!" ,  "warning" );
        return false; 
      }

      $('#datatable_user').DataTable().clear().destroy();
      $('#datatable_user').DataTable( {dom: 'Bfrtip',
        "ajax": "<?php echo e(url('getpersonall')); ?>", 
        "columnDefs": [
        { "visible": false, "targets": 1 },
        { "visible": false, "targets": 2 }
        ],
        "columns": [
        {
          data: null,
          className: "center",
          defaultContent: '<button  class="btn btn-primary btn-sm btnSelect">เลือก</button>'
        },
        { "data": "idPs" },
        { "data": "PsNnameF" },
        { "data": "PsNnameF" },
        { "data": "PsNameS" },
        { "data": "nameDep" }


        ]
      } );



      $('#modal_adduser').modal('show');
    });

    $('#datatable_user').on( 'click', 'button.btnSelect', function () {
      var table = $('#datatable_user').DataTable();
      var data = table.row( $(this).parents('tr') ).data();
      AddItem(data);

    } );

    
  })

  $("#confirm_menu").click(function(){
    if ($('#name_group').val() == 0) {
      swal ( " " ,  "กรุณาเลือกกลุ่มผู้ใช้งาน" ,  "warning" )
      return false; 
    }else{
      var checked_ids = [];
      var selectedNode= $('#jstree-proton-4').jstree(true).get_selected('full',true);

      for( x in selectedNode){
            // console.log(selectedNode[x].id);
            if(selectedNode[x].id.trim().substring(0,1) != "j"){
              checked_ids.push(selectedNode[x].id);
            }
          }

          $("#jstree-proton-4").find(".jstree-undetermined").each(function (i, element) {
            item = $(element).closest('.jstree-node').attr("id");
            console.log(item +">>>" + item.substring(0,1));
            if(item.trim().substring(0,1) != "j"){
              checked_ids.push(item);
            }
          });
          swal({
            title: "คุณแน่ใจใช่ไหม?",
            text: "ยืนยันการบันทึกข้อมูลนี้?",
            icon: "warning",
            buttons: true,
            dangerMode: true,
          })
          .then((willDelete) => {
            if (willDelete) {
          /*      console.log(checked_ids.join(','));
          return false;*/
          $.ajax({
            url:'<?php echo e(url("setpermis")); ?>',
            data:{idGrp:$("#name_group").val(),checked_ids:checked_ids.join(',')},
            type:'GET',
                //dataType:'json',
                cache:false,
                success:function(ret){
                  console.log(ret);

                },
                error:function(err){
                  alert('Error');
                  console.log(err.responseText);
                }
                
              });
          $("#frmPermissionGroup").submit();
          swal("ทำการบันทึกข้อมูลเรียบร้อย!", {
            icon: "success",
          });
        } else {
          swal("ยกเลิกทำการบันทึกข้อมูล!");
        }
      }); 

        }

      });



  function AddItem(row){
    str = '<tr style="background-color:#FF8C00;">'
    +'<td class="text-center"></td>'
    +'<td>'+row.PsNnameF+'</td>'
    +'<td  class="text-center">'+row.nameDep+'</td>'
    +'<td style="text-align: center;cursor:pointer;width:8%;"><a onclick="deletebill(this)"><i class="fas fa-trash-alt" style="color:#DD2C00;"></i></a></td>'
    +'<input type="hidden" name="idPs[]" value="'+row.idPs+'">'
    +'</tr>';
    $("#tbody_namegroup").append(str);
    $("#tbody_namegroup tr").each(function(index){
      $(this).children().eq(0).text(index +1);
    })
    $('#modal_adduser').modal('hide');
  }

  function deletebill(e){
    tag = $(e).parent().parent();
    tag.remove();
  }

  function del_idPs(id,id2,e) {
    swal({
      title: "คุณแน่ใจใช่ไหม?",
      text: "ยืนยันการลบข้อมูลนี้ออกจากระบบ?",
      icon: "warning",
      buttons: true,
      dangerMode: true,
    })
    .then((willDelete) => {
      if (willDelete) {
        swal("ทำการลบข้อมูลเรียบร้อย!", {
          icon: "success",
        });
        location.href = "<?php echo e(url('permisdelete')); ?>/"+id+'/'+id2;
      } else {
        swal("ยกเลิกทำการลบข้อมูล!");
      }
    }); 
  }

</script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make("layout.menu", \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH C:\xampp7\htdocs\natureplus\resources\views/hr/settingMenu.blade.php ENDPATH**/ ?>