<script type="text/javascript">
$(document).ready(function() {   
    $('select').select2();
    $.each($(".select2-container"), function(i, n) {
        $(n).next().show().fadeTo(0, 0).height("0px").css("left", "auto"); // make the original select visible for validation engine and hidden for us
        $(n).prepend($(n).next());
        $(n).delay(500).queue(function() {
            $(this).removeClass("validate[required]"); //remove the class name from select2 container(div), so that validation engine dose not validate it
            $(this).dequeue();
        });
    });
    //Datepicker
   jQuery('.datepicker2').datepicker({
            dateFormat : 'dd-mm-yy',
            changeMonth : true,
            changeYear : true
            });
   
   //Data table configurations
   var oTable = $("#test").dataTable({
        "iDisplayLength": 10,
        "aLengthMenu": [5,10,25,50,100],
        "sPaginationType": "full_numbers",
        "aoColumns": [  {"bSortable": false },
                        {"bSortable": false },
                    <?php foreach($tbHdr as $v): ?>
                        null,
                    <?php endforeach; ?>
                        {"bSortable": false }
                    ] 
    });
    oTable.fnSort( [ [0,'asc'] ] );

    //
    $('#startDate,#endDate').change(function(){
        var startDate = $('#startDate').val();
        var endDate = $('#endDate').val();
        var url = "<?php echo base_url(); ?>gstReport/DebtRecoverConfirm/"+startDate+'/'+endDate;
        window.location = url;
    });   

    $("table .checkall1").live("click", "checkall1",function(){           
        $(oTable.fnGetNodes()).find(':checkbox').prop('checked', this.checked);
    });

}); 

//posting for journal entry
function postbaddebt(){
    var badDebtID = '';
    $('.checkbox:checked').each(function(){
        if(badDebtID == ''){
            badDebtID += $(this).val();
        }else{
            badDebtID += ','+$(this).val();
        }
    });

    if(badDebtID == "") {
        alertbox("You need to select at least one transaction",2);
        return false;
    }
    jQuery.ajax({
        url: "<?php echo base_url() ?>gstReport/DebtRecoverPosting",
        async: false,
        type: "POST",
        data: 'badDebtID='+badDebtID,
        dataType: "json",
        success: function(data) {
            if(data.status == 'success'){                
                window.location.reload(true);
            }else{
                alertbox(data.message,2);
            }

        }
    });

}

//removing bad debt entry
function removebaddebt(){
    var badDebtID = '';
    $('.checkbox:checked').each(function(){
        if(badDebtID == ''){
            badDebtID += $(this).val();
        }else{
            badDebtID += ','+$(this).val();
        }
    });

    if(badDebtID == "") {
        alertbox("You need to select at least one transaction",2);
        return false;
    }

    jQuery.ajax({
        url: "<?php echo base_url() ?>gstReport/removeBadDebtRecover",
        async: false,
        type: "POST",
        data: 'badDebtID='+badDebtID,
        dataType: "json",
        success: function(data) {
            if(data.status == 'success'){                
                window.location.reload(true);
            }else{
                alertbox(data.message,2);
            }

        }
    });

}

</script>

<div id="content">                        
<div class="wrap">
<div class="head">
    <div class="info">
        <!--<h1><?php echo element('compName', $this->session->userdata('logged_in')) ?> [ 
            <?php echo element('compNo', $this->session->userdata('logged_in')) ?> ]</h1>-->
        <ul class="breadcrumb">
            <li><a href='<?php echo base_url(); ?>'><?php echo $this->lang->line('dashboard') ?></a> <span class="divider">-</span></li>
            <li><a href="<?php echo base_url(); ?>/locationtransfer"><?php echo $this->lang->line('module') ?></a> <span class="divider">-</span></li>
            <li class="active"><?php echo $this->lang->line('badDebtRecoverConfirm') ?></li>
        </ul>
    </div>
    <div class="search">
        <form action="<?php echo base_url() ?>admin/search" method="post">
            <input name="search_text" type="text" placeholder="search..."/>                                
            <button type="submit"><span class="i-magnifier"></span></button>
        </form>
    </div>                        
</div><!-- head --> 
<div class="content">
    <!--Company information start-->
    <?php //$this->load->view('layouts/rptheader'); ?>
    <!--Company information end-->
    
    <div class="wrap">        
        <div class="content">
            <div class="wrap">                 
                <div class="row-fluid">
                    <div class="span12">        
                    <div class="block">
                        <div class="block">
                    <div class="head">
     <h2><strong><?php echo $this->lang->line('badDebtRecoverHdr') ?></strong></h2>
