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

if(!isset($_SESSION['id'])){
    // echo "<script> window.location.href = 'index.php' </script>";
  }
  
  
  

?>

<?php  include 'include/head.php'; ?>
                    
    <!-- Start Content-->
    <div class="container-fluid">
                        
                        <!-- start page title -->
                        <div class="row">
                            <div class="col-12">
                                <div class="page-title-box">
                                    <div class="page-title-right">
                                        <ol class="breadcrumb m-0">
                                            <li class="breadcrumb-item"><a href="dashboard.php">Home</a></li>
                                            <li class="breadcrumb-item active">Apply For Loan</li>
                                        </ol>
                                    </div>
                                    <h4 class="page-title">Apply For Loan</h4>
                                </div>
                            </div>
                        </div>
                        <!-- end page title --> 

                        <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body">
                                    
                                    <?php
                                    if(isset($_POST['user_id']))
                                    {
                                        $user_idd = $_POST['user_id'];
                                        $select_name = mysqli_fetch_assoc(mysqli_query($conn,"select * from `user` where `id`='$user_idd'"));
                                       
                                        $select_share_money = mysqli_fetch_assoc(mysqli_query($conn,"select SUM(share_money_amount) AS amountt from `share_money` where `userid`='$user_idd'"));
                                        
                                         $selecttt = mysqli_fetch_assoc(mysqli_query($conn,"select * from `cd` where `userid`='$user_idd' ORDER BY id DESC"));
                                         
                                         $last_payment_month_name = $selecttt['cd_month'];
                                         $last_payment_month_number = date('n', strtotime($last_payment_month_name));
                                          $currentMonthName = date('F');
                                          $currentMonth_number = date('n');
                                         
                                         if($last_payment_month_number >$currentMonth_number){
                                              $pending_amount = 0;
                                         }else{
                                               $month_set = abs($last_payment_month_number - $currentMonth_number);
                                         }
                                       
                                         
                                        $pending_amount = $month_set * 200;
                                         
                                    }
                                    
                                    ?>

                    <?php
                    
 $select_cd_intrest_per = mysqli_fetch_assoc(mysqli_query($conn,"select * from `intrest_per`")); 
echo $cd_inter_per = $select_cd_intrest_per['socity_per'];
echo $cd_inter_perrr = $select_cd_intrest_per['loan_per'];

