<?php
defined('APK') or exit('Anda tidak dizinkan mengakses langsung script ini!');
?>
    <div class='row'>
        <div class='col-md-7'>
            <div class="card">
             <div class="card-header">
			    <div class='right'>
				  <button type="button" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#modal">
                            <i class="fas fa-upload"></i> Impor</button> 		
		         </div>
                  <h5 class='card-title'><i class="fas fa-phone"></i> Kontak Me</h5></div>    
			  
			 	
                <div class='card-body'>
                <div class='table-responsive'>
                    <table id='example1' style="font-size: 12px" class='table'>
                        <thead>
                            <tr>
                                <th width='5px'>#</th>
                                <th>Nama</th>
                                <th>No Whatsapp</th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $kontakme = mysqli_query($koneksi, "SELECT * FROM kontakme where iduser='$user[id_user]' ORDER BY nama_kontak ASC"); ?>
                            <?php while ($kontak = mysqli_fetch_array($kontakme)) : ?>
                                <?php $no++; ?>
                                <tr>
                                    <td><?= $no ?></td>
									 <td><?= $kontak['nama_kontak'] ?></td>
                                    <td><?= $kontak['nomor'] ?></td>
                           
                                    <td style="text-align:center">
                                        <div class=''>
                                            <a href="?pg=<?= $pg ?>&ac=edit&id=<?= $kontak['id'] ?>"> <button class='btn btn-sm btn-outline-success'><i class='fa fa-edit'></i></button></a>
                                           <button data-id="<?= $kontak['id'] ?>" class="hapus btn-sm btn btn-outline-danger"><i class="fas fa-trash"></i></button>
                                        </div>
                                    </td>
                                </tr>
                            <?php endwhile ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
	 <div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" aria-hidden="true">
      <div class="modal-dialog">
       <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="tambahjadwal">Impor Kontak</h5>
               <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                      <div class="modal-body">
                                <form id='formupload' enctype='multipart/form-data'>
                                    <div class='col-md-12'>
                                        <label>Pilih File</label>
                                        <input type='file' name='file' class='form-control' required='true' />
                                    </div><p>
                                    <div class='right'>
                                       
                                        <button type='submit'  class='btn btn-sm btn-outline-success'><i class='fa fa-upload'></i> Import</button>
                                    </div>
                                </form>
                            
                        <div class='col-md-12'>
                        <span>Import harus xlsx</span>
						<hr>
						<a href='template/masterkontak.xlsx' class="btn-link"><b> Download Format</b></a>
						
                    </div>
                </div>
            </div>
      
	 </div>
    </div>


	<script>
$('#formupload').submit(function(e){
    e.preventDefault();
    var data = new FormData(this);
    
    $.ajax(
    {
        type: 'POST',
        url: 'gateway/import.php',
        enctype: 'multipart/form-data',
        data: data,
        cache: false,
        contentType: false,
        processData: false,
        success: function(data)
        {
            
            iziToast.info(
            {
                title: 'Sukses!',
                message: 'Kontak berhasil disimpan',
				titleColor: '#FFFF00',
				messageColor: '#fff',
				backgroundColor: 'rgba(0, 0, 0, 0.5)',
				 progressBarColor: '#FFFF00',
                  position: 'topRight'
            });
            setTimeout(function()
            {
                window.location.reload();
            }, 2000);
            
        }
    });
    return false;
});
</script>

    <div class='col-md-5'>
        <?php if ($ac == '') : ?>
            <form id="formkontak">
                <div class="card">
             <div class="card-header">
                        <h5 class='card-title'>Tambah Kontak</h5>
                    </div>
                    <div class='card-body'>
                        <div class='form-group-sm'>
                            <label class="bold">Nama</label>
                            <input type='text' name='nama' class='form-control' required='true' autocomplete="off" />
                        </div>
                        <div class='form-group-sm'>
                            <label class="bold">Nomor WA</label>
                            <input type='number' name='nomor' class='form-control' required='true' autocomplete="off" />
							<input type='hidden' name='iduser' value="<?= $user['id_user'] ?>" />
                        </div><p>
                         <div class='right '>
                            <button type='submit' name='submit' class='btn btn-sm btn-outline-primary'><i class='fa fa-check-circle'></i> Simpan</button>
                        </div>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
			<script>
