 <!-- Main content -->
<section class="content">
  <div class="container-fluid">
    <div class="row">
      <div class="col-12">
        <div class="card">
          <div class="card-header">
            <div class="row">
              <div class="col-sm-6">
                <h3 class="card-title">User Akses</h3>
              </div>
              <div class="col-sm-6">
                <ol class="float-sm-right mb-0">
                  <a type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#staticBackdrop">Tambah</a>
                </ol>
              </div>
              <!-- Modal Tambah -->
              <div class="modal fade" id="staticBackdrop" data-backdrop="static" data-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
                <div class="modal-dialog modal-xl">
                  <div class="modal-content">
                    <form method="post" enctype="multipart/form-data">
                      <div class="modal-header bg-primary">
                        <h5 class="modal-title" id="staticBackdropLabel">Tambah User Akses</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                          <span aria-hidden="true">&times;</span>
                        </button>
                      </div>
                      <div class="modal-body">
                        <div class="row">
                          <div class="col-sm-6">
                            <div class="card-body pb-0">
                              <div class="form-group">
                                <label>Nama</label>
                                <input type="text" class="form-control" name="nma" required="required">
                              </div>
                              <div class="form-group">
                                <label>Nomor WhatsApp</label>
                                <input type="text" class="form-control" name="nwa" required="required">
                              </div>
                              <div class="form-group">
                                <label>Username</label>
                                <input type="text" class="form-control" name="usm" required="required">
                              </div>
                              <div class="form-group">
                                <label>Password</label>
                                <input type="text" class="form-control" name="psw" required="required">
                              </div>
                            </div>
                          </div>
                          <div class="col-sm-6">
                            <div class="card-body pb-0">
                              <div class="form-group">
                                <label>Group Akses</label>
                                <select class="form-control" name="iga" required="required">
                                  <?php 
                                  $List = new Listdata();
                                  $List->set_list("group_akses",$SqlDataWHId);
                                  foreach ($List->get_list() as $key => $ak): ?>
                                  <option value="<?php echo $ak['id']; ?>"><?php echo $ak['nama']; ?></option>
                                  <?php endforeach ?>
                                </select>
                              </div>
                              <div class="form-group">
                                <label>Data Server</label>
                                <div class="select2-primary">
                                  <select class="select2" name="ids[]" multiple="multiple" data-placeholder="Select a State" data-dropdown-css-class="select2-primary" style="width: 100%;" required="required">
                                    <?php 
                                    $List = new Listdata();
                                    $List->set_list("data_server",$SqlDataWH);
                                    foreach ($List->get_list() as $key => $value): ?>
                                    <option value="<?php echo $value['id']; ?>"><?php echo $value['nama_server']; ?></option>
                                    <?php endforeach ?>
                                  </select>
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <input type="submit" name="tambah" class="btn btn-primary" value="Tambah">
                      </div>
                    </form>
                    <?php
                    if (isset($_POST['tambah'])) 
                    {
                      $idsv = implode(",", $_POST['ids']);
                      $hashedPassword = password_hash($_POST[psw], PASSWORD_DEFAULT);

                      $input = mysqli_query($con,"INSERT INTO user_akses (nama, nomor_whatsapp, username, password, id_group_akses, id_data_server) VALUES ('$_POST[nma]','$_POST[nwa]','$_POST[usm]','$hashedPassword','$_POST[iga]','$idsv')");
                      if ($input) 
                      {
                        sweetAlert("User Akses telah di tambahkan", "success");
                      }
                    }
                    ?>
                  </div>
                </div>
              </div>
              <!-- END Modal Tambah -->
            </div>
          </div>
          <!-- /.card-header -->
          <div class="card-body">
            <table id="example3" class="table table-bordered table-striped">
              <thead>
              <tr>
                <th>Nama</th>
                <th>Nomor WhatsApp</th>
                <th>Server Akses</th>
                <th>Group Akses</th>
                <th>Action</th>
              </tr>
              </thead>
              <tbody>
              <?php
                $aserver = mysqli_query($con,"SELECT * FROM user_akses $SqlDataWHId");
                while ($server = mysqli_fetch_assoc($aserver)) 
                {
              ?>
              <tr>
                <td><?php echo $server['nama']; ?></td>
                <td><?php echo $server['nomor_whatsapp']; ?></td>
                <td>
                  <?php 
                    $datasv = explode(", ", $server['id_data_server']);
                    foreach ($datasv as $isidata)
                    {
                      $asv = mysqli_query($con,"SELECT nama_server FROM data_server WHERE id = '$isidata'");
                      $sv = mysqli_fetch_assoc($asv);
                      echo '<span class="badge badge-primary ml-1">'.$sv['nama_server'].'</span>';
                    } 
                  ?>
                </td>
                <td>
                  <?php 
                  $asva = mysqli_query($con,"SELECT nama FROM group_akses WHERE id = '$server[id_group_akses]'");
                  $sva = mysqli_fetch_assoc($asva);
                  echo $sva['nama']; 
                  ?>
                </td>
                <td>
                  <form method="post">
                    <a type="button" class="btn btn-secondary btn-sm ml-1" data-toggle="modal" data-target="#modal-edit<?php echo $server['id'];?>">Edit</a>
                    <input type="submit" name="hapus<?php echo $server['id'];?>" value="Hapus" class="btn btn-danger btn-sm ml-1" />
                  </form>
                </td>
              </tr>
              <!-- Modal Edit -->
              <div class="modal fade" id="modal-edit<?php echo $server['id'];?>" data-backdrop="static" data-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
                <div class="modal-dialog modal-xl">
                  <div class="modal-content">
                    <form method="post" enctype="multipart/form-data">
                      <div class="modal-header bg-secondary">
                        <h5 class="modal-title" id="staticBackdropLabel">Edit Mikrotik Router</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                          <span aria-hidden="true">&times;</span>
                        </button>
                      </div>
                      <div class="modal-body">
                        <div class="row">
                          <div class="col-sm-6">
                            <div class="card-body pb-0">
                              <div class="form-group">
                                <label>Nama</label>
                                <input type="text" class="form-control" value="<?php echo $server['nama']; ?>" name="nma" required="required">
                              </div>
                              <div class="form-group">
                                <label>Nomor WhatsApp</label>
                                <input type="text" class="form-control" value="<?php echo $server['nomor_whatsapp']; ?>" name="nwa" required="required">
                              </div>
                              <div class="form-group">
                                <label>Username</label>
                                <input type="text" class="form-control" value="<?php echo $server['username']; ?>" name="usm" required="required">
                              </div>
                              <div class="form-group">
                                <label>Ganti Password</label>
                                <input type="text" class="form-control" name="psw">
                                <i>Isi Password baru jika di perlukan</i>
                              </div>
                            </div>
                          </div>
                          <div class="col-sm-6">
                            <div class="card-body pb-0">
                              <div class="form-group">
                                <label>Group Akses</label>
                                <select class="form-control" name="iga">
                                  <?php 
                                  $List = new Listdata();
                                  $List->set_list("group_akses",$SqlDataWHId);
                                  foreach ($List->get_list() as $key => $ak): ?>
                                  <option value="<?php echo $ak['id']; ?>" <?php if($server['id_group_akses'] == $ak['id']) { echo "selected";} ?>><?php echo $ak['nama']; ?></option>
                                  <?php endforeach ?>
                                </select>
                              </div>
                              <div class="form-group">
                                <label>Data Server</label>
                                <div class="select2-primary">
                                  <select class="select2" name="ids[]" multiple="multiple" data-placeholder="Select a State" data-dropdown-css-class="select2-primary" style="width: 100%;">
                                    <?php 
                                    $List = new Listdata();
                                    $List->set_list("data_server",$SqlDataWH);
                                    foreach ($List->get_list() as $key => $value): ?>
                                    <option value="<?php echo $value['id']; ?>" <?php $c = $value['id']; $k = $server['id_data_server']; if(preg_match("/$c/i", $k)) { echo "selected";} ?> ><?php echo $value['nama_server']; ?></option>
                                    <?php endforeach ?>
                                  </select>
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <input type="submit" name="edit<?php echo $server['id'];?>" class="btn btn-warning" value="Edit">
                      </div>
                    </form>
                    <?php
                    if (isset($_POST['edit'.$server['id']])) 
                    {
                      $hashedPassword = (empty($_POST['psw'])) ? '' : ", password = '" . password_hash($_POST['psw'], PASSWORD_DEFAULT) . "'";

                      $idsv = implode(",", $_POST['ids']);
                      $update = mysqli_query($con, "UPDATE user_akses SET
                          id_data_server='$idsv',
                          id_group_akses='" . mysqli_real_escape_string($con, $_POST['iga']) . "',
                          nama='" . mysqli_real_escape_string($con, $_POST['nma']) . "',
                          nomor_whatsapp='" . mysqli_real_escape_string($con, $_POST['nwa']) . "',
                          username='" . mysqli_real_escape_string($con, $_POST['usm']) . "'"
                          . $hashedPassword . " 
                          WHERE id = '" . mysqli_real_escape_string($con, $server['id']) . "'"
                      );
                      if ($update) 
                      {
                        sweetAlert("User Akses telah di ubah", "success");
                      }
                    }

                    if (isset($_POST['hapus'.$server['id']]))
                    {
                      $iddata = $server['id'];
                      deleteAlert("user_akses", $iddata);
                    }
                    ?>
                  </div>
                </div>
              </div>
              <!-- END Modal Edit -->
            <?php } ?>
              </tbody>
            </table>
          </div>
          <!-- /.card-body -->
        </div>
        <!-- /.card -->
      </div>
      <!-- /.col -->
    </div>
    <!-- /.row -->
  </div>
  <!-- /.container-fluid -->
</section>
<!-- /.content -->