<?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>Editar Pedido</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-1">
            <label>
              Pedido CIF
              <input type="radio" name="cif" <?= ($array['requestInfo']['type'] == 'cif')?'checked':''; ?> >
            </label style="line-height: 22px;">
          </div>

          <div class="form-group col-md-2">
            <label>
              Pedido FOB
              <input type="radio" name="fob" <?= ($array['requestInfo']['type'] == 'fob')?'checked':''; ?> >
            </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="<?= $infoTecnic['type_request']; ?>">
                  <input type="hidden" name="status_save">
                  <input type="hidden" name="request_reference" value="<?= $infoTecnic['reference']; ?>">
                  <input type="hidden" name="read_to_save" value="<?= $infoTecnic['count_products']; ?>">
                  <input type="hidden" name="id_request" value="<?= $array['requestInfo']['id']; ?>">
                </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['requestInfo']['date_created']; ?>">
            </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['requestInfo']['user_name']; ?>">
            </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" value="<?= ($array['requestInfo']['id_client'])?$array['requestInfo']['id_client']:''; ?>" >
            <input type="text" name="search_client" class="form-control" id="search_client" value="<?= $array['clientInfo']['name'] ?>"> 
            <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 value="<?= $array['clientInfo']['name'] ?>">
            </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 value="<?= ($array['clientInfo']['type'] == 1)?$array['clientInfo']['cpf']:$array['clientInfo']['cnpj']; ?>">
            </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 value="<?= $array['clientInfo']['buyer'] ?>">
            </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 value="<?= $array['clientInfo']['first_phone'] ?>">
            </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">
              <option value="<?= $array['farmSelected']['id']; ?>"><?= $array['farmSelected']['name']; ?></option>
            </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="<?= $array['farmSelected']['name']; ?>" 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="<?= $array['farmSelected']['responsible']; ?>" 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="<?= $array['farmSelected']['phone']; ?>" 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="<?= $array['farmSelected']['state']; ?>" 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="<?= $array['farmSelected']['city_name']; ?>" 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" value="<?= $array['requestInfo']['id_provider']; ?>" >
            <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" value="<?= $group; ?>" >
          </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"><?= $infoTecnic['items_show']; ?></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" value="<?= $array['requestInfo']['total'] ?>">
          </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['receiver_form'] as $item): ?>
                <option value="<?= $item['id']; ?>" <?= ($array['requestInfo']['id_receiverform'] == $item['id'])?'selected':''; ?> ><?= $item['name']; ?></option>
              <?php endforeach; ?>

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

        <h4 class="bgb">Condições do Pedido</h4>

        <div class="form-row">
          <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" <?= ($array['receiverForm']['type_receiver'] == 0)?'selected':''; ?> >Selecionar</option>
              <option value="1" <?= ($array['receiverForm']['type_receiver'] == 1)?'selected':''; ?> >Pagamento Único</option>
              <option value="2" <?= ($array['receiverForm']['type_receiver'] == 2)?'selected':''; ?> >Parcelado</option>
            </select>
            
          </div>

          <div class="form-group col-md-2" id="in_cash" <?= ($array['receiverForm']['type_receiver'] == 1)?'style="display:block;"':''; ?> >
            <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;" value="<?= ($array['receiverForm']['type_receiver'] != 2)?$array['receiverForm']['date_receiver']:''; ?>" status="<?= ($array['receiverForm']['type_receiver'] != 2)?'true':''; ?>">
          </div>

          <div class="form-group col-md-2" id="in_term" <?= ($array['receiverForm']['type_receiver'] == 2)?'style="display:block;"':''; ?> >
            <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" value="<?= ($array['receiverForm']['type_receiver'] == 2)?$array['receiverForm']['date_receiver']:''; ?>" status="<?= ($array['receiverForm']['type_receiver'] == 2)?'true':''; ?>" >

              <select class="form-control" name="qtd_portion" id="qtd_portion" style="width:70px; margin-left: 10px;">
                <?php for($i=1; $i<21; $i++): ?>
                  <option value="<?= $i; ?>" <?= ($array['receiverForm']['installments'] == $i)?'selected':''; ?> ><?= $i; ?></option>
                <?php endfor; ?>
              </select>  
            </div>
          </div>
        </div>

        <!-- Apenas grupo sem estoque -->
        <div class="form-row" style="display:none;">
          <div class="form-group col-md-2">
            <label for="note_date">Quem pagará comissão a Supporte?</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">
            <label for="inputProvider">Receber comissão Qtos dias após a entrega?</label>
            <input type="text" class="form-control" name="receive_after_delivery" readonly 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" value="<?= ($array['otherExpenses'])?$array['otherExpenses']['value_unity']:''; ?>">
          </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" value="<?= ($array['otherExpenses'])?$array['otherExpenses']['note']:''; ?>" >
          </div>
        </div>

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

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

        <div class="form-row">
          <div class="form-group">
              <div class="form-check">
                <input class="form-check-input" type="checkbox" name="has_comission_salesman" id="exist_comission_salesman" <?= ($array['comissionSalesman'])?'checked':'' ?> >
                <label class="form-check-label" for="exist_comission_salesman">Habilitar</label>
              </div>
          </div>

          <div class="form-group col-md-3" style="display: flex;margin-left: 30px">
            <input type="hidden" name="salesman_selected" value="<?= $array['requestInfo']['id_salesman']; ?>" >
            <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 value="<?= ($array['comissionSalesman'])?$array['comissionSalesman']['name']:''; ?>" >
          </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 value="<?= (isset($array['comissionSalesman']['real_to_unity']))?$array['comissionSalesman']['real_to_unity']:''; ?>" >
          </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 value="<?= (isset($array['comissionSalesman']['percentage']))?$array['comissionSalesman']['percentage']:''; ?>" >
          </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 value="<?= (isset($array['comissionSalesman']['total']))?$array['comissionSalesman']['total']:''; ?>" >
          </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 -->
        <?php if(in_array('estimative_result_view', $permissions)): ?>

        <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 value="<?= $array['estimateResult']['coust_unity'] ?>" >
            </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 value="<?= $array['estimateResult']['exepenses_unity'] ?>" >
            </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 value="<?= $array['estimateResult']['comission_salesman_unity'] ?>" >
            </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 value="<?= $array['estimateResult']['total_coust'] ?>" >
            </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 value="<?= $array['estimateResult']['estimative_result'] ?>" >
          </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 value="<?= $array['estimateResult']['estimative_total'] ?>" >
          </div>
        </div>
        <?php endif; ?>

        <!-- 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."><?= $array['requestInfo']['note']; ?></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');?>