<script type="text/javascript">
    var type = '<?php echo $type; ?>';
    $(document).ready(function(){
        <!-- page script -->
        $('.datatables').DataTable();
        $('.datetimepicker').datetimepicker({
            viewMode: 'months',
            format: 'DD-MM-YYYY'
        });

        $('#expenseList').find(':checkbox').prop('checked', false)
    });

    function delete_selected_row(){
        try {
            var oTable = $('#expenseList').dataTable();
            var aTrs = oTable.fnGetNodes();
            for ( var i=0 ; i<aTrs.length ; i++ ){

                if($('input:checked', aTrs[i]).val()){
                    oTable.fnDeleteRow(aTrs[i]);
                    if(type == 'cogs')  rowNo = 6;
                    else                rowNo = 7;
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url(); ?>expenses/removePayment",
                        data: {
                                payId:$('td:nth-child('+rowNo+') input',aTrs[i]).attr('data-id'),
                                journalNo:$('td:nth-child('+rowNo+') input',aTrs[i]).attr('data-journalid')
                              },
                        dataType:'json',
                        success: function(data){
                            if(data.status = 'success'){
                                $('#btn_delete_expense').attr('disabled','disabled');
                                notify("Transaction deleted successfully","<?php echo $this->lang->line($type); ?>",'success');
                            }
                        }
                    });
                }
            }
            if(type == 'cogs')
                datatable_reoder_rowno("expense");
            total =  datatable_total("expense", 2);
            $("#new_sale_grandtotal").val(accounting.formatNumber(total,2));
        } catch (err) {

        }
    }

    function enabled_or_disabled_delete_btn(me){

        if($(me).is(":checked")){
            $("#btn_delete_expense").removeAttr("disabled");
        }else{
            var oTable = $("#expenseList").dataTable();
            $(oTable.fnGetNodes()).find('input[type=checkbox] :checked');
            var notfind = true;
            $(oTable.fnGetNodes()).find(':checkbox:checked').each(function(){
                $("#btn_delete_expense").removeAttr("disabled");
                notfind = false;
               return true;
            });
            if(notfind){
                 $("#btn_delete_expense").attr("disabled","disabled");
            }
        }
    }


    function select_or_unselect_all(me){
         var oTable = $("#expenseList").dataTable();
        if($(me).is(":checked")){
            $(oTable.fnGetNodes()).find(':checkbox').prop('checked', true);
            $("#btn_delete_expense").removeAttr("disabled");
        }else{
            $(oTable.fnGetNodes()).find(':checkbox').prop('checked', false);
            $("#btn_delete_expense").attr("disabled","disabled");
        }
    }

    function conf_delete_sales(me){
        confirmBox('delete_selected_row()', '<?php echo $this->lang->line('msgconfirm'); ?>');
    }
