<?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 pledges </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/pledges_crud.php" class="row" method="POST" enctype="multipart/form-data" onsubmit="return validation()" id="add_service">
                  <?php
                  $stmt = $conn->prepare("SELECT * FROM `pledges` 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 for="id_label_multiple">Member<span class="text-danger">*</span> </label>
                              <select class="form-control postName" id="role1" aria-label="Default select example" type="text" name="member" >
                              <option value="">Select Member</option>
                               <?php    
                                $sql1=$conn->prepare("select * from `member` where delete_status='0' ");
                                $sql1->execute();
                                $row1=$sql1->fetchAll();
                                //print_r($row);
                                //exit;
                                foreach ($row1 as $key1) {?>
                                <option value="<?php echo $key1['name'];?> ">
                                       <?php echo $key1['name'];?>
                                        </option>
                                    <?php } ?>
                              </select>
                           </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['cam_name'];?> ">
                                       <?php echo $key1['cam_name'];?>
                                        </option>
                                    <?php } ?>
                              </select>
                           </div>
                     
               <div class="form-group col-md-6">
                    <label class="control-label">Amount<span class="text-danger">*</span></label>
                    <input class="form-control to" type="text" name="pledges_amount" value="<?php echo $key['pledges_amount'];?>"placeholder="Enter amount" />
                  </div>

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

                <div class="form-group col-sm-6">
                         <label class="control-label">Recurring<span class="text-danger">*</span></label>
                          <select class="form-control" name="recurring">
                        <option value="">Select </option>
                      <option  <?php  if($key['status']=="1"){ echo "selected";}?> value="1">Yes</option>
                        <option  <?php  if($key['status']=="2"){ echo "selected";}?> value="2">No</option>
                       
                        </select>
                        </div>
                      <div class="form-group col-md-6">
                                    <label>Notes<span class="text-danger">*</span></label>
                                    <textarea name="notes"  cols="20" rows="10" class="form-control text-area-5"><?php echo $key['notes'];?></textarea>
                                     </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 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");
  $.validator.addMethod("numbersonly", function(value, element) {
    return this.optional(element) || /^[0-9]+$/.test(value);
}, "Please enter numbers only");
    $('#add_service').validate({
      rules: {
        branch: {
          required: true
          
        },
         member: {
          required: true
          
        },
         campaign: {
          required: true
         
        },
         pledges_amount: {
          required: true,
          numbersonly: true
        },
        notes: {
          required: true
        }
      },
      messages: {
        name: {
          required: "Please enter the name",
          alphanumeric: "Only alphanumeric characters are allowed"
        },
        notes: {
          required: "Please enter the notes"
        }
      }
    });
}
</script>