<?php include("conexao.php");
      $conn = selectAllFinanc(); 
?>

<link rel="stylesheet" href="<?php echo base_url(); ?>assets/js/jquery-ui/css/smoothness/jquery-ui-1.9.2.custom.css" />
<script type="text/javascript" src="<?php echo base_url() ?>assets/js/jquery-ui/js/jquery-ui-1.9.2.custom.js"></script>
<script type="text/javascript" src="<?php echo base_url() ?>assets/js/jquery.validate.js"></script>

<link rel="stylesheet" href="<?php echo base_url() ?>assets/trumbowyg/ui/trumbowyg.css">
<script type="text/javascript" src="<?php echo base_url() ?>assets/trumbowyg/trumbowyg.js"></script>
<script type="text/javascript" src="<?php echo base_url() ?>assets/trumbowyg/langs/pt_br.js"></script>

<script src="<?php echo base_url()?>assets/js/jquery.mask.min.js"></script>
<script src="<?php echo base_url()?>assets/js/sweetalert2.all.min.js"></script>
<script src="<?php echo base_url()?>assets/js/funcoes.js"></script>


<style>
    .ui-datepicker {
        z-index: 9999 !important;
    }

    .trumbowyg-box {
        margin-top: 0;
        margin-bottom: 0;
    }
</style>

<div class="row-fluid" style="margin-top:0">
    <div class="span12">
        <div class="widget-box">
            <div class="widget-title">
                <span class="icon">
                    <i class="icon-tags"></i>
                </span>
                <h5>Cadastro de OS</h5>
            </div>
            <div class="widget-content nopadding tab-content">
                <div class="span12" id="divProdutosServicos" style=" margin-left: 0">

                    <ul class="nav nav-tabs">
                        <li class="active" id="tabDetalhes"><a href="#tab1" data-toggle="tab">Detalhes da OS</a></li>
                    </ul>
                    <div class="tab-content">
                        <div class="tab-pane active" id="tab1">
                          <div class="span12" id="divCadastrarOs">
                                <?php if ($custom_error == true) { ?>
                                    <div class="span12 alert alert-danger" id="divInfo" style="padding: 1%;">Dados incompletos, verifique os campos com asterisco ou se selecionou corretamente cliente, responsável e garantia.<br />Ou se tem um cliente e um termo de garantia cadastrado.</div>
                                <?php
                                } ?>
                                <form action="<?php echo current_url(); ?>" method="post" id="formOs">
                                    <div class="span12" style="padding: 1%">
                                      <div class="span6">
                                          <h4>Cliente<span class="required">* </span>
                                           </label>
                                            <input id="cliente" class="span12" type="text" name="cliente" value="" />
                                      <input id="clientes_id" class="span12" type="hidden" name="clientes_id" value="" /> <div class="addclient"><?php if ($this->permission->checkPermission($this->session->userdata('permissao'), 'aCliente')) { ?>
										<a href="#modalCadastro" data-toggle="modal" role="button" class="btn btn-mini btn-info"><i class="fas fa-plus"></i> Adicionar Cliente</a>
											<?php } ?></div>
                                      </h4></div>
                                     
                                     
                                     
                                      <div class="span6"><label for="tecnico"><h4>Técnico / Responsável<span class="required">*</span></label><input id="tecnico" class="span12" type="text" name="tecnico" value="<?= $this->session->userdata('nome'); ?>" />
                                            <input id="usuarios_id" class="span12" type="hidden" name="usuarios_id" value="<?= $this->session->userdata('id'); ?>" />
                                        </div>
                                    </div>
                                    <div class="span12" style="padding: 1%; margin-left: 0">
                                        <div class="span3">
                                            <label for="status">Status<span class="required">*</span></label>
                                            <select class="span12" name="status" id="status" value="">
                                                <option value="Orçamento">Orçamento</option>
                                                <option value="Aberto">Aberto</option>
                                                <option value="Aguardando Peças">Aguardando</option>
                                               
                                            </select>
                                        </div>
                                        <div class="span3">
                                            <label for="dataInicial">Data Inicial<span class="required">*</span></label>
                                            <input id="dataInicial" autocomplete="off" class="span12 datepicker" type="text" name="dataInicial" value="<?php echo date('d/m/Y'); ?>" />
                                        </div>
                                        <div class="span3">
                                            <label for="dataFinal">Data Final<span class="required">*</span></label>
                                            <input id="dataFinal" autocomplete="off" class="span12 datepicker" type="text" name="dataFinal" value="<?php echo date('d/m/Y'); ?>" />
                                        </div>
                                        <div class="span3"></div>
                                    </div>
								<div class="span12 well" style="padding: 1%; margin-left: 0">

                                        
                                        <div class="span3">
                                            <label for="marca"><h4>Equipamento</label>
                                            <?php
                                            $js = array(
                                                'id' => 'marcas',
                                                'class' => 'span12'
                                            );
                                            $options = array($result->idMarcas => $result->marca) + $marcas;
                                            echo form_dropdown('marca', $options, 0, $js);
                                            ?>
											
										</div>                                        
                                        <div class="span3">
                                            <label for="modelo"><h4>Modelo</label>
                                            <select name="modelo" id="modelos" class="span12">
                                                <option value="<?php echo $result->idModelos ?>"><?php echo $result->modelos ?></option>
                                            </select>
                                        </div>
                                        <div class="span3">
                                            <label for="equipamento"><h4>Cor</label>
                                            <?php
                                            $js = array(
                                                'id' => 'equipamento',
                                                'class' => 'span12'
                                            );
                                            $options = array($result->idEquipamentos => $result->equipamento) + $equipamentos;
                                            echo form_dropdown('equipamento', $options, 0, $js);
                                            ?>
                                        </div>
                                        <div class="span3">
                                            <label for="Serie"><h4>NS ou IMEI</label>
                                            <input id="Serie" type="text" class="span12" name="Serie" value="<?php echo $result->nr_serie ?>"  />

                                        </div>
 
										<div class="span2" style="margin-left: 0;">
                                            <label for="acessorio">Parceria</label>
                                            <select class="span12" name="acessorio" id="acessorio" value="">
                                                <option <?php if($result->acessorio == 'selecione'){echo 'selected';} ?> value="selecione">Selecione</option>
                                                <option <?php if($result->acessorio == 'Sim'){echo 'selected';} ?> value="Sim">Sim</option>
                                                <option <?php if($result->acessorio == 'Não'){echo 'selected';} ?> value="Não">Não</option>
                                            </select>
                                        </div>
                                        <div id="descAcessorioSim" class="span10" style="margin-left: 1; display: none;">
                                            <label for="descAcessorio">Descrição dos Acessórios</label>
                                            <input class="span12" type="text" class="form-control" name="descAcessorio" value="<?php echo $result->descAcessorio ?>">
                                        </div>
                                        <div id="descAcessorioNao" class="span10" style="margin-left: 1; display: none;">
                                            <label for="descAcessorio">Descrição dos Acessórios</label>
                                            <input class="span12" type="text" class="form-control" readonly="readonly" name="descAcessorio" value="Nenhum parceiro no processo.">
                                        </div>
											
											
											
                                </div>
								<br>
								
								  <div class="span12" style="padding: 1%; margin-left: 0">
                          <div class="span6 offset3" style="text-align: center">
                                            <button class="btn btn-success" id="btnContinuar"><i class="fas fa-plus"></i> Continue</button>
                                            <a href="<?php echo base_url() ?>index.php/os" class="btn"><i class="fas fa-backward"></i> Voltar</a>
                                            

                                    </div>
                                  </div>
                                </form>
                      </div>
                        </div>
                    </div>
                </div>
                
            </div>
        </div>
    </div>
