<?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>Pedidos</h3>
    <div class="users">
      <div class="bar-top" style="justify-content: flex-end;">
        <!-- <a href="requests/addRequest" class="btn btn-primary mr">Adicionar Pedido</a> -->
        <!-- Pesquisas -->

        <div class="right">
        <!-- End Busca por status -->
        <div class="right one">
          <form method="POST" action="<?=$base;?>/requests">

            <div class="requests-search-period">
              <input type="text" name="filter[dateInit]" id="dateInit" class="form-control" placeholder="00/00/0000" value="<?= $filter['dateInit'];?>">

              <input type="text" name="filter[dateEnd]" id="dateEnd" class="form-control" placeholder="00/00/0000" value="<?= $filter['dateEnd'];?>">
            </div>

            <select class="form-control" name="filter[searchType]" id="option" style="width: 200px;">
              <option value="">Selecionar</option>
              <option value="clear">Limpar</option>

              <?php foreach($array['requestsStatus'] as $item): ?>
                <option value="<?= $item['id']; ?>" <?= ($filter['searchType'] == $item['id'])?'selected':''; ?> ><?= $item['name']; ?></option>
              <?php endforeach; ?>
            </select>            

            <!-- Busca por digitação -->
            <div class="right two">
              <!--Blue select-->
              <select class="form-control" name="filter[opt]" id="opt">
                <option value="1" <?= ($filter['opt'] == 1)?'selected':''; ?> >Cliente</option>
                <option value="2" <?= ($filter['opt'] == 2)?'selected':''; ?> >Fornecedor</option>
                <option value="3" <?= ($filter['opt'] == 3)?'selected':''; ?> >Número</option>
                <option value="4" <?= ($filter['opt'] == 4)?'selected':''; ?> >Vendedor</option>
              </select>

              <input type="hidden" name="filter[optionList]" value="<?= $filter['optionList']; ?>">
              <input type="text" name="filter[tag]" class="form-control" placeholder="Digite aqui..." value="<?= $filter['tag'];?>">
              <div class="show-result"></div>              
            </div>
            <!-- End Busca por digitação -->
            <button type="submit" class="proccess">PROCESSAR</button>
          </form>
        </div>

        <!-- End Busca por status -->

        </div>

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

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

      <!-- /Message -->

      <table class="table table-hover">
        <tbody>
          <tr>
            <th scope="col">Nrº</th>
            <th scope="col">Emissão</th>
            <th scope="col">Cliente</th>
            <th scope="col">Fornecedor</th>
            <th scope="col">Vendedor</th>
            <th scope="col">Status</th>
            <th scope="col">Qtd</th>
            <th scope="col">Saldo</th>
            <th scope="col">R$</th>
            <th scope="col">Total R$</th>
            <th scope="col" style="width: 160px!important;">Ações</th>
          </tr>
          <?php foreach($array['requestList'] as $key => $item): ?>
            <tr>
              <td><?= $item['id']; ?></td>
              <td><?= $item['date_created']; ?></td>
              <td><?= $item['client']; ?></td>
              <td><?= $item['provider']; ?></td>
              <td><?= (!empty($item['salesman']))?$item['salesman']:'Supporte Agro'; ?></td>
              <td><?= $item['status_name']; ?></td>
              <td><?= $item['amount'];?></td>
              <td><?= $item['balance'];?></td>
              <td><?= $item['value_unit'];?></td>
              <td><?= $item['total'];?></td>
              <td style="width: 160px!important;">
                <?php if($item['status'] == 1): ?>
                  <a href="<?=$base;?>/requestsedit&r=<?= $item['id']; ?>" class="btn btn-info">Editar</a>
                <?php else: ?>
                  <a href="<?= $base; ?>/requestsview&r=<?= $item['id']; ?>" class="btn btn-info">Visualizar</a>
                  <a href="<?= $base; ?>/printout&r=<?= $item['id']; ?>" target="_blank" class="btn btn-success">Imprimir</a>
                <?php endif; ?>
              </td>
            </tr>
          <?php endforeach; ?>

        </tbody>
      </table>

      <nav aria-label="...">
        <ul class="pagination pagination-sm">
          
          <?php if($array['p'] > 1): ?>
            <li class="page-item"><a class="page-link" href="<?= $base; ?>/requests?p=<?= ($array['p']-1); ?><?= $array['op']; ?>"><</a></li>
          <?php endif; ?>

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

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

      <div class="modal-confirme-cancel">
        <div class="modal-content">
          <h4>Deseja excluir o pedido e todas suas dependências?</h4>
          <hr>
          <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/requests.js"></script>

  </div>  		
</div>
<!-- /.content-system -->

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