<script type="text/javascript">
var oTable;
$(document).ready(function() {
    /* Build the DataTable with third column using our custom sort functions */
    $('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
    oTable = $("#test").dataTable({
        "iDisplayLength": 5,
        "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'] ] );
    
    $("table .checkall1").live("click", "checkall1",function(){           
        $(oTable.fnGetNodes()).find(':checkbox').prop('checked', this.checked);
        calculBadDebt();
    });
    
    
    $("table.editable").on("click",".remove",function(){
        rRow = $(this).parents("tr");
        ID = $('td:first', $(this).parents('tr')).text(); 
        $("#row_delete").dialog("open");
    });
    
    
});

function getDebt(){
    console.log($("#debtDate").val()+"<->"+$("#custList").val());
    jQuery.ajax({
        url: "<?php echo base_url() ?>gstReport/getBadCust",
        async: false,
        type: "POST",
        data: "customerID="+$("#custList").val()+'&date='+$("#debtDate").val(),
        dataType: "json",
        success: function(data) {
            if(data){
                //Items listing
                var serial = 1;
                oTable.fnClearTable();
                jQuery.each(data,function(i,v){
                    var rowIndex = oTable.fnAddData( [
                        v.ID,
                        serial++,
                        v.invoiceNo,
                        v.invDate,
                        v.dueDays,
                        accounting.formatNumber(v.totalAmnt,2),
                        accounting.formatNumber(v.gstAmnt,2),
                        accounting.formatNumber(v.outAmnt,2),
                        accounting.formatNumber(v.badDebtsAmnt,2),
                        v.taxCode,
                        '<td class="tac"><input type="checkbox" class="checkbox" id="checkbox"/></td>'
                    ] );
                    var row = oTable.fnGetNodes(rowIndex);
                    $(row).find('td:nth-child(1)').css('display','none');
                    $(row).find('td').addClass('tac');
                    $(row).find('td:nth-child(5),td:nth-child(6),td:nth-child(7),td:nth-child(8)').removeClass('tac').addClass('tar');
                    //$(row).find('td:nth-child(11)').addClass('tac');
                    //$(row).find('td:nth-child(1),td:nth-child(13)').css('display','none');
                    //$(row).find('td:nth-child(9)').append('<input type="hidden" value="'+v.outputTaxID+'" />');
                });
            }else{
                oTable.fnClearTable();
            }
        }
    });

    //reconsiliation
    $('.checkbox').live('change',function(){
        calculBadDebt();
    });
}

function saveBadDebt(){
    calculBadDebt();
    var invID = "";
    $('.checkbox:checked').each(function(){
        if(invID == ''){
            invID += $(this).parents('tr').find('td:nth-child(1)').text();
        }else{
            invID += ','+$(this).parents('tr').find('td:nth-child(1)').text();
        }
    });

    if(invID == "") {
        alert("You need to select at least one invoice");
        return false;
    }

    jQuery.ajax({
        url: "<?php echo base_url() ?>gstReport/saveBadCust",
        async: false,
        type: "POST",
        data: 'invID='+invID
            +'&custID='+$("#custList").val()
            +'&transDate='+$("#debtDate").val()
            +'&valueBadDebt='+accounting.unformat($("#valBadDebt").val())
            +'&gstBadDebt='+accounting.unformat($("#gstBadDebt").val()),
            //+'&badDebtIncGST='+accounting.unformat($("#valBadDebtIncgst").val()),
        dataType: "json",
        success: function(data) {
            if(data){                
                //location.reload();
            }
        }
    });
}

function calculBadDebt(){
    var valBadDebt = 0;
    var gstBadDebt = 0;
    //var valBadDebtIncgst = 0;
    $('.checkbox:checked').each(function(){
        valBadDebt = valBadDebt+parseFloat(accounting.unformat($(this).parents('tr').find('td:nth-child(8)').text()));
        gstBadDebt = gstBadDebt+parseFloat(accounting.unformat($(this).parents('tr').find('td:nth-child(9)').text()));
    });  
    $("#valBadDebt").val(accounting.formatNumber(valBadDebt,2));  
    $("#gstBadDebt").val(accounting.formatNumber(gstBadDebt,2));            
    //valBadDebtIncgst = valBadDebt + gstBadDebt;
    //$("#valBadDebtIncgst").val(accounting.formatNumber(valBadDebtIncgst,2));

}

function printGSTreport(){
    var customerID = $('#custList').select2('val');
    var debtDate = $('#debtDate').val();
    //debtDate = $.datepicker.formatDate('yy-mm-dd', debtDate);


    var strWindowFeatures = "menubar=no,location=yes,resizable=no,scrollbars=yes,status=yes";
    window.open("<?php echo base_url()?>gstReport/gstOutputReliefPrint/"+debtDate+'/'+customerID, "GST Report",strWindowFeatures);
    console.log('um here');
}




</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 $this->lang->line('dashboard') ?></a> <span class="divider">-</span></li>
                <li><a href="<?php echo base_url() ?>ap/home"><?php echo $this->lang->line('module') ?></a> <span class="divider">-</span></li>
                <li class="active"><?php echo $this->lang->line('outDebtHdr') ?></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">
        <div class="wrap">                    
            <div class="row-fluid">
                <div class="span12">
                    <div class="block">
                        <?php
                            echo form_open('#',array("id"=>"gstReport"));
                        ?>
                        <div class="head">
                            <h2><?php echo $this->lang->line('badDebt') ?></h2>
                        </div><!-- head -->
                        <div class="content np">
                            <div class="controls-row"><!-- Row 5-->
                                <div class="span6 scCol">
                                    <div class="span12 tac">
                                        <div class="span3 tal"><?php echo $this->lang->line('custName'); ?></div>
                                        <?php
                                            $preselperiod = '';
                                            $options = '';
                                            $options['all'] = $this->lang->line('all');
                                            if($customer){
                                                foreach($customer as $row){
                                                    $options[$row->ID] = $row->customerName;
                                                }
                                            }
                                            $js='class="span9" id="custList"';

                                            echo form_dropdown('custID', $options, $preselperiod, $js);
                                        ?>
                                    </div>
                                </div>
                                <div class="span6 scCol">
                                    <div class="span3"><?php echo $this->lang->line('date') ?></div>
                                    <div class="span8">
                                        <div class="input-prepend span12">
                                            <span class="add-on span2"><i class="i-calendar"></i></span>
                                            <?php
                                            $data = array(
                                                'name' => 'debtDate',
                                                'id' => 'debtDate',
                                                'type' => 'text',
                                                'onkeydown' => 'return false',
                                                'class' => 'span10 datepicker2 validate[required] financhk',);
                                            echo form_input($data, date("d-m-Y"));
                                            ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <!-- <div class="controls-row">
                                <div class="span6 scCol">
                                    <div class="span3"><?php echo $this->lang->line('formNo') ?></div>
                                    <div class="span9">
                                        <?php
                                        $data = array(
                                            'value'=>$formNo,
                                            'name'=>'formNo',
                                            'id'=>'formNo',
                                            'style'=>'color: red;',
                                            'class'=>'span6',);
                                        echo form_input($data, set_value($formNo));
                                        ?>
                                        <?php
                                        $data = array(
                                            'value'=>"0000",
                                            'name'=>'formID',
                                            'id'=>'formID',
                                            'style'=>'',
                                            'disabled'=>'disabled',
                                            'placeholder'=>'0000',
                                            'class'=>'span6 validate[required]',);
                                        echo form_input($data);
                                        ?>
                                    </div>
                                </div>
                            </div> -->
                        </div><!-- content -->
                        <div class="footer">
                            <div class="side fr">
                                <div class="btn-group">getGST()
                                    <button id="addRow" onclick="getDebt()" class="btn-form btn-success-form" type="button"><?php echo $this->lang->line('submit'); ?></button>
                                </div>
                            </div>
                        </div><!-- footer -->
                        <?php
                            echo form_close();
                        ?>
                    </div><!-- block -->
                    <div class="block">                            
                        <div class="head">
                            <h2><?php echo $this->lang->line('outDebtHdr') ?></h2>
                            <ul class="buttons">
                                 <li><a href="javascript:" title="GST Report print" onclick="printGSTreport()"><?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 class="" style="display:none"></th>
                                    <th class="tac">#</th>
                                <?php foreach($tbHdr as $row):
                                        if(in_array($row,$alignRight)):
                                    ?>
                                        <th class="tar"><?php echo $this->lang->line($row); ?></th>
                                    <?php
                                        else:
                                    ?>
                                        <th class="tac"><?php echo $this->lang->line($row); ?></th>
                                    <?php
                                        endif;
                                endforeach; ?>
                                    <th class="tac"><input type="checkbox" id="checkall1" class="checkall1"/></th>
                                </tr>
                                </thead>
                                <tbody>
                                <?php $bil = 0;
                                    if(isset($datatbls) && !empty($datatbls)):
                                        foreach($datatbls as $item):
                                        $bil++?>
                                        <tr class="itemList">
                                            <td class="tac" style="display:none"></td>
                                            <td class="tac"><?php echo $bil; ?></td>
                                        <?php 
                                        foreach($tbHdr as $row): 
                                            if(in_array($row,$alignRight)):
                                        ?>
                                            <td class="tar"><?php echo number_format($item->$row,2); ?></td>
                                        <?php 
                                            else:
                                        ?>
                                            <td class="tac"><?php echo $item->$row; ?></td>
                                        <?php 
                                            endif;
                                        endforeach; ?>
                                            <td class="tac"><input type="checkbox" class="checkbox" id="checkbox"/></td>
                                        </tr>
                                        <?php 
                                        endforeach;
                                    endif;?>
                                </tbody>         
                            </table>
                        </div><!-- content -->

                        <!--Hello world-->


                        <div class="span4 scCol fr">
                            <div class="block" id="grid_block_4">
                                <div class="content np">
                                    <div class="controls-row">

                                        <div class="span5"><?php echo $this->lang->line('valueofBadDebt') ?></div>
                                        <div class="span7">
                                            <input id="valBadDebt" class="span12 amount" type="text" placeholder="0" disabled="disabled" >
                                        </div>
                                    </div><!-- row control -->
                                    <div class="controls-row">

                                        <div class="span5"><?php echo $this->lang->line('gstBadDebt') ?></div>
                                        <div class="span7">
                                            <input id="gstBadDebt" class="span12 amount" type="text" placeholder="0" disabled="disabled" >
                                        </div>
                                    </div><!-- row control -->
                                    <!-- <div class="controls-row">
                                        <div class="span5"><?php echo $this->lang->line('valBadDebtIncgst') ?></div>
                                        <div class="span7">
                                            <input id="valBadDebtIncgst" disabled="disabled"  class="span12 amount" type="text" placeholder="0">
                                        </div>
                                    </div> --><!-- row control -->

                                </div>
                                <div class="footer">
                                    <div class="side fr">
                                        <div class="btn-group">
                                            <!-- <button id="calcul" onclick="getTotal()" class="btn btn-warning" type="button">
                                                <i class="i-checkmark-3"></i><?php echo $this->lang->line('calTotal') ?></button> -->
                                            <button id="calcul" onclick="saveBadDebt()" class="btn-icon btn-mini btn-success-form" style="margin-left:5px;" type="button">
                                                <i class="i-checkmark-3"></i><?php echo $this->lang->line('saveBadDebt') ?></button>
                                        </div><!-- btn group -->
                                    </div><!-- side fr -->
                                </div><!-- footer -->
                            </div><!-- grid block 1-->
                        </div><!-- row-fluid scRow-->


                        <!--Hello world-->




                    </div><!-- block -->
                </div><!-- span12 -->
            </div><!-- row-fluid -->    
        </div><!-- wrap -->
    </div><!-- content -->
    </div><!-- wrap -->
</div><!-- content -->
<div class="dialog" id="row_edit" style="display: none;" title=". <?php echo $this->lang->line("edit") ?> . ?">
    <p><?php echo $this->lang->line('message1');?></p>
</div>  