</script>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1 class="module-highlight">
            <?php if ($type == "loan"): ?>
              <span class="number">4.2</span>
          <?php elseif ($type == "asset"): ?>
              <span class="number">3.3</span>
          <?php elseif ($type == "operational"): ?>
              <span class="number">3.2</span>
            <?php else: ?>
              <span class="number">3.1</span>
            <?php endif; ?>
        <?php echo $this->lang->line($title); ?>
        <?php if ($type == "loan"): ?>
          <small><?php echo $this->lang->line('moduleGeneral'); ?></small>
        <?php else: ?>
          <small><?php echo $this->lang->line('moduleExpense'); ?></small>
        <?php endif; ?>
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i><?php echo $this->lang->line('home'); ?></a></li>
            <li><a href="#"><?php echo $this->lang->line('moduleExpense'); ?></a></li>
            <li class="active"><?php echo $this->lang->line($title); ?></li>
        </ol>
    </section>
    <!-- Main content -->
    <section class="content">

        <div class="row">
          <?php //var_dump($dataKotak);
		  $this->load->view('layouts/kotak',$dataKotak);?>
        </div>

        <div class="row">
            <div class="col-xs-12">
                <div class="box box-primary">
                    <div class="box-header with-border">
                      <label class="box-title"><?php echo $this->lang->line($title.'SubHdr'); ?></label>
                        <div class="box-title pull-right">
                          <?php if($type == "operational"): ?>
                            <a href="<?php echo base_url().'expenses/addNew/'.$type; ?>" class="btn btn-sm bg-olive"><span class="fa fa-plus fa-fw"></span> <?php echo $this->lang->line('addNewOE'); ?></a>
                            <button id="btn_delete_expense" class="btn bg-red btn-sm" disabled="disabled" onclick="conf_delete_sales(this)"><span class="fa fa-trash fa-fw"></span> <?php echo $this->lang->line('deleteOE'); ?></button>
                            <a href="<?php echo base_url().'expenses/printAll/'.$type.'/'.$dateFrom.'/'.$dateTo; ?>" class="btn bg-orange btn-sm <?php echo (count($datatbls))?'':'disabled'; ?>"><span class="fa fa-print fa-fw"></span> <?php echo $this->lang->line('printOE'); ?></a>
                          <?php elseif($type == "cogs"): ?>
                            <a href="<?php echo base_url().'expenses/addNew/'.$type; ?>" class="btn btn-sm bg-olive"><span class="fa fa-plus fa-fw"></span> <?php echo $this->lang->line('addNewCogs'); ?></a>
                            <button id="btn_delete_expense" class="btn bg-red btn-sm" disabled="disabled" onclick="conf_delete_sales(this)"><span class="fa fa-trash fa-fw"></span> <?php echo $this->lang->line('deleteCogs'); ?></button>
                            <a href="<?php echo base_url().'expenses/printAll/'.$type.'/'.$dateFrom.'/'.$dateTo; ?>" class="btn bg-orange btn-sm <?php echo (count($datatbls))?'':'disabled'; ?>"><span class="fa fa-print fa-fw"></span> <?php echo $this->lang->line('printCogs'); ?></a>
                          <?php elseif($type == "asset"): ?>
                            <a href="<?php echo base_url().'expenses/addNew/'.$type; ?>" class="btn btn-sm bg-olive"><span class="fa fa-plus fa-fw"></span> <?php echo $this->lang->line('addNewAsset'); ?></a>
                            <button id="btn_delete_expense" class="btn bg-red btn-sm" disabled="disabled" onclick="conf_delete_sales(this)"><span class="fa fa-trash fa-fw"></span> <?php echo $this->lang->line('deleteAsset'); ?></button>
                            <a href="<?php echo base_url().'expenses/printAll/'.$type.'/'.$dateFrom.'/'.$dateTo; ?>" class="btn bg-orange btn-sm <?php echo (count($datatbls))?'':'disabled'; ?>"><span class="fa fa-print fa-fw"></span> <?php echo $this->lang->line('printAsset'); ?></a>
                          <?php else: ?>
                            <a href="<?php echo base_url().'expenses/addNew/'.$type; ?>" class="btn btn-sm bg-olive"><span class="fa fa-plus fa-fw"></span> <?php echo $this->lang->line('addNewLoan'); ?></a>
                            <button id="btn_delete_expense" class="btn bg-red btn-sm" disabled="disabled" onclick="conf_delete_sales(this)"><span class="fa fa-trash fa-fw"></span> <?php echo $this->lang->line('deleteLoan'); ?></button>
                            <a href="<?php echo base_url().'expenses/printAll/'.$type.'/'.$dateFrom.'/'.$dateTo; ?>" class="btn bg-orange btn-sm <?php echo (count($datatbls))?'':'disabled'; ?>"><span class="fa fa-print fa-fw"></span> <?php echo $this->lang->line('printLoan'); ?></a>
                          <?php endif; ?>
                        </div>
                    </div>
                    <!-- /.box-header -->
                    <div class="box-header with-border">
                        <form method="post" class="form-horizontal" action="<?php echo base_url().'expenses/type/'.$type;  ?>">
                          <fieldset class="fsStyle">
                              <legend class="legendStyle">
                                  <a href="javascript:void(0);" data-toggle="collapse" data-target="#filter"><?php echo $this->lang->line('selectDate'.$type); ?></a>
                              </legend>
                              <div id="filter" class="row collapse in">
                                  <div class="form-group col-sm-5">
                                      <label for="dateFrom" class="col-sm-4 col-sm-offset-2 control-label"><?php echo $this->lang->line('dateFrom'); ?>: </label>
                                      <div class="col-sm-6">
                                          <input type="text" class="form-control datetimepicker" id="dateFrom" name="dateFrom" value="<?php echo date("d-m-Y", strtotime($dateFrom)); ?>" placeholder="<?php echo $this->lang->line('dateFrom'); ?>" />
                                      </div>
                                  </div>
                                  <div class="form-group col-sm-5">
                                      <label for="dateTo" class="col-sm-4 col-sm-offset-2 control-label"><?php echo $this->lang->line('dateTo'); ?>: </label>
                                      <div class="col-sm-6">
                                          <input type="text" id="dateTo" name="dateTo" class="form-control datetimepicker" value="<?php echo date("d-m-Y", strtotime($dateTo)); ?>" placeholder="<?php echo $this->lang->line('dateTo'); ?>">
                                      </div>
                                  </div>
                                  <div class="form-group col-sm-2">
                                      <button type="submit" class="btn btn-primary search hidden-xs"><?php echo $this->lang->line('search'); ?></button>
                                      <button type="submit" class="btn btn-primary search hidden-lg hidden-sm hidden-md pull-right" style="margin-right:20px;"><?php echo $this->lang->line('search'); ?></button>
                                  </div>
                              </div>
                          </fieldset>
                        </form>
                    </div>
                    <!-- /.box-header -->
                    <div class="box-body table-scroll-xs">
                        <table id="expenseList" class="table table-bordered table-striped datatables">
                            <thead>
                                <tr>
                                    <th class='text-center'><?php echo $this->lang->line('no'); ?></th>
                                    <th class='text-center'><?php echo $this->lang->line('date'); ?></th>
                                    <th><?php echo $this->lang->line('formNo'); ?></th>
                                    <?php if($type != 'cogs'): ?>
                                    <th><?php echo $this->lang->line('remarks'); ?></th>
                                    <?php endif;?>
                                    <th class="text-right"><?php echo $this->lang->line('amountRm'); ?></th>
                                    <th class="text-center"><?php echo $this->lang->line('print'); ?></th>
                                    <th class="text-center"><input type="checkbox" onchange="select_or_unselect_all(this)" /></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                    if(!empty($datatbls)):
                                      $i = 1;
                                      foreach($datatbls as $row):
                                    ?>
                                <tr>
                                    <td class='text-center'><?php echo $i++; ?></td>
                                    <td class='text-center'><?php echo $row->payDate; ?></td>
                                    <td><a href="<?php echo base_url()."expenses/addNew/".$type."/".$row->ID; ?>"><?php echo $row->formNo; ?></a></td>
                                    <?php if($type != 'cogs'): ?>
                                    <td><?php echo $row->memo; ?></td>
                                    <?php endif;?>
                                    <td class="text-right"><?php echo number_format($row->amountPaid,2) ?></td>
                                    <td class="text-center"><a href="<?php echo base_url().'expenses/printDetail/'.$row->ID."/".$type; ?>" class="btn btn-primary btn-sm"><span class="fa fa-print"></span></a></td>
                                    <td class="text-center"><input type="checkbox" onchange="enabled_or_disabled_delete_btn(this)" name="expense_id[]" data-id = "<?php echo $row->ID; ?>" data-journalId = "<?php echo $row->jId; ?>" /></td>
                                </tr>
                                <?php
                                    endforeach;
                                    endif;
                                    ?>
                            </tbody>
                        </table>
                    </div>
                    <!-- /.box-body -->
                </div>
                <!-- /.box -->
            </div>
            <!-- /.col -->
        </div>
        <!-- /.row -->
    </section>
    <!-- /.content -->
</div>
<!-- /.content-wrapper -->
