<?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 Monthly Loan</li>
                                        </ol>
                                    </div>
                                    <h4 class="page-title">Apply Monthly Loan</h4>
                                </div>
                            </div>
                        </div>
                        <!-- end page title --> 

                        <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body">
                                    
                                    <?php
                                    
 $select_cd_intrest_per = mysqli_fetch_assoc(mysqli_query($conn,"select * from `intrest_per`")); 
$cd_inter_per = $select_cd_intrest_per['loan_per'];

                                    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_loan_money = mysqli_fetch_assoc(mysqli_query($conn,"select * from `loan_desc` where `userid`='$user_idd' ORDER BY id DESC"));
                                        $first_loan_amount = $select_loan_money['loan_amount'];
                                        $av_blance = $select_loan_money['available_balance'];
                                         $intrest = $select_loan_money['available_balance'] * ($cd_inter_per / 100);
                                         
                                         $round_inrest =  round($intrest);
                                         
                                        //  panalty Amount jitna bhe uska avaibale balance h uske 0.1 % nikal rahe h because isme 100000 ka 100 aise he banega
                                          $date = date("d");
$currun_intrest = 0;
$balance = $select_loan_money['available_balance'];
$interst_amounttt = $balance * 0.001;

if ($date === '16') {
    $currun_intrest = $interst_amounttt;
} elseif ($date === '17') {
    $currun_intrest = $interst_amounttt * 2;
} elseif ($date === '18') {
    $currun_intrest = $interst_amounttt * 3;
} elseif ($date === '19') {
    $currun_intrest = $interst_amounttt * 4;
} elseif ($date === '20') {
    $currun_intrest = $interst_amounttt * 5;
}elseif ($date === '21') {
    $currun_intrest = $interst_amounttt * 6;
}elseif ($date === '22') {
    $currun_intrest = $interst_amounttt * 7;
}elseif ($date === '23') {
    $currun_intrest = $interst_amounttt * 8;
}elseif ($date === '24') {
    $currun_intrest = $interst_amounttt * 9;
}elseif ($date === '25') {
    $currun_intrest = $interst_amounttt * 10;
}

$currun_intrest;

$round_currunt = round($currun_intrest);
                                    }
                                    
                                    ?>

                    <?php
if(isset($_POST['submit'])){
   $user_idd = $_POST['uid'];
$loan_amount = $_POST['loan_amount'];
$av_balance      = $_POST['av_balance'];
$last_loan_amount = $_POST['first_loan_amount'];
$monthly_panlty  = $_POST['c_interst'];
$rate_of_interest = $av_balance * ($cd_inter_per / 100);
$cd_amount       = $_POST['cd_amount'];
$p_mode_detail   = $_POST['p_mode_detail'];
$mode_number     = $_POST['mode_number'];
$total_deduct_amount =  $cd_amount + $monthly_panlty + $rate_of_interest;
 $loan_return = $loan_amount - $total_deduct_amount;
 $avaibale_balance = $av_balance - $loan_return;
    
$date = date("Y/m/d");
$currentMonthName = date('F');

$selecttt_user_id = mysqli_query($conn, "SELECT * FROM `loan_desc` WHERE `userid` = '$user_idd'");
if(mysqli_num_rows($selecttt_user_id) <= 0) {
    echo "<script>alert('User ID Not Correct')</script>";
}else{
   $insertttttt = 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`,`month_payment`, `payment_mod`, `payment_mode_number`) 
                   VALUES ('$user_idd','$loan_amount','0','$rate_of_interest','$monthly_panlty','$cd_amount','0','$total_deduct_amount','$loan_return','$avaibale_balance',
                   '$date','$currentMonthName','$loan_amount','$p_mode_detail','$mode_number')");
                   
                  // print_r($insertttttt);
                  if($insertttttt)
                  {
                      echo "<script>alert('Your Loan Amount Is Submited')</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')");
                  }
     }
}
?>

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

<h6>Available Loan Amount</h6> <?php echo isset($select_loan_money['available_balance']) ? $select_loan_money['available_balance'] : 0; ?>
<h6>Current Month Interest(<?php echo $cd_inter_per;?>)</h6> <?php echo $intrest . ' (' . $round_inrest . ')';?>
<h6>Current Month Penalty</h6> <?php echo $currun_intrest . ' (' . $round_currunt . ')';?>


<?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">Monthly 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 $currun_intrest;?>" name="c_interst">-->
                                                          <input type="hidden" value="<?php echo $intrest;?>" name="interst">
                                                          <input type="hidden" value="<?php echo $first_loan_amount;?>" name="first_loan_amount">
                                                          <input type="hidden" value="<?php echo $av_blance;?>" name="av_balance">
                                                    </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['c_interst']) ? htmlspecialchars($_POST['c_interst']) : ''; ?>" name="c_interst" class="form-control" placeholder="Monthly Penalty" 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" required>-->
                                                <!--    </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>
        
        