<?php
session_start();
include '../assets/constant/config.php';

try {
  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
  echo "Connection failed: " . $e->getMessage();
}
?>
<?php include('include/sidebar.php'); ?>

<?php include('include/header.php'); ?>

<!--  connect author at mdkhairnar92@gmail.com  -->

<div class="page-content-wrapper ">

  <div class="container-fluid">

    <div class="row">
      <div class="col-sm-12">
        <div class="page-title-box">
          <div class="btn-group float-right">


          </div>
          <h4 class="page-title">Edit member</h4>
        </div>
      </div>
      <div class="clearfix"></div>
    </div>
    <!-- end page title end breadcrumb -->
    <div class="row">
      <div class="col-lg-12">
        <div class="card">
          <div class="card-body">

            <div class="tab-content">

              <div class="tab-pane active p-3" id="home" role="tabpanel">
                <form name="myform" action="app/member_crud.php" class="row" method="POST" enctype="multipart/form-data" onsubmit="return validation()" id="add_service">
                  <?php
                  $stmt = $conn->prepare("SELECT * FROM `member` WHERE id=? ");
                  $stmt->execute([$_POST['id']]);
                  $record = $stmt->fetchAll();

                  foreach ($record as $key) { ?>
         
         <input class="form-control" type="hidden" name="id" value="<?php echo $key['id']; ?>">
                  
                   <div class="form-group col-md-6">
                              <label for="id_label_multiple">Branch<span class="text-danger">*</span> </label>
                              <select class="form-control postName" id="role" aria-label="Default select example" type="text" name="branch" >
                              <option value="">Select branch</option>
                               <?php    
                                $sql1=$conn->prepare("select * from `branch` where delete_status='0' ");
                                $sql1->execute();
                                $row1=$sql1->fetchAll();
                                //print_r($row);
                                //exit;
                                foreach ($row1 as $key1) {?>
                                <option value="<?php echo $key1['branch_name'];?> ">
                                       <?php echo $key1['branch_name'];?>
                                        </option>
                                    <?php } ?>
                              </select>
                           </div>
                  <div class="form-group col-md-6">
                    <label class="control-label">Full Name<span class="text-danger">*</span></label>
                    <input class="form-control" type="text" name="name" value="<?php echo $key['name'];?>" />
                  </div>

                  <div class="form-group col-sm-6">
                         <label class="control-label">Gender<span class="text-danger">*</span></label>
                          <select class="form-control" name="gender">
                        <option value="">Select gender</option>
                        <option <?php  if($key['gender']=="1"){ echo "selected";}?> value="1">Male</option>
                        <option <?php  if($key['gender']=="2"){ echo "selected";}?> value="2" >Female</option>
                       
                        </select>
                        </div>
                      <div class="form-group col-sm-6">
                         <label class="control-label">Marital status<span class="text-danger">*</span></label>
                          <select class="form-control" name="marital">
                        <option value="">Select Marital status</option>
                        <option <?php  if($key['marital']=="1"){ echo "selected";}?> value="1" >Single</option>
                        <option <?php  if($key['marital']=="2"){ echo "selected";}?> value="2" >Divorce</option>
                       <option <?php  if($key['marital']=="3"){ echo "selected";}?> value="3">Seperated</option>
                        <option <?php  if($key['marital']=="4"){ echo "selected";}?> value="4">Married</option>
                       
                        </select>
                        </div>
                            <div class="form-group col-sm-6">
                         <label class="control-label"> Status<span class="text-danger">*</span></label>
                          <select class="form-control" name="status">
                        <option  value="">Select status</option>
                        <option <?php  if($key['status']=="1"){ echo "selected";}?>  value="1">inactive</option>
                        <option <?php  if($key['status']=="2"){ echo "selected";}?> value="2">visitor</option>status
                       <option <?php  if($key['status']=="3"){ echo "selected";}?> value="3">attender</option>
                        
                       
                        </select>
                        </div>

                 <div class="form-group col-md-6">
                    <label class="control-label">Phone number</label>
                    <input class="form-control" type="text" name="phone_no" value="<?php echo $key['phone_no'];?>">
                  </div>
                         <div class="form-group col-md-6">
                    <label class="control-label">email</label>
                    <input class="form-control" type="email" name="email" value="<?php echo $key['email'];?>">
                  </div>
                    <div class="form-group col-md-6">
                    <label class="control-label">BOD</label>
                    <input class="form-control" type="date" name="bod" value="<?php echo $key['bod'];?>">
                  </div>
                      <div class="form-group col-md-6">
                    <label class="control-label">Address</label>
                    <input class="form-control" type="text" name="address" value="<?php echo $key['address'];?>">
                  </div>

<div class="form-group col-md-6">
                              <label for="id_label_multiple">Campaign<span class="text-danger">*</span> </label>
                              <select class="form-control postName" id="role2" aria-label="Default select example" type="text" name="campaign" >
                              <option value="">Select </option>
                               <?php    
                                $sql1=$conn->prepare("select * from `Campaign` where delete_status='0' ");
                                $sql1->execute();
                                $row1=$sql1->fetchAll();
                                //print_r($row);
                                //exit;
                                foreach ($row1 as $key1) {?>
                                <option value="<?php echo $key1['id'];?> ">
                                       <?php echo $key1['cam_name'];?>
                                        </option>
                                    <?php } ?>
                              </select>
                           </div>





<div class="form-group col-md-6">
                                                        <label class="control-label">photo<span class="text-danger"></span></label>
                                                        <input class="form-control" type="file" name="photo" accept=".png,.jpeg,.jpg"  >
                                                 </div>


                    </div> 

                 
                             
                             <div class="form-group col-md-6">
                                    <label>Description<span class="text-danger">*</span></label>
                                    <textarea name="description"  cols="30" rows="10" class="form-control text-area-5"><?php echo $key['description'];?></textarea>
                                     </div>
              
                            <div class="form-group col-md-6">
                    <label class="control-label">Files doc, pdf, image<span class="text-danger">*</span></label>
                    <input class="form-control" type="file" name="photo1" accept=".doc,.docx">
                  </div>


                           <div class="col-md-12">
                    <h6 class="p-2 seo-info" style="background-color: lightgrey;">Custom Field</h6>
                  </div>

                  <div class="form-group col-md-6">
                    <label class="control-label">location</label>
                    <input class="form-control" type="text" name="location" value="<?php echo $key['address'];?>">
                  </div>

                  <div class="form-group col-sm-6">
                         <label class="control-label"> Group<span class="text-danger">*</span></label>
                          <select class="form-control" name="group">
                        <option value="">Select group</option>
                        <option <?php  if($key['group']=="1"){ echo "selected";}?>  value="1">usering</option>
                        <option <?php  if($key['group']=="1"){ echo "selected";}?>  value="2">protocol</option>group
                       
                        </select>
                        </div>

                  <div class="form-group col-md-6">
                    <label class="control-label">Testfield</label>
                    <textarea class="form-control" type="text" name="testfield"><?php echo $key['testfield'];?></textarea>
                  </div>

                
                  <div class="form-group col-md-6">
                    <label name="department" value="">Department<span class="text-danger">*</span></label>
                         <div>
                            
                         <input type="radio" name="department" value="KAMA" >
                                        KAMA
                         <input type="radio" name="department" value="SUNDAY_SCHOOL"> 
                                         SUNDAY SCHOOL

                         <input type="radio" name="department" value=" CMA" > 
                                       CMA
                        <input type="radio" name="department" value=" YOUTH"> 
                                      YOUTH
                                        
                         </div>
                                </div> 




                        <div class="form-group col-md-6">
                                    <label name="uplata" value="">Uplata<span class="text-danger">*</span></label>
                         <div>
                            
                         <input type="checkbox" name="uplata" value="2021" >
                                         2021

                         <input type="checkbox" name="uplata" value="2022" > 
                                        2022

                         <input type="checkbox" name="uplata" value="2023"  > 
                                       2023
                        <input type="checkbox" name="uplata" value="2024" > 
                                       2024
                                        
                         </div>
                                </div>




                  <div class="form-group col-md-12">

                    <button class="btn btn-primary" type="submit" name="update" onclick="addService()">Update</button>

                  </div>
                  <?php } ?>
                </form>
              </div>


            </div>

          </div>
        </div><!--end card-->
      </div><!--end col-->
    </div><!--end row-->
  </div> <!-- Page content Wrapper -->
