<?php $this->load->view('meta') ?>
  <div class="wrapper">
<?php $this->load->view('sidebar') ?>
<?php $this->load->view('navbar') ?>

        <div class="container-fluid">

          <!-- DataTales Example -->
          <div class="card shadow mb-4">
            <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
              <h6 class="m-0 font-weight-bold text-primary"><?php echo $title; ?></h6>
              <div class="dropdown no-arrow">
                <button class="btn btn-sm btn-success" id="btnAdd" onclick="tambah()" ><span class="fa fa-plus"></span> Tambah Data</button>
              </div>
            </div>
            <div class="card-body">
              <div class="table-responsive">
                <table class="table table-bordered datatable" cellspacing="0">
                  <thead>
                    <tr>
                      <th>No</th>
                      <th>Nama</th>
                      <th>Username</th>
                      <th>Akses Form</th>
                      <th>Action</th>
                    </tr>
                  </thead>
                  <tbody>
                  <?php
				  $no=1;
				  foreach ($user as $user){
				  ?>
                    <tr>
                      <td><?php echo $no; ?></td>
                      <td><?php echo $user->nama_user; ?></td>
                      <td><?php echo $user->username; ?></td>
                      <td>
                        <?php
                        $jenis_form=$this->Kategori_admin_model->get_jenis_form($user->id_user);
                        foreach ($jenis_form as $jenis_form) {
                          echo "- $jenis_form->nama_jenis_form <br/>";
                        }
                        ?>
                      </td>
                        <td class="center">
                            <button type="button" id="edit<?= encrypt($user->id_user) ?>" class="btn btn-success btn-sm" data-toggle="tooltip" title="Ubah" onclick="ubah('<?= encrypt($user->id_user) ?>')"><i class="fas fa-fw fa-pencil-alt"></i></button>
                            <button type="button" class="btn btn-danger btn-sm" data-toggle="tooltip" title="Hapus" onclick="hapus('<?= encrypt($user->id_user) ?>')"><i class="fas fa-fw fa-trash-alt"></i></button>
                        </td>
                    </tr>
                  <?php
				  $no++;
				  }
				  ?>
                  </tbody>
                </table>
              </div>
            </div>
          </div>

        </div>


    <!-- Modal -->
    <div id="modalAdd" class="modal fade" role="dialog">
        <div class="modal-dialog modal-lg">
                <!-- Modal content-->
            <div class="modal-content">
                <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                    <h6 class="m-0 font-weight-bold text-primary">Tambah User</h6>
                      <div class="dropdown no-arrow">
	                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                      </div>
                </div>
                <form id="formAdd" class="form-horizontal" method="post">
                    <div class="modal-body">
                    <div class="container-fluid">
                    <div class="row">
                        <div class="col-md-3">Nama User</div>
                        <div class="col-md-8">
                        <input type="text" class="form-control form-control-user" name="nama_user" placeholder="Nama" required>
                          <br/>
                      </div>
                    </div>                      
                    <div class="row">
                        <div class="col-md-3">Username</div>
                        <div class="col-md-8">
                        <input type="text" class="form-control form-control-user" name="username" placeholder="Username" required>
                          <br/>
                      </div>
                    </div>
                    <div class="row">
                        <div class="col-md-3">Password</div>
                        <div class="col-md-8">
                        <input type="password" class="form-control form-control-user" name="password" placeholder="Password">
                        <br/>
                      </div>
                    </div>
                    <div class="row">
                        <div class="col-md-3">Re Password</div>
                        <div class="col-md-8">
                        <input type="password" class="form-control form-control-user" name="repassword" placeholder="Password">
                        <br/>
                      </div>
                    </div>
                    <div id="kolom_daftar_kategori2"></div>

                    </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">Batal</button>
                        <button id="btnSubmitAdd" type="submit" class="btn btn-success">Submit</button>
                    </div>
                </form>
            </div>
        </div>                    
    </div>
    
    
    <div id="modalEdit" class="modal fade" role="dialog">
        <div class="modal-dialog modal-lg">
                <!-- Modal content-->
            <div class="modal-content">
                <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                    <h6 class="m-0 font-weight-bold text-primary">Edit User</h6>
                      <div class="dropdown no-arrow">
	                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                      </div>
                </div>
                <form id="formEdit" class="form-horizontal" method="post">
                    <input type="hidden" name="id_user" id="id_user" class="form-control" required />
                    <div class="modal-body">
                    <div class="container-fluid">
                    <div class="row">
                        <div class="col-md-3">Nama User</div>
                        <div class="col-md-8">
                        <input type="text" class="form-control form-control-user" name="nama_user" id="nama_user" placeholder="Nama" required>
                          <br/>
                      </div>
                    </div>
                    <div class="row">
                        <div class="col-md-3">Username</div>
                        <div class="col-md-8">
	                      <input type="text" class="form-control form-control-user" name="username" id="username" placeholder="Username" required>
                          <br/>
	                    </div>
                    </div>
                    <div class="row">
                        <div class="col-md-3">Password</div>
                        <div class="col-md-8">
	                      <input type="password" id="password" class="form-control form-control-user" name="password" placeholder="Password">
                          <span style="color:red; font-size:10px;">Kosongkan jika tidak dirubah</span>
                          <br/><br/>
	                    </div>
                    </div>
                    <div id="kolom_daftar_kategori"></div>
                    </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">Batal</button>
                        <button id="btnSubmitEdit" type="submit" class="btn btn-success">Submit</button>
                    </div>
                </form>
            </div>
        </div>                    
    </div>
                        
                        
                        
    <?php $this->load->view('footer') ?>
  </div><!-- ./wrapper -->
  <?php $this->load->view('js') ?>
	<!-- DATA TABLES-->
  <script>
  	function tambah(){
		$('#btnAdd').html('<i class="fas fa-spinner fa-spin"></i> Loading...');
		$('#formAdd')[0].reset();
    $('#kolom_daftar_kategori2').slideDown('normal').load('<?php echo base_url() ?>user/get_data_kategori',{ 
              id_user: 0});

		$('#btnAdd').html('<span class="fa fa-plus"></span> Tambah Data');
		$('#modalAdd').modal('show');
	}
	
	$('#formAdd').submit(function(e){
		e.preventDefault();
		var data = $(this).serialize();
		$('#btnSubmitAdd').html('<i class="fas fa-spinner fa-spin"></i> Loading...');
		$('#btnSubmitAdd').attr('disabled', true);
		$.ajax({
			type : 'post',
			url : '<?php echo base_url(); ?>user/add_admin_supervisor',
			data : 'submit=true&'+data
		}).done(function(res){
			$('#btnSubmitAdd').html('Submit');
			$('#btnSubmitAdd').removeAttr('disabled');
			if(res=='1'){
				$("#modalAdd").modal('toggle');
			  swal({
				title: 'Data Behasil Ditambah',
				text: 'Data berhasil disimpan ke database.',
				type: 'success',
				closeOnConfirm: false
			  }, function(isConfirm){
				if(isConfirm){
				  location.reload();
				}
			  })
			}else if(res=='3'){
        swal("Kesalahan!", "Username sudah dipakai sebelumnya", 'error');
      }else if(res=='2'){
        swal("Kesalahan!", "Password tidak sama!", 'error');
      }else{
        swal("Kesalahan!", "Masukkan data dengan lengkap!", 'error');
      }
		}).fail(function(res){
			console.log(res);
		});
	});
	
	function ubah(id){
		$('#edit'+id).html('<i class="fas fa-spinner fa-spin"></i>');
		$('#formEdit')[0].reset();
		$.ajax({
			type: 'post',
			url: '<?php echo base_url(); ?>user/get_data',
			data: 'id='+id
		}).done(function(res){
			var data = $.parseJSON(res);
			$('#nik').val(data.nik);
			$('#username').val(data.username);
			$('#alamat_user').val(data.alamat_user);
			$('#nama_user').val(data.nama_user);
			$('#id_user').val(id);
      var id_user=data.id_user;
      $('#kolom_daftar_kategori').slideDown('normal').load('<?php echo base_url() ?>user/get_data_kategori',{ 
                    id_user: id_user});

			$('#edit'+id).html('<i class="fas fa-fw fa-pencil-alt"></i>');
			$('#modalEdit').modal('show');

		}).fail(function(res){
			console.log(res);
		});
	}
	
	
	$('#formEdit').submit(function(e){
		e.preventDefault();
		var data = $(this).serialize();
		$('#btnSubmitEdit').html('<i class="fas fa-spinner fa-spin"></i> Loading...');
		$('#btnSubmitEdit').attr('disabled', true);
		$.ajax({
			type : 'post',
			url : '<?php echo base_url(); ?>user/update_admin_supervisor',
			data : 'submit=true&'+data
		}).done(function(res){
			$('#btnSubmitEdit').html('Submit');
			$('#btnSubmitEdit').removeAttr('disabled');
			if(res=='1'){
				$("#modalEdit").modal('toggle');
			  swal({
				title: 'Data Behasil Diubah',
				text: 'Data berhasil disimpan ke database.',
				type: 'success',
				closeOnConfirm: false
			  }, function(isConfirm){
				if(isConfirm){
				  location.reload();
				}
			  })
			}else{
			swal("Kesalahan!", "Masukkan data dengan lengkap", 'error');
			}
		}).fail(function(res){
			console.log(res);
		});
	});




	function approve(code){
            swal({
                    title: "Apakah anda yakin?",
                    text: "Data pendaftar akan diapprove!",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#EF5350",
                    confirmButtonText: "Ya, Approve",
                    cancelButtonText: "Batal",
                    closeOnConfirm: false,
                    showLoaderOnConfirm: true
                },
                function(){
                      $.ajax({
                        type: 'post',
                        url: '<?php echo base_url(); ?>user/approve',
                        data: {
                          submit: true,
                          id: code
                        }
                      }).done(function(){
                        swal({
                            title: "Berhasil",
                            text: "Data Pendaftaran Berhasil Diapprove",
                            type: "success",
                            showCancelButton: false,
                            closeOnConfirm: true
                        },
                        function(isConfirm){
                            if (isConfirm) {
                            location.reload();
                            }
                        });
                    });
                }
            );
          }


	function hapus(code){
            swal({
                    title: "Apakah anda yakin?",
                    text: "Data akan dihapus permanen dari Database!",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#EF5350",
                    confirmButtonText: "Hapus",
                    cancelButtonText: "Batal",
                    closeOnConfirm: false,
                    showLoaderOnConfirm: true
                },
                function(){
                      $.ajax({
                        type: 'post',
                        url: '<?php echo base_url(); ?>user/delete',
                        data: {
                          submit: true,
                          id: code
                        }
                      }).done(function(){
                        swal({
                            title: "Berhasil",
                            text: "Data Berhasil Dihapus",
                            type: "success",
                            showCancelButton: false,
                            closeOnConfirm: true
                        },
                        function(isConfirm){
                            if (isConfirm) {
                            location.reload();
                            }
                        });
                    });
                }
            );
          }
  </script>

</body>
</html>
