<?php  
// fungsi untuk pengecekan tampilan form
// jika form add data yang dipilih
if ($_GET['form']=='add') { ?>
  <!-- tampilkan form add data -->
	<!-- Content Header (Page header) -->
  <section class="content-header">
    <h1>
      <i class="fa fa-edit icon-title"></i> Input User
    </h1>
    <ol class="breadcrumb">
      <li><a href="?module=beranda"><i class="fa fa-home"></i> Beranda </a></li>
      <li><a href="?module=user"> User </a></li>
      <li class="active"> Input </li>
    </ol>
  </section>

  <!-- Main content -->
  <section class="content">
    <div class="row">
      <div class="col-md-12">
        <div class="box box-primary">
          <!-- form start -->
          <form role="form" class="form-horizontal" method="POST" action="modules/user/proses.php?act=insert" enctype="multipart/form-data">
            <div class="box-body">

              <div class="form-group">
                <label class="col-sm-2 control-label">Username</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" name="username" autocomplete="off" required>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label">Password</label>
                <div class="col-sm-5">
                  <input type="password" class="form-control" name="password" autocomplete="off" required>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label">Nama User</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" name="nama_user" autocomplete="off" required>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label">Hak Akses</label>
                <div class="col-sm-5">
                  <select class="chosen-select" name="hak_akses" data-placeholder="-- Pilih --" autocomplete="off" required>
                    <?php
                        echo"<option value='$data[loc]'>$data[nama_lokasi]</option>";
                        $query = mysqli_query($mysqli, "SELECT * FROM is_hak_akses WHERE status='Y' order by id ASC") 
                                                    or die('Ada kesalahan pada query tampil : '.mysqli_error($mysqli));

                        while ($data = mysqli_fetch_assoc($query)) { 
                            echo"<option value='$data[akses]'>$data[akses]</option>";
                        }
                    ?>
                  </select>
                </div>
              </div>
            </div><!-- /.box body -->

            <div class="box-footer">
              <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                  <input type="submit" class="btn btn-primary btn-submit" name="simpan" value="Simpan">
                  <a href="?module=user" class="btn btn-default btn-reset">Batal</a>
                </div>
              </div>
            </div><!-- /.box footer -->
          </form>
        </div><!-- /.box -->
      </div><!--/.col -->
    </div>   <!-- /.row -->
  </section><!-- /.content -->
<?php
}

