<?php
error_reporting(0);
$tabactv='ss';
$listactiv='ss';
$user_id=$this->session->userdata('user_id');
if(!$user_id){
  redirect('user/login_view', 'refresh');
}
$type=$this->db->query("select type from sale  
  ");
$data='';
foreach ($type->result() as $key => $type){
  $data.=$type->type;
}
 $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';  
}
?>
<!DOCTYPE html>
<html lang="en">
<?php
$this->load->view('head.php');
?>

<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().'pay_loan/create' ?>">All Pay</a></li>
                        <?php
                $type=$this->db->query("select * from pay_loan_category where buisness_id='$b_type' 
                 and status=1 ORDER BY category ASC ");
                $data='';
                foreach ($type->result() as $key => $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().'pay_loan/create/'.$data.'">'. ucfirst($type).'</a> </li>';
                }
                ?>
                    </ul>


                </div>
                <div class="panel-heading headingnone">
                    <h2 class="transac">Pay</h2>
                    <button class="transaction btn btn-success" onclick="add_expense()" data-toggle="modal" data-target="#myModal" data-backdrop="static" data-keyboard="false">Loan Pay</button>
                </div>
                <div class="main-content">

                    <select id="filterprofit">
                        <option value="Total"><a href="#">Total Pay </a></option>
                        <option value="Last week"><a href="#">Last week </a></option>
                        <option value="This Month"><a href="#">This Month </a></option>
                        <option value="Last Month"><a href="#">Last Month </a></option>
                        <option value="This Year"><a href="#">This Year </a></option>
                        <option value="Last Year"><a href="#">Last Year </a></option>
                    </select>

                    <div class="row">
                        <div class="col-xs-12 col-md-12 col-md-12 top-row color-profit-filter nwo">
                            <p>
                                <strong class="totalexpense">0€</strong>
                                <span class="filtervalue">Total </span>&nbsp;<span>Expenses </span>&nbsp;
                            </p>

                        </div>
                    </div>
                    <form method="post" action="<?php echo site_url('pay_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" id="from" type="date" name="from" value="<?php echo $from ; ?>">
                            </label>
                            <label>To<input class="form-control" id="to" type="date" name="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('pay_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('payloan')" 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/payloan');?>" 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("pay_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>TYPE</th>
                                            <th>Note</th>
                                            <th>CASH PAY</th>

                                            <th>CHEQUE PAY</th>
                                            <th class="total">TOTAL</th>
                                            <th class="ac">ACTION</th>
                                        </tr>
                                        <input class="savebtn btn btn-danger" id="reset" type="submit" onclick="dmycheck()" value="Delete all" />
                                    </thead>
                                    <tbody id="result">
                                        <?php
                        
                                    $cheque=0.0; 
                                   $cash=0.0;
                                    $card=0.0;
                                    $tr=0.0;
                                    $total=0.0;
                        for ($i = 0; $i < count($deptlist); ++$i)     
                          { 
                        $value=(unserialize($deptlist[$i]->bank_info)); 
                            
                        $cash+=isset($value[$cashbank]) ? $value[$cashbank]:0.00 ;
                        $cashval=isset($value[$cashbank]) ? $value[$cashbank]:0.00 ;
                        $cheque+=$deptlist[$i]->total-$cashval; ?>
                                        <div id="start">
                                            <tr class="delete_mem<?php echo $deptlist[$i]->id; ?>">
                                                <td class="ac"><input type="checkbox" id="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().'pay_loan/create/'.$deptlist[$i]->loantypeid ;?>"><?php echo ucfirst($deptlist[$i]->loantype); ?></a></td>
                                                <td><?php echo ucfirst($deptlist[$i]->note); ?></td>
                                                <td><?php if(isset($value[$cashbank])) echo number_format($value[$cashbank],2).' €' ;  ?></td>

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

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

                                            </tr>
                                        </div>
                                        <?php
            
                                   
                                       
                                       $total+=$deptlist[$i]->total;
                                       
                                    }
                       
                          ?>



                                        <tr class="total-value">
                                            <td class="ac">
                                            </td>
                                            <td></td>
                                            <td></td>
                                            <td class="total-val">Total</td>

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

                                            <td id="totalsale" class="total"><?php echo  number_format($total,2) ;?>&nbsp;€</td>
                                            <td class="ac"></td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>

                        </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">Pay Loan</h4>
                                </div>
                                <div class="modal-body">
                                    <form method="post" id="form" action="<?php echo site_url('pay_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" type="date" id="date" name="date" value="<?php echo date("Y-m-d"); ?>">
                                            </div>
                                            <input type="hidden" id="dtp_input1" value="" /><br />
                                        </div>
                                        <div class="form-group invoice">
                                            <label for="pwd">Note:</label>
                                            <input type="text" class="form-control bankcal" name="note" id='note'>
                                        </div>
                                        <label for="usr">Type:</label>

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



                                                <select name="select" class="form-control select " required>
                                                    <option id="select" class="type" value="">Select Type First</option>

                                                    <?php  $type=$this->db->query("select * from pay_loan_category  where buisness_id='$b_type' and status=1  order by category
                                      ");
                                    $data='';
                                    foreach ($type->result() as $key => $type){
                                      echo 
                                      '<option id="expense'.$type->id.'" value="'.$type->id.'">'.$type->category.'</option>';
                                    }
                                    ?>
                                                </select>


                                            </div>
                                        </div>

                                        <div class="grop">
                                            <br>

                                        </div>
                                        <div class="form-group bank ">
                                            <label for="pwd">bank:</label>
                                            <br>
                                            <div class="expensebank">

                                            </div>
                                        </div>
                                        <div class="form-group total">
                                            <label for="pwd">Total:</label>
                                            <input type="hidden" class="form-control" name="edit_id" id='id'>
                                            <input type="hidden" class="form-control" name="total" id='total'>
                                            <label class="form-control" name="permin" value="00" id='answer'></label>
                                        </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() {
            $("#filterprofit").on("change", function() {
                $(".filtervalue").text($("#filterprofit").val());
                ajaxfilter($("#filterprofit").val());
            }).trigger("change");

        });
    </script>
    <script>
        function ajaxfilter(value) {

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

                    $('.totalexpense').text((data.totalexpense).toFixed(2) + ' €');

                }
            });
        }
    </script>
    <script>
        function add_expense() {
            save_method = 'add';
            $('#form')[0].reset(); // reset form on modals
            $('.form-group').removeClass('has-error');
            $("#answer").text(00);
            $("#id").val('');
            $("#note").val('');
            $("#total").val(00);
            $('#select').attr('selected', 'selected');
            $('.name').attr('selected', 'selected');
            $.ajax({
                url: "<?php echo base_url()?>expense/newcheck",
                type: "POST",
                data: {
                    'uristring': 1
                },
                dataType: 'json',
                success: function(data) {
                    $("#date").val(data.date);
                    $(".expensebank").html(data.html);
                    //$("#purchase_bank_check").html(data.html);

                }
            });
        }
    </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();



            } 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);
                    // $('#bankname').val(data.type);
                    $('#bank' + id).val(data.sum);
                    if (checkBox.checked == true) {

                        swal(data.type + ' Amount ' + data.sum + '€');
                        $('#text' + id).focus();
                    }
                    $('#bankvalue' + id).val(data.sum);
                    var text = document.getElementById("text" + id).value;


                }
            });
        }
    </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>
        function myFunction(ref) {
            var datastr = ref;

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

                }
            });
        }
    </script>
    <script>

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

    <script>
        function dmycheck() {
            $('#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,
                        },
                        function() {
                            var fav = [];
                            $.each($("input[name='selected_item']:checked"), function() {

                                myFunction($(this).val());


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

                }
            });

        }
    </script>
    <script>
        function smyFunction(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;

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

                        success: function(data) {


                        }
                    });
                    swal("Deleted!", "Your data has been deleted.", "success");
                    $(".delete_mem" + ref).fadeOut('slow');
                    //$('#totalcash').val(data.cash);



                });
        }
    </script>
    <script>
        function edit(ref) {
            var datastr = ref;
            $.ajax({
                url: "<?php echo base_url()?>pay_loan/show",
                type: "POST",
                data: {
                    'uristring': datastr
                },
                dataType: 'json',
                success: function(data) {
                    var myTag = (data.type);
                    $('select[name="select"]').val(data.type);
                    $('#sel').remove();
                    $('#Resources').val(data.cash);
                    $('#Minutes').val(data.bank);
                    $('#ticket').val(data.ticket);
                    $('#total').val(data.total);
                    $('#answer').html(data.total);
                    $('#note').val(data.note);
                    $('#id').val(data.id);
                    $("#date").val(data.date);
                    $('.expensebank').html(data.html);

                }
            });
        }
    </script>


    <script>
        function page(value) {

            $('#pagelimit').val(value);
            $.ajax({
                url: "<?php echo base_url()?>pay_loan/pagelimit",
                type: "POST",
                data: {
                    'limit': value
                },
                dataType: 'json',
                success: function(data) {
                    $('#pagevalue').val(data.limit);
                }
            });
        }
    </script>

</body>

</html>