<?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">Add Contribution</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/contribution_crud.php" class="row" method="POST" enctype="multipart/form-data" onsubmit="return validation()" id="add_service">

                  
                                  <div class="form-group col-md-6">
                              <label for="id_label_multiple">Branch<span class="text-danger">*</span> </label>
                              <select class="form-control postName select2" 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 select2"  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="row">
    <div class="card">
        <div class="card-body">
<div class="form-group col-md-12">
    <div class="mydiv">
        <div class="form-group row control-group after-add-more subdiv">
            <div class="col-sm-4">
                <label>Amount</label>
                <input type="text" name="amount[]" class="form-control">
            </div>
            <div class="form-group col-md-6">
                <label for="id_label_multiple">Batch<span class="text-danger">*</span> </label>
                <select class="form-control postName select2" aria-label="Default select example" type="text" name="batch[]" >
                    <option value="">Select</option>
                   <?php    
                                $sql1=$conn->prepare("select * from `batch` 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">Type<span class="text-danger">*</span> </label>
                <select class="form-control postName" id="type" aria-label="Default select example" type="text" name="contritype[]" >
                    <option value="">Select</option>
                    <?php    
                                $sql1=$conn->prepare("select * from `contritype` 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">Fund<span class="text-danger">*</span> </label>
                <select class="form-control postName" id="fund" aria-label="Default select example" type="text" name="fund[]" >
                    <option value="">Select</option>
                    <?php    
                                $sql1=$conn->prepare("select * from `fund` 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">Payment Method<span class="text-danger">*</span> </label>
                <select class="form-control postName" id="payment_method" aria-label="Default select example" type="text" name="paymentmethod[]" >
                    <option value="">Select</option>
                    <?php    
                                $sql1=$conn->prepare("select * from `paymentmethod` 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="col-sm-3">
                <button class="btn btn-success add-more" type="button"><i class="fa fa-plus"></i></button>
            </div>
        </div>
    </div>
    <div class="copy hide" style="display:none;">
        <div class="form-group control-group row subdiv">
            <div class="col-sm-4">
                <label>Amount</label>
                <input type="text" name="amount[]" class="form-control">
            </div>
            <div class="form-group col-md-6">
                <label for="id_label_multiple">Batch<span class="text-danger">*</span> </label>
                <select class="form-control postName" id="batch" aria-label="Default select example" type="text" name="batch[]" >
                    <option value="">Select</option>
                    <?php    
                                $sql1=$conn->prepare("select * from `batch` 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">Type<span class="text-danger">*</span> </label>
                <select class="form-control postName" id="type" aria-label="Default select example" type="text" name="contritype[]" >
                    <option value="">Select</option>
                    <?php    
                                $sql1=$conn->prepare("select * from `contritype` 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">Fund<span class="text-danger">*</span> </label>
                <select class="form-control postName" id="fund" aria-label="Default select example" type="text" name="fund[]" >
                    <option value="">Select</option>
                   <?php    
                                $sql1=$conn->prepare("select * from `fund` 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">Payment Method<span class="text-danger">*</span> </label>
                <select class="form-control postName" id="payment_method" aria-label="Default select example" type="text" name="paymentmethod[]" >
                    <option value="">Select</option>
                     <?php    
                                $sql1=$conn->prepare("select * from `paymentmethod` 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="col-sm-3">
                <button class="btn btn-danger remove" type="button"><i class="fa fa-minus"></i></button>
            </div>
        </div>
    </div>
</div>
</div>
</div>
</div>
                  

                  </div>

</div>

<div class="form-group col-md-6">
                    <label class="control-label">date</label>
                    <input class="form-control" type="date" name="date">
                  </div>

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

                        <button class="btn btn-primary" type="submit" name="submit" onclick="return pay_now()">Save</button> 
                       <!-- <button type="button" class="btn btn-primary" id="rzp-button1" onclick="pay_now()">Pay</button>  -->

                  </div>
                </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 type="text/javascript">
  function addService() {
    $('#add_service').validate({
      rules: {
        branch: {
          required: true,
        },
        member: {
          required: true
        },
        'amount[]': {
          required: true
        },
        'batch[]': {
          required: true
        },
        'contritype[]': {
          required: true
        },
        'fund[]': {
          required: true
        },
        'paymentmethod[]': {
          required: true
        },
        date: {
          required: true
        }
      },
      messages: {
        branch: {
          required: "Please select a branch"
        },
        member: {
          required: "Please select a member"
        },
        'amount[]': {
          required: "Please enter an amount"
        },
        'batch[]': {
          required: "Please select a batch"
        },
        'contritype[]': {
          required: "Please select a contritype"
        },
        'fund[]': {
          required: "Please select a fund"
        },
        'paymentmethod[]': {
          required: "Please select a payment method"
        },
        date: {
          required: "Please select a date"
        }
      }
    });
  }
</script>


<!-----------payment------->
<!-- <script>
function pay_now() {
    // Fetch form data
    let form = document.forms['myform'];
    let formData = new FormData(form);

    // AJAX call to save the form data temporarily before initiating payment
    fetch('app/contribution_crud.php', {
        method: 'POST',
        body: formData
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            let options = {
                "key": "rzp_test_D2r1vGHsLrVCXK", // Enter the Key ID generated from the Dashboard
                "amount": data.amount, // Amount in paisa
                "currency": "INR",
                "name": "Your Company Name",
                "description": "Test Transaction",
                // "image": "https://example.com/your_logo",
                "order_id": data.order_id, // Pass the `order_id` created by your backend
                "handler": function (response){
                    // Call function to handle the success payment
                    handlePaymentSuccess(response.razorpay_payment_id, data.contribution_id);
                },
                "prefill": {
                    "name": data.name,
                    "email": data.email,
                    "contact": data.contact
                },
                "theme": {
                    "color": "#3399cc"
                }
            };
            let rzp1 = new Razorpay(options);
            rzp1.open();
        } else {
            alert('Failed to process payment. Please try again.');
        }
    })
    .catch(error => console.error('Error:', error));
}

function handlePaymentSuccess(payment_id, contribution_id) {
    // Make another AJAX call to finalize the payment and save it permanently
    fetch('app/contribution_crud.php', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({ payment_id: payment_id, contribution_id: contribution_id, action: 'finalize_payment' })
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            alert('Payment successful and data saved!');
            window.location.href = 'success_page.php'; // Redirect to a success page
        } else {
            alert('Payment successful but failed to save data. Contact support.');
        }
    })
    .catch(error => console.error('Error:', error));
}
</script> -->