<?php

 $tabactv='ss';
     $listactiv='ss';
$user_id=$this->session->userdata('user_id');
if(!$user_id){
  redirect('user/login_view', 'refresh');
}
$type=$this->db->query("select * from  worker  
  ");
$data='';
foreach ($type->result() as $key => $type){
  $data.=$type->month;
}
$value='';
$limit=$this->db->query("select page_limit from page_limit");
foreach ($limit->result() as $key => $value)
$limit=$value->page_limit;
?>
<?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';  
}
?>
<!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">
                      <?php    $type=$this->db->query("select * from worker_name where buisness_type='$b_type' 
                 and status=1 ORDER BY name 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().'worker/home' ?>">All</a></li>
                        <?php
              
                $data='';
                foreach ($type->result() as $key => $type){
                  $data=$type->id;
                  $type=$type->name;
                     if($data==$tabsearch OR $tabpagi==$data){
                     $active='active';   
                    }
                    else{
                        $active='';
                    }
                  echo '<li class="category-item page-header '.$active.'" data-name="'.strtolower($type).'"><a  href="'.base_url().'worker/home/'.$data.'">'. ucfirst($type).'</a> </li>';
                }
                ?>
                    </ul>
                  <?php } ?>
                </div>
                <div class="panel-heading headingnone">
                    <h2 class="transac">Salary Details</h2>
                    <button class="transaction btn btn-success" data-toggle="modal" data-target="#myModal" onclick="add_workers()" data-backdrop="static" data-keyboard="false">New Payment</button>
                </div>
                <div class="main-content">


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

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

                        </div>
                    </div>
                    <form method="post" action="<?php echo site_url('worker/home');?>">
                        <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('worker/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('whome')" 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/worker');?>" 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("worker/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>MONTH</th>
                                            <th>NAME</th>
                                            <th>CASH PAY</th>
                                            <th>CHEQUE PAY</th>
                                            <th class="total">SALARY</th>
                                            <th class="ac">ACTION</th>
                                        </tr>
                                        <input class="savebtn btn btn-danger" id="reset" type="submit" onclick="wmycheck()" value="Delete all" />
                                    </thead>
                                    <tbody>
                                        <?php
                                                   $cash=0.0; 
                                                $cheque=0.0; 
                                                $salary=0.0; 
                                                for ($i = 0; $i< count($deptlist); ++$i) { 
                                                    
                                                    $value=(unserialize($deptlist[$i]->bank_info)); $cash+=$value[$cashbank];
                                                    $cheque+=$deptlist[$i]->salary-($value[$cashbank]);
                                                      ?>
                                        <tr class="delete_mem<?php echo $deptlist[$i]->id; ?>">
                                            <td class="ac"><input type="checkbox" id="check" 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]->month); ?>
                                            </td>
                                            <td><a href="<?php echo base_url().'worker/home/'.$deptlist[$i]->workerid ;?>"><?php echo ucfirst($deptlist[$i]->workername) ; ?></a></td>

                                            <td class="center">
                                                <?php echo number_format($value[$cashbank],2); ?>&nbsp;€</td>
                                            <td class="center">
                                                <?php echo number_format($deptlist[$i]->salary-($value[$cashbank]),2); ?>&nbsp;€</td>
                                            <td class="center total">
                                                <?php echo  number_format($deptlist[$i]->salary,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 id="printpagebutton" class="btn btn-danger btn-xs" onclick="smyFunction(<?php echo $deptlist[$i]->id;?>)">Delete</a></td>
                                        </tr>
                                        <?php
                
                                  
                                                          $salary+=$deptlist[$i]->salary;
                       
                                                             ?>

                                        <?php }?>

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

                                            <td id="workersalary">
                                                <?php echo  number_format($salary,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">Worker</h4>
                                </div>
                                <div class="modal-body">
                                    <form method="post" id="form" action="<?php echo site_url('worker/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">Month:</label>

                                        <div id="autoUpdate" class="autoUpdate">
                                            <div class="form-group type ">
                                                <select name="month" class="form-control select " required>
                                                    <option class="month" value="">Select Month First</option>
                                                    <option value="January" id="January">January</option>
                                                    <option value="February" id="February">February</option>
                                                    <option value="March" id="March">March</option>
                                                    <option value="April" id="April">April</option>
                                                    <option value="May" id="May">May</option>
                                                    <option value="June" id="June">June</option>
                                                    <option value="July" id="July">July</option>
                                                    <option value="August" id="August">August</option>
                                                    <option value="September" id="September">September</option>
                                                    <option value="October" id="October">October</option>
                                                    <option value="November" id="November">November</option>
                                                    <option value="December" id="December">December</option>
                                                </select>

                                            </div>
                                        </div>

                                        <div class="grop">
                                            <br>

                                        </div>
                                        <div class="form-group cash">
                                            <label for="pwd">Name:</label>
                                            <?php 
                        $type=$this->db->query("select * from worker_name  where buisness_type='$b_type' and status=1 
                          ");
                        $data='';
                        foreach ($type->result() as $key => $type){
                          $data.=$type->name;

                        }

                        ?>

                                            <select name="select" class="form-control select " required>
                                                <?php if($data) { ?>
                                                <option class="name" value="">Select Worker Name First</option>
                                                <?php } else {?>
                                                <option id="name1" value="">Create Worker Name First</option>
                                                <?php } ?>
                                                <?php  $type=$this->db->query("select * from worker_name  where buisness_type='$b_type' and status=1 order by name");
                          $data='';

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

                          }

                          ?>

                                            </select>


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

                                            </div>
                                        </div>

                                        <div class="form-group total">
                                            <label for="pwd">Salary:</label>
                                            <input type="hidden" class="form-control" name="edit_id" id='id'>
                                            <input type="hidden" class="form-control" name="total" id='total'>
                                            <label class="form-control" name="permin" value="00" id='answer'></label>
                                        </div>

                                        <input type="submit" class="btn btn-success" value="submit">
                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                                </div>
                            </div>
                        </div>
                    </div>

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

            </div>
        </div>

    </div>

    <?php $this->load->view('footer.php'); ?>
    <script type="text/javascript" src="<?= base_url() ?>assets/admin/js/jquery-1.8.3.min.js" charset="UTF-8"></script>
    <script type="text/javascript" src="<?= base_url() ?>assets/admin/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="<?= base_url() ?>assets/admin/js/my.js"></script>
    <script type="text/javascript" src="<?= base_url() ?>assets/admin/js/bootstrap-datetimepicker.fr.js" charset="UTF-8"></script>
    <script src="https://code.jquery.com/jquery-1.9.1.js"></script>
    <script>
        $(function() {
            $("#filterprofit").on("change", function() {
                $(".filtervalue").text($("#filterprofit").val());
                ajaxfilter($("#filterprofit").val());
            }).trigger("change");

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

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

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

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

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

            $.ajax({
                url: "<?php echo base_url()?>worker/delete",
                type: "POST",
                dataType: 'json',
                data: {
                    'uristring': datastr
                },
                success: function(data) {
                    $(".delete_mem" + ref).fadeOut('slow');
                    $('#workercash').text(data.cash + '€');
                    $('#workercheque').text(data.cheque + '€');
                    $('#workersalary').text(data.salary + '€');
                }
            });
        }
    </script>
    <script>
    </script>
    <script>
        $(document).ready(function() {
            $('#checkbox1').change(function() {
                if (!this.checked)
                    //  ^
                    $('#autoUpdate').fadeIn('slow');
                else
                    $('#autoUpdate').fadeOut('slow');
            });
        });
    </script>
    <script type="text/javascript">
        function printDiv(divName) {
            var printButton = document.getElementById("printpagebutton");
            //Set the print button visibility to 'hidden' 
            printButton.style.visibility = 'hidden';
            //Print the page content
            var printContents = document.getElementById(divName).innerHTML;
            var originalContents = document.body.innerHTML;
            document.body.innerHTML = printContents;
            window.print();
            document.body.innerHTML = originalContents;
        }
    </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 wmycheck() {
            $('#reset').click(function() {
                if (!$('#check').is(':checked')) {
                    swal({
                        title: "Please Select a Record Only!",

                        type: "warning",

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

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


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

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

                    $.ajax({
                        url: "<?php echo base_url()?>worker/delete",
                        type: "POST",
                        dataType: 'json',
                        data: {
                            'uristring': datastr
                        },
                        success: function(data) {
                            $('#workercash').text(data.cash + '€');
                            $('#workercheque').text(data.cheque + '€');
                            $('#workersalary').text(data.salary + '€');
                        }
                    });
                    swal("Deleted!", "Your data has been deleted.", "success");
                    $(".delete_mem" + ref).fadeOut('slow');



                });



        }
    </script>
    <script>
        function edit(ref) {
            var datastr = ref;

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

            $.ajax({
                url: "<?php echo base_url()?>worker/show",
                type: "POST",
                data: {
                    'uristring': datastr
                },
                dataType: 'json',
                success: function(data) {
                    var month = (data.month);
                    var name = (data.workerid);
                    $('select[name="select"]').val(data.name);
                    $('select[name="month"]').val(data.month);
                    $('#month').remove();
                    $('#name').remove();
                    $('#name').text(data.name);
                    $('#Minutes').val(data.cash);
                    $('#date').val(data.date);
                    $('#ticket').val(data.cheque);
                    $('#total').val(data.salary);
                    $('#answer').html(data.salary);
                    $('#id').val(data.id);
                    $(".workerbank").html(data.html);
                    // $('#myModal').modal('show');
                    //$(".delete_mem" + ref).fadeOut('slow');
                }
            });
        }
    </script>
    <script>
        function add_workers() {
            save_method = 'add';
            $('#form')[0].reset(); // reset form on modals
            $('.form-group').removeClass('has-error');
            $("#answer").text(00);
            $("#id").remove();
            $("#total").val(00);
            $('.month').attr('selected', 'selected');
            $('.name').attr('selected', 'selected');
            $.ajax({
                url: "<?php echo base_url()?>worker/newcheck",
                type: "POST",
                data: {
                    'uristring': 1
                },
                dataType: 'json',
                success: function(data) {
                    $('#date').val(data.date);
                    $(".workerbank").html(data.html);
                    //$("#purchase_bank_check").html(data.html);

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

        function pbankFunction(id) {

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

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

                $('#text' + id).focus();

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

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

            }
            $.ajax({
                url: "<?php echo base_url()?>purchases/bankvalue",
                type: "POST",
                data: {
                    'uristring': id
                },
                dataType: 'json',
                success: function(data) {
                    var myTag = (data.id);
                    // $('#bankname').val(data.type);
                    $('#bank' + id).val(data.sum);
                    if (checkBox.checked == true) {

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


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

            $('#pagelimit').val(value);
            $.ajax({
                url: "<?php echo base_url()?>worker/pagelimit",
                type: "POST",
                data: {
                    'limit': value
                },
                dataType: 'json',
                success: function(data) {
                    $('#pagevalue').val(data.limit);
                }
            });
        }
    </script>
    <script>
        function workertabfilter2(tabid) {
            $('.tabsearch').val(tabid);
            $.ajax({
                url: "<?php echo base_url()?>worker/activepagination",
                type: "POST",
                data: {
                    'workertabid': tabid,
                    'workerpage': 5,
                },
                dataType: 'json',
                success: function(data) {
                    if (data.success) {
                        window.location.href = "<?php echo site_url('worker/home'); ?>";
                    } else {
                        return false;
                    }

                }
            });
        }
    </script>

</body>

</html>