<?php
session_start();
if(isset($_SESSION['username'])){

?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    <meta charset="utf-8" />
    <?php include('includes/styles.php'); ?>
    <title> Edit Clergy | <?php echo $church_name; ?></title>
   
</head>

<?php

?>

<body class="fixed-header dashboard">
<?php include('includes/sidebar.php'); ?>
<?php $base_url = $conn->base_url(); ?>
    <?php
      if(isset($_GET['clergy'])){
          $clergy_id = $_GET['clergy'];
          $sql = "SELECT * FROM clergy WHERE id =:id";
          $conn->query($sql);
          $conn->bind(":id", $clergy_id);
          $result = $conn->fetchSingle();
          $db_id = $result->id;
          if($clergy_id == $db_id){
              $clergy_name = $result->name;
              $designation = $result->designation;
              $district = $result->districtID;
              $church = $result->church;
              $sex = $result->sex;
              $phone = $result->phone;
              $address = $result->address;
              $regdate = $result->reg_date;
              $amount = $result->amount;
              $bible = $result->bible;
              $conyear = $result->conyear;



              //fetch class name
              $sql = "SELECT name FROM districts WHERE id =:id";
              $conn->query($sql);
              $conn->bind(":id", $district);
              $districtName = $conn->fetchColumn();
              
    ?>

<div class="page-container ">
    <?php include('includes/header.php'); ?>
    <!-- END HEADER -->
    <!-- START PAGE CONTENT WRAPPER -->
    <div class="page-content-wrapper ">
        <!-- START PAGE CONTENT -->
        <div class="content ">
            <!-- START CONTAINER FLUID -->
            <div class=" container-fluid   container-fixed-lg">
                <!-- START BREADCRUMB -->
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="<?php echo $base_url; ?>">Home</a></li>
                    <li class="breadcrumb-item"><a href="<?php echo $base_url; ?>pastor">Clergies</a></li>
                    <li class="breadcrumb-item active">Edit Clergy</li>
                </ol>
                <!-- END BREADCRUMB -->
                <div class="row m-t-50">
                <div class="col-xl-8 col-lg-8">
                        <!-- START card -->
                        <div class="card">
                            <div class="card-header ">
                                <div class="card-title">Form A
                                </div>
                            </div>
                            <div class="card-body">
                                <form action="" class="" method="post" id="clergy-edit">
                                    <h3 style="margin-bottom: 0" class="mw-80">Edit Clergy's Data Below</h3>
                                    <p class="mw-80 m-b-50">All fields marked <span style="color: red">*</span> must be filled
                                    
                                        <div class="form-group form-group-default required">
                                            <label>Clergy's Name</label>
                                            <input type="text" class="form-control" id="name" required name="name" value="<?php echo $clergy_name; ?>">
                                        </div>


                                        <div class="form-group form-group-default required">
                                            <label>Conference Year</label>
                                            <input type="text"  name="conyear" id="conyear" class="form-control" value="<?php echo $conyear; ?>" required>
                                        </div>

                                        <div class="form-group form-group-default required">
                                            <label>Designation</label>
                                            <input type="text"  name="designation" id="designation" class="form-control" value="<?php echo $designation; ?>" required>
                                        </div>

                                        <div class="form-group form-group-default required">
                                        <label>Amount</label>
                                        <input type="text" oninput="myFunction()" id="postcontent" class="autonumeric form-control" name="null" value="<?php echo $amount; ?>">
                                        </div>
                                        
                                        <textarea hidden id="demo" name="amount" rows="5"><?php echo $amount; ?></textarea>
                                        
                                        
                                        <div class="form-group form-group-default form-group-default-select2 required">
                                            <label class="">District</label>
                                            <?php

                                            //fetch all classes
                                            $sql = "SELECT * FROM districts WHERE name!=:class";
                                            $conn->query($sql);
                                            $conn->bind(":class", $districtName);
                                            $details = $conn->fetchMultiple();

                                            
                                            ?>
                                            <select name="district" id="districtID"  class="full-width select2-hidden-accessible" required  data-placeholder="Select District" data-init-plugin="select2" aria-hidden="true">
                                            <option value="<?php echo $district; ?>" selected> <?php echo $districtName; ?> (Selected)</option>
                                                <?php
                                                if($conn->rowCount() > 0){
                                                    foreach ($details as $district) {
                                                        ?>
                                                        <option value="<?php echo $district->id; ?>">
                                                            <?php echo $district->name; ?></option>
                                                        <?php
                                                    }
                                                }else {
                                                    ?>
                                                <option value="">No District Found</option><?php
                                                }

                                                ?>
                                            </select>
                                        </div> 

                                        

                                        <div class="form-group form-group-default required">
                                            <label>Church Name</label>
                                            <input type="text"  name="church" id="church" class="form-control" value="<?php echo $church; ?>" required>
                                        </div>


                                        <div class="form-group form-group-default form-group-default-select2 required">
                                            <label class="">Sex</label>
                                            <select name="sex" id="sex" data-parsley-trigger="keyup" class="full-width select2-hidden-accessible" required data-placeholder="Select Sex" data-init-plugin="select2" aria-hidden="true" value="<?php echo $sex; ?>">
                                                <option value="<?php echo $sex; ?>" selected> <?php echo $sex; ?> (Selected)</option>
                                                <option value="Male">Male</option>
                                                <option value="Female">Female</option>
                                            </select>
                                        </div>



                                    <div class="form-group form-group-default input-group disabled">
                                        <div class="form-input-group date">
                                            <label class="fade">Registration Date & Time</label>
                                            <input readonly type="text" class="form-control" <?php  
    $orgDate = $regdate;  
    $newDate = date("l, jS F, Y h:i A", strtotime($orgDate)); 
?>  value="<?php echo $newDate; ?>"  placeholder="Pick a date" id="null" name="null">
                                        </div>
                                        <!-- <div class="input-group-append">
                                            <span class="input-group-text"><i class="pg-icon">calendar</i></span>
                                        </div> -->
                                    </div>

                                        <div class="form-group form-group-default">
                                            <label>Phone Number</label>
                                            <input type="tel" name="phone" id="phone" class="form-control" maxlength="11"
                                                   data-parsley-type="number" data-parsley-length="[11,11]" pattern="\d{11}" value="<?php echo $phone; ?>">
                                        </div>

                                        <div class="form-group form-group-default">
                                            <label>Residential Address</label>
                                            <input type="text" name="address" id="address" class="form-control" value="<?php echo $address; ?>">
                                        </div>

                                        <div class="form-group form-group-default">
                                                <label class="">Bible Study Group </label>
                                                <input type="text" name="bible" id="bible" value="<?php echo $bible; ?>" class="form-control">
                                            </div>
                                          
                                        <input type="hidden" name="id" value="<?php echo $clergy_id; ?>">  
        
                                    <div class="row m-t-50">
                                        <div class="col-xl-6">
                                            <p class="small-text hint-text">Note: Please crosscheck all data provided before submitting to database </p>
                                        </div>
                                        <div class="col-xl-6">
                                            <input aria-label="" type="submit" value="Update Clergy" name="add_clergy" id="submit" class="btn btn-primary pull-right btn-lg btn-block btn-animated from-left">
                                                
                                        </div>
                                    </div>


                                    <div id="success-msg"></div>

                                </form>
                            </div>
                        </div>
                        <!-- END card -->
                    </div>


                    <div class="col-lg-4">
                        
                        <div id="card-linear-color" class="card card-default">
                            <div class="card-header  ">
                                <div class="card-title">Instructions
                                </div>
                                <div class="card-controls">
                                    <ul>
                                        <li><a href="#" class="card-collapse" data-toggle="collapse"><i class="card-icon card-icon-collapse"></i></a>
                                        </li>
                                        <li><a href="#" id="newsreload" onclick="loadDoc()" class="card-refresh" data-source="add-member-new.php" data-toggle="refresh"><i class="card-icon card-icon-refresh"></i></a>
                                        </li>
                                        <li><a href="#" class="card-close" data-toggle="close"><i class="card-icon card-icon-close"></i></a>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                            <div class="card-body" id="news">
                                <?php include('notice.php'); ?>
                            </div>
                        </div>
                    </div>


                </div>
                </div>
            </div>
            <!-- END CONTAINER FLUID -->
        </div>
        <!-- END PAGE CONTENT -->


        <?php include('includes/foot.php'); ?>
    </div>
    <!-- END PAGE CONTENT WRAPPER -->
</div>





<!-- END QUICKVIEW-->

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


    <script>
        $('[data-toggle="tooltip"]').tooltip();
    </script>
    
   

    <script>
        $('#clergy-edit');
        $('#clergy-edit').on('submit', function(event){
            event.preventDefault();
            if ($('#clergy-edit').parsley().isValid()){
                $.ajax({
                    url: "<?php echo $base_url; ?>process-edit-clergy.php",
                    method: "POST",
                    data: new FormData(this),
                    contentType: false,
                    processData: false,
                    beforeSend: function () {
                        $('#submit').attr('disabled', 'disabled');
                        $('#submit').val('Updating Details, Please Wait...');

                    },

                    success:function (data) {
                        $('#clergy-edit').parsley().reset();
                        $('#submit').attr('disabled', false);
                        $('#submit').val('Update Clergy');
                        $('#success-msg').html(data);
                    }
                })
            }

        })
    </script>



<script>
    function loadDoc() {
        const xhttp = new XMLHttpRequest();
        xhttp.onload = function() {
            document.getElementById("news").innerHTML = this.responseText;
        }
        xhttp.open("GET", "../notice.php", true);
        xhttp.send();
    }

    setTimeout(function () {document.getElementById("newsreload").click();}, 2000);
</script>



<script>
    $(function() {
        setTimeout(function() { $("#notify").fadeOut(1500); }, 10000)

    })
</script>
<script>
 

function myFunction() 
{
var x = document.getElementById("postcontent").value;
document.getElementById("demo").innerHTML = x.replace(/[.,]+/g, '');
}
</script>

<?php
}else{
    ?>
    
  <?php include('session.php'); ?>

    <?php

}
      }}
?>