<?php
 $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 OR $tabtype){
     
     $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;
}
//print_r($bankname);
?>

<!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 purchase-menus">
                    <?php $type=$this->db->query("select * from purchases_type where  buisness_type='$b_type'  
                 and status=1 ORDER BY type ASC ");
                     if(!empty($type->result())){ ?>
                    <br>
                    <div class="d-flex">
                       <div class="search-wrapper" style="max-width: 400px;">
              
                <input type="text" id="searchBox" class="form-control search-field" placeholder="Search...">
                     <i class="fa fa-search search-icon"></i>
                   
                 
               
                 </div>
                     <button id="toggleBtn" class="show-hide-btn">Show All</button>
                    </div>
              
                <ul class="nav nav-tabs" id="categoryTabs">

                <li class="page-header <?php echo $tabactv; ?>"><a href="<?php echo base_url().'purchases/home' ?>">All Purchases</a></li>
                <?php
                
                $data='';
     
                foreach ($type->result() as $key => $type){
                  $data=$type->id;
                  $type=$type->type;
                  
                    if($data==$tabsearch OR $tabpagi==$data){
                     $active='active';   
                    }
                    else{
                        $active='';
                    }
                    
                    if($catmark[$data] < 0){
                        $background='background: #c61d23 !important; color:#ffff;';
                    }
                    else{
                        $background='green'; 
                    }
                  echo '<li class="page-header '.$active.'  category-item" data-name="'.strtolower($type).'"><a style="'.$background.'"   href="'.base_url().'purchases/home/'.$data.'">'. ucfirst($type).'</a> </li>';

                }
                ?>
                    </ul>
                 
                    <?php } ?>
                </div>
                <div class="panel-heading headingnone">
                    <h2 class="transac">Purchases</h2>
                    <div class="panel panel-success neworder">
                        <?php /*$totalorder=0.0; $totalpay=0.0; $unpaid=0.0;
                foreach ($calculate as $key=>$result) {
                           $totalorder+=$result->total_order; $totalpay+=$result->total_order; $unpaid+=$result->due_balance;}    */  ?>

                        <div class="panel-heading headingnone purchases-pan">

                            <button class="transaction btn btn-success nworder">New Order</button>

                        </div>
                        <div class="panel-body nworder-body" id="body">
                            <div class="form-group row">

                                <div class="col-md-12 col-xs-12">
                                    <button class="newtransaction btn btn-success" onclick="newpurchases()" data-toggle="modal" data-target="#myModal" data-backdrop="static" data-keyboard="false">New Order</button>
                                    <button class="newtransaction2 btn btn-success" id="myBtn" style="margin-left: 36px;">Pay Order</button>
                                </div>


                            </div>

                            <div class="form-group row" style="margin-left:auto;margin-right:auto;">
                                <div class="col-md-2 col-md-offset-5">

                                </div>
                            </div>

                        </div>
                    </div>

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




                    <div class="filter-section-div">
                        <select id="filterprofit">
                            <option value="Total Purchases"><a href="#">Total Purchases </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="totalorder">0€</strong>
                                    <span class="filtervalue">Total purchases </span><span> </span>&nbsp;
                                </p>
                                <p>
                                    <strong class="paytotal"></strong>
                                    <span class="filtervalue"></span><span> paid</span>&nbsp;
                                </p>
                                <p>
                                    <strong class="unpaid"></strong>
                                    <span class="filtervalue"> </span> <span> Unpaid</span>&nbsp;
                                </p>
                            </div>
                        </div>
                    </div>
                    <!-- /.panel-heading -->
                    <form method="post" action="<?php echo site_url('purchases/home');?>">
                        <input type="hidden" name="tabsearch" value="" class="purtabsearch">
                        <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('purchases/home');?>" 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('home')" 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/purchases');?>" 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("purchases/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 ORDERS</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
                        $bankkey='';
                        $bankinfo=array();
                           $pay=0.0;
                          $order=0.0;
                          $due=0.0;
                          $due_pay=0.0;
                       // print_r($deptlist);
                        for ($i = 0; $i < count($deptlist); ++$i) 

                          { 
                        $bankinfo=unserialize($deptlist[$i]->bank_info);
                             
                        if(!empty($bankinfo) && $bankinfo ){
                            
                           foreach($bankinfo as $key=> $bank){
                               if(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" 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().'purchases/home/'.$deptlist[$i]->purtypeid ;?>"><?php echo ucfirst($deptlist[$i]->purtype); ?></a></td>
                                            <td class="center"><?php echo  ($deptlist[$i]->invoice_number); ?></td>
                                            <td class="center"><?php echo number_format($deptlist[$i]->total_order, 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_order;
                            $due+=$deptlist[$i]->due_balance;
                                     
                                      
                        
                       
                          ?>

                                        <?php  $cols=''; } ?>



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








                        </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">Purchases</h4>
                                </div>
                                <div class="modal-body">
                                    <form method="post" id="form" action="<?php echo site_url('purchases/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  $type=$this->db->query("select * from purchases_type where buisness_type='$b_type' and status=1 order by type 
                            ");
                          $data='';

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

                          }

                          ?>

                                                </select>

                                            </div>
                                        </div>
                                        <div class="form-group invoice">
                                            <label for="pwd">Note:</label>
                                            <input type="text" class="form-control" name="invoice" id='invoicenumber'>
                                        </div>
                                        <input type="hidden" class="form-control p_edit" name="pur_edit" id='p_edit'>
                                        <div class="form-group ticket">
                                            <label for="pwd">Total Order:</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 " id='city' onclick="pur_bankshow(this)" required>
                                                <option id="typepay" value="">Select Pay Type</option>
                                                <option id="NoPay" 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="purchase_bank" style="display:none;">

                                                <?php 
                 
                    ?>

                                            </div>
                                        </div>

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

                                            <input type="hidden" class="form-control bankcal" value="00" name="bank" id='Minutes' onKeyPress="purchase()" onKeyUp="purchase()" name="bank" id='Minutes' step=any>

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

                                        </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 id="payModal" class="pmodal">

                        <!-- Modal content -->
                        <div class="pmodal-content">
                            <span class="close" onclick="closeDialog(this)">&times;</span>

                            <p>Pay Order..</p>


                            <!-- Modal content-->


                            <form method="post" id="pay_order" action="<?php echo site_url('purchases/pay');?>">
                                <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" name="date" id="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 ">

                                        <?php 
                    $type=$this->db->query("select type from purchases_type where buisness_type='$b_type'  
                      ");
                    $data='';
                    foreach ($type->result() as $key => $type){
                      $data.=$type->type;

                    }

                   ?>

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

                                            <?php if($data) { ?>
                                            <option id="category" value="">Select Category First</option>
                                            <?php } else {?>
                                            <option id="category1" value="">Create Purchases Category First</option>
                                            <?php } ?>
                                            <?php 
                      $type=$this->db->query("select * from purchases_type where buisness_type='$b_type'  
                       and status=1 order by type ");
                      $data='';

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

                      }

                      ?>

                                        </select>








                                    </div>
                                </div>
                                <div class="form-group ticket">
                                    <label for="pwd">Total Order:</label>
                                    <input type="number" step=any class="form-control" name="ticket" id="total_ord" onKeyPress="purchases()" onKeyUp="purchases()">
                                </div>

                                <div class="form-group cash">
                                    <label for="pwd">Pay Type:</label>
                                    <select name="pay_type" class="form-control select " onclick="bankshowpay(this)" required>
                                        <option id="pay_typ" value="">Select pay type</option>
                                        <option id="no_pay" value="No Pay">No Pay</option>
                                        <option value="Cash">Cash</option>
                                        <option value="CB">CB</option>
                                        <option value="Cheque">Cheque</option>
                                        <!--<option value="cash">CASH</option>
                    <option value="tr">TR</option>-->
                                    </select>
                                </div>
                                <div class="form-group" id="purchase_bankpay" style="display:none;">
                                    <?php 
                    $type=$this->db->query("select * from bank_name  where  status=1 
                                      and buisness_id='$b_type' ");
                 
                     foreach ($type->result() as $key => $type){
                      
                    echo '<div id="checkboxdiv" style="display:inline-block;vertical-align:top;max-width:100px"><input  type="checkbox" id="payCheck'.$type->id.'" name="select_bank['.$type->id.']" onclick="paybankFunction('.$type->id.')" value="'.$type->id.'"  >'.$type->type.' </br>
                   
                      <input id="paytext'.$type->id.'" type="text" name="bank_card['.$type->id.']" class="banktext paybankclass" style="display:none; width:100%" value="" onKeyPress="paybankvalue('.$type->id.')" onKeyUp="paybankvalue('.$type->id.')"></br>
                       <input id="paybank'.$type->id.'" type="hidden"  class="banktext class" style="display:none; width:100%" value="" ></br></div>  <input type="hidden" id="bankvalue'.$type->id.'">';
                      }
                    ?>

                                </div>

                                <div class="form-group bank">
                                    <label for="pwd">Pay Total:</label>
                                    <input type="number" class="form-control pay " step=any name="bank" id="pay_total" onKeyPress="purchases()" onKeyUp="purchases()" readonly>
                                </div>

                                <!--<div class="form-group">
                  <label for="pwd">Pay Total:</label>
                  <input type="number" class="form-control pay " step=any name="bank" id="paytotal" onKeyPress="purchases()" onKeyUp="purchases()">
                </div>-->

                                <div class="form-group total">
                                    <label for="pwd">Due Balance:</label>
                                    <input type="hidden" class="form-control" name="total" id="total_due">
                                    <input type="hidden" class="form-control" name="totaldu" id="tdue">
                                    <label class="form-control" style="display:block!important; width:100%;" name="permin" id="due_bal"></label>
                                </div>


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

                            <button type="button" onclick="closeDialog(this)" class="btn btn-danger " data-dismiss="pmodal" style="float:right;">Close</button>

                        </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>
        $('.newtransaction').click(function() {
            var modalDiv = $('#myModal');
            modalDiv.modal({
                backdrop: false,
                show: true
            });

            $('.modal-dialog').draggable({
                handle: ".modal-header"
            });
        });
    </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 type="text/javascript">
        $(document).ready(function() {
            $('#country').on('change', function() {
                var countryID = $(this).val();
                var status = $("#state").val();

                if (countryID) {
                    $.ajax({
                        url: "<?php echo base_url()?>purchases/show",
                        type: "POST",
                        dataType: 'json',

                        data: {
                            'category': countryID,
                            'type': status,
                        },

                        success: function(data) {
                            var totalpay = (data.res);
                            //$('#pay_total').val(data.res);
                            $('#total_ord').val(data.res1);
                            $('#due_bal').html((data.res2).toFixed(2));
                            $('#pay_total').val(data.res);
                            $('#total_due').val(data.res2);
                            $('#type').text(data.res3);
                            //$('#pay_typ').text(data.res5);


                        }

                    });
                } else {


                }
            });

            $('#state').on('change', function() {
                //var pay =$("#state option[value='no pay']").text()
                var stateID = $(this).val();
                var status = $("#country").val();
                var pay = document.getElementById('no').innerHTML;
                if (stateID) {

                } else {

                }
                if (stateID) {

                    $.ajax({
                        type: 'POST',
                        url: "<?php echo base_url()?>purchases/show",
                        type: "POST",
                        dataType: 'json',
                        //  data    : {'type':stateID,'category':status},
                        //data    : {'type':stateID,},
                        success: function(data) {


                        }
                    });
                } else {
                    $('#city').html('<option value="">Select Region first</option>');
                }



            });
            $('#city').on('change', function() {
                var zipID = $(this).val();

                if (zipID) {
                    $.ajax({
                        type: 'POST',
                        url: 'ajaxData.php',
                        data: 'zip_id=' + zipID,
                        success: function(html) {
                            $('#postal').html(html);
                        }
                    });
                } else {
                    $('#postal-code').html('<option value="">Select ville first</option>');
                }
            });

        });
    </script>

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

            $.ajax({
                url: "<?php echo base_url()?>purchases/value_delete",
                type: "POST",
                dataType: 'json',
                data: {
                    'uristring': datastr
                },
                success: function(data) {
                    $(".delete_mem" + ref).fadeOut('slow');
                    $('#pay').text((data.pay).toFixed(2) + '€');
                    $('#order').text((data.order).toFixed(2) + '€');
                    $('#due').text((data.due).toFixed(2) + '€');

                }
            });
        }
    </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()?>purchases/value_delete",
                        type: "POST",
                        data: {
                            'uristring': datastr
                        },
                        dataType: 'json',
                        success: function(data) {

                            $('#pay').text((data.pay).toFixed(2) + '€');
                            $('#order').text((data.order).toFixed(2) + '€');
                            $('#due').text((data.due).toFixed(2) + '€');


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


                });

            // 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);
            $("#paytotal").val('');
            $("#invoicenumber").val('');
            $("#id").val('');


            $('#purchase_bank').fadeOut('slow');

            $("#ticket").val('');
            $(".checkboxdivedit").remove();
            $(".bankclass").val('');
            $(".bankcal").val('');
            $('#type').removeAttr('selected', 'selected');
            $('#pay_typ').removeAttr('selected');
            $('#typepay').attr('selected', 'selected');

            $('#category').attr('selected', 'selected');
            $.ajax({
                url: "<?php echo base_url()?>purchases/newcheck",
                type: "POST",
                data: {
                    'uristring': 1
                },
                dataType: 'json',
                success: function(data) {
                    $(".bankclass").val('');
                    $(".bankcal").val('');
                    $("#date").val(data.date);
                    $("#purchase_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()?>purchases/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.category);
                    $('select[name="pay_type"]').val(data.type);

                    $('#purchase_bank').html(data.html);
                    $('#Minutes').val(data.pay_total);
                    $('#paytotal').val(data.pay_total);
                    $('#ticket').val(data.total_order);
                    $('#total').val(data.due);
                    $('#answer').html(data.due);
                    $('#purchase_bank').toggle('slow');
                    $("#date").val(data.date);
                    $('#invoicenumber').val(data.invoice);
                    // $('#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();


            } 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>
        var total = 0;

        function paybankFunction(id) {

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

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


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

                $('#paytext' + id).fadeOut('slow');
                $('#paytext' + 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);
                    $('#paybank' + id).val(data.sum);
                    if (checkBox.checked == true) {
                        swal(data.type + ' Amount ' + data.sum + '€');
                        $('#paytext' + id).focus();
                    }
                    $('#bankvalue' + id).val(data.sum);
                    var text = document.getElementById("paytext" + id).value;

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

            $('#pagelimit').val(value);
            $.ajax({
                url: "<?php echo base_url()?>purchases/pagelimit",
                type: "POST",
                data: {
                    'limit': value
                },
                dataType: 'json',
                success: function(data) {
                    $('#pagevalue').val(data.limit);
                }
            });
        }
    </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() {
            $("#filterprofit").on("change", function() {
                $(".filtervalue").text($("#filterprofit").val());
                ajaxfilter($("#filterprofit").val());
            }).trigger("change");

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

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

                    $('.totalorder').text((data.totalorder).toFixed(2) + ' €');
                    $('.paytotal').text((data.totalpay).toFixed(2) + ' €');
                    $('.unpaid').text((data.unpaid).toFixed(2) + ' €');
                }
            });
        }
    </script>
    <script>
        function purtabfilter2(tabid) {
            $('.tabsearch').val(tabid);
            $.ajax({
                url: "<?php echo base_url()?>purchases/activepurchasespagination",
                type: "POST",
                data: {
                    'purchasestabid': tabid,
                    'purchasespage': 3,
                },
                dataType: 'json',
                success: function(data) {
                    if (data.success) {
                        window.location.href = "<?php echo site_url('purchases/home'); ?>";
                    }

                }
            });
        }

        $('.transaction.btn.btn-success.nworder').on('click', function() {
            $('#body.nworder-body').slideToggle();
        });
    </script>
    <style>
        .redclass {
            background: red !important;
        }
    </style>
</body>

</html>