<?php 
require('includes/header_empty.php');
require('includes/setup.inc.php'); 
?>	
<head>
<script src="https://www.google.com/recaptcha/api.js?onload=CaptchaCallback&render=explicit" async defer></script>
</head>

<!--begin::Content-->
<div class="content d-flex flex-column flex-column-fluid" id="kt_content">
	<!--begin::Entry-->
	<div class="d-flex flex-column-fluid">
		<!--begin::Container-->
		<div class="container">
			<!--begin::Dashboard--> 
			<!--begin::Row-->
			<div class="row">
				<div class="col-lg-12">
					<!--begin::List Widget 11-->
					<div class="card card-custom card-stretch gutter-b">
						<!--begin::Header-->
						<div class="card-header border-0">
							<h3 class="card-title font-weight-bolder text-dark">Borang Permohonan Pendaftaran Pengguna (Penternak)</h3>
						</div>
						<!--end::Header-->
						<!--begin::Body-->
						<div class="card-body pt-0">
							<div class="d-flex align-items-center mb-9 bg-light-success rounded p-5">
								<form action="javascript:;" class="form-horizontal" name="pemohon_form" id="pemohon_form">
									<div class="form-body">
										<div class="row">
											<?php
												$panduanKataLaluan = '- Tidak kurang dari 8 karakter<br>- Gabungan alphabet, nombor dan simbol<br>- Simbol yang dibenarkan adalah !@#$*_ sahaja';
												createTextBox('Nama', 'nama', 'Nama', $indicator_List['nama'], 3, $readonly, 7, 12, '', 'text', 'Sila masukkan nama pemohon');
												createTextBox('No. Telefon Pejabat/Bimbit', 'no_tel', 'No. Telefon Pejabat/Bimbit', $indicator_List['no_tel'], 3, $readonly, 7, 12, '', 'numonly');
												createTextBox('Emel', 'emel', 'Emel', $indicator_List['emel'], 3, $readonly, 7, 12, '', 'email');
												//createDropDown($PDO, 'Jabatan', 'i_jabatan', 'id_jabatan', $indicator_List['id_jabatan'], 'Sila Pilih Jabatan', 7, 3, 12, $readonly);
												$my_sql = 'SELECT id_peranan AS id, desk_peranan as name 
														   FROM t_peranan 
														   WHERE id_peranan NOT IN (1,2,3,10)
														   ORDER BY susunan';
												//createDropDown_sql($PDO, 'Peranan', $my_sql, 'id_peranan', $indicator_List['id_peranan'], 'Sila Pilih Peranan', 7, 3, 12, $readonly);
												//createDropDown($PDO, 'Parlimen', 'i_parlimen', 'id_parlimen', $indicator_List['id_parlimen'], 'Sila Pilih Parlimen', 7, 3, 12, $readonly);
												//createDropDown($PDO, 'DUN', 'i_dun', 'id_dun', $indicator_List['id_dun'], 'Sila Pilih DUN', 7, 3, 12, $readonly);
												//createDropDown($PDO, 'Daerah', 'i_daerah', 'id_daerah', $indicator_List['id_daerah'], 'Sila Pilih Daerah', 7, 3, 12, $readonly);
												createTextBox('Id Pemohon (No Kad Pengenalan Baru)', 'pemohon_login', 'Id Pemohon (No Kad Pengenalan Baru)', $indicator_List['login'], 3, $readonly, 7, 12, '', 'numonly', 'Sila masukkan Login ID Pemohon','maxlength="12"');
												createTextBox('Kata laluan', 'pemohon_pwd', 'Kata laluan', '', 3, $readonly, 7, 12, '', 'password', $panduanKataLaluan);
												createTextBox('Kata laluan (Ulang)', 'pemohon_pwd2', 'Kata laluan (Ulang)', '', 3, $readonly, 7, 12, '', 'password');
												createDropDown($PDO, 'Soalan Rahsia', 'i_soalanrahsia', 'id_soalanrahsia', $indicator_List['id_soalanrahsia'], 'Pilih Soalan Rahsia', 7, 3, 12, $readonly);
												createTextBox('Jawapan Rahsia', 'jawapan_rahsia', 'Jawapan Rahsia', $indicator_List['jawapan_rahsia'], 3, $readonly, 7, 12, '', 'text');
											?>
										</div>
									</div>
									<div class="col-12" align="center" id="RecaptchaField1" data-callback="enableSubmitBtn"></div>
									<div class="col-12" style="text-align:center" title="Sila tandakan saya buka robot'">
										<button type="button" class="btn btn-primary" id="saveBtn" name="saveBtn" disabled> Daftar</a>
									</div>
								</form>
							</div>
						</div>
						<!--end::Body-->
					</div>
					<!--end::List Widget 11-->
				</div>
			</div>
			<!--end::Row-->
			<!--end::Dashboard-->
		</div>
		<!--end::Container-->
	</div>
	<!--end::Entry-->
