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

$data='';

$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;

  }
?>
<!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">
                    <div class="panel-heading headingnone">
                        <h2 class="transac">Balance Transfer</h2>
                        <button class="transaction btn btn-success" onclick="add_expense()" data-toggle="modal" data-target="#myModal" data-backdrop="static" data-keyboard="false">Balance Transfer</button>
                    </div>
                </div>
                <div class="main-content">


                    <form method="post" action="<?php echo site_url('Balance/transfer');?>">
                        <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('Balance/transfer');?>" class="btn btn-default filterreset" >
                            Reset
                           </a>

                        </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('transfer')" 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/transfer');?>" style="display:inline;">
                            <button type="submit" title="Export" data-toggle="tooltip"><span class="glyphicon glyphicon-ice-lolly-tasted"></span></button>

                            <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" style="margin-top: -4px;" onclick="window.location='<?php echo site_url("Balance/trash ");?>'"><span class="glyphicon glyphicon-trash"></button>
                    </div>


                    <div class="panel-body " id="printableArea">
                        <div class="tab-content">
                            <div id="home" class="tab-pane fade in active">
                                <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>FROM</th>
                                            <th>AMOUNT</th>

                                            <th>TO</th>
                                            <th>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
                         if($from AND $to){
                          $query=$this->db->query("select page_limit from page_limit");
                          $data='';
                          foreach ($query->result() as $key => $limit){
                             $limit=$limit->page_limit; 
                          }
                         
                                   $cash=0.0;
                                    $card=0.0;
                                    $tr=0.0;
                                    $total=0.0;
                          $cheque=0.0;
                         $bankfromtotal=0; $banktototal=0;
                        $query=$this->db->query("select * from transfer where (date BETWEEN '".$from."' AND '".$to."' ) and  status=1 and buisness_type='$b_type' order by CONVERT(DATE, date) desc 
                         LIMIT ".$limit." ");
                        foreach ($query->result() as $key => $value){
                            $id=$value->id;
                           // print_r($value);
                            
                                    $cash+=$value->cash_pay;
                                       $card+=$value->bank_card;
                                        $cheque+=$value->cheque_pay;
                                       $total+=$value->total;
                                      $bankfromtotal +=$value->amount; 
                                      $banktototal +=$value->total;
                                        echo '  <tr class="delete_mem'.$value->id.' ?>">
                                        <td class="ac">'.'<input type="checkbox" id="chk" name="selected_item" value="'.$value->id.'" />'.'</td>
                                        <td>'. date('d F Y',strtotime($value->date)).'</td>
                                        <td>'. ucfirst($value->bank_from).'</td>
                                        <td>'. number_format($value->amount,2).'€'.'</td>
                                        <td class="center">'.($value->bank_to).'</td>

                                        <td class="center total">'. number_format($value->total,2).'€'.'</td>
                                        <td class="ac"><a class="btn edit" data-toggle="modal" data-target="#myModal" onclick="edit('.$id.')">Edit</a> <a class="btn btn-danger btn-xs" onclick="smyFunction('.$id.')">Delete</a></td>
                                        </tr>'; } } else { $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)); ?>
                                            <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>
                                                        <?php echo ucfirst($deptlist[$i]->bank_from); ?>
                                                    </td>
                                                    <td>
                                                        <?php echo number_format($deptlist[$i]->amount,2) ; ?>€</td>

                                                    <td class="center ticket">
                                                        <?php echo ucfirst($deptlist[$i]->bank_to); ?>
                                                    </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
            
                                    $amount+=$deptlist[$i]->amount;
                                      
                                       
                                    }
                       
                          ?>

                                            <?php }
                          ?>

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

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

                                                <td id="totalsale">
                                                    <?php echo  number_format($banktototal,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">Balance Transfer</h4>
                                </div>
                                <div class="modal-body">
                                    <form method="post" id="form" action="<?php echo site_url('Balance/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>


                                        <div class="grop">
                                            <br>

                                        </div>
                                        <div class="form-group bank ">
                                            <label for="pwd">From:</label>
                                            <input type="hidden" value="" id="bank_from" name="bank_from">
                                            <br>
                                            <div class="expensebank">

                                            </div>
                                        </div>
                                        <label for="usr">to:</label>

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


                                                <select name="select" class="form-control select " required>

                                                    <option class="sel" class="type" value="">Select Bank First</option>



                                                    <?php 
                                    foreach ($bank as  $type){
                                      echo 
                                      '<option id="type'.$type->id.'" value="'.$type->type.'">'.$type->type.'</option>';
                                    }
                                    ?>
                                                </select>


                                            </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>

            </div>
        </div>
    </div>
    <div class="pagi">
        <?php echo $pagination_link; ?>
    </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 add_expense() {
            save_method = 'add';
            $('#form')[0].reset(); // reset form on modals
            $('.form-group').removeClass('has-error');
            $("#answer").text(00);
            $("#id").remove();
            $("#total").val(00);
            $('.sel').attr('selected', 'selected');
            $('.name').attr('selected', 'selected');
            $.ajax({
                url: "<?php echo base_url()?>Balance/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');



            } 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) {
                        $('#bank_from').val(data.type);
                        swal(data.type + ' Amount ' + data.sum + '€');
                    }

                    $('#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()?>balance/delete",
                type: "POST",
                data: {
                    'uristring': datastr
                },
                dataType: 'json',
                success: function(data) {
                    $(".delete_mem" + ref).fadeOut('slow');
                    $('#cash').text(data.cash + '€');
                    $('#totalbank').text(data.bank + '€');
                    $('#totalticket').text(data.ticket + '€');
                    $('#totalsale').text(data.total + '€');

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

                        });

                }
            });

        }
    </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()?>balance/delete",
                        type: "POST",
                        dataType: 'json',
                        data: {
                            'uristring': datastr
                        },

                        success: function(data) {


                            $('#cash').text(data.cash + '€');
                            $('#totalbank').text(data.bank + '€');
                            $('#totalticket').text(data.ticket + '€');
                            $('#totalsale').text(data.total + '€');
                            //$('#answer').html(data.total);
                            //$('#id').val(data.id);
                        }
                    });
                    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;
            $('#id').val(datastr);
            $.ajax({
                url: "<?php echo base_url()?>Balance/show",
                type: "POST",
                data: {
                    'uristring': datastr
                },
                dataType: 'json',
                success: function(data) {


                    $('select[name="select"]').val(data.bank_to);
                    $('#sel').remove();
                    $('#bank_from').val(data.type);
                    $('#Minutes').val(data.bank);
                    $('#ticket').val(data.ticket);
                    $('#total').val(data.total);
                    $('#answer').html(data.total);
                    $("#date").val(data.date);
                    $('.expensebank').html(data.html);

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

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


</body>

</html>