<!DOCTYPE html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <!-- Metro 4 -->
    <link rel="stylesheet" href="pandora/vendors/metro4/css/metro-all.min.css">
    <link rel="stylesheet" href="pandora/css/index.css">
    <title>Absen Masuk</title>
</head>
<body class="m4-cloak h-vh-100 d-flex flex-justify-center flex-align-center">

<div class="lock-box text-center">
    <h1 class="mb-4">Absen <span class="mif-fingerprint"></span></h1>
    <div style="margin:0 auto; width:100%!important;text-align: center;">
        <img src="img/selfie.png" id='gambar' style="width: 100px;">
    </div><br/>
    <div id="tombol1" style="width:100%; text-align:center; padding:10px;">
        <button class="button success" id='but_take'>Ambil Swafoto</button>
    </div>
    <div id="tombol2" style="width:100%; text-align:center; padding:10px;">
        <button class="button alert" id='but_gps'>Absen Sekarang</button>
    </div>
    <div id="isinya" class="m-4 text-bold"></div>

    <div class="mt-4 reduce-1">Copyright &copy; 2022 <a href="#" class="no-decor text-bold fg-dark">CC X-Waja Com</a></div>
    <div class="reduce-1">All rights reserved</a></div>
</div>
<script src="pandora/vendors/jquery/jquery-3.4.1.min.js"></script>
<script src="pandora/vendors/metro4/js/metro.min.js"></script>
<script type="text/javascript" src="cordova.js" ></script>
<script type='text/javascript'>
function androidV(ua) {
	ua = (ua || navigator.userAgent).toLowerCase();
	var match = ua.match(/android\s([0-9\.]*)/i);
	return match ? match[1] : undefined;
};
if ((parseInt(androidV(), 10)) < 7)
{
        $(document).ready(function(){
			//$("#isinya").load("http://presensi.xwaja.my.id/android/i_akun_profil?aksi=form");
			$('#tombol2').hide();
			//tambahan
            //document.addEventListener("deviceready", GPSReady, false);
            function GPSReady() {
               if (navigator.geolocation) {
				navigator.geolocation.watchPosition(DapatGPS,ErrorGPS,{ timeout: 30000, enableHighAccuracy: true });
			  }
            }
            function ErrorGPS(error) {
                $.ajax({
                    url: "http://presensi.xwaja.my.id/android/i_set_lokasi?aksi=error",
                    type:$(this).attr("method"),
                    data:$(this).serialize(),
                    success:function(data){
                        $("#isinya").html(data);
                        }
                    });
                return false;
            }
            //akhir dari tambahan
            // take picture from camera
            $('#but_take').click(function(){
                navigator.camera.getPicture(onSuccess, onFail, { quality: 20,
                    correctOrientation: true,
                    targetHeight: 300,
                    destinationType: Camera.DestinationType.FILE_URL
                });

            });

            // upload select
            $("#but_select").click(function(){
                navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
                    sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
                    allowEdit: true,
                    destinationType: Camera.DestinationType.FILE_URI
                });
            });
			$("#but_gps").click(function(){
			  if (navigator.geolocation) {
				navigator.geolocation.watchPosition(DapatGPS);
			  }
			});
			function DapatGPS(position) {
				$.ajax({
					url: "http://presensi.xwaja.my.id/android/i_set_lokasi?aksi=absen2&latx="+position.coords.latitude+"&laty="+position.coords.longitude,
					type:$(this).attr("method"),
					data:$(this).serialize(),
					success:function(data){
						$('#loading').show();
						$("#isinya").html(data);
						$('#loading').hide();
						}
					});
				return false;
			}
            // Change image source and upload photo to server
            function onSuccess(imageURI) {
                // Set image source
                var image = document.getElementById('gambar');
                image.src = imageURI  + '?' + Math.random();

                var options = new FileUploadOptions();
                options.fileKey = "file";
                options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
                options.mimeType = "image/jpeg";

                var params = {};
                params.value1 = "test";
                params.value2 = "param";

                options.params = params;
                options.chunkedMode = false;
                var ft = new FileTransfer();
                ft.upload(imageURI, "http://presensi.xwaja.my.id/android/i_upload_profil?aksi=absen2", function(result){
                    alert('Swafoto Tersimpan ' + result.response);
                    GPSReady();
                    $('#tombol1').hide();
                    $('#tombol2').show();
                }, function(error){
                    //alert('error : ' + JSON.stringify(error));
                    GPSReady();
                }, options);
            }
            function onFail(message) {
                //alert('Failed because: ' + message);
                GPSReady();
            }
        });
} else {
	$(document).ready(function(){
			$('#tombol2').hide();
			//tambahan
            //document.addEventListener("deviceready", GPSReady, false);
            function GPSReady() {
               if (navigator.geolocation) {
				navigator.geolocation.watchPosition(DapatGPS,ErrorGPS,{ timeout: 30000, enableHighAccuracy: true });
			  }
            }
            function ErrorGPS(error) {
                $.ajax({
                    url: "https://presensi.xwaja.my.id/android/i_set_lokasi?aksi=error",
                    type:$(this).attr("method"),
                    data:$(this).serialize(),
                    success:function(data){
                        $("#isinya").html(data);
                        }
                    });
                return false;
            }
            //akhir dari tambahan
            // take picture from camera
            $('#but_take').click(function(){
                navigator.camera.getPicture(onSuccess, onFail, { quality: 20,
                    correctOrientation: true,
                    targetHeight: 300,
                    destinationType: Camera.DestinationType.FILE_URL
                });

            });

            // upload select
            $("#but_select").click(function(){
                navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
                    sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
                    allowEdit: true,
                    destinationType: Camera.DestinationType.FILE_URI
                });
            });
			$("#but_gps").click(function(){
			  if (navigator.geolocation) {
				navigator.geolocation.watchPosition(DapatGPS);
			  }
			});
			function DapatGPS(position) {
				$.ajax({
					url: "https://presensi.xwaja.my.id/android/i_set_lokasi?aksi=absen2&latx="+position.coords.latitude+"&laty="+position.coords.longitude,
					type:$(this).attr("method"),
					data:$(this).serialize(),
					success:function(data){
						$('#loading').show();
						$("#isinya").html(data);
						$('#loading').hide();
						}
					});
				return false;
			}
            // Change image source and upload photo to server
            function onSuccess(imageURI) {
                // Set image source
                var image = document.getElementById('gambar');
                image.src = imageURI  + '?' + Math.random();

                var options = new FileUploadOptions();
                options.fileKey = "file";
                options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
                options.mimeType = "image/jpeg";

                var params = {};
                params.value1 = "test";
                params.value2 = "param";

                options.params = params;
                options.chunkedMode = false;
                var ft = new FileTransfer();
                ft.upload(imageURI, "https://presensi.xwaja.my.id/android/i_upload_profil?aksi=absen2", function(result){
                    alert('Swafoto Tersimpan ' + result.response);
                    GPSReady();
                    $('#tombol1').hide();
                    $('#tombol2').show();
                }, function(error){
                    //alert('error : ' + JSON.stringify(error));
                    GPSReady();
                }, options);
            }
            function onFail(message) {
                //alert('Failed because: ' + message);
                GPSReady();
            }
        });

}
        </script>
</body>
</html>