// jika form edit data yang dipilih
elseif ($_GET['form']=='edit') { 
  	if (isset($_GET['id'])) {
      // fungsi query untuk menampilkan data dari tabel user
      $query = mysqli_query($mysqli, "SELECT *,
                                      (select akses from is_hak_akses where akses=is_users.hak_akses) as hakses,
                                       (select nama from is_lokasi where id=is_users.loc) as nama_lokasi
                                      FROM is_users WHERE id_user='$_GET[id]'") 
                                      or die('Ada kesalahan pada query tampil data user : '.mysqli_error($mysqli));
      $data  = mysqli_fetch_assoc($query);
  	}	
?>
	<!-- tampilkan form edit data -->
  <!-- Content Header (Page header) -->
  <section class="content-header">
    <h1>
      <i class="fa fa-edit icon-title"></i> Ubah Data User
    </h1>
    <ol class="breadcrumb">
      <li><a href="?module=beranda"><i class="fa fa-home"></i> Beranda</a></li>
      <li><a href="?module=user"> User </a></li>
      <li class="active"> Ubah </li>
    </ol>
  </section>

  <!-- Main content -->
  <section class="content">
    <div class="row">
      <div class="col-md-12">
        <div class="box box-primary">
          <!-- form start -->
          <form role="form" class="form-horizontal" method="POST" action="modules/user/proses.php?act=update" enctype="multipart/form-data">
            <div class="box-body">

              <input type="hidden" name="id_user" value="<?php echo $data['id_user']; ?>">

              <div class="form-group">
                <label class="col-sm-2 control-label">Username</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" name="username" autocomplete="off" value="<?php echo $data['username']; ?>" required>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label">Password</label>
                <div class="col-sm-5">
                  <input type="password" class="form-control" name="password" autocomplete="off" placeholder="Kosongkan password jika tidak diubah">
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label">Nama User</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" name="nama_user" autocomplete="off" value="<?php echo $data['nama_user']; ?>" required>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label">Email</label>
                <div class="col-sm-5">
                  <input type="email" class="form-control" name="email" autocomplete="off" value="<?php echo $data['email']; ?>">
                </div>
              </div>
            
              <div class="form-group">
                <label class="col-sm-2 control-label">Telepon</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" name="telepon" autocomplete="off" maxlength="13" onKeyPress="return goodchars(event,'0123456789',this)" value="<?php echo $data['telepon']; ?>">
                </div>
              </div>

            <div class="form-group">
                <label class="col-sm-2 control-label">Foto</label>
                <div class="col-sm-5">
                  <input type="file" name="foto">
                  <br/>
                <?php  
                if ($data['foto']=="") { ?>
                  <img style="border:1px solid #eaeaea;border-radius:5px;" src="images/user/user-default.png" width="128">
                <?php
                }
                else { ?>
                  <img style="border:1px solid #eaeaea;border-radius:5px;" src="images/user/<?php echo $data['foto']; ?>" width="128">
                <?php
                }
                ?>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label">Hak Akses</label>
                <div class="col-sm-5">
                  <select class="chosen-select" name="hak_akses" autocomplete="off" required>
                    <?php
                        echo"<option value='$data[hak_akses]'>$data[hakses]</option>";
                        $query2 = mysqli_query($mysqli, "SELECT * FROM is_hak_akses WHERE status='Y' order by id ASC") 
                                                    or die('Ada kesalahan pada query tampil : '.mysqli_error($mysqli));

                        while ($data2 = mysqli_fetch_assoc($query2)) { 
                            echo"<option value='$data2[hak_akses]'>$data2[akses]</option>";
                        }

                    ?>
                  </select>
                </div>
              </div>
              

              
              
              
              <div class="form-group">
                <label class="col-sm-2 control-label">Kategorial</label>
                <div class="col-sm-5">
                  <select class="chosen-select" name="lokasi[]" autocomplete="off" multiple >
                    <?php
                        echo"<option value='$data[hak_akses]'>$data[nama_lokasi]</option>";
                        $query2 = mysqli_query($mysqli, "SELECT * ,(select nama from is_sektor where id = is_lokasi.sektor) as namasektor 
															FROM is_lokasi WHERE status='Y' order by nama ASC") 
                                                    or die('Ada kesalahan pada query tampil : '.mysqli_error($mysqli));

                        while ($data2 = mysqli_fetch_assoc($query2)) { 
                            
                            
                                    $selected = "";
									if($data['loc'] != ""){
										$query22 = mysqli_query($mysqli, "SELECT id ,(select nama from is_sektor where id = is_lokasi.sektor) as namasektor
																			FROM is_lokasi WHERE id in ($data[loc])") 
																or die('Ada kesalahan pada query tampil : '.mysqli_error($mysqli));
										
										
										while ($data22 = mysqli_fetch_assoc($query22)) { 
											if ($data22['id'] == $data2['id']){
												$selected = "selected";
											}
										}
									}
                                                            
                                    echo"<option value='$data2[id]' $selected>$data2[nama] - $data2[namasektor]</option>";
                        }

                    ?>
                  </select>
                </div>
              </div>
              
              
              
            </div><!-- /.box body -->

            <div class="box-footer">
              <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                  <input type="submit" class="btn btn-primary btn-submit" name="simpan" value="Simpan">
                  <a href="?module=user" class="btn btn-default btn-reset">Batal</a>
                </div>
              </div>
            </div><!-- /.box footer -->
          </form>
        </div><!-- /.box -->
      </div><!--/.col -->
    </div>   <!-- /.row -->
  </section><!-- /.content -->
<?php
}
?>