<?php
session_start();
$title = "AdminAccount Setup";
$group = "adminSetup";
$menu = "adminSetup";
include('header.php');

$firstname="";
$lastname="";
$password="";
$email = "";
$role = "";
$date_created = "";
$is_active = "";
if(isset($_GET['id']))
{    
    $sql="select * from users where id=:id";
    $value=array(':id'=>$_GET['id']);
    $r=$con->select_query($sql,$value);
    foreach($r as $value)
    {
        $firstname=$value['firstname'];
        $lastname=$value['lastname'];
        $password=$value['$pword'];
        $email = $value['email'];
        $role = $value['role'];
        $date_created = $value['date_created'];
        $is_active = $value['is_active'];
    }
}
?>
<script>

function SaveInsert()
{
	var datastring = $("#adminSetupForm").serialize();
	var l = $( '.ladda-button-demo1' ).ladda();
	l.ladda( 'start');	
	$.ajax({
	            type: "POST",
	            url: "../include/insert_ajax.php",
	            data: datastring,
	            dataType: 'json',
	            cache: false,
	            success: function(data) {
	            	l.ladda( 'stop' );
	            	if(data.success == 1)
	            	{
	            		setTimeout(function() {
	                        toastr.options = {
	                            closeButton: true,
	                            progressBar: true,
	                            positionClass: "toast-top-full-width",
	                            showMethod: 'slideDown',
	                            timeOut: 4000
	                        };
	                        toastr.success('Admin added successfully');
	                        $("input[type='text'], input[type='email'], input[type='password']").val("");
	                    }, 1300);
	            	}
	            	else if(data.success == 2)
	            	{
                        data.error.forEach(dError => {
                            setTimeout(function() {
                                toastr.options = {
                                    closeButton: true,
                                    progressBar: true,
                                    positionClass: "toast-top-full-width",
                                    showMethod: 'slideDown',
                                    timeOut: 4000
                                };
                                toastr.error(dError);

                            }, 1300);
                        });
	            	}
	            	else if(data.success == 3)
	            	{
	            		setTimeout(function() {
	                        toastr.options = {
	                            closeButton: true,
	                            progressBar: true,
	                            positionClass: "toast-top-full-width",
	                            showMethod: 'slideDown',
	                            timeOut: 4000
	                        };
	                        toastr.error('Error, please enter required fields');

	                    }, 1300);
	            	}
	            },
	            error: function(){
	                  alert('error handling here');
	                  l.ladda( 'stop' );
	            }
	        });
}

// function SaveUpdate()
// {
// 	var datastring = $("#adminSetupForm").serialize();
	
// 	var l = $( '.ladda-button-demo2' ).ladda();
// 	l.ladda( 'start');
// 	$.ajax({
// 	            type: "POST",
// 	            url: "../include/update_ajax.php",
// 	            data: datastring,
// 	            dataType: 'json',
// 	            cache: false,
// 	            success: function(data) {
// 	            	l.ladda( 'stop' );
// 	            	if(data.success == 1)
// 	            	{
// 	            		setTimeout(function() {
// 	                        toastr.options = {
// 	                            closeButton: true,
// 	                            progressBar: true,
// 	                            positionClass: "toast-top-full-width",
// 	                            showMethod: 'slideDown',
// 	                            timeOut: 4000
// 	                        };
// 	                        toastr.success('Admin saved successfully');
// 	                    }, 1300);
// 	            	}
// 	            	else if(data.success == 2)
// 	            	{
//                         data.error.forEach(dError => {
//                             setTimeout(function() {
//                                 toastr.options = {
//                                     closeButton: true,
//                                     progressBar: true,
//                                     positionClass: "toast-top-full-width",
//                                     showMethod: 'slideDown',
//                                     timeOut: 4000
//                                 };
//                                 toastr.error(dError);

//                             }, 1300);
//                         });
// 	            	}
// 	            	else if(data.success == 3)
// 	            	{
// 	            		setTimeout(function() {
// 	                        toastr.options = {
// 	                            closeButton: true,
// 	                            progressBar: true,
// 	                            positionClass: "toast-top-full-width",
// 	                            showMethod: 'slideDown',
// 	                            timeOut: 4000
// 	                        };
// 	                        toastr.error('Error, please enter required fields');

// 	                    }, 1300);
// 	            	}
	            	
// 	            },
// 	            error: function(){
// 	                  alert('error handling here');
// 	                  l.ladda( 'stop' );
// 	            }
// 	        });
// }


</script>

   <style>
.input-daterange input {
    text-align: left;
}
</style> 

<div class="content-body">
        	<div class="row">
                    <div id="recent-transactions" class="col-12">
                        <div class="card">
                            <div class="card-header">
                                <h4 class="card-title">Admin Account Setup</h4>
                            </div>
                            <div class="card-content">
                            	<form method="post" action="admin_setup" id="adminSetupForm" enctype="multipart/form-data">
                                <div class="row row-pad">
                                    <div class="col-md-3">
                                        <label for="name">First Name</label>
                                    </div>
                                    <div class="col-md-6">
                                        <input type="text" name="firstname" class="form-control" value="<?php echo $firstname;?>" required/>
                                    </div>
                                </div>
                                
                                <div class="row row-pad">
                                    <div class="col-md-3">
                                        <label for="name">Last Name</label>
                                    </div>
                                    <div class="col-md-6">
                                        <input type="text" name="lastname" class="form-control" value="<?php echo $lastname;?>" required/>
                                    </div>
                                </div>
                                
                                <div class="row row-pad">
                                    <div class="col-md-3">
                                        <label for="name">Email</label>
                                    </div>
                                    <div class="col-md-6">
                                        <input type="email" name="email" class="form-control" value="<?php echo $email;?>" required/>
                                    </div>
                                </div>
                                
                                <div class="row row-pad">
                                    <div class="col-md-3">
                                        <label for="name">Password</label>
                                    </div>
                                    <div class="col-md-6">
                                        <input type="password" name="password" class="form-control" value=""/>
                                    </div>
                                </div>
                                
                                <div class="row row-pad">
                                    <div class="col-md-3">
                                        <label for="name">Repeat Password</label>
                                    </div>
                                    <div class="col-md-6">
                                        <input type="password" name="r_password" class="form-control" value=""/>
                                    </div>
                                </div>
                                                              
                                <div class="row row-pad">
                                    <div class="col-md-3">
                                        
                                    </div>
                                    <div class="col-md-6">
                                    <?php 
                                        // if(isset($_GET['id'])){
                                        //     echo '
                                        //     <input type="hidden" name="update" value="admin_setup"/>
                                        //     <input type="hidden" name="id" value="'.$_GET['id'].'"/>
                                        //     <button type="button" onclick="SaveUpdate()" class="ladda-button ladda-button-demo2 btn btn-primary"  data-style="zoom-in">Update Admin</button>';
                                        // } else {
                                            echo '
                                            <input type="hidden" name="insert" value="admin_setup"/>
                                            <button type="button" onclick="SaveInsert()" class="ladda-button ladda-button-demo1 btn btn-primary"  data-style="zoom-in">Add Admin</button>';
                                        // }
                                    ?>
                                        <a href="admins" class="btn btn-warning">Cancel</a>
                                    </div>
                                </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <!--/ Recent Transactions -->

   </div>    
    
    
    <?php include('footer.php');?>

   
