<?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>Relatório de Pedido(s)</h3>
    <div class="users">

      <div class="filters filter_responsive">

        <form method="POST" action="<?= $base; ?>/reportrequests">
          <input type="hidden" id="isclear" name="filter[clear]">
          <div class="filter-items">
            <div class="dates" style="display: flex; margin-right: 10px;">
              <input type="text" name="filter[d_init]" id="dateInit" class="form-control" style="width: 120px!important;" placeholder="00/00/0000" value="<?= $arrayInfo['filter']['d_init']; ?>">

              <input type="text" name="filter[d_end]" id="dateEnd" class="form-control" style="width: 120px!important; margin-left: 10px;" placeholder="00/00/0000" value="<?= $arrayInfo['filter']['d_end']; ?>">
            </div>

            <div class="b_situation form-control" id="b_situation">
              <a href="#">Status</a>
              <div class="status_r" style=" flex-direction: column;">
              <?php foreach($arrayInfo['status_list'] as $status): ?>
                <label>
                  <?= $status['name']; ?>
                  <input type="checkbox" id="<?= $status['id'] ?>" name="filter[status][]" <?= (!empty($arrayInfo['filter']['status']) && in_array($status['id'], $arrayInfo['filter']['status']))?'checked':''; ?> value="<?= $status['id'] ?>">
                </label>
              <?php endforeach; ?>
              </div>
            </div>

            <!-- Lista de Clientes -->
            <div style="position: relative;">
              <input type="hidden" name="filter[client]" value="<?= $arrayInfo['filter']['client']; ?>">

              <input type="text" name="filter[client_name]" style="width: 300px; margin-right: 10px;" class="form-control" id="search_client" placeholder="Pesquisar por cliente..." value="<?= $arrayInfo['filter']['client_name']; ?>">

              <div class="result-here searchresultclient"></div>
            </div>

            <!-- Lista de Fornecedores -->
            <div style="position: relative;">
              <input type="hidden" name="filter[provider]" value="<?= $arrayInfo['filter']['provider']; ?>">

              <input type="text" name="filter[provider_name]" style="width: 300px; margin-right: 10px;" class="form-control" id="search_provider" placeholder="Pesquisar por fornecedor..." value="<?= $arrayInfo['filter']['provider_name']; ?>">
              
              <div class="result-here searchresultsprov"></div>
            </div>

            <!-- Grupo de produtos -->
            <select class="form-control" name="filter[p_group]">
              <option value="">Grupo</option>
              <?php foreach($arrayInfo['group_list'] as $group): ?>
                <option value="<?= $group['id']; ?>" <?= (!empty($arrayInfo['filter']['p_group'] && $filter['p_group'] == $group['id']))?'selected':'' ?> ><?= $group['name'] ?></option>
              <?php endforeach; ?>
            </select>

            <!-- Lista de produtos -->
            <div class="p_situation form-control" id="p_situation">
              <a href="#">Produtos</a>
              <div class="status_p" style=" flex-direction: column;">
                <?php foreach($arrayInfo['products_list'] as $product): ?>

                  <label>
                    <?= $product['name']; ?>
                    <input type="checkbox" id="<?= $product['id']; ?>" name="filter[products][]" <?= (!empty($arrayInfo['filter']['products']) && in_array($product['id'], $arrayInfo['filter']['products']))?'checked':''; ?> value="<?= $product['id'] ?>">
                  </label>

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

            <!-- Lista de Vendedores -->
            <div style="position: relative; margin-right: 10px;">
              <input type="hidden" name="filter[salesman]" value="<?= $arrayInfo['filter']['salesman']; ?>">

              <input type="text" name="filter[salesman_name]" value="<?= $arrayInfo['filter']['salesman_name']; ?>" style="width: 300px;" class="form-control" placeholder="Pesquisar por vendedor...">
              
              <div class="result-here searchresultsalesman"></div>
            </div>

            <select class="form-control" name="filter[print]">
              <option value="">Formato</option>
              <option value="P" <?= ($arrayInfo['filter']['print'] == 'P')?'selected':'' ?> >Retrato</option>
              <option value="L" <?= ($arrayInfo['filter']['print'] == 'L')?'selected':'' ?> >Paisagem</option>
            </select>

            <input type="text" name="filter[limit]" id="max_result" class="form-control" value="<?= $arrayInfo['filter']['limit']; ?>" style="display: none;">

            <button type="submit" id="submit_form" class="proccess">PROCESSAR</button>
            <button id="reports_requests" class="proccess">LIMPAR</button>
          </div>
        </form>

        <div class="filter-buttons">
          <a href="<?= $base; ?>/reportrequestspdf" target="_blank" class="btn btn-success">PDF</a>
          <a href="<?= $base; ?>/reportrequestscsv" target="_blank" class="btn btn-success">Excel</a>
        </div>

      </div>


      <table class="table table-bordered">
        <tbody>
          <tr>
            <th scope="col">Item</th>
            <th scope="col">Data</th>
            <th scope="col">Nº</th>
            <th scope="col">Status do Pedido</th>
            <th scope="col">Cliente</th>
            <th scope="col">Fornecedor</th>
            <th scope="col">Produto de item</th>
            <th scope="col">Vendedor intermediário</th>
            <th scope="col">Peso</th>
            <th scope="col">Saldo</th>
            <th scope="col">Total do Pedido</th>
            <th scope="col">Resultado %</th>  
            <th scope="col" style="width: 60px!important;">Resultado Estimado</th>
          </tr>

          <?php foreach ($arrayInfo['content']['data'] as $key => $r_item): ?>
            <tr>
              <td><?= ($key + 1); ?></td>
              <td><?= date('d/m/Y', strtotime($r_item['date_created'])); ?></td>
              <td><?= $r_item['id']; ?></td>
              <td><?= $r_item['status_name']; ?></td>
              <td><?= $r_item['client']; ?></td>
              <td><?= $r_item['provider_name']; ?></td>
              <td><?= $r_item['product']['name']; ?></td>
              <td><?= (!empty($r_item['salesman_name']))?$r_item['salesman_name']:'Supporte Agro'; ?></td>
              <td><?= number_format($r_item['amount'], 2); ?></td>
              <td><?= number_format($r_item['balance'], 2); ?></td>
              <td><?= number_format($r_item['total'], 2, ',', '.'); ?></td>
              <td><?= number_format($r_item['estimative_result'], 2); ?></td>
              <td style="width: 60px!important;"><?= number_format($r_item['estimative_real'], 2, ',', '.'); ?></td>
            </tr>
          <?php endforeach; ?>      

        </tbody>
      </table>
      
      <!-- Total Pedidos -->
      <div class="view-total-purchase">
        <label style="font-size: 0.8rem;">Total em R$ de Pedidos:</label>
        <input type="text" class="form-control" readonly value="R$ <?= $arrayInfo['content']['total_requests']; ?>">
      </div>
      <!-- Total Resultado estimado -->
      <div class="view-total-purchase">
        <label style="font-size: 0.8rem;">Resultado estimado:</label>
        <input type="text" class="form-control" readonly value="R$ <?= $arrayInfo['content']['total_estimative'];; ?>">
      </div>

    </div>

    <script src="<?= $base; ?>/assets/js/reports.js"></script>
    <script src="<?= $base ?>/assets/js/requests_reports.js"></script>

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

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