<?php
    $sql_cek = "SELECT * FROM tb_profil WHERE id_profil='1'";
    $query_cek = mysqli_query($koneksi, $sql_cek);
    $data_cek = mysqli_fetch_array($query_cek,MYSQLI_BOTH);
?>

<div class="card card-primary">
	<div class="card-header">
		<h3 class="card-title">
			<i class="fa fa-edit"></i> Edit Profil Desa</h3>
	</div>
	<form action="" method="post" enctype="multipart/form-data">
		<div class="card-body">

			<input type='hidden' class="form-control" name="id_profil" value="<?php echo $data_cek['id_profil']; ?>" readonly/>

			<div class="form-group row">
				<label class="col-sm-2 col-form-label">Nama Desa</label>
				<div class="col-sm-6">
					<input type="text" class="form-control" id="nama_desa" name="nama_desa" value="<?php echo $data_cek['nama_desa']; ?>" required>
				</div>
			</div>

			<div class="form-group row">
				<label class="col-sm-2 col-form-label">Alamat</label>
				<div class="col-sm-6">
					<input type="text" class="form-control" id="alamat" name="alamat" value="<?php echo $data_cek['alamat']; ?>" required>
				</div>
			</div>

			<div class="form-group row">
				<label class="col-sm-2 col-form-label">Kecamatan</label>
				<div class="col-sm-6">
					<input type="text" class="form-control" id="kecamatan" name="kecamatan" value="<?php echo $data_cek['kecamatan']; ?>" required>
				</div>
			</div>

            <div class="form-group row">
				<label class="col-sm-2 col-form-label">Kabupaten</label>
				<div class="col-sm-6">
					<input type="text" class="form-control" id="kabupaten" name="kabupaten" value="<?php echo $data_cek['kabupaten']; ?>" required>
				</div>
			</div>

            <div class="form-group row">
				<label class="col-sm-2 col-form-label">Provinsi</label>
				<div class="col-sm-6">
					<input type="text" class="form-control" id="provinsi" name="provinsi" value="<?php echo $data_cek['provinsi']; ?>" required>
				</div>
			</div>

            <div class="form-group row">
				<label class="col-sm-2 col-form-label">Kepala Desa</label>
				<div class="col-sm-6">
					<input type="text" class="form-control" id="kepala_desa" name="kepala_desa" value="<?php echo $data_cek['kepala_desa']; ?>" required>
				</div>
			</div>

            <div class="form-group row">
				<label class="col-sm-2 col-form-label">NIP Kades</label>
				<div class="col-sm-6">
					<input type="text" class="form-control" id="nip_kades" name="nip_kades" value="<?php echo $data_cek['nip_kades']; ?>" placeholder="Jika ada">
				</div>
			</div>

		</div>
		<div class="card-footer">
			<input type="submit" name="Ubah" value="Simpan Perubahan" class="btn btn-success">
		</div>
	</form>
</div>

<?php
if (isset ($_POST['Ubah'])){
    $sql_ubah = "UPDATE tb_profil SET 
        nama_desa='".$_POST['nama_desa']."',
        alamat='".$_POST['alamat']."',
        kecamatan='".$_POST['kecamatan']."',
        kabupaten='".$_POST['kabupaten']."',
        provinsi='".$_POST['provinsi']."',
        kepala_desa='".$_POST['kepala_desa']."',
        nip_kades='".$_POST['nip_kades']."'
        WHERE id_profil='".$_POST['id_profil']."'";
    $query_ubah = mysqli_query($koneksi, $sql_ubah);
    mysqli_close($koneksi);

    if ($query_ubah) {
        echo "<script>
      Swal.fire({title: 'Ubah Data Berhasil',text: '',icon: 'success',confirmButtonText: 'OK'
      }).then((result) => {if (result.value)
        {window.location = 'index.php?page=data-profil';
        }
      })</script>";
      }else{
      echo "<script>
      Swal.fire({title: 'Ubah Data Gagal',text: '',icon: 'error',confirmButtonText: 'OK'
      }).then((result) => {if (result.value)
        {window.location = 'index.php?page=data-profil';
        }
      })</script>";
    }
}
?>