</div>

<!--Modal Cadastra Cliente-->
  <div id="modalCadastro" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <form id="formCliente" action="<?php echo base_url() ?>index.php/os/adicionarOs" method="post">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel"> <h4>Adicionar Cliente</h3>
  </div>
  
  <div class="modal-body">
      <div class="span12 alert alert-info" style="margin-left: 0"> ** Campos com asterisco são obrigatórios ** </div>
		<div class="span12" style="margin-left: 0"> 
        
				<div class="span12" style="margin-left: 0"> 
					<div class="span8">
					   <label for="nomeCliente">Nome<span class="required">*</span></label>
                       <input id="nomeCliente" type="text" name="nomeCliente" class="span12" value="<?php echo set_value('nomeCliente'); ?>"  />
						<input id="urlAtual" type="hidden" name="urlAtual" value="<?php echo current_url() ?>"/>
					</div>
					<div class="span4">
                        <label for="documento">CPF/CNPJ<span class="required">*</span></label>
                        <input id="documento" type="text"  name="documento" class="cpfcnpj span12" value="<?php echo set_value('documento'); ?>"  />
					</div>
				</div>
				<div class="span12" style="padding: 1%; margin-left: 0">
                	<div class="span3">
                      <label for="celular">WhatsApp<span class="required">*</span></label>
                        <input id="celular" method="post" type="text" name="celular" class="span12" value="<?php echo set_value('celular'); ?>"  maxlength="13" size="40" onkeypress="formatar_mascara(this,'##-#####-####')" placeholder = '34-90000-0000'/>
					</div>
					<div class="span3">
                        <label for="telefone">Outro Contato<span class="required">*</span></label>
                        <input id="telefone" method="post" type="text" name="telefone" class="span12" value="<?php echo set_value('telefone'); ?>"  maxlength="12" size="40" onkeypress="formatar_mascara(this,'##-####-####')" placeholder = '34-0000-0000'/>
                    </div>
					
										<div class="span6"></div>
				</div>
				<div class="span12" style="padding: 1%; margin-left: 0">
					
					<div class="span3"></div>
				</div>
		</div>  
				 
			<div class="span12" style="padding: 1%; margin-left: 0">
                    <div class="span3"></div>
					
					<div class="span3"></div>
			</div>  
       </div>
        
  <div class="modal-footer">
      <button class="btn btn-success">Salvar</button>
    <button class="btn btn-danger" data-dismiss="modal" aria-hidden="true">Cancelar</button>

  </div>
  
  </form>