</div> <!-- content -->

<?php include('include/footer.php'); ?>
<!--  Author Name: Mayuri K. 
 for any PHP, Codeignitor or Laravel website or software development contact me at mayuri.infospace@gmail.com  -->
<script>
    function validation() {
        var fileInput = document.getElementById('add_service').photo;
        var filePath = fileInput.value;
        var allowedExtensions = /\.(jpg|jpeg|png)$/i;

        if (!allowedExtensions.test(filePath)) {
            alert('Invalid file type! Please upload a JPG, JPEG, or PNG image.');
            fileInput.value = '';
            return false;
        }
        return true;
    }
</script>
<script>
    function validation() {
        var fileInput = document.getElementById('add_service').photo1;
        var filePath = fileInput.value;
        var allowedExtensions = /\.(jpg|jpeg|png|docx|pdf)$/i;

        if (!allowedExtensions.test(filePath)) {
            alert('Invalid file type! Please upload a JPG, JPEG, PNG, DOCx, or PDF file.');
            fileInput.value = '';
            return false;
        }
        return true;
    }
</script>
<script>
function addService() {
        jQuery.validator.addMethod("alphanumeric", function(value, element) {
            return this.optional(element) || /^(?=.[a-zA-Z])[a-zA-Z0-9\s!@#$%^&()_-]+$/.test(value);
        }, "Please enter alphanumeric characters only");

        jQuery.validator.addMethod("lettersonly", function(value, element) {
            return this.optional(element) || /^[a-zA-Z\s]*$/.test(value);
        }, "Please enter alphabet characters only");
        
    $('#add_service').validate({
        rules: {
            branch: {
                required: true
            },
            name: {
                required: true,
                alphanumeric: true // Custom rule for alphanumeric characters
            },
            gender: {
                required: true
            },
            marital: {
                required: true
            },
            phone_no: {
                required: true
            },
            email: {
                required: true,
                email: true // Built-in email validation rule
            },
            address: {
                required: true
            },
            campaign: {
                required: true
            },
            photo: {
                required: true
            },
            description: {
                required: true
            },
            photo1: {
                required: true
            },
            department: {
                required: true
            },
            uplata: {
                required: true
            }
        },
        messages: {
            name: {
                required: "Please enter the name",
                alphanumeric: "Only alphanumeric characters are allowed"
            },
            location: {
                required: "Please enter the location"
            },
            department: {
                required: "Please enter the department"
            },
            description: {
                required: "Please enter the description"
            }
        }
    });
}
</script>