</div><!-- head -->
                    <div class="content">    
                        <!--Location transfer top form-->  
                        <div class="row-fluid scRow">
                            <div class="span6 scCol">
                                <div class="controls-row" style="padding:10px 10px 8px;">
                                    <div class="span4"><?php echo form_label($this->lang->line('startDate')); ?></div>
                                    <div class="input-prepend">
                                        <span class="add-on"><i class="i-calendar"></i></span>
                                        <?php
                                            $data = array(
                                            'name' => 'selDateFrom',
                                            'id' => 'startDate',
                                            'type' => 'text',
                                            'class' => 'input-small datepicker2 financhk',);
                                            $js='';
                                            echo form_input($data, date('d-m-Y', strtotime($startDate)), $js);
                                            
                                        ?>       
                                    </div>
                                </div>                                                          
                            </div>
                            <div class="span6 scCol"> 
                                <div class="controls-row" style="padding:10px 10px 8px;">
                                    <div class="span4"><?php echo form_label($this->lang->line('endDate')); ?></div>
                                    <div class="input-prepend">
                                        <span class="add-on"><i class="i-calendar"></i></span>
                                        <?php
                                            $data = array(
                                            'name' => 'selDateFrom',
                                            'id' => 'endDate',
                                            'type' => 'text',
                                            'class' => 'input-small datepicker2 financhk',);
                                            $js='';
                                            echo form_input($data, date('d-m-Y', strtotime($endDate)), $js);
                                            
                                        ?>       
                                    </div>
                                </div>                                                                                                                            
                            </div>
                        </div>
                        <!--Location transfer top form-->
                    </div>
                    </div>    
                        <div class="block">                            
                            <div class="head">
                                <h2><?php echo $this->lang->line('titleDebtRecover') ?></h2>
                                <!--<ul class="buttons">
                                     <li><a href="<?php echo base_url()?>locationtransfer/printReport/<?php echo $dateFrom.'/'.$dateTo; ?>" title="updatePosting" onclick=""><?php echo $this->lang->line('print') ?></a></li>
                                     <li><a href="<?php echo base_url(); ?>locationtransfer/add" title="Add New"><span class="i-plus-2"></span></a></li>  
                                </ul>-->
                            </div><!-- head -->
                            <div class="content np table-sorting">
                                <table cellpadding="0" cellspacing="0" width="100%" id="test" class="editable itemTbl">
                                    <thead>
                                    <tr>
                                        <th style="display:none;">&nbsp;</th>
                                        <th class="tac span2">#</th>
                                    <?php foreach($tbHdr as $row): 
                                        if(!in_array($row,$alignRight)):
                                        ?>
                                        <th class="tac"><?php echo $this->lang->line($row); ?></th>
                                    <?php else:?>
                                        <th class="tar"><?php echo $this->lang->line($row); ?></th>
                                    <?php 
                                            endif;
                                        endforeach; ?>                          
                                        <th width="55" class="tac span2"><?php echo $this->lang->line('action');?></th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <?php $bil = 0; 
                                    if(isset($datatbls) && !empty($datatbls)):
                                        foreach($datatbls as $item):
                                        $bil++?>
                                        <tr class="itemList">
                                            <td class="ID" style="display:none"><?php echo $item->debtRecoverID; ?></td>
                                            <td class="tac"><?php echo $bil; ?></td>
                                        <?php foreach($tbHdr as $row): 
                                            $cls = 'tac';
                                            if(in_array($row,$alignRight)){
                                                $cls = 'tar';
                                                $item->$row = number_format($item->$row,2);
                                            }

                                        ?>
                                            <td class="<?php echo $cls; ?> span2">
                                                <?php
                                                    if($row == "transDate"):
                                                    echo "<a href='".base_url()."gstReport/editDebtRecover/".$item->debtRecoverID."' title='Edit Bad Debt'>".$item->$row."</a>"; 
                                                    else:
                                                    echo $item->$row; 
                                                    endif;
                                                ?>                                                      
                                            </td>
                                        <?php 
                                            endforeach; ?>    
                                            <td class="tac">
                                                <input type="checkbox" class="checkbox" id="checkbox" value="<?php echo $item->debtRecoverID; ?>" />
                                                <!-- <a class="btn btn-primary" href="<?php echo base_url()?>bankreconciliation/undo/<?php echo $item->ID ?>" name="Undo Re-conciliation" title="Undo Re-Conciliation"><span class="i-pen"></span></a> -->
                                            </td>
                                        </tr>
                                        <?php 
                                        endforeach;
                                    endif;
                                    ?>
                                    </tbody>         
                                </table>
                            </div><!-- content -->                              

                            <div class="footer">
                            <div class="side fr">
                                <div class="btn-group">
                                    <button type="submit" name="postcrdtn" onclick="removebaddebt()"  value="Posting" class="btn-form btn-danger-form remove"><?php echo $this->lang->line('remove');?>
                                    <button type="submit" name="postcrdtn" onclick="postbaddebt()"  value="Posting" class="btn-form btn-success-form confirm"><?php echo $this->lang->line('postcrdtn');?>
                                </button>                               </div>
                                </div>
                            </div>
                        </div><!-- block -->
                    </div><!-- span12 -->
                </div><!-- row-fluid -->    
            </div><!-- wrap -->
        </div><!-- content -->
    </div>
</div>
</div>
</div>
</div>
</div>