</div>

<script type="text/javascript">
    $(document).ready(function() {
		
		$('.addclient').hide();
        $("#cliente").autocomplete({
            source: "<?php echo base_url(); ?>index.php/os/autoCompleteCliente",
            minLength: 1,
            close: function(ui) { if(ui.label == 'Adicionar cliente...')ui.target.value = '';},
            select: function(event, ui) {
                if(ui.item.label == 'Adicionar cliente...')
                    $('.addclient').show();
                else
                    {
                        $("#clientes_id").val(ui.item.id);
                        $('.addclient').hide();
                    }
            }
        });
       /*  $("#cliente").autocomplete({
            source: "<?php echo base_url(); ?>index.php/os/autoCompleteCliente",
            minLength: 1,
            select: function(event, ui) {
                $("#clientes_id").val(ui.item.id);
            }
        }); */
        $("#tecnico").autocomplete({
            source: "<?php echo base_url(); ?>index.php/os/autoCompleteUsuario",
            minLength: 1,
            select: function(event, ui) {
                $("#usuarios_id").val(ui.item.id);
            }
        });
        $("#termoGarantia").autocomplete({
            source: "<?php echo base_url(); ?>index.php/os/autoCompleteTermoGarantia",
            minLength: 1,
            select: function(event, ui) {
                $("#garantias_id").val(ui.item.id);
            }
        });
		
				$(function () {
            $('#marcas').change(function () {
                $('#modelos').html("<option>Carregando...</option>");
                var id_marcas = $('#marcas').val();
                $.post("<?php echo base_url(); ?>index.php/os/buscaModelosbyMarcas", {
                    id_marcas: id_marcas
                }, function (data) {
                    $('#modelos').html(data);
                });
            });
        });
		
		if ( $("#acessorio").val() == "Sim") {
              $( "#descAcessorioSim > input" ).attr('name','descAcessorio');
              $( "#descAcessorioSim" ).show();
              $( "#descAcessorioNao > input" ).attr('name','');
              $( "#descAcessorioNao" ).hide();
          } else {
              $( "#descAcessorioNao > input" ).attr('name','descAcessorio');
              $( "#descAcessorioNao" ).show();
              $( "#descAcessorioSim > input" ).attr('name','');
              $( "#descAcessorioSim" ).hide();
          }
		  
		  
		  $( "#acessorio" ).change(function() {
          if ( $(this).val() == "Sim") {
              $( "#descAcessorioSim > input" ).attr('name','descAcessorio');
              $( "#descAcessorioSim" ).show();
              $( "#descAcessorioNao > input" ).attr('name','');
              $( "#descAcessorioNao" ).hide();
          } else {
              $( "#descAcessorioNao > input" ).attr('name','descAcessorio');
              $( "#descAcessorioNao" ).show();
              $( "#descAcessorioSim > input" ).attr('name','');
              $( "#descAcessorioSim" ).hide();
          }
        });
		

        $("#formOs").validate({
            rules: {
                cliente: {
                    required: true
                },
                tecnico: {
                    required: true
                },
                dataInicial: {
                    required: true
                },
                dataFinal: {
                    required: true
                }

            },
            messages: {
                cliente: {
                    required: 'Campo Requerido.'
                },
                tecnico: {
                    required: 'Campo Requerido.'
                },
                dataInicial: {
                    required: 'Campo Requerido.'
                },
                dataFinal: {
                    required: 'Campo Requerido.'
                }
            },
            errorClass: "help-inline",
            errorElement: "span",
            highlight: function(element, errorClass, validClass) {
                $(element).parents('.control-group').addClass('error');
            },
            unhighlight: function(element, errorClass, validClass) {
                $(element).parents('.control-group').removeClass('error');
                $(element).parents('.control-group').addClass('success');
            }
        });
        $(".datepicker").datepicker({
            dateFormat: 'dd/mm/yy'
        });
        $('.editor').trumbowyg({
            lang: 'pt_br'
        });
    });
</script>
