<?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>Novo Pedido Sem Estoque</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;?>/saverequest" id="form_validator">

        <div class="button fix-rgt">
          <a href="<?=$base;?>/requests" class="btn btn-info">Voltar</a>
          <a href="#" class="btn btn-warning" id="save_oppened" data-status="1" >Salvar em Aberto</a>
          <a href="#" class="btn btn-success" id="save_and_delivery" data-status="2" >Salvar e Entregar</a>
        </div>


        <!-- Tipo Pedido Cif ou Fob -->
        <h4 class="bgb">Informe o Tipo do Pedido</h4>

        <div class="form-row">
          <div class="form-group col-md-2">
            <label>
              Pedido CIF
              <input type="radio" name="cif">
            </label style="line-height: 22px;">
          </div>

          <div class="form-group col-md-2">
            <label>
              Pedido FOB
              <input type="radio" name="fob">
            </label>
          </div>          
        </div>

        <h4 class="bgb">Dados do Pedido</h4>
        <div class="form-row">
          <div class="oculted">
            <span>
              <i>
                <div>
                  <input type="hidden" name="type_request" value="nostock">
                  <input type="hidden" name="status_save">
                  <input type="hidden" name="request_reference" value="">
                  <input type="hidden" name="read_to_save" value="">
                  <input type="hidden" name="id_request" value="">
                </div>
              </i>
            </span>
          </div>

            <div class="form-group col-md-2" id="validate_note_date">
              <label for="note_date">Data</label>
              <input type="text" class="form-control" id="request_date" readonly value="<?= $array['date']; ?>">
            </div>

            <div class="form-group col-md-3" id="provider">
              <label for="inputProvider">Ultimo usuário</label>
              <input type="text" class="form-control" id="request_user" readonly value="<?= $array['user']; ?>">
            </div>        

        </div>

        <!-- Cliente -->

        <h4 class="bgb">Cliente</h4>

        <div class="form-row">
          <div class="form-group col-md-3" style="display: flex; position: relative;">
            <input type="hidden" name="client_selected">
            <input type="text" name="search_client" class="form-control" id="search_client"> 
            <a href="#" class="form-control btn-search" id="getClientInfo">Buscar</a>
            <div class="result-here searchresultclient"></div>
          </div>
        </div>

        <div class="form-row">

            <div class="form-group col-md-3">
              <label for="note_date">Razão social</label>
              <input type="text" class="form-control" id="c_name" readonly>
            </div>

            <div class="form-group col-md-3">
              <label for="inputProvider">CPF/CNPJ</label>
              <input type="text" class="form-control" id="c_cpf_cnpj" readonly>
            </div>

            <div class="form-group col-md-2">
              <label for="note_date">Contato</label>
              <input type="text" class="form-control" id="c_buyer_name" readonly>
            </div>

            <div class="form-group col-md-2">
              <label for="note_date">Fone do Contato</label>
              <input type="text" class="form-control" id="c_buyer_phone" readonly>
            </div>

        </div>

        <!-- Fazendas Cliente -->

        <h4 class="bgb">Fazenda(s) - Endereço de Entrega</h4>

        <div class="form-row">
          <div class="form-group col-md-3">
            <select name="farms_list" class="form-control"></select>
          </div>
        </div>

        <div class="form-row">
                <div class="form-group col-md-3">
                    <label for="farm_name">Nome da Fazenda</label>
                    <input type="text" class="form-control" id="farm_name" value="" readonly>
                </div>
                <div class="form-group col-md-3">
                    <label for="farm_responsible">Responsável</label>
                    <input type="text" class="form-control" id="farm_responsible" value="" readonly>
                </div>
                <div class="form-group col-md-2">
                    <label for="farm_phone">Fone Celular</label>
                    <input type="text" class="form-control" id="farm_phone" value="" readonly>
                </div>
                <div class="form-group col-md-1">
                    <label for="farm_state">Estado</label>
                    <input type="text" class="form-control" id="farm_state" value="" readonly>
                </div>
                <div class="form-group col-md-2">
                    <label for="farm_city">Cidade</label>
                    <input type="text" class="form-control" id="farm_city" value="" readonly>
                </div>
            </div>

        <!-- Fornecedor -->

        <h4 class="bgb">Fornecedor</h4>

        <div class="form-row">
          <div class="form-group col-md-3" style="display: flex; position: relative;">
            <input type="hidden" name="provider_selected">
            <input type="text" name="search_provider" id="search_provider" class="form-control">
            <div class="result-here searchresultsprov"></div>

            <a href="#" class="form-control btn-search" id="getProviderInfo">Buscar</a>
          </div>
        </div>

        <div class="form-row">

            <div class="form-group col-md-3">
              <label for="note_date">Razão social</label>
              <input type="text" class="form-control" id="provider_name" readonly>
            </div>

            <div class="form-group col-md-2">
              <label for="inputProvider">CPF/CNPJ</label>
              <input type="text" class="form-control" id="provider_type" readonly>
            </div>

            <div class="form-group col-md-2">
              <label for="note_date">Contato</label>
              <input type="text" class="form-control" id="provider_contact" readonly>
            </div>

            <div class="form-group col-md-2">
              <label for="note_date">Fone do Contato</label>
              <input type="text" class="form-control" id="provider_phone" readonly>
            </div>

        </div>

        <!-- Adicionar novo Item -->

        <h4 class="bgb">Itens do Pedido</h4>

        <div class="form-row">
          <div class="form-group col-md-3" style="display: flex;">
            <select name="product_list" class="form-control"></select>
            <input type="hidden" id="product_groupid">
          </div>
        </div>

        <div class="form-row">

          <div class="form-group col-md-1">
            <label for="note_date">% da Supporte</label>
            <input type="text" class="form-control" value="" name="percent_supp" id="percent_supp">
          </div>

          <div class="form-group col-md-2" id="validade_support_percent">
            <label for="note_date">Grupo</label>
            <input type="text" class="form-control" id="product_group" readonly>
          </div>

          <div class="form-group col-md-2">
            <label for="inputProvider">Unidade</label>
            <input type="text" class="form-control" id="product_unit" readonly>
          </div>

          <div class="form-group col-md-2">
            <label for="note_date">Apresentação</label>
            <input type="text" class="form-control" id="product_presentation" readonly>
          </div>

          <div class="form-group col-md-1">
            <label for="note_date">Quantidade</label>
            <input type="text" class="form-control" id="product_quantity" maxlength="10">
          </div>

          <div class="form-group col-md-1">
            <label for="note_date">Preço Custo</label>
            <input type="text" class="form-control" id="product_price_coust" readonly>
          </div>

          <div class="form-group col-md-1">
            <label for="note_date">Preço Venda</label>
            <input type="text" class="form-control" id="product_price_sale">
          </div>

          <div class="form-group col-md-1">
            <label for="note_date">Valor do Item</label>
            <input type="text" class="form-control" id="product_value_item" readonly>
          </div>

          <div class="form-group col-md-1">
            <label></label>
            <a href="#" class="form-control" id="addProductInRequest" style="width: 110px; text-align: center; margin-top: 8px;">Adicionar</a>
          </div>

        </div>

        <div class="form-row" id="insert_products"></div>

        <div class="form-row">
          <div class="view-total-request box-total-request">
            <label style="font-size: 0.8rem;">Valor Total do Pedido</label>
            <input type="text" class="form-control total-request" readonly name="total_amount" id="inputTotalAmount" placeholder="0,00">
          </div>
        </div>

        <!-- Condições do Pedido -->

        <h4 class="bgb">Forma de Recebimento</h4>

        <div class="form-row">
          <div class="form-group col-md-2">
            <label for="receiver_where">Selecionar forma de recebimento</label>
            <select class="form-control" name="receiver_where" id="receiver_where">
              <option value="0">Selecionar</option>

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

            </select>            
          </div>
        </div>

        <h4 style="display:none;" class="bgb">Condições do Pedido</h4>

        <div class="form-row" style="display:none;">
          <div class="form-group col-md-2">
            <label for="receiver_form">Pagamento Único / Parcelado</label>
            <select class="form-control" name="receiver_form" id="receiver_form">
              <option value="0">Selecionar</option>
              <option value="1">Pagamento Único</option>
              <option value="2">Parcelado</option>
            </select>
            
          </div>

          <div class="form-group col-md-2" id="in_cash">
            <label for="date_receiver">Data a receber</label>            
            <input type="text" class="form-control" name="date_receiver_incash" id="date_receiver" placeholder="00/00/0000" style="width:220px;">
          </div>

          <div class="form-group col-md-2" id="in_term">
            <label for="date_in_term">Data Primeiro Recebimento / Parcelas</label>

            <div class="fleft">
              <input type="text" class="form-control" name="date_receiver_interm" id="date_in_term" placeholder="00/00/0000"> 
              <select class="form-control" name="qtd_portion" id="qtd_portion" style="width:70px; margin-left: 10px;">
                <?php for($i=2; $i<21; $i++): ?>
                  <option value="<?= $i; ?>"><?= $i; ?></option>
                <?php endfor; ?>
              </select>  
            </div>
          </div>
        </div>

        <!-- Apenas grupo sem estoque -->
        <div class="form-row" >
          <div class="form-group col-md-2">
            <label for="note_date">Quem pagará comissão?</label>
            <select class="form-control" name="whats_pay_comission">
              <option value="1">Não informado</option>
              <option value="2">Cliente</option>
              <option value="3" selected>Fornecedor</option>
            </select>
          </div>

          <div class="form-group col-md-2" style="display:none;">
            <label for="inputProvider">Receber Qtos dias após a entrega?</label>
            <input type="text" class="form-control" name="receive_after_delivery" value="30">
          </div>
        </div>
        <!-- Fim Apenas grupo sem estoque -->

        <h4 class="bgb">Outras despesas</h4>

        <div class="form-row">
          <div class="form-group col-md-1">
            <label for="note_date">Valor</label>
            <input type="text" class="form-control" id="other_expenses_value" name="other_value" placeholder="0,00">
          </div>

          <div class="form-group col-md-3">
            <label for="note_date">Observação</label>
            <input type="text" class="form-control" id="other_expenses_description" name="other_description"  placeholder="Pedágio BR-153">
          </div>
        </div>

        <!-- Comissão do vendedor -->

        <h4 class="bgb">Comissão do vendedor</h4>

        <div class="form-row">
          <div class="form-group hide">
              <div class="form-check">
                <input class="form-check-input" type="checkbox" name="has_comission_salesman" id="exist_comission_salesman">
                <label class="form-check-label" for="exist_comission_salesman">Habilitar</label>
              </div>
          </div>

          <div class="form-group col-md-3" style="display: flex;">
            <input type="hidden" name="salesman_selected">
            <input type="text" name="search_salesman" id="search_salesman" class="form-control" readonly>
            <div class="result-here searchresultsalesman"></div>
          </div>

        </div>

        <div class="form-row">
          <div class="form-group col-md-3">
            <label for="salesman_name">Vendedor</label>
            <input type="text" class="form-control" id="salesman_name" readonly>
          </div>

          <div class="form-group col-md-2">
            <label for="real_to_unity">Reais/Unidade</label>
            <input type="text" class="form-control" id="real_to_unity" name="real_per_unity" readonly>
          </div>

          <div class="form-group col-md-2" id="validade_salesman_percentage">
            <label for="salesman_percentage">Percentual</label>
            <input type="text" class="form-control" id="salesman_percentage" name="salesman_percentage" readonly>
          </div>

          <div class="form-group col-md-2">
            <label for="salesman_comission_value">Valor da Comissão</label>
            <input type="text" class="form-control" id="salesman_comission_value" name="comission_value" readonly>
          </div>

          <div class="form-group col-md-3" style="display:none;">
            <label for="days_of_payment_comission">Pagar Qtos dias apos a Entrega?</label>
            <input type="text" class="form-control" id="days_of_payment_comission" name="days_of_payment_comission" readonly value="30">
          </div>
        </div>

        <!-- Resultado Estimado -->
        <div class="hide">
          <h4 class="bgb">Resultado Estimado</h4>

          <div class="form-row" id="result_est_unit">

              <div class="form-group col-md-2">
                <label for="estimative_custo_for_unity">Custo R$/Unid</label>
                <input type="text" class="form-control" id="estimative_coust_for_unity" name="estimative_coust_for_unity" readonly>
              </div>

              <div class="form-group col-md-2" style="display:none;">
                <label for="estimative_freight_price_unity">Frete R$/Unid</label>
                <input type="text" class="form-control" id="estimative_freight_price_unity" name="estimative_freight_price_unity" readonly>
              </div>

              <div class="form-group col-md-2">
                <label for="estimaive_others_unity">Outras Despesas R$/Unid</label>
                <input type="text" class="form-control" id="estimaive_others_unity" name="estimaive_others_unity" readonly>
              </div>

              <div class="form-group col-md-2">
                <label for="estimative_commision_salesman">Comissão Vend R$/Unid</label>
                <input type="text" class="form-control" id="estimative_commision_salesman" name="estimative_commision_salesman" readonly>
              </div>

              <div class="form-group col-md-2">
                <label for="estimative_total_couste">Total de Custo</label>
                <input type="text" class="form-control" id="estimative_total_couste" name="estimative_total_couste" readonly>
              </div>

          </div>

          <div class="form-row" id="result_est_real">
            <div class="form-group col-md-2">
                <label for="estimative_result">Resultado - %</label>
                <input type="text" class="form-control" id="estimative_result" name="estimative_result" readonly>
              </div>

              <div class="form-group col-md-2">
                <label for="estimative_result_real">Resultado - R$</label>
                <input type="text" class="form-control" id="estimative_result_real" name="estimative_result_real" readonly>
              </div>

          </div>
        </div>

        <!-- Observações -->

        <h4 class="bgb">Observações</h4>

        <div class="form-row">
          <div class="form-group col-md-8">
            <label for="note"></label>
            <textarea class="form-control" rows="5" name="note" placeholder="Alguma observação sobre o Pedido."></textarea>
          </div>
        </div>

        <hr style="padding: 3px 0; background: #00688B; border: 0">

        <div class="button">
          <a href="<?=$base;?>/requests" class="btn btn-info">Voltar</a>
          <a href="#" class="btn btn-warning" id="save_oppened" data-status="1" >Salvar em Aberto</a>
          <a href="#" class="btn btn-success" id="save_and_delivery" data-status="2" >Salvar e Entregar</a>
        </div>

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

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

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