<?php if($this->session->flashdata('add_dc_success')){ ?>
    <div class="alert alert-success solid" style='text-align:center;'><?php echo $this->session->flashdata('add_dc_success'); ?></div>
<?php } ?>
    <div id='add_dc_success' class='alert alert-success solid' style="display: none;">
    </div>    
    <div id='no_stock' class='alert alert-danger solid' style="display: none;">
    </div> 
            <div class="welcome-text">
                            <h4>Receving Payment</h4>
                        </div>
                <div class="col-lg-12">
                
                    
                <?php echo form_open(base_url(''),array('class'=>"form-horizontal add_dc", 'id'=>"add_dc")); ?>
                    <div class="row">
                            <div class="col-lg-5 col-md-4 col-sm-8">
                                <div class="form-group">
                                    <label class="form-label">Total Amount</label>
                                    <input type="text" required name="total_value" id="total_value" readonly="readonly" value="<?php
                                     $total = $get_bill_data[0]['total_value']*95.5/100; 
                                     $pai_amount = bill_pay_amount($get_bill_data[0]['so_id']);
                                     $total_amount  = $total-$pai_amount;
                                     echo intval($total_amount); 
                                     
                                     ?>" class="form-control required">
                                </div>
                                

                                <div class="form-group">
                                    <label class="form-label">Receive Amount</label>
                                    <input type="text" name="amount" id="amount" required="required" placeholder="Amount" class="form-control ">
                                </div>
                            </div>

                             <div class="col-lg-5 col-md-4 col-sm-8">
                               <div class="form-group">
                                    <label class="form-label">Payment Mode</label>
                                    <select name="mode" id="mode" required="required" class="form-control" tabindex="-98">
                                    <option value="">Select Mode</option>
                                    <option value="1">CHEQUE</option>
                                    <option value="2">ONLINE FUNDS</option>
                                    </select>
                                </div>
                                
                                <div id="chequeTextBox" class="form-group" style="display: none;">
                                    <label class="form-label">Cheque Number</label>
                                    <input type="number" name="cheque_number" id="cheque_number" class="form-control" />
                                </div>

                                <div id="fundsTextBox" class="form-group" style="display: none;">
                                    <label class="form-label">Funds Details</label>
                                    <input type="text" name="fund_no" id="fund_no" class="form-control" />
                                </div>
                                
                            </div>
                            
                             <div class="col-lg-4 col-md-4 col-sm-8">
                               <div class="form-group">
                                    <label class="form-label">Deposite at</label>
                                    <select name="bank" id="bank" required="required" class="form-control" tabindex="-98">
                                    <option value="">Select Bank</option>
                                    <?php foreach($banks as $bank){ ?>
                                    <option value="<?php echo $bank['id']; ?>"><?php echo $bank['bank']; ?></option>
                                   <?php } ?>
                                    </select>
                                </div>
                            </div>
                       <input type="hidden" name="so_id" id="so_id" value="<?php echo $get_bill_data[0]['so_id']; ?>" class="form-control required">
                       <input type="hidden" name="created" id="created" value="<?php echo date('Y-m-d H:i:s'); ?>" class="form-control required">
                       <input type="hidden" name="user_id" id="user_id" value="<?php echo $this->session->userdata('id');?>">
                    </div>
                    <?php form_close();?>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <button type="submit" id="dc_dis" class="btn btn-primary">Submit <i class="icon-arrow-right14 position-right"></i></button>
                    </div> 
             </div>

    <script>
    $(document).ready(function() {
    $('#mode').change(function() {
        var selectedMode = $(this).val();
        
        if (selectedMode === '1') {
            $('#chequeTextBox').show();
            $('#fundsTextBox').hide();
        } else if (selectedMode === '2') {
            $('#chequeTextBox').hide();
            $('#fundsTextBox').show();
        } else {
            $('#chequeTextBox').hide();
            $('#fundsTextBox').hide();
        }
    });
    });
    </script>
   <script type="text/javascript">
    var dc = $('#add_dc');   
    dc.on('submit',function(e){
        e.preventDefault();
            var dcdata = dc.serialize();
            var total = parseInt($("#total_value").val());
            var qty = parseInt($("#amount").val());
            if (total < qty) {
                alert("Receive Amount Greater Then SO Amount..!");
            }else{
            $("#dc_dis").attr('disabled', true);
            $.ajax({
                type:'POST',
                url:'<?php echo base_url('sales_order/add_bill_receviepayment'); ?>',
                data:dcdata,
                dataType:'json',
                success:function(data){
                    console.log('first'+data.id);
                    // alert(data);
                    if(data.status=="true"){
                        $('#add_dc_success').show().html('Payment added Successfully');
                        setTimeout(function(){location.href="<?php echo base_url('Sales_order/billing'); ?>"} , 3000);
                    }
                },
                error:function(data){console.log(data.responseText);}
            });
        }
    });
 </script>