<?php 
// error_reporting(0);
?>
 <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1 class="m-0 text-dark">Fees Instalment</h1>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="<?php echo base_url() ?>admin/admin/dashboard">Dashboard</a></li>
              <li class="breadcrumb-item"><a href="#">Fees Management</a></li>
              <li class="breadcrumb-item active">Fees Instalment</li>

            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    <!-- /.content-header -->
    <?php echo form_open(base_url().'admin/fees_management/add_new_instalment');?>
    <section class="content">
      <div class="container-fluid">

        <div class="row">
          <div class="col-sm-12">
            <div class="card card-default">
              <div class="card-header">
                <h3 class="card-title">Add Instalment for <b><?php echo $student->user_name; ?></h3>
              </div>
              <!-- /.card-header -->
              <div class="card-body">
                <div class="row">

                  <div class="col-md-12">
                    <div class="form-group">
                       <label for="inputEmail" class="col-sm-12 col-form-label">Select Course <span class="required">*</span></label>
                       <div class="col-sm-12">
                          <select class="form-control" onchange="get_course_price(this.value)" name="fee_student_course" placeholder="Enter Notice Content" required="required" id="fee_student_course">
                            <option value="">Select</option>
                          </select>
                       </div>
                    </div>
                  </div>

                  <div class="col-md-2">
                    <div class="form-group">
                       <label for="inputEmail" class="col-sm-12 col-form-label">Course Price <span class="required">*</span></label>
                       <div class="col-sm-12">
                          <input class="form-control"  name="fee_student_course_price" placeholder="Enter Course Price" required="required" id="course_price" readonly>
                       </div>
                    </div>
                  </div>
                  <div class="col-md-2">
                    <div class="form-group">
                       <label for="inputEmail" class="col-sm-12 col-form-label">Course Discount (Rs.) <span class="required">*</span></label>
                       <div class="col-sm-12">
                          <input class="form-control" onkeyup="percantage(this.value)" name="fee_student_course_discount" placeholder="Enter Course Discount" required="required" id="course_discount">
                       </div>
                    </div>
                  </div>
                  <div class="col-md-2">
                    <div class="form-group">
                       <label for="inputEmail" class="col-sm-12 col-form-label">Total Amount <span class="required">*</span></label>
                       <div class="col-sm-12">
                          <input class="form-control"  name="fee_student_course_main_price"  required="required" id="main_price" readonly>
                       </div>
                    </div>
                  </div>
                  <div class="col-md-1">
                    <div class="form-group">
                       <label for="inputEmail" class="col-sm-12 col-form-label">CGST <b id="cgst_p"></b><span class="required">*</span></label>
                       <div class="col-sm-12">
                          <input class="form-control"  name="fee_student_course_cgst"  required="required" id="cgst" readonly>
                       </div>
                    </div>
                  </div>
                  <div class="col-md-1">
                    <div class="form-group">
                       <label for="inputEmail" class="col-sm-12 col-form-label">SGST <b id="sgst_p"></b><span class="required">*</span></label>
                       <div class="col-sm-12">
                          <input class="form-control"  name="fee_student_course_sgst"  required="required" id="sgst" readonly>
                       </div>
                    </div>
                  </div>
                  <div class="col-md-2">
                    <div class="form-group">
                       <label for="inputEmail" class="col-sm-12 col-form-label">Down Payment <span class="required">*</span></label>
                       <div class="col-sm-12">
                          <input class="form-control" onkeyup="calculate()" name="fee_student_course_down_pay" placeholder="Enter Down Payment" required="required" id="down_payment">
                       </div>
                    </div>
                  </div>
                  <script type="text/javascript">
                    function percantage(e) 
                    {
                      var course_price = $('#course_price').val();
                      // var tax = (course_price * <?php echo user()->user_gst; ?>) / 100;
                      var main_per = course_price - e;
                      $('#main_price').val(main_per);

                      var tax = "<?php echo user()->user_gst; ?>";
                      var cgst = tax / 2;
                      var sgst = tax / 2;
                      var main_cgst = (main_per * cgst) / 100;
                      var main_sgst = (main_per * sgst) / 100;

                      $("#cgst").val(main_cgst);
                      $("#sgst").val(main_sgst);

                      $('#due_ammount').val(main_per + main_cgst + main_sgst);

                    }
                    function calculate() 
                    {
                      var main_price = $('#main_price').val();
                      var down_payment = $('#down_payment').val();
                      var main_cgst = $("#cgst").val();
                      var main_sgst = $("#sgst").val();

                      var ttl_p = parseInt(main_price) + parseInt(main_cgst) ;
                      var ttl_2 = parseInt(ttl_p) + parseInt(main_sgst) ;
                      var due_amt = ttl_2 - down_payment;
                      $('#due_ammount').val(due_amt);
                    }
                  </script>
                  <div class="col-md-2">
                    <div class="form-group">
                       <label for="inputEmail" class="col-sm-12 col-form-label">Total Due Amount <span class="required">*</span></label>
                       <div class="col-sm-12">
                          <input class="form-control"  name="fee_student_course_due"  required="required" id="due_ammount" readonly>
                       </div>
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="form-group">
                       <label for="inputEmail" class="col-sm-12 col-form-label">No of Inst<span class="required">*</span></label>
                       <div class="col-sm-12">
                          <input class="form-control"  name="fee_student_course_instalment" placeholder="Enter No of Instalment" required="required" id="no_instalment">
                       </div>
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="form-group">
                       <label for="inputEmail" class="col-sm-12 col-form-label">Days<span class="required">*</span></label>
                       <div class="col-sm-12">
                          <input class="form-control"  name="fee_student_course_days" placeholder="Enter Days" required="required" id="no_days">
                       </div>
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="form-group">
                       <label for="inputEmail" class="col-sm-12 col-form-label">Start Day<span class="required">*</span></label>
                       <div class="col-sm-12">
                          <input type="date" class="form-control"  name="fee_student_course_start_day" placeholder="Enter Days" id="start_date" required="required" >
                       </div>
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="form-group">
                       <label for="inputEmail" class="col-sm-12 col-form-label">Exam Approve Without Instalment<span class="required">*</span></label>
                       <div class="col-sm-12">
                          <select type="date" class="form-control"  name="fee_student_exam_approve" placeholder="Enter Days" required="required" id="exam_approve">
                            <option value="0">No</option>
                            <option value="1">Yes</option>
                          </select>
                       </div>
                    </div>
                  </div>

                  <input type="hidden" class="form-control"  name="fee_student_id" value="<?php echo $student->user_id; ?>">

                  <div class="col-md-12 mb-3" id="btn_hide">
                    <div class="form-group">
                       <button name="form1" onclick="generate_table()" style="float: right" type="button" class="btn btn-success btn-sm">Generate</button>
                    </div>
                  </div>
                  <div class="col-md-12">
                    <table id="example3" class="table table-bordered table-striped">
                      <thead>
                      <tr>
                        <th>Date</th>
                        <th>Amount</th>
                        <th>Status</th>
                      </tr>
                      </thead>
                      <tbody id="data">
                        
                      </tbody>
                    </table>
                    <div class="col-md-12" id="gen_btn">
                      <div class="form-group">
                         <button name="form1" style="float: right" type="submit" class="btn btn-warning">Save</button>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>

    <section class="content">
      <div class="container-fluid">
        <div class="card card-default">
            <!-- /.card-header -->
            <div class="card-body">
              
            </div>
          </div>
      </div><!--/. container-fluid -->
    </section>
    <?php echo form_close();?>
    <!-- /.content -->
  </div>
