<?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><?= ($array['deliveryInfo']['type_delivery'] == 'separate_deliveries')?'Entregas':'Entrega Única'; ?></h3>

    <div style="box-sizing:border-box; padding-left:50px;">
      <h4>Nr. do Pedido: <?= $array['requestInfo']['id']; ?></h4>
      <h4>Cliente: <?= $array['requestInfo']['client_name']; ?></h4>
      
      <h4>Fornecedor (es): <?= $array['provider_title']; ?> </h4>

      <h4>SALDO: <?= $array['unit_value']; ?>(s) </h4>
    </div>
    <div class="users">
      
      <!-- Message -->

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

      <!-- /Message -->

      <table class="table table-bordered" id="deliveryItems">
        <tbody>
          <tr>
            <th scope="col">Nr</th>
            <th scope="col">Data de Entrega</th>
            <th scope="col">Data de Edição</th>
            <th scope="col">Situação</th>
            <th scope="col">Nr Nota</th>
            <th scope="col">Qtde</th>
            <th scope="col">Valor R$</th>
            <th scope="col">Fazenda</th>
            <th scope="col" style="width: 160px!important;">Ações</th>
          </tr>
          <?php foreach($array['deliveryItems'] as $key => $item): ?>
          <tr>
            <td><?= $key + 1; ?></td>
            <td><?= $item['date_deliverie']; ?></td>
            <td><?= $item['date_edition']; ?></td>
            <td><?= $item['situation_name']; ?></td>
            <td><?= $item['shipping_invoice']; ?></td>
            <td><?= $item['qtde']; ?></td>
            <td><?= $item['value']; ?></td>
            <td><?= $item['farm_name']; ?></td>
            <td>
              <?php if($item['status'] == 6 || $item['status'] == 7): ?>
                <button data-ref="<?= $item['id'] ?>" data-group="<?= $item['product_group']; ?>" class="btn btn-info view-edit">visualizar</button>
              <?php elseif($item['status'] == 4 || $item['status'] == 5): ?>
                <button data-ref="<?= $item['id'] ?>" data-group="<?= $item['product_group']; ?>" class="btn btn-info view-edit">editar</button>
              <?php else: ?>
                <button data-ref="<?= $item['id'] ?>" data-group="<?= $item['product_group']; ?>" class="btn btn-info view-edit">editar</button>
                <button data-ref="<?= $item['id'] ?>" class="btn btn-primary cancel">cancelar</button>
              <?php endif; ?>
            </td>
          </tr>
          <?php endforeach; ?>
        </tbody>
      </table>

      <div class="modal-stock">
        <div class="modal-content">
          <input type="hidden" name="cancel_clicked_item">
          <hr>
          <h4 class="qtd_days">Não será possível desfazer este cancelamento, deseja prosseguir?</h4>
          <hr class="qtd_days">
          <textarea class="form-control" placeholder="Motivo do cancelamento..." id="reason_canceled"></textarea>
          <p class="alert-cancel" style="color: #FF0000;"></p>
          <hr class="qtd_days">
          <div class="modal-button">
            <a href="#" id="close_cancel_deliverie" class="btn btn-info">Voltar</a>
            <a href="#" id="confirm_cancel_deliverie" class="btn btn-danger">Confirmar Cancelamento</a>
          </div>
        </div>
      </div>

      <!-- Modal adicionar nova carga -->
      <div class="modal-new-charge" style="display:none;">
        <div class="modal-content">
          <input type="hidden" name="nc_id_delivery" value="<?= $array['deliveryInfo']['id']; ?>">
          <input type="hidden" name="nc_id_request" value="<?= $array['requestInfo']['id']; ?>">

          <input type="hidden" name="nc_id_product" value="<?= $array['deliveryItems'][0]['id_product']; ?>">
          <input type="hidden" name="nc_id_provider" value="<?= $array['deliveryItems'][0]['id_provider']; ?>">
          <input type="hidden" name="nc_id_client" value="<?= $array['deliveryItems'][0]['id_client']; ?>">
          <input type="hidden" name="nc_product_group" value="<?= $array['deliveryItems'][0]['product_group']; ?>">

          <h4 class="qtd_days">Criação de Carga</h4>
          <hr class="qtd_days">
          <input type="text" name="charge_quantity" id="charge_quantity" class="form-control" placeholder="Digite o Peso da Carga">
          <hr class="qtd_days">
          <input type="text" name="date_delivery" id="date_delivery" class="form-control" placeholder="Digite a Data da Entrega">
          <hr class="qtd_days">
          <div class="modal-button">
            <a href="#" id="close_cancel" class="btn btn-info">Voltar</a>
            <a href="#" id="confirm_charge" class="btn btn-success">Adicionar Carga</a>
          </div>
        </div>
      </div>
      <!-- Fim Modal adicionar nova carga -->

      <!-- Modal zerar Saldo -->
      <div class="modal-balance-zero" style="display: none;">
        <div class="modal-content">
          <input type="hidden" name="zb_id_delivery" value="<?= $array['deliveryInfo']['id']; ?>">

          <input type="hidden" name="clicked_item">
          <hr>
          <h4 class="qtd_days">Não será possível desfazer esta ação!</h4>
          <hr class="qtd_days">
          <h4 class="qtd_days">Deseja zerar o saldo dessa entrega?</h4>
          <hr class="qtd_days">
          <div class="modal-button">
            <a href="#" id="close_cancel" class="btn btn-info">Voltar</a>
            <button id="confirm_zero_balance" class="btn btn-success">Zerar</button>
          </div>
        </div>
      </div>
      <!-- Fim Modal zerar Saldo -->


      <!-- Modal de edição -->

      <div class="edit-charge">
        <div class="modal-content">

          <h3>Dados da Entrega</h3>
          <input type="hidden" name="id_item_delivery">
          <input type="hidden" name="product_selected_group">

          <div class="row one">
            <label>
              Data de Entrega Prevista
              <input class="form-control" type="text" name="delivery_date" id="delivery_date" status="true" placeholder="00/00/0000">
            </label>

            <label>
              Data de Edição
              <input class="form-control" type="text" name="edit_date" readonly placeholder="00/00/0000">
            </label>

            <label>
              Saldo
              <input class="form-control" type="text" name="balance" readonly>
            </label>            
          </div>

          <div class="row two">
            <label>
              Quantidade Prevista
              <input class="form-control" type="text" name="amount_preview" readonly>
            </label>

            <label>
              Quantidade da Balança
              <input class="form-control" type="text" name="amount_balance" id="amount_balance" placeholder="0,000">
            </label>

            <label>
              Nr da Nota Fiscal
              <input class="form-control" type="text" name="shipping_invoice" id="shipping_invoice">
            </label> 
          </div>

          <div class="row tree">
            <label>
              Valor
              <input class="form-control" type="text" name="total_balance" readonly placeholder="0,00">
            </label>
          </div>

          <div class="row four">
            <textarea class="form-control" name="note" rows="5" placeholder="Observação sobre a entrega..."></textarea>
          </div>

          <div class="row five">
            <button id="close-delivery-info" class="btn btn-info">fechar</button>
            <button id="save-info-delivery" class="btn btn-primary">salvar</button>
            <button id="delivery-complete" class="btn btn-success">entregar</button>
          </div>
          
        </div>
      </div>

      <div class="button">
        <a href="<?= $base; ?>/requestsview&r=<?= $array['requestInfo']['id']; ?>" class="btn btn-info">Voltar</a>

        <?php if($array['deliveryItems'][0]['product_group'] != '2'): ?>
        <button id="new-charge" class="btn btn-success">Criar Carga</button>
        <?php endif; ?>

        <?php if($array['verify_deliveries'] < 1 && $array['deliveryItems'][0]['product_group'] != '2'): ?>
          <button id="zero_balance" class="btn btn-warning">Zerar Saldo</button>
        <?php endif; ?>
      </div>
      
    </div>

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

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

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