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

    
?>
<?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='';
   
 }
else{
   
   $tabactv='active';
     $listactv='';  
}

?>
<!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().'user/search/' ?>">All Sale</a></li>
                        <?php
               
                foreach ($saletype as $key => $type){
                  $data=$type->id;
                  $type=$type->type;
                    if($data==$tabsearch OR $tabpagi==$data){
                     $active='active';   
                    }
                    else{
                        $active='';
                    }
                    
                  echo '<li class="page-header '.$active.'"><a   href="'.base_url().'user/search/'.$data.'">'. ucfirst($type).'</a> </li>';
                }
                ?>
                    </ul>

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


                    <div style="padding-bottom: 44px;">
                        <select id="filterprofit">
                            <option value="Total Sale"><a href="#">Total Sale </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 ">
                                <p>
                                    <strong class="totalsale">0€</strong>
                                    <span class="filtervalue">Total Sale </span><span> </span>&nbsp;
                                </p>
                                <?php 
               
                foreach ($saletype as $key => $saletype){
                
                     
                    echo ' <p>
                  <strong class="tab'.$saletype->id.'"></strong>
                  <span class="filtervalue"></span><span> '.$saletype->type.'</span>&nbsp;
                </p>';
                }
                ?>




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

                    <form method="post" action="<?php echo site_url('user/search');?>">
                        <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('user/search');?>" 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('search')" 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/action');?>" 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" id="from" type="hidden" name="from" value="<?php echo $from ; ?>">
                            <input class="form-control" id="from" type="hidden" name="to" 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" data-toggle="tooltip" style="margin-top: -4px;" onclick="window.location='<?php echo site_url("user/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>

                                            <?php  $type=$this->db->query("select * from bank_name  where  status=1 and record=1 and buisness_id='$b_type'
                                      ");
                                   
                                    foreach ($type->result() as $key => $type){
                                      echo 
                                      '<th>'.ucfirst($type->type).'</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
                    
                     // else {
                        
                                   $cash=0.0;
                                    $card=0.0;
                                    $tr=0.0;
                                    $total=0.0;
                          //$value=(unserialize($deptlist->bank_info));
                         
                        for ($i = 0; $i < count($deptlist); ++$i) 
                           
                          { 
                       ?>
                                        <div id="start">
                                            <tr class="delete_mem<?php echo $deptlist[$i]->id; ?>">
                                                <td class="ac"><input type="checkbox" class="ac" 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().'user/search/'.$deptlist[$i]->saleid ;?>"><?php echo ucfirst($deptlist[$i]->saletype); ?></a></td>




                                                <?php  $type=$this->db->query("select * from bank_name  where status=1 
                                      and record=1 and buisness_id='$b_type'");
              
                                  $value=(unserialize($deptlist[$i]->bank_info));
          
                                  //print_r($value);
                                    foreach ($type->result() as $key => $type){
                                       
                                    //echo $type->id;
                                        if($value[$type->id]){
                                      //foreach ($value as $key => $bankval) 
                            
                                       //{ 
                                           
                                        //if($type->id==$key){
      
                                            echo '<td>'.number_format($value[$type->id],2).' €'.'</td>';
                                            $sum[$type->id]+=($value[$type->id]);
                                         //}
                                        
                                           
                                         
        
                                     // } 
                                        }
                                    else{
                                         echo '<td>0.00 €</td>';
                                        $sum[$type->id]+=0.0;
                                    }
                                        
                                    }
                         
                                    
                                    ?>



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


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

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

                                        <?php //}
                          ?>

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



                                            <?php  $typen=$this->db->query("select * from bank_name  where status=1 
                                      and record=1 and buisness_id='$b_type'");
                               foreach ($typen->result() as $key => $typen){
                              //for ($m = 1; $m <=$row; ++$m) 
                            
                                      // { 
                                  // print_r($sum);
                                      //if($sum[$typen->id]){
                                          echo '<td id="bank'.$typen->id.'">'.number_format($sum[$typen->id],2).' €'.'</td>';  
                                        
                                      //}
                                   //else{
                                       //  echo '<td id="bank'.$typen->id.'">0.00 €</td>';
                                  // }
                                        
                            
                                         
                                               
                                      // }
                               }
                                     ?>


                                            <td id="totalsale" class="total"><?php echo  number_format($total,2) ;?>€</td>
                                            <td></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" id="closemodal" data-dismiss="modal">&times;</button>
                                    <h4 class="modal-title">Sale</h4>
                                </div>
                                <div class="modal-body">
                                    <form method="post" id="form" action="<?php echo site_url('user/value');?>">
                                        <input type="hidden" name="id" value="<?php echo $this->session->userdata('user_id'); ?>">
                                        <div class="first-pop-group">
                                            <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="" />
                                            </div>

                                            <div id="autoUpdate" class="autoUpdate">
                                                <div class="form-group type ">
                                                    <label for="usr">Type:</label>
                                                    <?php 
                                  $type=$this->db->query("select type from sale_type where buisness_type='$b_type' and status=1 
                                    ");
                                  $data='';
                                  foreach ($type->result() as $key => $type){
                                    $data.=$type->type;
                                  }
                                   ?>
                                                    <select name="select" class="form-control select " required>
                                                        <?php if($data) { ?>
                                                        <option class="sel" id="sel" value="">Select a type</option>
                                                        <?php } else {?>
                                                        <option id="sel1" value="">Create Sale Type First</option>
                                                        <?php } ?>
                                                        <?php  $type=$this->db->query("select * from sale_type  where buisness_type='$b_type' and status=1 order by type
                                      ");
                                    $data='';
                                    foreach ($type->result() as $key => $type){
                                      echo 
                                      '<option class="type'.$type->id.'" value="'.$type->id.'">'.$type->type.'</option>';
                                    }
                                    ?>
                                                    </select>

                                                </div>

                                            </div>
                                        </div>



                                        <div class="form-group bankdiv">

                                            <label for="pwd">Payment option:</label>

                                            <div id="checkboxarea">



                                            </div>
                                        </div>



                                        <div class="grop">
                                            <br>

                                        </div>

                                        <div class="form-group bank">
                                            <label for="pwd">Sub-total:</label>
                                            <!--<input type="number" class="form-control " name="bank" value="00" id=''>-->
                                            <input type="hidden" class="form-control bankresult" name="bank" id='Minutes'>
                                            <label class="form-control total_value" name="permin" value="" id='totalbankamount'></label>

                                        </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="totalval">
                                            <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>
        <?php $this->load->view('footer.php'); ?>
    </div>

    <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>
        $('#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 demyFunction(ref) {
            var datastr = ref;

            $.ajax({
                url: "<?php echo base_url()?>user/delete",
                type: "POST",
                data: {
                    'uristring': datastr
                },
                dataType: 'json',
                success: function(data) {
                    $(".delete_mem" + ref).fadeOut('slow');

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


        }
    </script>
    <script>
        function add_transaction(id) {

            save_method = 'add';
            $('#form')[0].reset(); // reset form on modals
            $('.form-group').removeClass('has-error');
            $("#answer").text(00);
            $("#totalbankamount").text(00);
            $(".checkboxdivedit").remove();
            $('.sel').attr('selected', 'selected');

            $('.bankdiv').show();

            $("#id").val('');


            $.ajax({
                url: "<?php echo base_url()?>user/newcheck",
                type: "POST",
                data: {
                    'uristring': 1
                },
                dataType: 'json',
                success: function(data) {
                    $("#date").val(data.date);
                    $("#checkboxarea").html(data.html);

                }
            });


        }
    </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() {
                                //alert($(this).val());
                                demyFunction($(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()?>user/delete",
                        type: "POST",
                        dataType: 'json',
                        data: {
                            'uristring': datastr
                        },

                        success: function(data) {

                            /*  $.each(data.bank, function(index) {
                                         $('#bank'+index).text((data.bank[index]).toFixed(2) +' €');
                                         });*/

                            //$('#totalsale').text((data.total).toFixed(2) +'€');

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



                });
        }
    </script>
    <script>
        function edit(ref) {
            $('#checkboxdiv').remove();

            var datastr = ref;
            $.ajax({
                url: "<?php echo base_url()?>user/show",
                type: "POST",
                data: {
                    'uristring': datastr
                },
                dataType: 'json',
                success: function(data) {
                    $('#checkboxarea').html(data.bank);
                    var myTag = (data.type);
                    $('select[name="select"]').val(data.saletype);
                    //$('.type'+myTag).attr('selected', 'selected');
                    $('#Resources').val(data.cash);
                    $('#Minutes').val((data.total));
                    $('#ticket').val(data.ticket);
                    $('#total').val((data.total));
                    $("#date").val(data.date);
                    $('#totalval').val(data.total);
                    $('#totalbankamount').html(data.totalbank);
                    $('#answer').html((data.total));
                    $('#id').val(data.id);

                }

            });
        }
    </script>
    <script>
        function updateresult() {
            //alert("hello");
            $.ajax({
                url: "<?php echo base_url()?>user/delete",
                // type: "POST",
                dataType: 'json',
                success: function(data) {

                    $('#totalcash').val(data.cash);


                }
            });
        }
    </script>
    <script>
        $('#closemodal').click(function() {
            // $(".checkboxdivedit").empty();
            $('#myModal').modal('hide');
            // $("#checkboxarea").append('<div>f </div>');

        });
    </script>

    <script>
        function tabfilter2(tabid) {
            $('.tabsearch').val(tabid);
            $.ajax({
                url: "<?php echo base_url()?>user/activepagination",
                type: "POST",
                data: {
                    'tabid': tabid,
                    'page': 2,
                },
                dataType: 'json',
                success: function(data) {
                    if (data.tabid) {
                        window.location.href = "<?php echo site_url('user/search'); ?>";
                    }


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

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

    <script>
        $(function() {
            $("#filterprofit").on("change", function() {
                $(".filtervalue").text($("#filterprofit").val());
                ajaxfilter($("#filterprofit").val());
            }).trigger("change");

        });
    </script>
    <script>
        function ajaxfilter(value) {
            var active = '<?php echo $tabpagi;?>';
            //alert(active); 


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

                    $('.totalsale').text((data.totalsale).toLocaleString(undefined, {
                        minimumFractionDigits: 2
                    }) + ' €');
                    $.each(data.tab, function(index) {
                        $('.tab' + index).text((data.tab[index]).toLocaleString(undefined, {
                            minimumFractionDigits: 2
                        }) + ' €');
                    });
                    /*$('.tab1').text(data.tab1 +' €');
                    $('.tab2').text(data.tab2 +' €');
                    $('.tab3').text(data.tab3 +' €');*/
                }
            });
        }
    </script>
</body>

</html>