<!-- 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">Group 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>
            </div>
          </div>
          <!-- /.card-header -->
          <!-- 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 Group 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-4">
                        <div class="card-body pb-0">
                          <div class="form-group">
                            <label>Nama Server</label>
                            <select class="form-control" name="ids">
                              <?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 class="form-group">
                            <label>Nama</label>
                            <input type="text" class="form-control" name="nmk" required="required">
                          </div>
                          <div class="form-group form-check-inline">
                            <input type="checkbox" class="form-check-input" value="Y" name="market">
                            <label class="form-check-label">Data Hanya Tiap Marketing</label>
                          </div>
                          <div class="form-group form-check-inline">
                            <input type="checkbox" class="form-check-input" value="Y" name="tagihanberjalan">
                            <label class="form-check-label">Hanya Muncul Tagihan Bulan Berjalan di Tunggakan</label>
                          </div>
                        </div>
                      </div>
                      <div class="col-sm-8">
                        <div class="card-body pb-0">
                          <div class="form-group">
                            <label>List Akses</label><br>
                            <div class="row">
                              <?php
                                // Query untuk mengambil data menu
                                $db = mysqli_query($con, "SELECT * FROM master_menu WHERE user_akses = '1' AND master = '0' ORDER BY urutan");

                                // Periksa jika query berhasil
                                if ($db) {
                                    while ($menu = mysqli_fetch_assoc($db)) {
                                        $menu_id = htmlspecialchars($menu['id']);
                                        $menu_name = htmlspecialchars($menu['nama']);
                                        $user_menu = !empty($menu['user_menu']) ? explode("_", $menu['user_menu']) : [];

                                        echo '<div class="col-6">';
                                        echo '    <div class="form-group form-check">';
                                        echo '        <input type="checkbox" class="form-check-input" value="' . $menu_id . '" name="menu_ids[' . $menu_id . '][menu]" checked>';
                                        echo '        <label class="form-check-label" for="exampleCheck' . $menu_id . '">' . $menu_name . '</label>';
                                        echo '    </div>';

                                        if ($user_menu) {
                                            foreach ($user_menu as $keymenu) {
                                                $keymenu = htmlspecialchars($keymenu);
                                                echo '    <div class="form-group form-check-inline">';
                                                echo '        <input type="checkbox" class="form-check-input" value="' . $keymenu . '" name="menu_ids[' . $menu_id . '][sub_menu][]" checked>';
                                                echo '        <label class="form-check-label" for="exampleCheck' . $keymenu . '">' . $keymenu . '</label>';
                                                echo '    </div>';
                                            }
                                        }

                                        echo '</div>';
                                    }
                                } else {
                                    echo 'Error executing query: ' . mysqli_error($con);
                                }
                                ?>

                            </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="tambah1" class="btn btn-primary" value="Tambah">
                  </div>
                </form>
                <?php
                if (isset($_POST['tambah1'])) {
                  if ($_SERVER['REQUEST_METHOD'] === 'POST') {

                    if (isset($_POST['menu_ids'])) {
                        $menu_ids = $_POST['menu_ids'];
                    }

                    $json_string = json_encode($menu_ids, JSON_PRETTY_PRINT);
                    echo $json_string;
                    $input = mysqli_query($con,"INSERT INTO group_akses (id_data_server, nama, akses, per_marketing, tagihan_berjalan) VALUES ('$_POST[ids]','$_POST[nmk]','$json_string', '$_POST[market]', '$_POST[tagihanberjalan]')");

                    if ($input) 
                    {
                      sweetAlert("Data telah di tambah", "success", "?".encrypt_url('group_akses'));
                    }
                  }

                } 
                ?>
              </div>
            </div>
          </div>
          <div class="card-body">
            <table id="example1" class="table table-bordered table-striped">
              <thead>
              <tr>
                <th>Server</th>
                <th>Nama Group</th>
                <th>Marketing</th>
                <th>Tunggakan Bulan Berjalan</th>
                <th>Action</th>
              </tr>
              </thead>
              <tbody>
              <?php
                $apaket = mysqli_query($con,"SELECT * FROM v_group_akses $SqlDataWHId");
                while ($group = mysqli_fetch_assoc($apaket)) 
                { 
              ?>
              <tr>
                <td><?php echo $group['nama_server']; ?></td>
                <td><?php echo $group['nama']; ?></td>
                <td><?php echo $group['per_marketing']; ?></td>
                <td><?php echo $group['tagihan_berjalan']; ?></td>
                <td>
                  <form method="post">
                    <a type="button" class="btn btn-secondary btn-sm m-1" data-toggle="modal" data-target="#modal-edit<?php echo $group['id'];?>">Edit
                    </a>
                    <input type="submit" class="btn btn-danger btn-sm m-1" name="hapus<?php echo $group['id'];?>" value="Hapus">
                  </form>
                </td>
              </tr>
              <!-- Modal Info -->
              <div class="modal fade" id="modal-edit<?php echo $group['id'];?>" data-backdrop="static" data-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
              <!-- <div class="modal fade" id="modal-edit<?php echo $group['id'];?>"> -->
                <div class="modal-dialog modal-xl">
                  <div class="modal-content">
                      <div class="modal-header bg-secondary">
                        <h5 class="modal-title" id="staticBackdropLabel">Edit Group 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">
                      <form method="post" enctype="multipart/form-data">
                        <div class="row">
                          <div class="col-sm-4">
                            <div class="card-body pb-0">
                              <div class="form-group">
                                <label>Nama Server</label>
                                <select class="form-control" name="eids">
                                  <?php 
                                  $List = new Listdata();
                                  $List->set_list("data_server",$SqlDataWH);
                                  foreach ($List->get_list() as $key => $value): ?>
                                  <option value="<?php echo $value['id']; ?>" <?php if($value['id'] == $group['id_data_server']) { echo "selected";} ?>><?php echo $value['nama_server']; ?></option>
                                  <?php endforeach ?>
                                </select>
                              </div>
                              <div class="form-group">
                                <label>Nama</label>
                                <input type="text" class="form-control" name="enmk" value="<?php echo $group['nama']; ?>" required="required">
                              </div>
                              <div class="form-group form-check-inline">
                                <input type="checkbox" class="form-check-input" value="Y" name="market" <?php if($group['per_marketing'] == 'Y') { echo "checked"; } ?>>
                                <label class="form-check-label">Data Hanya Tiap Marketing</label>
                              </div>
                              <div class="form-group form-check-inline">
                                <input type="checkbox" class="form-check-input" value="Y" name="tagihanberjalan" <?php if($group['tagihan_berjalan'] == 'Y') { echo "checked"; } ?>>
                                <label class="form-check-label">Hanya Muncul Tagihan Bulan Berjalan di Tunggakan</label>
                              </div>
                            </div>
                          </div>
                          <div class="col-sm-8">
                            <div class="card-body pb-0">
                              <div class="form-group">
                                <label>List Akses</label><br>
                                <div class="row">
                                  <?php
                                    // Query untuk mendapatkan data dari database
                                    $db = mysqli_query($con, "SELECT * FROM master_menu WHERE user_akses = '1' AND master = '0' ORDER BY urutan");

                                    if ($db) {
                                        // JSON data (pastikan JSON ini berasal dari sumber yang benar)
                                        $json_data = $group['akses'];
                                        
                                        // Decode JSON data
                                        $json_array = json_decode($json_data, true);
                                        // echo "<pre>";
                                        // print_r($json_array);
                                        // echo "</pre>";

                                        while ($menu = mysqli_fetch_assoc($db)) {
                                            $menu_id = htmlspecialchars($menu['id']);
                                            $menu_name = htmlspecialchars($menu['nama']);
                                            $user_menu = !empty($menu['user_menu']) ? explode("_", $menu['user_menu']) : [];

                                            $checked_menu = isset($json_array[$menu_id]);

                                            echo '<div class="col-6">';
                                            echo '    <div class="form-group form-check">';
                                            echo '        <input type="checkbox" class="form-check-input" value="' . $menu_id . '" name="menu_ids[' . $menu_id . '][menu]" ' . ($checked_menu ? 'checked' : '') . '>';
                                            echo '        <label class="form-check-label" for="exampleCheck' . $menu_id . '">' . $menu_name . '</label>';
                                            echo '    </div>';

                                            if ($user_menu) {
                                                $sub_menu_data = $checked_menu ? $json_array[$menu_id]['sub_menu'] : [];
                                                foreach ($user_menu as $keymenu) {
                                                    $keymenu = htmlspecialchars($keymenu);
                                                    $checked_submenu = in_array($keymenu, $sub_menu_data);
                                                    echo '    <div class="form-group form-check-inline">';
                                                    echo '        <input type="checkbox" class="form-check-input" value="' . $keymenu . '" name="menu_ids[' . $menu_id . '][sub_menu][]" ' . ($checked_submenu ? 'checked' : '') . '>';
                                                    echo '        <label class="form-check-label" for="exampleCheck' . $keymenu . '">' . $keymenu . '</label>';
                                                    echo '    </div>';
                                                }
                                            }

                                            echo '</div>';
                                        }

                                        // Pastikan untuk membebaskan hasil query
                                        mysqli_free_result($db);
                                    } else {
                                        echo 'Error executing query: ' . mysqli_error($con);
                                    }

                                    // Tutup koneksi
                                    // mysqli_close($con);
                                    ?>
                                </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 $group['id'];?>" class="btn btn-warning" value="Edit">
                      </div>
                    </form>
                    <?php
                      if (isset($_POST['edit'.$group['id']])) 
                      {
                        if ($_SERVER['REQUEST_METHOD'] === 'POST') {

                          if (isset($_POST['menu_ids'])) {
                              $menu_ids = $_POST['menu_ids'];
                          }

                          $json_string = json_encode($menu_ids, JSON_PRETTY_PRINT);
                          echo $json_string;
                          $edit = mysqli_query($con, "UPDATE group_akses SET id_data_server='$_POST[eids]', nama='$_POST[enmk]', akses='$json_string', per_marketing = '$_POST[market]', tagihan_berjalan = '$_POST[tagihanberjalan]' WHERE id = '$group[id]'");

                          if ($edit) 
                          {
                            sweetAlert("Data telah di ubah", "success", "?".encrypt_url('group_akses'));
                          }
                        }
                      }
                      if (isset($_POST['hapus'.$group['id']])) {
                        $iddata = $group['id'];
                        deleteAlert("group_akses", $iddata);
                        }
                    ?>
                  </div>
                </div>
              </div>
              <!-- END Modal Info -->
            <?php } ?>
              </tbody>
            </table>
          </div>
          <!-- /.card-body -->
        </div>
        <!-- /.card -->
      </div>
      <!-- /.col -->
    </div>
    <!-- /.row -->
  </div>
  <!-- /.container-fluid -->
</section>
<!-- /.content