<script type="text/javascript">
$(function () {
    $("#example3").DataTable({
      "responsive": true, "lengthChange": true, "autoWidth": false,  "searching": true, info: false, paging: false,
    }).buttons().container().appendTo('#example3_wrapper .col-md-6:eq(0)');

    get_course_by_student_id(<?php echo $student->user_id; ?>)
  });
function generate_table() {

  var due_ammount = $('#due_ammount').val();
  var no_instalment = $('#no_instalment').val();
  var no_days = $('#no_days').val();
  var start_date = $('#start_date').val();
    $.ajax({
      beforeSend: function() {
         $(".loader").show();
      },
      type: "POST",
      dataType:"JSON",
      async: true,
      data: {due_ammount:due_ammount, no_instalment:no_instalment, no_days:no_days, start_date:start_date},
      url: '<?php echo base_url(); ?>admin/fees_management/generate_table',
      success: function (data) 
      {
          $('#data').html(data);
          $('#btn_hide').hide();
          $('#gen_btn').show();
          $(".loader").hide();
      },
      error: function (e) {
        console.log(e.responseText)
        $(".loader").hide();
      }
    });
  }
function get_course_by_student_id(e) 
{
  $.ajax({
    beforeSend: function() {
       $(".loader").show();
    },
    type: "GET",
    dataType:"JSON",
    async: true,
    url: '<?php echo base_url(); ?>admin/students/get_course_by_student_id_for_add_installment/'+e,
    success: function (data) 
    {
        $('#fee_student_course').html(data.option);
        $(".loader").hide();
    },
    error: function (e) {
      console.log(e.responseText)
      $(".loader").hide();
    }
  });
}
function get_course_price(e) 
{
  var st_id = '<?php echo $student->user_id; ?>';
  $.ajax({
    beforeSend: function() {
       $(".loader").show();
    },
    type: "GET",
    dataType:"JSON",
    async: true,
    url: '<?php echo base_url(); ?>admin/students/get_course_price_by_course_id/'+e+'/'+st_id,
    success: function (data) 
    {
      console.log(data)
        $('#course_price').val(data.price);
        $('#main_price').val(data.main_price);
        $('#course_discount').val(data.course_discount);
        $('#down_payment').val(data.down_payment);
        $('#no_instalment').val(data.no_instalment);
        $('#no_days').val(data.no_days);
        $('#start_date').val(data.start_date);
        $("#exam_approve").val(data.exam_approve);

        var tax = "<?php echo $app_setting->app_gst; ?>";
        var cgst = tax / 2;
        var sgst = tax / 2;
        $("#cgst_p").html('('+cgst+'%)');
        $("#sgst_p").html('('+sgst+'%)');

        if(data.due_ammount == 0)
        {
          var main_tax = (data.main_price * tax) / 100;
          const num1 = parseInt(data.main_price);
          const num2 = parseInt(main_tax);
          var ttl_p = num1 + num2
          $('#due_ammount').val(ttl_p);
        }
        else
        {
          $('#due_ammount').val(data.due_ammount);
        }

        var main_cgst = (data.main_price * cgst) / 100;
        var main_sgst = (data.main_price * sgst) / 100;


        $("#cgst").val(main_cgst);
        $("#sgst").val(main_sgst);



        if(data.no_instalment > 0)
        {
          get_instalment_table_by_user_id(st_id, e);
        }
        
        $(".loader").hide();
    },
    error: function (e) {
      console.log(e.responseText)
      $(".loader").hide();
    }
  });
}

function get_instalment_table_by_user_id(st_id, e) 
{
    $.ajax({
      beforeSend: function() {
         $(".loader").show();
      },
      type: "GET",
      dataType:"JSON",
      async: true,
      url: '<?php echo base_url(); ?>admin/fees_management/get_instalment_table_by_user_id/'+e+'/'+st_id,
      success: function (data) 
      {
          $('#data').html(data);
          $(".loader").hide();
      },
      error: function (e) {
        console.log(e.responseText)
        $(".loader").hide();
      }
  });
}
</script>
<style type="text/css">
#gen_btn
{
  display: none;
}
</style>