</div>
<!--end::Content-->
					
<?php 
require('includes/footer.php');
?>	
<script type="text/javascript">
    var CaptchaCallback = function() {
        grecaptcha.render('RecaptchaField1', {'sitekey' : '6Lc_UxsUAAAAAE4GXSjP-91d_s3s_HV5oDKNhtVU',
        'callback' : "enableSubmitBtn"});
    };
</script>
<script type="text/javascript">
  var onloadCallback = function() {
      grecaptcha.render( 'captcha2', {
        'sitekey' : '6Lc_UxsUAAAAAE4GXSjP-91d_s3s_HV5oDKNhtVU',
        });
    alert("grecaptcha is ready!");
  };
</script>
<script>
var enableBtn1 = false;
function enableSubmitBtn(){
    document.getElementById("saveBtn").disabled = false;
    enableBtn1 = true;
   }
</script>
<script>
$(document).ready(function(){  

	$('.select2').select2({ width: '100%' }); 
	
	$('#saveBtn').click(function() {
		var pemohon_list = [[]];
		pemohon_list.push(['nama', 'Nama', 'nm']);
		pemohon_list.push(['no_tel', 'No. Telefon Pejabat/Bimbit', 'tb']);
		pemohon_list.push(['emel', 'Email', 'em']);
		pemohon_list.push(['pemohon_login', 'Login ID', 'kp']);
		pemohon_list.push(['pemohon_pwd', 'Kata Laluan', 'pwd']);
		pemohon_list.push(['id_soalanrahsia', 'Soalan Rahsia', 'dd']);
		pemohon_list.push(['jawapan_rahsia', 'Jawapan Rahsia', 'tb']);
		
		if (checkInput(pemohon_list))
		{
			if($('#pemohon_pwd').val() == $('#pemohon_pwd2').val())
			{
				$(this).prop("disabled",true);
				var param = $('#pemohon_form').serialize();
				$.ajax({
					type: "POST", 
					url: "register-insert.php", 
					data: param,

					success: function(res){
						if(res > 0)
						{
							swal.fire({
								text: "Berjaya daftar penternak, proses pengesahan akan dimaklumkan",
								icon: "success",
								buttonsStyling: false,
								confirmButtonText: "OK!",
								customClass: {
									confirmButton: "btn font-weight-bold btn-light-primary"
								}
							}).then(function() {
								window.location = "../index.php";
							});
						}
						else if (res == -888)
						{
							swal.fire({
								text: "Id Pemohon telah wujud di dalam sistem",
								icon: "error",
								buttonsStyling: false,
								confirmButtonText: "OK!",
								customClass: {
									confirmButton: "btn font-weight-bold btn-light-primary"
								}
							}).then(function() {
								$('#pemohon_login').focus();
								$(this).prop("disabled",false);									
								});							
						}
						else if (res == -777)
						{
							swal.fire({
								text: "Emel telah wujud di dalam sistem",
								icon: "error",
								buttonsStyling: false,
								confirmButtonText: "OK!",
								customClass: {
									confirmButton: "btn font-weight-bold btn-light-primary"
								}
							}).then(function() {
								$('#emel').focus();
								$(this).prop("disabled",false);									
								});							
						}
						else
						{
							swal.fire({
								text: "Error. " + res,
								icon: "error",
								buttonsStyling: false,
								confirmButtonText: "OK!",
								customClass: {
									confirmButton: "btn font-weight-bold btn-light-primary"
								}
							}).then(function() {
								$(this).prop("disabled",false);										
							});
						}
						
					} 
				});  //end ajax
			}
			else
			{
				swal.fire({
					text: "Kata laluan tidak sepadan",
					icon: "error",
					buttonsStyling: false,
					confirmButtonText: "OK!",
					customClass: {
						confirmButton: "btn font-weight-bold btn-light-primary"
					}
				}).then(function() {
					$('#pemohon_pwd2').focus();
				});
				return false;
			}
		}
	});		
});		
</script>