<?php $render('header', ['loggedUser'=>$loggedUser, 'title'=>$titlePage]); ?>
<?=$render('sidebar', [
  'loggedUser'=>$loggedUser, 
  'pageActive'=>$pageActive,
  'permissions'=>$permissions
]);?>

<!-- Content System -->
<div class="content-wrapper">
  <div class="pages">
    <h3>Compras</h3>
    <div class="users">
    <div class="bar-top">
      <a href="<?=$base;?>/addpurchase" class="btn btn-primary mr">Adicionar Compra</a>

      <div class="right">
        <!-- Busca por status -->
        <div class="right one">
          <form method="POST" action="<?=$base;?>/purchases">
            <select class="form-control" name="filter[searchType]" id="option">
              <option>Selecionar</option>
              <option value="clear">Limpar</option>
              <option value="1" <?= (!empty($arrayInfo['filter']['searchType']) && $arrayInfo['filter']['searchType'] == 1)?'selected':''; ?> >Produto</option>                
              <option value="2" <?= (!empty($arrayInfo['filter']['searchType']) && $arrayInfo['filter']['searchType'] == 2)?'selected':''; ?> >Fornecedor</option>
            </select>

            <select class="form-control" name="filter[searchProduct]" id="productList">
              <?php foreach($productList as $product): ?>
                <option <?= ($arrayInfo['filter']['searchProduct'] == $product['id'] )?'selected':''; ?>  value="<?= $product['id']; ?>" ><?= $product['name']; ?></option>
              <?php endforeach; ?>
            </select>

            <select class="form-control" name="filter[searchProvider]" id="providerList">
              <?php foreach($providerList as $provider): ?>
                <option <?= ($arrayInfo['filter']['searchProvider'] == $provider['id'] )?'selected':''; ?> value="<?= $provider['id']; ?>" ><?= $provider['company_name']; ?></option>
              <?php endforeach; ?>
            </select>

            <!-- Busca por digitação -->
            <div class="right two">
              <!--Blue select-->
              <select class="form-control opt-purchases" name="filter[opt]" id="opt">
                <option value="note_number">Número da nota</option>
              </select>
              <input type="text" name="filter[tag]" value="<?= (!empty($arrayInfo['filter']['tag']))?$arrayInfo['filter']['tag']:''; ?>" class="form-control" placeholder="Digite aqui..." id="busca">
            </div>
            <!-- End Busca por digitação -->

            <input type="submit" class="proccess" value="PROCESSAR">
          </form>
      </div>
      <!-- End Busca por status --> 
    </div>

    </div>
      <!-- Message -->

      <?php if(!empty($error)): ?>
        <div class="alert <?=$error['alertType'];?>" role="alert">
          <?php echo $error['msg']; ?>
        </div>
      <?php endif; ?>

      <!-- /Message -->

      <table class="table table-hover" id="table_purchases">
        <tbody>
          <tr>
            <th scope="col">Nº nota</th>      
            <th scope="col">Data</th>
            <th scope="col">Valor da nota</th>
            <th scope="col">Tipo de pagamento</th>
            <th scope="col">Adiantamento R$</th>
            <th scope="col">%</th>
            <th scope="col">Org. Pagamento</th>
            <th scope="col">Produto</th>
            <th scope="col">Fornecedor</th>
            <th scope="col">Ações</th>
          </tr>
              
          <?php foreach($purchasesList as $purchase): ?>
            <tr>
              <td><?= $purchase['note_number']?></td>
              <td><?= $purchase['note_date']?></td>          
              <td><?= $purchase['total_amount']?></td>
              <td><?= $purchase['payment_type']?></td>
              <td><?= $purchase['total_advance']?></td>
              <td><?= $purchase['advance']; ?></td>
              <td><?= $purchase['form_to_pay']; ?></td>
              <td><?= $purchase['product']?></td>              
              <td><?= $purchase['provider']?></td>
              <td style="width:150px!important;">
                <a href="<?=$base;?>/viewpurchase&p=<?=$purchase['id'];?>" class="btn btn-info mbt">Visualizar</a>
                <button data-button="<?=$purchase['id'];?>" class="btn btn-danger mbt">Excluir</button>
              </td>
            </tr>
          <?php endforeach; ?>

        </tbody>
      </table>

      <nav aria-label="...">
        <ul class="pagination pagination-sm">

          <?php if($arrayInfo['p'] > 1): ?>
            <li class="page-item"><a class="page-link" href="<?= $base; ?>/purchases?p=<?= ($arrayInfo['p']-1); ?><?= $arrayInfo['op']; ?>"><</a></li>
          <?php endif; ?>

          <li class="page-item">
            <form method="GET" id="form-pagination">
              <input type="text" name="p" value="<?= $arrayInfo['p']; ?>" class="form-control input-pagination">
            </form>
          </li>

          <?php if($arrayInfo['p'] != $arrayInfo['p_count'] && !empty($arrayInfo['p_count'])): ?>
            <li class="page-item"><a class="page-link" href="<?= $base; ?>/purchases?p=<?= ($arrayInfo['p']+1); ?><?= $arrayInfo['op']; ?>">></a></li>
          <?php endif; ?>

        </ul>
      </nav>

      <div class="modal-confirme-cancel">
        <div class="modal-content">
          <h4>Deseja excluir esse registro?</h4>
          <div class="msg-delete-purchase">
            <strong>Esta ação provocará as seguintes mudanças:</strong>
            <p>Exclusão de contas a pagar.</p>
            <p>Redução de estoque dos produtos da compra.</p>
            <p>Exclusão da compra e seus dependentes.</p>
          </div>
          <hr>
          <input type="hidden" name="clicked">
          <div class="modal-button">
            <a href="#" id="confirm_delete" class="btn btn-success">Sim</a>
            <a href="#" id="cancel_delete" class="btn btn-info">Não</a>
          </div>
        </div>
      </div>

    </div>
    <script src="<?=$base?>/assets/js/purchases.js"></script>
  </div>  		
</div>
<!-- /.content-system -->

<?=$render('footer');?>