<?php

include('../class/Appointment.php');

$object = new Appointment;

if(!$object->is_login())
{
    header("location:".$object->base_url."");
}

if($_SESSION['arsiparis_type'] != 'User')
{
    header("location:".$object->base_url."");
}

$object->query = "
    SELECT * FROM arsiparis_table
    WHERE arsiparis_nip = '".$_SESSION["arsiparis_id"]."'
    ";

$result = $object->get_result();

include('header.php');

?>

                    <!-- Page Heading -->
                    <!--h1 class="h3 mb-4 text-gray-800">Profile</h1-->

                    <!-- DataTales Example -->
                    
                    <form method="post" id="profile_form" enctype="multipart/form-data">
                        <div class="row"><div class="col-md-10"><span id="message"></span><div class="card shadow mb-4">
                            <div class="card-header py-3">
                                <div class="row">
                                    <div class="col">
                                        <h6 class="m-0 font-weight-bold text-primary">Profile</h6>
                                    </div>
                                    <div clas="col" align="right">
                                        <input type="hidden" name="action" value="arsiparis_profile" />
                                        <input type="hidden" name="hidden_id" id="hidden_id" />
                                        <button type="submit" name="edit_button" id="edit_button" class="btn btn-primary btn-sm"><i class="fas fa-edit"></i> Edit</button>
                                        &nbsp;&nbsp;
                                    </div>
                                </div>
                            </div>
                            <div class="card-body">
                                <!--<div class="row">
                                    <div class="col-md-6">!-->
                                        <span id="form_message"></span>
                                        <div class="form-group">
                                            <div class="row">
                                                <div class="col-md-6">
                                                    <label>Email Address <span class="text-danger">*</span></label>
                                                    <input type="text" name="arsiparis_email_address" id="arsiparis_email_address" class="form-control" required data-parsley-type="email" data-parsley-trigger="keyup" />
                                                </div>
                                                <div class="col-md-6">
                                                    <label>Password <span class="text-danger">*</span></label>
                                                    <input type="password" name="arsiparis_password" id="arsiparis_password" class="form-control" required  data-parsley-trigger="keyup" />
                                                </div>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <div class="row">
                                                <div class="col-md-6">
                                                    <label>NIP <span class="text-danger">*</span></label>
                                                    <input type="text" name="arsiparis_nip" id="arsiparis_nip" class="form-control" required readonly data-parsley-trigger="keyup" />
                                                </div>
                                                <div class="col-md-6">
                                                    <label>Name <span class="text-danger">*</span></label>
                                                    <input type="text" name="arsiparis_name" id="arsiparis_name" class="form-control" required data-parsley-pattern="/^[a-zA-Z., \s]+$/" data-parsley-trigger="keyup" />
                                                </div>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <div class="row">
                                                <div class="col-md-6">
                                                    <label>Phone </label>
                                                    <input type="text" name="arsiparis_hp" id="arsiparis_hp" data-parsley-pattern="/^[0-9 \s]+$/" class="form-control" data-parsley-trigger="keyup"/>
                                                </div>
                                                <div class="col-md-6">
                                                    <label>Position</label>
                                                    <input type="text" name="arsiparis_position" id="arsiparis_position" class="form-control" />
                                                </div>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label>Image <span class="text-danger">*</span></label>
                                            <br />
                                            <input type="file" name="arsiparis_profile_image" id="arsiparis_profile_image" />
                                            <div id="uploaded_image"></div>
                                            <input type="hidden" name="hidden_arsiparis_profile_image" id="hidden_arsiparis_profile_image" />
                                        </div>
                                    <!--</div>
                                </div>!-->
                            </div>
                        </div></div></div>
                    </form>
                <?php
                include('footer.php');
                ?>

<script>
$(document).ready(function(){

    <?php
    foreach($result as $row)
    {
    ?>
    $('#hidden_id').val("<?php echo $row['arsiparis_nip']; ?>");
    $('#arsiparis_email_address').val("<?php echo $row['arsiparis_email']; ?>");
    $('#arsiparis_password').val("<?php echo $row['arsiparis_password']; ?>");
	$('#arsiparis_name').val("<?php echo $row['arsiparis_name']; ?>");
    $('#arsiparis_hp').val("<?php echo $row['arsiparis_hp']; ?>");
    $('#arsiparis_nip').val("<?php echo $row['arsiparis_nip']; ?>");
	$('#arsiparis_position').val("<?php echo $row['arsiparis_position']; ?>");
    $('#uploaded_image').html('<img src="<?php echo $row["arsiparis_image"]; ?>" class="img-thumbnail" width="100" /><input type="hidden" name="hidden_arsiparis_profile_image" value="<?php echo $row["arsiparis_image"]; ?>" />');

    $('#hidden_arsiparis_profile_image').val("<?php echo $row['arsiparis_image']; ?>");
    <?php
    }
    ?>
	
    $('#arsiparis_profile_image').change(function(){
        var extension = $('#arsiparis_profile_image').val().split('.').pop().toLowerCase();
        if(extension != '')
        {
            if(jQuery.inArray(extension, ['png','jpg']) == -1)
            {
                alert("Invalid Image File");
                $('#arsiparis_profile_image').val('');
                return false;
            }
        }
    });
    $('#profile_form').parsley();

	$('#profile_form').on('submit', function(event){
		event.preventDefault();
		if($('#profile_form').parsley().isValid())
		{		
			$.ajax({
				url:"profile_action.php",
				method:"POST",
				data:new FormData(this),
                dataType:'json',
                contentType:false,
                processData:false,
				beforeSend:function()
				{
					$('#edit_button').attr('disabled', 'disabled');
					$('#edit_button').html('wait...');
				},
				success:function(data)
				{
					$('#edit_button').attr('disabled', false);
                    $('#edit_button').html('<i class="fas fa-edit"></i> Edit');

                    $('#arsiparis_email_address').val(data.arsiparis_email_address);
                    $('#arsiparis_password').val(data.arsiparis_password);
                    $('#arsiparis_name').val(data.arsiparis_name);
                    $('#arsiparis_hp').val(data.arsiparis_hp);
                    $('#arsiparis_nip').text(data.arsiparis_nip);
                    $('#arsiparis_position').text(data.arsiparis_position);
                    if(data.arsiparis_profile_image != '')
                    {
                        $('#uploaded_image').html('<img src="'+data.arsiparis_profile_image+'" class="img-thumbnail" width="100" />');

                        $('#user_profile_image').attr('src', data.arsiparis_profile_image);
                    }

                    $('#hidden_arsiparis_profile_image').val(data.arsiparis_profile_image);
						
                    $('#message').html(data.success);

					setTimeout(function(){

				        $('#message').html('');

				    }, 5000);
				}
			})
		}
	});

});
</script>