<?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>Dados da Compra Lançada</h3>
    <div class="users">
      <!-- Message -->

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

      <!-- /Message -->
      <form method="POST" action="<?= $base; ?>/products/addPurchaseAction">
        <h4 class="bgb">Dados do Fornecedor</h4>

        <div class="form-row sm-red">
          <div class="form-group col-md-3" id="validate_note_number">
            <label for="inputNoteNumber">Número da Nota (*)</label>
            <input type="text" class="form-control" name="note_number" readonly value="<?= $purchase['note_number']; ?>" required id="inputNoteNumber" placeholder="01234567895">
          </div>

          <div class="form-group col-md-4" id="provider">
            <label for="inputProvider">Fornecedor (*)</label>
            <input type="text" name="provider" class="form-control" readonly value="<?= $purchase['provider_name']; ?>">
          </div>

          <div class="form-group col-md-2">
            <label for="note_date">Data da Nota (*)</label>
            <input type="text" class="form-control" placeholder="00/00/0000" readonly name="date_note" value="<?= date('d/m/Y', strtotime($purchase['note_date'])); ?>">
          </div>

          <div class="form-group col-md-2">
            <label for="form_to_pay">Origem do Pagamento</label>
            <select name="form_to_pay" id="form_to_pay" class="form-control" readonly>
              <option value="0">Selecionar</option>
              <?php foreach($payment_form as $item): ?>
                <option value="<?= $item['id']; ?>" <?= ($purchase['id_payment_form'] == $item['id'])?'selected':'' ?> ><?= $item['name']; ?></option>
              <?php endforeach; ?>
            </select>
          </div>
        </div>

        <h4 class="bgb">Produtos Da Nota</h4>

        <div class="form-row">
          <table class="table table-bordered">
            <tbody>
              <tr>
                <th scope="col">Item</th>
                <th scope="col">Produto</th>
                <th scope="col">Grupo</th>
                <th scope="col">Unidade</th>
                <th scope="col">Apresentação</th>
                <th scope="col">Quantidade</th>
                <th scope="col">Custo</th>            
                <th scope="col">Total do Item</th>            
              </tr>

            <?php foreach($purchaseItems as $key => $item): ?>
              <tr>
                <td><?= $key + 1; ?></td>
                <td><?= $item['product']; ?></td>
                <td><?= $item['group_p']['name']; ?></td>
                <td><?= $item['unity']['name']; ?></td>
                <td><?= $item['presentation']['name']; ?></td>
                <td><?= $item['quantity']; ?></td>
                <td><?= $item['product_price']; ?></td>
                <td><?= $item['total']; ?></td>           
              </tr>
            <?php endforeach; ?>
            </tbody>
          </table>
        </div>

        <div class="form-row"id="view-purchase">
          <div class="view-total-purchase">
            <span>Total da Compra: R$ <?= $purchase['total_amount']; ?></span>
          </div>
        </div>

        <h4 class="bgb">Condições de Pagamento</h4>

        <div class="form-row" style="box-sizing: border-box; padding: 0 10px;">

          <div class="form-group col-md-2">
            <label>Forma:</label> 
            <input type="text" readonly value="<?= $purchase['payment_type']; ?>" class="form-control" style="width: 120px;">
          </div>

          <div class="form-group col-md-2">
            <label>% de Adiantamento:</label>
            <input type="text" readonly value="<?= $purchase['advance']; ?>" class="form-control" style="width: 100px;">
          </div>

          <div class="form-group col-md-2">
            <label>Valor adiantamento:</label>
            <input type="text" readonly value="<?= $purchase['totalAdvance']; ?>" class="form-control" style="width: 100px;">
          </div>

          <div class="form-group col-md-3">
            <label>Qtd. de pagamento desta compra: </label>
            <input type="text" readonly value="<?= $purchase['c_payments']; ?>" class="form-control" style="width: 100px;">
          </div>

          <div class="form-group col-md-2">
            <label>de</label>
            <input type="text" readonly value="<?= number_format($purchasePayments[0]['price'], 2, ',', '.'); ?>" class="form-control" style="width: 100px;">
          </div>

          <div class="form-row">
            <div class="form-group" style="padding: 0 10px;" id="dates-payment">
              <label style="display: block;">Datas do pagamento (clique para editar)</label>
              <?php foreach($purchasePayments as $payment): ?>
                <input type="text" id="<?= $payment['id']; ?>" class="form-control" style="float:left; margin: 0 10px 10px 0; width: 110px; cursor: pointer;" readonly value="<?= date('d/m/Y', strtotime($payment['date_payment'])); ?>">
              <?php endforeach; ?>
            </div>
          </div>

        </div>

        <div class="modal-button" style="padding: 0 10px;">
          <a href="<?= $base; ?>/purchases" class="btn btn-info">Voltar</a>
        </div>

        <div class="modal-alter-date">
          <div class="modal-content">
            <input type="hidden" id="date-alter" value="">
            <h4>Digite a nova data</h4>
            <hr>
            <input type="text" name="alter-date-payment" id="alter-date" class="form-control" placeholder="00/00/0000">
            <p class="payment-alter"></p>
            <hr>
            <div class="modal-button">
              <a href="#" id="alter-date-action" class="btn btn-success">Alterar</a>
              <a href="#" id="cancel-alter-date" class="btn btn-info">Voltar</a>
            </div>
          </div>
        </div>
      </form>
    </div>
    <script src="<?= $base ?>/assets/js/purchases.js"></script>
  </div>  		
</div>
<!-- /.content-system -->

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