<?php
    include 'inc/header.php'; 
    include("functions.php");
    ini_set('display_errors', '1');
    ini_set('display_startup_errors', '1');
    error_reporting(E_ALL);
    $update = false;
    $users = "";
    $user_idd = 0;
    if(isset($_GET['user_id'])){
        $update = true;
        $users = get_user_by_id($_GET['user_id']);
        $user_idd = $_GET['user_id'];
    }
?>

    <!--== BODY CONTNAINER ==-->
    <div class="container-fluid sb2">
        <div class="row">
            <?php include 'inc/sidebar.php';?>
            <div class="sb2-2">
                <div class="sb2-2-2">
                    <ul>
                        <li><a href="#"><i class="fa fa-home" aria-hidden="true"></i> Home</a>
                        </li>
                        <li class="active-bre"><a href="#">Admin Form</a>
                        </li>
                    </ul>
                </div>
                <div class="sb2-2-3">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="box-inn-sp">
                                <div class="inn-title">
                                    <h4>Add New Admin</h4>
                                </div>
                                <div class="tab-inn">
                                    <form id="addUserForm" class="mb-3" action="ajax-request.php" method="POST" autocomplete="off">
                                        <div class="row">
                                            <div class="input-field col s6">
                                                <input id="add-user-firstname" type="text" class="validate" name="fname" 
                                                value="<?php echo (($update === true) ? ($users['fname']) : ""); ?>">
                                                <label for="add-user-firstname">First Name</label>
                                            </div>
                                            <div class="input-field col s6">
                                                <input id="add-user-lastname" type="text" class="validate" name="lname" 
                                                value="<?php echo (($update === true) ? ($users['lname']) : ""); ?>">
                                                <label for="add-user-lastname">Last Name</label>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="input-field col s6">
                                                <input id="phone" type="number" class="validate" name="phone"
                                                value="<?php echo (($update === true) ? ($users['phone']) : ""); ?>">
                                                <label for="phone">Mobile</label>
                                            </div>
                                            <div class="input-field col s6">
                                                <input id="city" type="text" class="validate" name="city"
                                                value="<?php echo (($update === true) ? ($users['city']) : ""); ?>">
                                                <label for="city">City</label>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="input-field col s12">
                                                <input autocomplete="off" id="asd" type="email" class="validate" name="email" 
                                                value="<?php echo (($update === true) ? ($users['email']) : ""); ?>" >
                                                <label for="email">Email</label>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="input-field col s12">
                                                <input id="password" type="password" class="validate" name="password" autocomplete = "new-password">
                                                <label for="password">Password</label>
                                            </div>
                                        </div>

                                        <!-- hidden Fields -->
                                        <input type="hidden" name="uname" value="" id="uname" 
                                        value="<?php echo (($update === true) ? ($users['name']) : ""); ?>">
                                        <input type="hidden" name="role" id="role" value="1">

                                        <?php
                                        if($update == false){
                                        ?>
                                            <input type="hidden" name="admin-add-user" id="admin-add-user" value="1">
                                            <div class="row">
                                                <div class="input-field col s12">
                                                    <input type="submit" class="waves-effect waves-light btn-large" value="ADD USER">
                                                </div>
                                            </div>
                                        <?php
                                        }else{
                                        ?>
                                            <input type="hidden" name="admin-update-user" id="admin-update-user" value="1">
                                            <div class="row">
                                                <div class="input-field col s12">
                                                    <input type="submit" class="waves-effect waves-light btn-large" value="UPDATE USER">
                                                </div>
                                            </div>
                                        <?php
                                        }
                                        ?>
                                        <input type="hidden" name="user_id" id="user_id" value="<?=$user_idd;?>">


                                        


                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php include 'inc/footer.php';?>
<script type="text/javascript">
   $(document).ready(function() {

  $("#addUserForm").on('submit', (function(e) {
   e.preventDefault();

   console.log("form submitted here");
    $.ajax({
      url: $(this).attr('action'),
      type: "POST",
      data: new FormData(this),
      dataType: "json",
      contentType: false,
      cache: false,
      processData: false,

      success: function(data) {
        if(data.success == 1){
            promptMsg(1,data.msg);
            // location.reload();
            window.location.href = "/all-admins.php";
        }
        else{
            promptMsg(0,data.msg);
        }
              
      },
      error: function(e) {
        console.log(e);
      }
    });

  }));

   $("#add-user-lastname").keyup(function(){
      var fname = $("#add-user-firstname").val();
      var lname = $("#add-user-lastname").val();
      var uname = fname+" "+lname;

      $("#uname").val(uname);
   });
   $("#add-user-firstname").keyup(function(){
      var fname = $("#add-user-firstname").val();
      var lname = $("#add-user-lastname").val();
      var uname = fname+" "+lname;

      $("#uname").val(uname);
   });


  });
</script>