$('#formkontak').submit(function(e){
    e.preventDefault();
    var data = new FormData(this);
    $.ajax(
    {
        type: 'POST',
        url: 'gateway/crud_kontak.php?pg=tambah',
        enctype: 'multipart/form-data',
        data: data,
        cache: false,
        contentType: false,
        processData: false,
        success: function(data){   
         if (data == 'OK') {		
            iziToast.info(
            {
                title: 'Sukses!',
                message: 'Data berhasil disimpan',
				titleColor: '#FFFF00',
				messageColor: '#fff',
				backgroundColor: 'rgba(0, 0, 0, 0.5)',
				 progressBarColor: '#FFFF00',
                  position: 'topRight'
            });
            setTimeout(function()
            {
                window.location.reload();
            }, 2000);
             } else {
			 iziToast.warning(
            {
                title: 'Gagal!',
                message: 'Nomor WA sudah ada',
				titleColor: '#FFFF00',
				messageColor: '#fff',
				backgroundColor: '#8b0000',
				 progressBarColor: '#FFFF00',
                  position: 'topRight'
            });
            setTimeout(function(){
                window.location.reload();
            }, 2000);	
           }			
        }
    });
    return false;
});
</script>			
        <?php elseif ($ac == 'edit') : ?>
            <?php
            $id = $_GET['id'];
            $k = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM kontakme WHERE id='$id'"));
            if (isset($_POST['submit'])) {
                $nomor = $_POST['nomor'];
                $nama = $_POST['nama'];
                $nama = str_replace("'", "&#39;", $nama);
               $cek = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM kontakme WHERE nomor='$nomor'"));
                if ($cek > 0) {
                   $info = info("Nomor $nomor sudah ada!", "NO");
                } else {
                    
                       
                        $exec = mysqli_query($koneksi, "UPDATE kontakme SET nama_kontak='$nama',nomor='$nomor' WHERE id='$_GET[id]'");
                        (!$exec) ? $info = info("Gagal menyimpan!", "NO") : jump("?pg=$pg");
                    
                }
            }
            ?>
            <form action='' method='post'>
                 <div class="card">
             <div class="card-header">
                        <h5 class='card-title'>Edit</h5>
                        
                    </div>
                    <div class='card-body'>				
                        <div class='form-group-sm'>
                            <label class="bold">Nama</label>
                            <input type='text' name='nama' class='form-control' value="<?=$k['nama_kontak'] ?>" required='true' autocomplete="off" />
                        </div>
                        <div class='form-group-sm'>
                            <label class="bold">Nomor WA</label>
                            <input type='number' name='nomor' class='form-control' value="<?=$k['nomor'] ?>" required='true' autocomplete="off" />
                        </div><p>
                        <div class='right '>
                            <button type='submit' name='submit' class='btn btn-sm btn-flat btn-outline-primary'><i class='fa fa-check-circle'></i>Simpan</button>
                            <a href='?pg=<?= $pg ?>' class='btn btn-sm btn-outline-danger' title='Batal'><i class='fa fa-times-circle'></i>Back</a>
                        </div>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        
        <?php endif ?>
    </div>
</div>
<script>
$('#example1').on('click', '.hapus', function() {
        var id = $(this).data('id');
        console.log(id);
        Swal.fire({
				  title: 'Are you sure?',
				  text: "You won't be able to revert this!",
				  type: 'warning',
				  showCancelButton: true,
				  confirmButtonColor: '#3085d6',
				  cancelButtonColor: '#d33',
				  confirmButtonText: 'Yes, delete it!'	
        }).then((result) => {
            if (result.value) {
                $.ajax({
                   url: 'gateway/crud_kontak.php?pg=hapus',
                    method: "POST",
                    data: 'id=' + id,
                    success: function(data) {
                 iziToast.warning(
            {
                title: 'Sukses!',
                message: 'Data berhasil dihapus',
				titleColor: '#FFFF00',
				messageColor: '#fff',
				backgroundColor: 'rgba(0, 0, 0, 0.5)',
				 progressBarColor: '#FFFF00',
                  position: 'topRight'			  
                });
                        setTimeout(function() {
                            window.location.reload();
                        }, 2000);
                    }
                });
            }
            return false;
        })

    });

</script>