if(isset($_POST['submit'])){
    $user_idd = $_POST['uid'];
    $share_money = $_POST['share_money'];
    $loan_amount = $_POST['loan_amount'];
    $interest_amount = $loan_amount * 0.10;
    $socity_found = $loan_amount * ($cd_inter_per / 100);
     $rate_of_interest = $loan_amount * ($cd_inter_perrr / 100);
    $cd_amount       = $_POST['cd_amount'];
    $first_guranter = $_POST['first_guranter'];
    $second_guranter = $_POST['second_guranter'];
    $third_guranter  = $_POST['third_guranter'];
    $fourth_guranter = $_POST['fourth_guranter'];
    $fofth_guranter  = $_POST['fofth_guranter'];
    $sixth_guranter  = $_POST['sixth_guranter'];
    $seventh_guranter= $_POST['seventh_guranter'];
    $p_mode_detail   = $_POST['p_mode_detail'];
    $mode_number     = $_POST['mode_number'];
    $share_mnoney_amount = $_POST['share_money_amount'];    
    $total_deduct_amount = $socity_found + $cd_amount + $share_mnoney_amount;
    $total_give_amount   = $loan_amount - $total_deduct_amount;
     $date =  $date = date("Y/m/d");
     $currentMonthName = date('F');

    // if($interest_amount > $share_money){
    //     $error = "Your Loan Amount Grater Than Share Money";
    // }else{
        $inserttt = mysqli_query($conn,"INSERT INTO `loan`(`userid`, `loan_amount`, `first_society_fund`, `monthly_Interest`, `monthly_Penalty`, `cd_deduction`,`share_money`, `total_deduct_amount`,
                    `loan_return`, `available_balance`, `date`, `month_name`, `first_guarantor_id`, `second_guarantor_id`, `third_guarantor_id`, `fourth_guarantor_id`, 
                    `fifth_guarantor_id`, `sixth_guarantor_id`, `seventh_guarantor_id`, `payment_mod`, `payment_mode_number`,`user_give_amount`) VALUES ('$user_idd','$loan_amount',
                     '$socity_found','0','0','$cd_amount','$share_mnoney_amount','$total_deduct_amount','0','$loan_amount','$date','$currentMonthName','$first_guranter','$second_guranter','$third_guranter',
                      '$fourth_guranter','$fofth_guranter','$sixth_guranter','$seventh_guranter','$p_mode_detail','$mode_number','$total_give_amount')");
        
        //print_r($inserttt);
        if($inserttt)
        {
            echo "<script>alert('Give User To " . $total_give_amount . " Amount')</script>";
            echo "<script>alert('Your Loan Amount Is Register')</script>";
            echo "<script>window.location.href='apply_loan.php'</script>";
            
            $insertt_cd = mysqli_query($conn,"INSERT INTO `cd`(`userid`, `cd_amount`, `cd_Interest`, `cd_date`, `cd_month`) VALUES 
                          ('$user_idd','$cd_amount','0','$date','$currentMonthName')");
                          
        $insertttt = mysqli_query($conn,"INSERT INTO `loan_desc`(`userid`, `loan_amount`, `first_society_fund`, `monthly_Interest`, `monthly_Penalty`, `cd_deduction`,`share_money`, `total_deduct_amount`,
       `loan_return`, `available_balance`, `date`, `month_name`, `first_guarantor_id`, `second_guarantor_id`, `third_guarantor_id`, `fourth_guarantor_id`, 
       `fifth_guarantor_id`, `sixth_guarantor_id`, `seventh_guarantor_id`, `payment_mod`, `payment_mode_number`,`user_give_amount`) VALUES ('$user_idd','$loan_amount',
        '$socity_found','0','0','$cd_amount','$share_mnoney_amount','$total_deduct_amount','0','$loan_amount','$date','$currentMonthName','$first_guranter','$second_guranter','$third_guranter',
            '$fourth_guranter','$fofth_guranter','$sixth_guranter','$seventh_guranter','$p_mode_detail','$mode_number','$total_give_amount')");
                          
        
         $insert = mysqli_query($conn,"INSERT INTO `share_money`(`userid`, `share_money_amount`,  `share_money_date`, `month_name`) VALUES 
                                                   ('$user_idd','$share_mnoney_amount','$date','$currentMonthName')");                  
        }
    //}
}
?>

<h6>User Name</h6> <?php echo $select_name['first_name'] . ' ' . $select_name['last_name']; ?>

<h6>Share Money Amount</h6> <?php echo isset($select_share_money['amountt']) ? $select_share_money['amountt'] : 0; ?>

<h6>Total Pending Cd Amount</h6> <?php echo $pending_amount; ?>

<?php
// Check if $error is set and not empty, then display it
if(isset($error) && !empty($error)){
    echo "<h6>Error:</h6> <h6 style=\"color:red;\">$error</h6>";
}
?>
                                       
                                           <div class="col-lg-12">
                                               <form action=""  method="POST" >
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">User ID</label>
                                                        <input type="text" onchange="this.form.submit()" value="<?php echo isset($_POST['user_id']) ? htmlspecialchars($_POST['user_id']) : ''; ?>" name="user_id" class="form-control" placeholder="User ID" required>
                                                    </div>
                                                    </form>
                                                </div>
                    <form action=""  method="POST" enctype="multipart/form-data">
                                
                                    <!-- end nav-->
                                    <div class="tab-content">
                                        <div class="tab-pane show active">
                                            <div class="row">
                                                
                                                
                                                
                                                <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Loan Amount</label>
                                                        <input type="text" value="<?php echo isset($_POST['loan_amount']) ? htmlspecialchars($_POST['loan_amount']) : ''; ?>" name="loan_amount" class="form-control" placeholder="Loan Amount" required>
                                                          <input type="hidden" value="<?php echo $user_idd;?>" name="uid">
                                                          <input type="hidden" value="<?php echo $select_share_money['amountt'];?>" name="share_money">
                                                    </div>
                                                </div>
                                                
                                                <!--<div class="col-lg-12">-->
                                                    <!-- Date Range Picker With Times -->
                                                <!--    <div class="mb-3">-->
                                                <!--        <label for="example-select" class="form-label">First Socity Found</label>-->
                                                <!--        <input type="email" value="<?php echo isset($_POST['f_socity_found']) ? htmlspecialchars($_POST['f_socity_found']) : ''; ?>" name="f_socity_found" class="form-control" placeholder="First Socity Found" required>-->
                                                <!--    </div>-->
                                                <!--</div>-->
                                                
                                                <!--<div class="col-lg-12">-->
                                                    <!-- Date Range Picker With Times -->
                                                <!--    <div class="mb-3">-->
                                                <!--        <label for="example-select" class="form-label">Monthly Interest</label>-->
                                                <!--        <input type="text" value="<?php echo isset($_POST['ac_number']) ? htmlspecialchars($_POST['ac_number']) : ''; ?>" name="ac_number" id="ac_number" onInput="checkmobile()" class="form-control" placeholder="User Acount Number" required>-->
                                                <!--           <span id="check-mobile"></span>-->
                                                <!--    </div>-->
                                                <!--</div>-->
                                                
                                                <!--<div class="col-lg-12">-->
                                                    <!-- Date Range Picker With Times -->
                                                <!--    <div class="mb-3">-->
                                                <!--        <label for="example-select" class="form-label">Monthly Penalty</label>-->
                                                <!--        <input type="text" value="<?php echo isset($_POST['phone']) ? htmlspecialchars($_POST['phone']) : ''; ?>" name="phone" class="form-control" placeholder="User Phone Number" required>-->
                                                <!--    </div>-->
                                                <!--</div>-->
                                                
                                                <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">CD Amount</label>
                                                        <input type="text" value="<?php echo isset($_POST['cd_amount']) ? htmlspecialchars($_POST['cd_amount']) : ''; ?>" name="cd_amount" class="form-control" placeholder="CD Amount">
                                                    </div>
                                                </div>
                                                
                                                <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                       <label for="example-select" class="form-label">Share Money Amount (If You Have Less Share Money)</label>
                                                        <input type="text" value="<?php echo isset($_POST['share_money_amount']) ? htmlspecialchars($_POST['share_money_amount']) : ''; ?>" name="share_money_amount" class="form-control" placeholder="Share Money Amount">
                                                    </div>
                                                </div>
                                                
                                                <!--<div class="col-lg-12">-->
                                                    <!-- Date Range Picker With Times -->
                                                <!--    <div class="mb-3">-->
                                                <!--        <label for="example-select" class="form-label">Total Deduct Amount</label>-->
                                                <!--        <input type="text" value="<?php echo isset($_POST['total_amount']) ? htmlspecialchars($_POST['total_amount']) : ''; ?>" name="total_amount" class="form-control" placeholder="Tota Amount" required>-->
                                                <!--    </div>-->
                                                <!--</div>-->
                                                
                                                <!--<div class="col-lg-12">-->
                                                    <!-- Date Range Picker With Times -->
                                                <!--    <div class="mb-3">-->
                                                <!--        <label for="example-select" class="form-label">Loan Return</label>-->
                                                <!--        <input type="text" value="<?php echo isset($_POST['aadhar']) ? htmlspecialchars($_POST['aadhar']) : ''; ?>" name="aadhar" class="form-control" placeholder="User Aadhar Number" required>-->
                                                <!--    </div>-->
                                                <!--</div>-->
                                                
                                                <!--<div class="col-lg-12">-->
                                                    <!-- Date Range Picker With Times -->
                                                <!--    <div class="mb-3">-->
                                                <!--        <label for="example-select" class="form-label">Available balance</label>-->
                                                <!--        <input type="text" value="<?php echo isset($_POST['pan']) ? htmlspecialchars($_POST['pan']) : ''; ?>" name="pan" class="form-control" placeholder="Pan Number" required>-->
                                                <!--    </div>-->
                                                <!--</div>-->
                                                
                                                <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">First Guarantor ID</label>
                                                        <input type="text" value="<?php echo isset($_POST['first_guranter']) ? htmlspecialchars($_POST['first_guranter']) : ''; ?>" name="first_guranter" class="form-control" placeholder="First Guarantor ID" >
                                                    </div>
                                                </div>
                                                
                                                 <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Second Guarantor ID</label>
                                                        <input type="text" value="<?php echo isset($_POST['second_guranter']) ? htmlspecialchars($_POST['second_guranter']) : ''; ?>" name="second_guranter" class="form-control" placeholder="Second Guarantor ID" >
                                                    </div>
                                                </div>
                                                
                                                 <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Third Guarantor ID</label>
                                                        <input type="text" value="<?php echo isset($_POST['third_guranter']) ? htmlspecialchars($_POST['third_guranter']) : ''; ?>" name="third_guranter" class="form-control" placeholder="Third Guarantor ID" >
                                                    </div>
                                                </div>
                                                
                                                 <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Fourth Guarantor ID</label>
                                                        <input type="text" value="<?php echo isset($_POST['fourth_guranter']) ? htmlspecialchars($_POST['fourth_guranter']) : ''; ?>" name="fourth_guranter" class="form-control" placeholder="Fourth Guarantor ID" >
                                                    </div>
                                                </div>
                                                
                                                 <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Fifth Guarantor ID</label>
                                                        <input type="text" value="<?php echo isset($_POST['fofth_guranter']) ? htmlspecialchars($_POST['fofth_guranter']) : ''; ?>" name="fofth_guranter" class="form-control" placeholder="Fifth Guarantor ID" >
                                                    </div>
                                                </div>
                                                
                                                 <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Sixth Guarantor ID</label>
                                                        <input type="text" value="<?php echo isset($_POST['sixth_guranter']) ? htmlspecialchars($_POST['sixth_guranter']) : ''; ?>" name="sixth_guranter" class="form-control" placeholder="Sixth Guarantor ID" >
                                                    </div>
                                                </div>
                                                
                                                 <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Seventh Guarantor ID</label>
                                                        <input type="text" value="<?php echo isset($_POST['seventh_guranter']) ? htmlspecialchars($_POST['seventh_guranter']) : ''; ?>" name="seventh_guranter" class="form-control" placeholder="Seventh Guarantor ID" >
                                                    </div>
                                                </div>
                                                
                                                
                                                <div class="col-lg-12">
                                                   <!-- Date Range Picker With Times -->
                                                   <div class="mb-3">
                                                       <label for="payment-mode" class="form-label">Payment Mode</label>
                                                       <select name="p_mode_detail" id="payment-mode" class="form-control" required>
                                                           <option value="">Select Payment Mode</option>
                                                           <option value="phone_pay">Phone Pay</option>
                                                           <option value="cash">Cash</option>
                                                           <option value="upi">UPI</option>
                                                           <option value="account_transfer">Account Transfer</option>
                                                       </select>
                                                   </div>
                                               </div>
                                                
                                                <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Payment Mode Number(example:cheque no,upi no,account no)</label>
                                                        <input type="text" value="<?php echo isset($_POST['mode_number']) ? htmlspecialchars($_POST['mode_number']) : ''; ?>" name="mode_number" class="form-control" placeholder="Cheque no,Upi No,Account No" required>
                                                    </div>
                                                </div>
                                                
                                                <!--<div class="col-lg-12">-->
                                                    <!-- Date Range Picker With Times -->
                                                <!--    <div class="mb-3">-->
                                                <!--        <label for="example-select" class="form-label">Joining Ammount</label>-->
                                                <!--        <input type="text" value="5000" name="j_amount" class="form-control" placeholder="User Joing Amount" required>-->
                                                <!--    </div>-->
                                                <!--</div>-->
                                                
                                                <!--<div class="col-lg-12">-->
                                                    <!-- Date Range Picker With Times -->
                                                <!--    <div class="mb-3">-->
                                                <!--        <label for="example-select" class="form-label">Profile Image</label>-->
                                                <!--        <input type="file" value="" name="pimage" class="form-control" placeholder="Give image" required>-->
                                                <!--    </div>-->
                                                <!--</div>-->
                                                
                                                <!--<div class="col-lg-12">-->
                                                    <!-- Date Range Picker With Times -->
                                                <!--    <div class="mb-3">-->
                                                <!--        <label for="example-select" class="form-label">Aadhar Front Image</label>-->
                                                <!--        <input type="file" value="" name="a_f_image" class="form-control" placeholder="Give image" required>-->
                                                <!--    </div>-->
                                                <!--</div>-->
                                                
                                                <!-- <div class="col-lg-12">-->
                                                    <!-- Date Range Picker With Times -->
                                                <!--    <div class="mb-3">-->
                                                <!--        <label for="example-select" class="form-label">Aadhar Back Image</label>-->
                                                <!--        <input type="file" value="" name="a_b_image" class="form-control" placeholder="Give image" required>-->
                                                <!--    </div>-->
                                                <!--</div>-->
                                                
                                                <!-- <div class="col-lg-12">-->
                                                    <!-- Date Range Picker With Times -->
                                                <!--    <div class="mb-3">-->
                                                <!--        <label for="example-select" class="form-label">Pan Card Image</label>-->
                                                <!--        <input type="file" value="" name="pan_image" class="form-control" placeholder="Give image" required>-->
                                                <!--    </div>-->
                                                <!--</div>-->
                                                
                                                <!--<div class="col-lg-12">-->
                                                    <!-- Date Range Picker With Times -->
                                                <!--    <div class="mb-3">-->
                                                <!--        <label for="example-select" class="form-label">Signature Image</label>-->
                                                <!--        <input type="file" value="" name="sign_image" class="form-control" placeholder="Give Signatureimage" required>-->
                                                <!--    </div>-->
                                                <!--</div>-->
                                                
                                                <!--<div class="col-lg-12">-->
                                                    <!-- Date Range Picker With Times -->
                                                <!--    <div class="mb-3">-->
                                                <!--        <label for="example-select" class="form-label">Electricity Bill Image</label>-->
                                                <!--        <input type="file" value="" name="bill_image" class="form-control" placeholder="Give ">-->
                                                <!--    </div>-->
                                                <!--</div>-->
                                                
                                                <!--<div class="col-lg-12">-->
                                                    <!-- Date Range Picker With Times -->
                                                <!--    <div class="mb-3">-->
                                                <!--        <label for="example-select" class="form-label">Rent agreement Image	</label>-->
                                                <!--        <input type="file" value="" name="rent_agreement" class="form-control" placeholder="Give ">-->
                                                <!--    </div>-->
                                                <!--</div>-->
                                                
                                                
                                                
                                                <!--<div class="col-lg-12">-->
                                                    <!-- Date Range Picker With Times -->
                                                <!--    <div class="mb-3">-->
                                                <!--        <label for="example-select" class="form-label">Offer Value</label>-->
                                                <!--        <input type="text" value="" name="value" class="form-control" placeholder="offer value">-->
                                                <!--    </div>-->
                                                <!--</div>-->


                                            </div>

                                            <div class="row">
                                                <div class="clearfix"></div>
                                                <div class="col-lg-6">
                                                    <!-- Predefined Date Ranges -->
                                                    <div class="mb-3">
                                                        <input type="submit" id="submit" class="btn btn-success" value="submit" name="submit">
                                                        
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <!-- end preview-->

                                        
                                        <!-- end preview code-->
                                    </div>
                                    <!-- end tab-content-->
                    </form>
                                </div>
                                <!-- end card-body -->
                            </div>
                            <!-- end card-->
                        </div>
                            <!-- end col -->
            </div> <!-- end row-->


                    
    </div> <!-- container -->


<?php  
include 'include/footer.php'; 
?>
<!-- editor -->
        <script src='https://cdn.ckeditor.com/4.13.0/standard/ckeditor.js'></script>
        <script src='https://code.jquery.com/jquery-3.4.1.min.js'></script>
        <script id="rendered-js" >
            CKEDITOR.replace('editor1');
            CKEDITOR.on('instanceReady', function (evt)
            {
            var editor = evt.editor;

            editor.on('change', function (e) {
                var contentSpace = editor.ui.space('contents');
                var ckeditorFrameCollection = contentSpace.$.getElementsByTagName('iframe');
                var ckeditorFrame = ckeditorFrameCollection[0];
                var innerDoc = ckeditorFrame.contentDocument;
                var innerDocTextAreaHeight = $(innerDoc.body).height();
                console.log(innerDocTextAreaHeight);
            });
            });
            //# sourceURL=pen.js
            
            function checkmobile() {
    jQuery.ajax({
    url: "check_availability.php",
    data:'checkmobilel='+$("#ac_number").val(),
    type: "POST",
    success:function(data){
        $("#check-mobile").html(data);
    },
    error:function (){}
    });
}
        </script>
        
        