<section class="content-header">
    <h1><i class='fa-fw fa fa-shopping-basket'></i> <?php echo $_translator->_getTranslation('Compras');?></small></h1>
    <ol class="breadcrumb">
        <li><a href="<?php echo ROOT_HOST?>/Controller/Inicio.php"><i class="fa fa-dashboard"></i><?php echo $_translator->_getTranslation('Inicio')?></a></li>
      <li class="active"><?php echo $_translator->_getTranslation('Compras');?></li>
    </ol>
</section>
<section class="content">
<div class="box">
  <div class="box-header with-border">
    <h3 class="box-title"></h3>
    <div class="box-tools pull-right">
      <span class="btn btn-default pull-right _advancedSearch"><i class='fa fa-search-plus'></i> <?php echo $_translator->_getTranslation('Busqueda avanzada')?></span>  
      <span class="btn btn-default pull-right _addPurchase"><i class='fa fa-plus'></i> <?php echo $_translator->_getTranslation('Agregar compra')?></span>  
    </div><!-- /.box-tools -->
  </div><!-- /.box-header -->
  <div class="box-body">
    <div class='flashmessenger'><?php $flashmessenger->showMessage(null);?></div>
     <div class="clear"></div>
     <div class="table-responsive">
        <table id="tblVendors" class="table table-bordered table-condensed table-striped table-hover font-size-12 datatable_whit_filter_column _hideSearch" style="width:100%">
            <thead>
                <th class="text-center"><?php echo $_translator->_getTranslation('Proveedor');?></th>  
                <th class="text-center"><?php echo $_translator->_getTranslation('Fecha de compra');?></th>    
                <th class="text-center"><?php echo $_translator->_getTranslation('Compra #');?></th>      
                <th class="text-right"><?php echo $_translator->_getTranslation('Total');?></th>    
                <th class="text-right"><?php echo $_translator->_getTranslation('Balance');?></th> 
                <th class="text-center"><?php echo $_translator->_getTranslation('Status');?></th>    
                <th class="col-md-1 col-xs-1 text-center"><?php echo $_translator->_getTranslation('Accion');?></th>    
            </thead>
            <tfoot>
                <th class="filter text-center"><?php echo $_translator->_getTranslation('Proveedor');?></th>  
                <th class="filter text-center"><?php echo $_translator->_getTranslation('Fecha de compra');?></th>    
                <th class="filter text-center"><?php echo $_translator->_getTranslation('Compra #');?></th>      
                <th class="filter text-right"><?php echo $_translator->_getTranslation('Total');?></th>
                <th class="filter text-right"><?php echo $_translator->_getTranslation('Balance');?></th>
                <th class="filter text-center"><?php echo $_translator->_getTranslation('Status');?></th>    
                <th></th>    
            </tfoot>
        <tbody class="purchases">
        <?php
        if($_listPurchases){
        foreach($_listPurchases as $purchase){?>
            <tr>
            <td class="text-center" data-id="<?php echo $purchase['id']?>"> <?php echo $purchase['vendor_name']?></td>
            <td class="text-center" data-id="<?php echo $purchase['id']?>"> <?php echo $purchase['purchase_date_formated']?></td>
            <td class="text-center" data-id="<?php echo $purchase['id']?>"> <?php echo $purchase['purchase_number']?></td>
            <td class="text-right" data-id="<?php echo $purchase['id']?>"> <?php echo number_format($purchase['total'],2)?></td>
            <td class="text-right" data-id="<?php echo $purchase['id']?>"> <?php echo number_format($purchase['balance'],2)?></td>
            <td class="text-center" data-id="<?php echo $purchase['id']?>"> <?php echo $purchase['status_name']?></td>
            <td class="text-center">
                <span class="btn btn-default _edit" data-id="<?php echo $purchase['id']?>"><i class="fa fa-edit"></i></span>
                <span class="btn btn-danger _delete" data-id="<?php echo $purchase['id']?>"><i class="fa fa-trash"></i></span>
            </td>
            </tr>
        <?php }
        }?>
        </tbody>
        </table>
     </div>
  </div><!-- /.box-body -->
</div><!-- /.box -->
</section>
<?php include ROOT."/View/Modal/addPurchase.php";?>
<?php include ROOT."/View/Modal/advancedSearch.php";?>
<?php if(isset($_noValid) || isset($_GET['addnew'])){?> <script>$('#modalAddPurchase').modal('show');</script> <?php }?>
<script>
    $('#purchase_date').datetimepicker({format: 'DD/MM/YYYY'});   
    $('#vendor_id').select2();        
    $('._addPurchase').on('click',function(){
        clearForm('purchase');                
        $('form[name=purchase] #action').val('insert');
        $('form[name=purchase] #id').val('');
        $('.flashmessenger').html('');
         _getTranslation('Agregar compra',function(msj){ $('#title_modal_purchase').html(msj);});
        $('#modalAddPurchase').modal('show');
    });
    
    $('._closeModalPurchase').on('click',function(){
        clearForm('purchase');
        $('.flashmessenger').html('');
        $('#modalAddPurchase').modal('hide');
    });
    
    $('tbody.purchases td ._edit').on('click',function(e){
        if (!$(e.target).closest('._delete').length) {
            clearForm('purchase');
            $('.flashmessenger').html('');
           _getTranslation('Editar compra',function(msj){ $('#title_modal_purchase').html(msj);});
            var id = $(this).data('id');
            setDataToEditPurchase(id);
        }       
    }); 
    
    $('tbody.purchases td ._delete').on('click',function(){
        var id = $(this).data('id');
        deletePurchase(id);
    });
    
    $('._advancedSearch').on('click',function(){
        $('form[name=advanced_search] #fechaInicio,form[name=advanced_search] #fechaFin').datetimepicker({format: 'DD/MM/YYYY'});   
        $('form[name=advanced_search] select').select2();
        $('#modalAdvancedSearch').modal('show'); 
    });
</script>