<?php
$tabactv='ss';
     $listactiv='ss';
$user_id=$this->session->userdata('user_id');
if(!$user_id){
  redirect('user/login_view', 'refresh');
}
 $value='';

?>
<?php 
$b_type='';
 $bus_type=$this->db->query("select * from buisness_type where user_id='$user_id' 
    ");
  $data='';
  foreach ($bus_type->result() as $key => $bus_type){
    $b_type=$bus_type->buisness_id;

  }
if($tabsearch OR $tabpagi){
     
     $tabactv='ss';
     $listactiv='ss';
 }
else{
   
  $tabactv='active';
     $listactv='in active';  
}
$type=$this->db->query("select * from bank_name  where status=1   and buisness_id='$b_type'");
              
$bankname=array();                             
foreach ($type->result() as $key => $type){
    $bankname[$type->id]=$type->type;
}

?>

<!DOCTYPE html>
<html lang="en">

<?php
$this->load->view('head.php');
?>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>
    <div id="wrapper">
        <div class="admin-title">
            <div class="row">
                <?php $this->load->view('header.php'); ?>
            </div>
        </div>
        <div class="content-area">
            <div class="row">

                <div class="admin-menu">
                    <?php $this->load->view('sidebar.php'); ?>
                </div>

                <div class="content-menu">
                    <ul class="nav nav-tabs">

                        <li class="page-header <?php echo $tabactv; ?>"><a href="<?php echo base_url().'loan/create' ?>">All Loan</a></li>
                        <?php
                
                foreach ($category as $type){
                  $data=$type->id;
                  $type=$type->category;
                    if($data==$tabsearch OR $tabpagi==$data){
                     $active='active';   
                    }
                    else{
                        $active='';
                    }
                     
                   
                  echo '<li class="page-header '.$active.'"><a   href="'.base_url().'loan/create/'.$data.'">'. ucfirst($type).'</a> </li>';

                }
                ?>
                    </ul>

                </div>
                <div class="panel-heading headingnone">
                    <h2 class="transac">Loan</h2>

                    <button class="transaction btn btn-success nworder" data-toggle="modal" data-target="#myModal" data-backdrop="static" data-keyboard="false" onclick="newpurchases()">New Loan</button>


                </div>
                <div class="main-content">

                    <div class="banktab content-menu">

                    </div>



                    <!-- /.panel-heading -->
                    <form method="post" action="<?php echo site_url('loan/create');?>">
                        <input type="hidden" name="tabsearch" value="" class="tabsearch">
                        <div class="input dashboard-input" style="padding-top: 83px;">
                            <label>From
                                <input class="form-control" type="date" name="from" id="from" value="<?php echo $from ?>"></label>

                            <label>To
                                <input class="form-control" type="date" name="to" id="to" value="<?php echo $to ?>"></label>
                            <button type="submit" id="submit" class="btn btn-default">
                                <span class="glyphicon glyphicon-search"></span> Search
                            </button>
                              <a href="<?php echo site_url('loan/create');?>" class="btn btn-default filterreset" >
                            Reset
                           </a>
                            <input type="hidden" value="<?php   echo $tabpagi ;?>" name="tabsearchpagi">
                        </div>
                        <div id="status-area" class="f-message" style="display:none; background: #2fffea;"></div>

                    </form>
                    <div class="action_btn dashboard-actbtn">
                        <button type="button" title="Setting" onclick="page('loan')" data-toggle="modal" data-target="#setting"><i class="fa fa-cog fa-fw"></i></button>

                        <form method="post" action="<?php echo site_url('Excel_export/loan');?>" style="display:inline;">
                            <button type="submit" title="Export" data-toggle="tooltip"><span class="glyphicon glyphicon-ice-lolly-tasted"></span></button>
                            <input type="hidden" name="tab" value="<?php   echo $tabpagi ;?>">
                            <input class="form-control" type="hidden" name="from" id="from" value="<?php echo $from ?>">
                            <input class="form-control" type="hidden" name="to" id="from" value="<?php echo $to ?>">
                        </form>

                        <button class="p-btn" title="Print" onclick="printDiv('printableArea')" value="Print Invoice" data-toggle="tooltip"><span class="glyphicon glyphicon-print"></span></button>
                        <button class="p-btn" title="Trash" style="margin-top: -4px;" onclick="window.location='<?php echo site_url("loan/trash ");?>'"><span class="glyphicon glyphicon-trash"></span></button>
                    </div>
                    <div class="panel-body " id="printableArea">

                        <div class="tab-content">

                            <div id="home">


                                <table width="100%" class="table table-striped table-bordered table-hover mytable" id="dataTables-example">

                                    <thead>
                                        <tr>

                                            <th class="ac">
                                                <p><label><input type="checkbox" id="checkAll" name="check_list[]" /> </label></p>
                                            </th>
                                            <th>DATE</th>
                                            <th>CATEGORY</th>
                                            <th>Note</th>
                                            <th>TOTAL Loan</th>
                                            <th style="text-align: left;">PAY TYPE</th>
                                            <th class="total">PAY TOTAL</th>

                                            <th>DUE BALANCE</th>


                                            <th class="ac">ACTION</th>

                                        </tr>
                                        <input class="savebtn btn btn-danger" id="reset" type="submit" onclick="mycheck()" value="Delete all" />
                                    </thead>
                                    <tbody>

                                        <?php
                      
                      
                           $pay=0.0;
                          $order=0.0;
                          $due=0.0;
                          $due_pay=0.0;
                        for ($i = 0; $i < count($deptlist); ++$i) 

                          { 
                        $bankinfo=unserialize($deptlist[$i]->bank_info);
                             //print_r($bankinfo);exit;
                        if(!empty($bankinfo) && $bankinfo ){
                            
                           foreach($bankinfo as $key=> $bank){
                               if(isset($bankname[$key]) && isset($bankname[$key])){
                                   $keys[] =$bankname[$key];
                               }
                              
                          }  
                            $bank=implode(',',$keys);
                           if($bank){
                            $paybank=' ('.$bank.') ';
                             $keys=array();   
                            }
                            else{
                                $paybank='';
                            }
                           
                        }                    
                        ?>

                                        <tr class="delete_mem<?php echo $deptlist[$i]->id; ?>">

                                            <td class="ac"><input type="checkbox" class="chk" name="selected_item" value="<?php echo ($deptlist[$i]->id); ?>" /></td>

                                            <td> <?php echo date('d F Y',strtotime($deptlist[$i]->date)) ; ?> </td>
                                            <td><a href="<?php echo base_url().'loan/create/'.$deptlist[$i]->loantypeid ;?>"><?php echo ucfirst($deptlist[$i]->loantype); ?></a></td>
                                            <td class="center">
                                                <?php echo  ($deptlist[$i]->note); ?>
                                            </td>
                                            <td class="center">
                                                <?php echo number_format($deptlist[$i]->total_loan, 2); ?>&nbsp;€</td>
                                            <td style="text-align: left;">
                                                <?php echo ucfirst($deptlist[$i]->pay_type).$paybank ; ?> </td>

                                            <td class="center total">
                                                <?php echo  number_format($deptlist[$i]->pay_total, 2); ?>&nbsp;€</td>

                                            <td class="center">
                                                <?php echo  number_format($deptlist[$i]->due_balance, 2); ?>&nbsp;€</td>


                                            <td class="ac"><a class="btn ac edit" data-toggle="modal" data-target="#myModal" onclick="pedit(<?php echo $deptlist[$i]->id;?>)">Edit</a> <a class="btn btn-danger btn-xs ac" onclick="pmyFunction(<?php echo $deptlist[$i]->id;?>)">Delete</a></td>

                                        </tr>
                                        <?php
                
                           $pay+=$deptlist[$i]->pay_total;
                            $order+=$deptlist[$i]->total_loan;
                            $due+=$deptlist[$i]->due_balance;
                  
                          ?>

                                        <?php }?>


                                        <tr class="total-value">
                                            <td>
                                            </td>
                                            <td></td>
                                            <td class="ac"></td>
                                            <td class="total-val">Total</td>
                                            <td id="order">
                                                <?php echo  number_format($order, 2) ; ?>&nbsp;€</td>
                                            <td class="total-val"></td>

                                            <td id="pay">
                                                <?php echo number_format($pay, 2) ;?>&nbsp;€</td>
                                            <td id="due">
                                                <?php echo  number_format($due, 2) ;?>&nbsp;€</td>

                                            <td></td>
                                        </tr>
                                    </tbody>
                                </table>







                            </div>
                            <!--   bank tab-->



                            <!-- end-->

                            <?php 

                   



                        ?>


                        </div>
                    </div>



                    <div class="modal fade" id="myModal" role="dialog">
                        <div class="modal-dialog">

                            <!-- Modal content-->
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal">&times;</button>

                                    <h4 class="modal-title">Wholesale</h4>
                                </div>
                                <div class="modal-body">
                                    <form method="post" id="form" action="<?php echo site_url('loan/value');?>">
                                        <input type="hidden" name="id" value="<?php echo $this->session->userdata('user_id'); ?>">
                                        <div class="form-group">
                                            <label for="usr">Date</label>
                                            <div class="input-group date form_datetime col-md-5" data-date="1979-09-16T05:25:07Z" data-date-format="dd MM yyyy - HH:ii p" data-link-field="dtp_input1">
                                                <input class="form-control" size="16" id="date" type="date" name="date" value="<?php echo date(" Y-m-d "); ?>">


                                            </div>
                                            <input type="hidden" id="dtp_input1" value="" /><br />
                                        </div>
                                        <label for="usr">Category:</label>


                                        <div id="autoUpdate" class="autoUpdate">
                                            <div class="form-group type ">



                                                <select name="select" class="form-control select" id='sel1' required>
                                                    <?php if($data) { ?>
                                                    <option id="category" value="">Select Category First</option>
                                                    <?php } else {?>
                                                    <option id="category" value="">Create Sale Category First</option>
                                                    <?php } ?>
                                                    <?php 

                          foreach ($category as  $type){
                            
                            echo '<option id="type'.$type->id.'" value="'.$type->id.'">'.$type->category.'</option>';

                          }

                          ?>

                                                </select>

                                            </div>
                                        </div>
                                        <div class="form-group invoice">
                                            <label for="pwd">Note:</label>
                                            <input type="text" class="form-control bankcal" name="invoice" id='invoicenumber'>
                                        </div>
                                        <div class="form-group ticket">
                                            <label for="pwd">Total Loan:</label>
                                            <input type="number" class="form-control bankcal" value="00" name="ticket" id='ticket' onKeyPress="bankvalue()" onKeyUp="bankvalue()" step=any>
                                        </div>

                                        <div class="grop">
                                            <br>

                                        </div>
                                        <div class="form-group cash">
                                            <label for="pwd">Pay Type:</label>
                                            <select name="pay_type" class="form-control select " required id='city' onclick="whole_bankshow(this)">
                                                <option id="typepay" value="">Select Pay Type</option>
                                                <option id="no" value="No Pay">No Pay</option>
                                                <option id="Cash" value="Cash">Cash</option>
                                                <option id="CB" value="CB">CB</option>
                                                <option id="Cheque" value="Cheque">Cheque</option>
                                                <!-- <option  id="Cash"  value="cash">CASH</option>
                        <option id="TR" value="ticket">TR</option>-->
                                            </select>
                                        </div>

                                        <div id="edit_purchases_bank">
                                            <div class="form-group" id="wholesale_bank" style="display:none;">




                                            </div>
                                        </div>

                                        <div class="form-group" id="purchase_bank_check">
                                        </div>
                                        <div class="form-group bank">
                                            <label for="pwd">Pay Total:</label>
                                            <input type="number" class="form-control bankcal" value="00" name="bank" id='Minutes' onKeyPress="purchase()" onKeyUp="purchase()" step=any readonly>
                                        </div>

                                        <div class="form-group total">
                                            <label for="pwd">Due Balance:</label>
                                            <input type="hidden" class="form-control" name="total" id='total'>
                                            <label class="form-control" name="permin" id='answer'></label>
                                            <input type="hidden" class="form-control" name="pur_edit" id='p_edit'>
                                        </div>

                                        <input type="submit" class="btn btn-success" value="submit">
                                    </form>
                                </div>
                                <div class="modal-footer">

                                    <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                                </div>
                            </div>

                        </div>
                    </div>

                    <div class="pagi">
                        <?php if (!$from and !$to ){echo $pagination_link;} ?>
                    </div>
                </div>

            </div>
        </div>
    </div>


    <?php $this->load->view('footer.php'); ?>
    <script type="text/javascript" src="<?= base_url() ?>assets/admin/js/jquery-1.8.3.min.js" charset="UTF-8"></script>
    <script type="text/javascript" src="<?= base_url() ?>assets/admin/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="<?= base_url() ?>assets/admin/js/my.js"></script>
    <script type="text/javascript" src="<?= base_url() ?>assets/admin/js/bootstrap-datetimepicker.fr.js" charset="UTF-8"></script>
    <script src="https://code.jquery.com/jquery-1.9.1.js"></script>
    <script>
        function purbankshow(that) {


            var value = that.value;

            $.ajax({
                type: 'POST',
                url: "<?php echo base_url()?>purchases/cash",
                type: "POST",
                dataType: 'json',
                data: {
                    'category': value,
                },
                //  data    : {'type':stateID,'category':status},
                //data    : {'type':stateID,},
                success: function(data) {
                    if (value == 'cash' || value == 'ticket') {
                        swal(value + ' Amount ' + data.cash + '€');
                    }



                }
            });
        }
    </script>
    <script>
        $('#submit').click(function() {
            if ($('#from').val() == '' && $('#to').val() == '') {
                swal({
                    title: "Please Select a Date Range First!",

                    type: "warning",

                    confirmButtonText: 'ok!'
                });
                return false;
            }
        });
    </script>

    <script>
        $(".panel-success").on('show.bs.collapse', function() {
            $('.buttonCon').addClass('vis');
        }).on('hide.bs.collapse', function() {
            $('.buttonCon').removeClass('vis');
        })
    </script>


    <script>
        function myFunction(ref) {
            var datastr = ref;

            $.ajax({
                url: "<?php echo base_url()?>loan/value_delete",
                type: "POST",
                dataType: 'json',
                data: {
                    'uristring': datastr
                },
                success: function(data) {
                    $('#wrapper').load(location.href + '#dataTables-example');

                }
            });
        }
    </script>


    <script>
        $(document).ready(function() {
            $('#checkbox1').change(function() {
                if (!this.checked)
                    //  ^
                    $('#autoUpdate').fadeIn('slow');
                else
                    $('#autoUpdate').fadeOut('slow');
            });
        });
    </script>

    <script>
        function pmyFunction(ref) {
            swal({

                    title: "Are you sure?",
                    text: "You will be able to recover the data from trash!",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonClass: "btn-danger",
                    confirmButtonText: "Yes, delete it!",
                    closeOnConfirm: false
                },
                function() {
                    var datastr = ref;

                    //var datastr='ref='+ encodeURIComponent(ref)

                    $.ajax({
                        url: "<?php echo base_url()?>loan/value_delete",
                        type: "POST",
                        data: {
                            'uristring': datastr
                        },
                        dataType: 'json',
                        success: function(data) {


                        }
                    });
                    swal("Deleted!", "Your data has been deleted.", "success");
                    $(".delete_mem" + ref).fadeOut('slow');
                    $('#wrapper').load(location.href + '#dataTables-example');

                });

            // alert(ref);



        }
    </script>
    <script>
        function newpurchases() {

            save_method = 'add';
            $('#form')[0].reset();
            $('#form').trigger("reset"); // reset form on modals
            $('.form-group').removeClass('has-error');
            $("#answer").text(00);
            $("#p_edit").val('');
            $("#ticket").val('');
            $("#invoicenumber").val('');
            $("#Minutes").val('');
            $("#paytotal").val('');
            $(".checkboxdivedit").remove();



            $('#typepay').attr('selected', 'selected');

            $('#category').attr('selected', 'selected');
            $.ajax({
                url: "<?php echo base_url()?>sale/newcheck",
                type: "POST",
                data: {
                    'uristring': 1
                },
                dataType: 'json',
                success: function(data) {
                    $(".bankclass").val('');
                    $(".bankcal").val('');

                    $("#date").val(data.date);
                    $("#wholesale_bank").html(data.html);
                    //$("#purchase_bank_check").html(data.html);

                }
            });
        }
    </script>
    <script>
        function pedit(ref) {

            var datastr = ref;
            $('#p_edit').val(datastr);
            // alert(ref);
            //var datastr='ref='+ encodeURIComponent(ref)

            $.ajax({
                url: "<?php echo base_url()?>loan/pur_edit",
                type: "POST",
                data: {
                    'uristring': datastr
                },
                dataType: 'json',
                success: function(data) {
                    var myTag = (data.type);

                    var pay = (data.option);



                    $('select[name="select"]').val(data.option);
                    $('select[name="pay_type"]').val(data.type);


                    $('#wholesale_bank').html(data.html);
                    $('#Minutes').val(data.pay_total);
                    $('#ticket').val(data.total_order);
                    $('#total').val(data.due);
                    $('#answer').html(data.due);
                    $('#wholesale_bank').toggle('slow');
                    $('#invoicenumber').val(data.invoice);
                    $("#date").val(data.date);
                    // $('#myModal').modal('show');
                    //$(".delete_mem" + ref).fadeOut('slow');
                }
            });
        }
    </script>


    <script>
    </script>
    <script>
        var total = 0;

        function pbankFunction(id) {

            var checkBox = document.getElementById("myCheck" + id);

            //var text = document.getElementById("text"+id);
            if (checkBox.checked == true) {
                $('#text' + id).toggle('slow');
                $('#text' + id).focus();
                $('#text' + id).val(null);

            } else {
                var text = document.getElementById("text" + id).value;

                $('#text' + id).fadeOut('slow');
                $('#text' + id).val(null);
                bankvalue(text);

            }
            $.ajax({
                url: "<?php echo base_url()?>purchases/bankvalue",
                type: "POST",
                data: {
                    'uristring': id
                },
                dataType: 'json',
                success: function(data) {
                    var myTag = (data.id);

                    $('#bank' + id).val(data.sum);

                    $('#bankvalue' + id).val(data.sum);
                    var text = document.getElementById("text" + id).value;


                }
            });
        }
    </script>

    <script>
        function input(addbank) {

            $("#Minutes").on('input', function() {
                //alert(total);
                var value = parseFloat($(this).val());
                //var value2=parseFloat(total);

                if (value > total) {

                    swal('you have reached a limit of--' + addbank);

                } else {

                }
            });
        }
    </script>
    <script>
        function mycheckFunction() {
            var checkBox = document.getElementById("myCheck");
            var text = document.getElementById("text");
            if (checkBox.checked == true) {
                text.style.display = "block";
                $('#autoUpdate').fadeOut('slow');

            } else {
                $('#autoUpdate').fadeIn('slow');
                text.style.display = "none";
            }
        }

        function mycheck() {
            $('#reset').click(function() {
                if (!$('.chk').is(':checked')) {
                    swal({
                        title: "Please Select a Record Only!",

                        type: "warning",

                        confirmButtonText: 'ok!'
                    });
                    return false;
                } else {
                    swal({
                            title: "Are you sure?",
                            text: "You will be able to recover the data from trash!",
                            type: "warning",
                            showCancelButton: true,
                            confirmButtonClass: 'btn-danger',
                            confirmButtonText: 'Yes, delete it!',
                            closeOnConfirm: false,
                            //closeOnCancel: false
                        },
                        function() {
                            var fav = [];
                            $.each($("input[name='selected_item']:checked"), function() {



                                myFunction($(this).val());
                                //fav.push($(this).val());

                                //alert(fav);

                            });
                            swal("Deleted!", "Your data has been deleted!", "success");

                        });



                }


            });

        }
    </script>


    <script>
        function page(value) {

            $('#pagelimit').val(value);
            $.ajax({
                url: "<?php echo base_url()?>loan/pagelimit",
                type: "POST",
                data: {
                    'limit': value
                },
                dataType: 'json',
                success: function(data) {
                    $('#pagevalue').val(data.limit);
                }
            });
        }
    </script>
    <script>
        $('.transaction.btn.btn-success.nworder').on('click', function() {
            $('#body.nworder-body').slideToggle();
        });
    </script>
</body>

</html>