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

<!-- Content System -->
<div class="content-wrapper">
  <div class="pages btp">
    <h3>Contas a Pagar</h3>
    <div class="users">
      <div class="bar-top">
        <a href="<?=$base;?>/billstopayadd" class="btn btn-primary mr">Adicionar Conta</a>
        <!-- Pesquisas -->

        <div class="right">
          <!-- End Busca por status -->
          <div class="right one">
            <form method="POST" id="form_filter" action="<?=$base;?>/billstopay">
              <input type="hidden" id="isclear" name="filter[clear]">
              <div class="requests-search-period" id="filter_date">
                <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[option_filter]" id="option_filter" style="width: 240px;margin-right: 10px;">
                <option value="" <?= (!empty($filter['option_filter']) && $filter['option_filter'] == '')?'selected':''; ?> >Selecionar</option>
                <option value="1" <?= (!empty($filter['option_filter']) && $filter['option_filter'] == 1)?'selected':''; ?> >Tipo de Conta</option>
                <option value="2" <?= (!empty($filter['option_filter']) && $filter['option_filter'] == 2)?'selected':''; ?> >Forma de Pagamento</option>
                <option value="3" <?= (!empty($filter['option_filter']) && $filter['option_filter'] == 3)?'selected':''; ?> >Situação da Conta</option>
              </select>

              <div class="b_type form-control" id="b_type" style="display:none;">
                <a href="#">Selecionar</a>
                <input type="hidden" id="type_b" name="filter[type_b]" value="<?= $filter['type_b']; ?>">
                <div class="bills_type" style="display: none; flex-direction: column;">
                  <?php foreach($array['billstopay_type'] as $item): ?>

                    <label>
                      <?= $item['name']; ?>
                      <input type="checkbox" id="<?= $item['id'] ?>" name="filter[b_type][]" <?= (in_array($item['id'], $filter['b_type']))?'checked':''; ?> value="<?= $item['id'] ?>">
                    </label>

                  <?php endforeach; ?>
                </div>
              </div>

              <div class="p_form form-control" id="p_form" style="display:none;">
                <a href="#">Selecionar</a>
                <input type="hidden" id="form_p" name="filter[form_p]" value="<?= $filter['form_p']; ?>">
                <div class="form_p" style="display: none; flex-direction: column;">
                  <?php foreach($array['payment_form'] as $item): ?>

                    <label>
                      <?= $item['name']; ?>
                      <input type="checkbox" id="<?= $item['id'] ?>" name="filter[p_form][]" <?= (in_array($item['id'], $filter['p_form']))?'checked':''; ?> value="<?= $item['id'] ?>">
                    </label>

                  <?php endforeach; ?>
                </div>
              </div>

              <div class="b_situation form-control" id="b_situation" style="display:none;">
                <a href="#">Selecionar</a>
                <input type="hidden" id="situation_b" name="filter[situation_b]" value="<?= $filter['situation_b']; ?>">
                <div class="situation_b" style="display: none; flex-direction: column;">
                  <?php foreach($array['billstopay_situation'] as $item): ?>

                    <label>
                      <?= $item['name']; ?>
                      <input type="checkbox" id="<?= $item['id'] ?>" name="filter[b_situation][]" <?= (in_array($item['id'], $filter['b_situation']))?'checked':''; ?> value="<?= $item['id'] ?>">
                    </label>

                  <?php endforeach; ?>
                </div>
              </div>

              <select class="form-control" name="filter[status]" id="option_status" style="width: 140px;margin-right: 10px;">
                <option value="" <?= (!empty($filter['status']) && $filter['status'] == '')?'selected':''; ?> >Status</option>
                <option value="1" <?= (!empty($filter['status']) && $filter['status'] == 1)?'selected':''; ?> >Ativa</option>
                <option value="2" <?= (!empty($filter['status']) && $filter['status'] == 2)?'selected':''; ?> >Inativa</option>
              </select>

              <select class="form-control" name="filter[searchBy]" id="searchBy" style="width: 145px;margin-right: 10px;">
                <option value="">Selecionar</option>
                <option value="provider_description" <?= (!empty($filter['searchBy']) && $filter['searchBy'] == 'provider_description')?'selected':''; ?>>Recebedor</option>

                <!-- Inserido em Alterações 13/09/2021 -->
                <option value="document_number" <?= (!empty($filter['searchBy']) && $filter['searchBy'] == 'document_number')?'selected':''; ?>>Nr Documento</option>

                <option value="id_request" <?= (!empty($filter['searchBy']) && $filter['searchBy'] == 'id_request')?'selected':''; ?>>Nr Pedido</option>
                <!-- End Inserido em Alterações 13/09/2021 -->
              </select>
              
              <input type="text" name="filter[searchText]" id="searchText" class="form-control" autocomplete="off" style="width: 200px!important;" placeholder="nome do fornecedor" value="<?= (!empty($filter['searchText']))? $filter['searchText'] :''; ?>">

              <input style="margin-left:10px; width: 50px;" type="text" name="filter[limit]" id="max_result" class="form-control" value="<?= $filter['limit']; ?>">
              
              <button type="submit" id="submit_form" class="proccess">PROCESSAR</button>
              <button id="clear_all" class="proccess">LIMPAR</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 -->


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

            <?php if($array['p'] > 1): ?>
              <li class="page-item"><a class="page-link" href="<?=$base;?>/billstopay?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; ?>/billstopay?p=<?= ($array['p']+1); ?><?= $array['op']; ?>">></a></li>
            <?php endif; ?>

          </ul>
        </nav>

        <div class="btn-pay-accounts">
          <button class="btn btn-success">Pagar Selecionados</button>
        </div>

      </div>      

      <div class="hide">
        <span class="hide">
          <strong class="hide">
            <input type="hidden" name="accounts_selected">
          </strong>
        </span>
      </div>

      <table class="table table-hover">
        <tbody>
          <tr>
            <th scope="col"><input type="checkbox" id="select_all"></th>
            <th scope="col">Dt Venc</th>
            <th scope="col">Descrição</th>
            <th scope="col">Nr Documento</th>
            <th scope="col">Nr Pedido</th>
            <th scope="col">Tipo de Conta</th>
            <th scope="col">Situação</th>
            <th scope="col">Valor Princ</th>
            <th scope="col">Valor a Pagar</th>
            <th scope="col">Valor Residual</th>
            <th scope="col">Data PGTO</th>
            <th scope="col" style="width: 140px!important;">Ações</th>
          </tr>
          <?php foreach($array['billsToPayList'] as $key => $item): ?>
            <tr>
              <?php if($item['id_situation'] == 2): ?>
                <td class="select_checkbox"><input type="checkbox" name="btp-<?= $item['id']; ?>" data-val="<?= $item['id']; ?>"></td>
              <?php else: ?>
                <td></td>
              <?php endif; ?>
              
              <td><?= $item['expirate_date']; ?></td>         
              <td><?= $item['provider_description']; ?></td>
              <td><?= $item['document_number']; ?></td>
              <td><?= $item['id_request']; ?></td>
              <td><?= $item['billstopay_description']; ?></td>
              <td><?= $item['situation']['name']; ?></td>
              <td><?= $item['main_value']; ?></td>
              <td><?= $item['value_to_pay']; ?></td>
              <td><?= $item['residual_value']; ?></td>
              <td><?= $item['payment_date']; ?></td>
              <td style="width: 140px!important;">
                <a href="<?= $base; ?>/billstopayedit&btp=<?= $item['id']; ?>" class="btn btn-info">Editar</a>
                <?php if($item['status'] == 1): ?>
                  <a href="#" id="inativate" data-id="<?= $item['id']; ?>" data-status="<?= $item['status']; ?>" class="btn btn-danger">Inativar</a>
                <?php else: ?>
                  <a href="#" id="activate" data-id="<?= $item['id']; ?>" data-status="<?= $item['status']; ?>" class="btn btn-success">Ativar</a>
                <?php endif; ?>
                
              </td>
            </tr>
          <?php endforeach; ?>

        </tbody>
      </table>

      <div class="flex-2 align-top">

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

            <?php if($array['p'] > 1): ?>
              <li class="page-item"><a class="page-link" href="<?=$base;?>/billstopay?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; ?>/billstopay?p=<?= ($array['p']+1); ?><?= $array['op']; ?>">></a></li>
            <?php endif; ?>

          </ul>
        </nav>

        <div>
          <!-- Total a Pagar -->
          <div class="view-total-purchase">
            <label style="font-size: 0.8rem;">Total a Pagar em R$:</label>
            <input type="text" class="form-control" readonly value="<?= $array['valueTotalThisPage']; ?>">
          </div>

          <!-- Total Pago -->
          <div class="view-total-purchase">
            <label style="font-size: 0.8rem;">Total Pago em R$:</label>
            <input type="text" class="form-control" readonly value="<?= $array['paiOutTotalThisPage']; ?>">
          </div>

          <!-- Total Contas selecionadas -->
          <div class="view-total-purchase">
            <label style="font-size: 0.8rem;">Total Selecionado em R$:</label>
            <input type="text" class="form-control" readonly name="total_selected" value="0,00">
          </div>
        </div>
        
      </div>

      <!-- <div class="modal-stock">
        <div class="modal-content">
          <input type="hidden" name="clicked_item">
          <hr>
          <h4 class="qtd_days"></h4>
          <hr class="qtd_days">
          <div class="modal-button">
            <a href="#" id="confirm_cancel" class="btn btn-danger">Sim</a>
            <a href="#" id="close_cancel" class="btn btn-info">Não</a>
          </div>
        </div>
      </div> -->

      <!-- Modal pagar selecionados -->
      <div class="modal-set-accounts-pay">
        <div class="modal-content">
          <h3>Pagar contas Selecionadas</h3>

          <label>Forma de Pagamento</label>
          <div class="form-row" style="margin-bottom: 10px;">
            <select class="form-control" name="payment_form">
              <option value="">Selecionar</option>
              <?php foreach($array['payment_form'] as $item): ?>
                <option value="<?= $item['id']; ?>" ><?= $item['name']; ?></option>
              <?php endforeach; ?>
            </select>
          </div>

          <div class="form-row" style="margin-bottom: 20px;">
            <label>N° da nota</label>
            <input type="text" name="note_number" id="note_number" class="form-control">
          </div>

          <div class="form-row" style="margin-bottom: 20px;">
            <label>Data de Pagamento</label>
            <input type="text" name="payment_date" id="payment_date" class="form-control" placeholder="00/00/0000">
          </div>

          <div class="modal-button">
            <a href="#" id="confirm_payment" class="btn btn-success">Pagar</a>
            <a href="#" id="close_account_payment" class="btn btn-info">Cancelar</a>
          </div>
        </div>
      </div>

    </div>

    <div class="message-add" id="show-msg-add"></div>

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

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