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

<div class="widget-box">
   <div class="widget-title">
      <ul class="nav nav-tabs">
         <li class="active"><a data-toggle="tab" href="#tab1"><i class="fas fa-users"></i> DADOS DO CLIENTE</a></li>
         <li><a data-toggle="tab" href="#tab2"><i class="fas fa-diagnoses"></i> ORDENS DE SERVIÇO</a></li>
		 <li id="tabVenda"><a href="#tab3" data-toggle="tab"><i class="fas fa-shopping-cart"></i> VENDAS</a></li>
		 <li id="tabTotal"><a href="#tab4" data-toggle="tab"><i class="fas fa-hand-holding-usd"></i> TRANSAÇÕES</a></li>
         
		 <div class="buttons">
            <?php if ($this->permission->checkPermission($this->session->userdata('permissao'), 'eCliente')) {
               echo '<a title="Icon Title" class="btn btn-mini btn-info" href="' . base_url() . 'index.php/clientes/editar/' . $result->idClientes . '"><i class="fas fa-edit"></i> Editar</a>';
               } ?>
            <a title="Voltar" class="btn btn-mini btn-inverse" href="<?php echo site_url() ?>/clientes"><i class="fas fa-arrow-left"></i> Voltar</a>
         </div>
      </ul>
   </div>
   <div class="widget-content tab-content">
      <div id="tab1" class="tab-pane active" style="min-height: 300px">
         <div class="accordion" id="collapse-group">
            <div class="accordion-group widget-box">
               <div class="accordion-heading">
                  <div class="widget-title">
                     <a data-parent="#collapse-group" href="#collapseGOne" data-toggle="collapse">
                        <span class="icon"><i class="fas fa-user"></i></span>
                        <h5>Dados Pessoais</h5>
                     </a>
                  </div>
               </div>
               <div class="collapse in accordion-body" id="collapseGOne">
                  <div class="widget-content">
                     <table class="table table-bordered">
                        <tbody>
                           <tr>
                              <td style="text-align: right; width: 30%"><strong>Nome</strong></td>
                              <td>
                                 <?php echo $result->nomeCliente ?>
                                <span class="buttons">
                                 <?php if ($this->permission->checkPermission($this->session->userdata('permissao'), 'eOs')) {
    $this->load->model('os_model');
    $zapnumber = preg_replace("/[^0-9]/", "", $result->celular);
    
    if (!empty($zapnumber)) {
    }        echo '<td nowrap><a title="Enviar WhatsApp" class="btn btn-mini btn-success" id="enviarWhatsApp" target="_blank" href="https://api.whatsapp.com/send?phone=55' . $zapnumber . '"><i class="fab fa-whatsapp"></i></a>  </td>';
	

} ?>
                              </span></td>
                           </tr>
                           <tr>
                              <td style="text-align: right"><strong>Documento</strong></td>
                              <td>
                                 <?php echo $result->documento ?>
                              </td>
                           </tr>
                           <tr>
                              <td style="text-align: right"><strong>Data de Cadastro</strong></td>
                              <td>
                                 <?php echo date('d/m/Y', strtotime($result->dataCadastro)) ?>
                              </td>
                           </tr>
                           <tr>
                              <td style="text-align: right"><strong>Tipo do Cliente</strong></td>
                              <td>
                                 <?php echo $result->fornecedor == true ? 'Fornecedor' : 'Cliente'; ?>
                              </td>
                           </tr>
                        </tbody>
                     </table>
                  </div>
               </div>
            </div>
            <div class="accordion-group widget-box">
               <div class="accordion-heading">
                  <div class="widget-title">
                     <a data-parent="#collapse-group" href="#collapseGTwo" data-toggle="collapse">
                        <span class="icon"><i class="fas fa-phone-alt"></i></span>
                        <h5>Contatos</h5>
                     </a>
                  </div>
               </div>
               <div class="collapse accordion-body" id="collapseGTwo">
                  <div class="widget-content">
                     <table class="table table-bordered">
                        <tbody>
                           <tr>
                              <td style="text-align: right; width: 30%"><strong>Contato:</strong></td>
                              <td>
                                 <?php echo $result->contato ?>
                              </td>
                           </tr>
                           <tr>
                              <td style="text-align: right; width: 30%"><strong>Telefone</strong></td>
                              <td>
                                 <?php echo $result->telefone ?>
                              </td>
                           </tr>
                           <tr>
                              <td style="text-align: right"><strong>Celular</strong></td>
                              <td>
                                 <?php echo $result->celular ?>
                              </td>
                           </tr>
                           <tr>
                              <td style="text-align: right"><strong>Email</strong></td>
                              <td>
                                 <?php echo $result->email ?>
                              </td>
                           </tr>
                        </tbody>
                     </table>
                  </div>
               </div>
            </div>
            <div class="accordion-group widget-box">
               <div class="accordion-heading">
                  <div class="widget-title">
                     <a data-parent="#collapse-group" href="#collapseGThree" data-toggle="collapse">
                        <span class="icon"><i class="fas fa-map-marked-alt"></i></span>
                        <h5>Endereço</h5>
                     </a>
                  </div>
               </div>
               <div class="collapse accordion-body" id="collapseGThree">
                  <div class="widget-content">
                     <table class="table table-bordered">
                        <tbody>
                           <tr>
                              <td style="text-align: right; width: 30%"><strong>Rua</strong></td>
                              <td>
                                 <?php echo $result->rua ?>
                              </td>
                           </tr>
                           <tr>
                              <td style="text-align: right"><strong>Número</strong></td>
                              <td>
                                 <?php echo $result->numero ?>
                              </td>
                           </tr>
                           <tr>
                              <td style="text-align: right"><strong>Complemento</strong></td>
                              <td>
                                 <?php echo $result->complemento ?>
                              </td>
                           </tr>
                           <tr>
                              <td style="text-align: right"><strong>Bairro</strong></td>
                              <td>
                                 <?php echo $result->bairro ?>
                              </td>
                           </tr>
                           <tr>
                              <td style="text-align: right"><strong>Cidade</strong></td>
                              <td>
                                 <?php echo $result->cidade ?> -
                                 <?php echo $result->estado ?>
                              </td>
                           </tr>
                           <tr>
                              <td style="text-align: right"><strong>CEP</strong></td>
                              <td>
                                 <?php echo $result->cep ?>
                              </td>
                           </tr>
                        </tbody>
                     </table>
                  </div>
               </div>
            </div>
         </div>
      </div>
      <!--Tab 2-->
      <div id="tab2" class="tab-pane" style="min-height: 300px">
         <?php if (!$results) { ?>
         <table class="table table-bordered ">
            <thead>
               <tr style="backgroud-color: #2D335B">
                  <th>N° OS</th>
                  <th>Data Inicial</th>
                  <th>Data Final</th>
                  <th>Descricao</th>
				  <th>Faturado</th>
                  <th>Defeito</th>
                  <th></th>
               </tr>
            </thead>
            <tbody>
               <tr>
                  <td colspan="6">Nenhuma OS Cadastrada</td>
               </tr>
            </tbody>
         </table>
         <?php
            } else { ?>
         <table class="table table-bordered ">
            <thead>
               <tr style="backgroud-color: #2D335B">
                  <th>N° OS</th>
                  <th>Data Inicial</th>
                  <th>Data Final</th>
                  <th>Descricao</th>
				  <th>Status</th>
				  <th>Faturado</th>
                  <th>Defeito</th>
                  <th></th>
               </tr>
            </thead>
            <tbody>
               <?php
                  foreach ($results as $r) {
                      $dataInicial = date(('d/m/Y'), strtotime($r->dataInicial));
                      $dataFinal = date(('d/m/Y'), strtotime($r->dataFinal));
                      echo '<tr>';
                      echo '<td>' . $r->idOs . '</td>';
                      echo '<td>' . $dataInicial . '</td>';
                      echo '<td>' . $dataFinal . '</td>';
                      echo '<td>' . $r->descricaoProduto . '</td>';
					  echo '<td>' . $r->status . '</td>';
					  echo '<td>R$ ' . number_format($r->valorTotal, 2, ',', '.') . '</td>';
					  echo '<td>' . $r->defeito . '</td>';
                  
                      echo '<td>';
                      if ($this->permission->checkPermission($this->session->userdata('permissao'), 'vOs')) {
                          echo '<a href="' . base_url() . 'index.php/os/visualizar/' . $r->idOs . '" style="margin-right: 1%" class="btn tip-top" title="Ver mais detalhes"><i class="fas fa-eye"></i></a>';
                      }
                      if ($this->permission->checkPermission($this->session->userdata('permissao'), 'eOs')) {
                          echo '<a href="' . base_url() . 'index.php/os/editar/' . $r->idOs . '" class="btn btn-info tip-top" title="Editar OS"><i class="fas fa-edit"></i></a>';
                      }
                  
                      echo  '</td>';
                      echo '</tr>';
                  } ?>
               <tr>
               </tr>
            </tbody>
         </table>
         <?php
            } ?>
      </div>
	  
<!--Vendas-->
<!--Tab 3-->
<div id="tab3" class="tab-pane" style="min-height: 300px">
<?php

if(!$resultvendas){?>

<table class="table table-bordered ">
    <thead>
        <tr style="backgroud-color: #2D335B">
            <th>Nº</th>
			
            <th>Data da Venda</th>
			<th>Data da Fatura</th>
			<th>R$ Faturado</th>
			<th>R$ A Faturar</th>
            <th></th>
        </tr>
    </thead>
    <tbody>

        <tr>
            <td colspan="7">Nenhuma venda Cadastrada</td>
        </tr>
    </tbody>
</table>

<?php } else{?>
<table class="table table-bordered ">
     <thead>
        <tr style="backgroud-color: #2D335B">
            <th>Nº</th>
			
            <th>Data da Venda</th>
			<th>Faturado?</th>
			
            <th>R$ Faturado</th>
			
            <th></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($resultvendas as $r) {
            $dataVenda = date(('d/m/Y'),strtotime($r->dataVenda));
			$dataFatura = date(('d/m/Y'),strtotime($r->dataFatura));
            if($r->faturado == 1){$faturado = 'Sim';} else{ $faturado = 'Não';}           
            echo '<tr>';
            echo '<td>'.$r->idVendas.'</td>';
			//echo '<td>'.$r->nf.'</td>';
            echo '<td>'.$dataVenda.'</td>';
			//echo '<td>'.$dataFatura.'</td>';
			echo '<td>' . $faturado . '</td>';
			
            //echo '<td><a href="'.base_url().'index.php/clientes/visualizar/'.$r->idClientes.'">'.$r->nomeCliente.'</a></td>';
            //echo '<td>'.$faturado.'</td>';
			
			echo '<td>R$ ' . number_format($r->valorTotal, 2, ',', '.') . '</td>';
            //echo '<td>'.$r->desconto.'</td>';
            echo '<td>';
            if($this->permission->checkPermission($this->session->userdata('permissao'),'vVenda')){
                echo '<a style="margin-right: 1%" href="'.base_url().'index.php/vendas/visualizar/'.$r->idVendas.'" class="btn tip-top" title="Ver mais detalhes"><i class="fas fa-eye"></i></a>'; 
                //echo '<a style="margin-right: 1%" href="'.base_url().'index.php/vendas/imprimir/'.$r->idVendas.'" target="_blank" class="btn btn-inverse tip-top" title="Imprimir"><i class="icon-print"></i></a>'; 
            }
            if($this->permission->checkPermission($this->session->userdata('permissao'),'eVenda')){
                echo '<a style="margin-right: 1%" href="'.base_url().'index.php/vendas/editar/'.$r->idVendas.'" class="btn btn-info tip-top" title="Editar venda"><i class="fas fa-edit"></i></a>'; 
            }
            if($this->permission->checkPermission($this->session->userdata('permissao'),'dVenda')){
                echo '<a href="#modal-excluir" role="button" data-toggle="modal" venda="'.$r->idVendas.'" class="btn btn-danger tip-top" title="Excluir Venda"><i class="fas fa-trash"></i></a>'; 
            }

            echo '</td>';
            echo '</tr>';
        }?>
        <tr>
            
        </tr>
    </tbody>
</table>
	
<?php } ?>

</div>        


	  
<!--Tab 4-->
        <div id="tab4" class="tab-pane" style="min-height: 300px">
		<?php

			if(!$transacoes){?>

		<table class="table table-bordered ">
			<thead>
			<tr style="backgroud-color: #2D335B">
             <th>ID</th>
            <th>Tipo</th>
            <th>Cliente / Fornecedor</th>
            <th>Descrição</th>
            <th>Vencimento</th>
            <th>Status</th>
            <th>Valor</th>
			<th>Ações</th>
            
        </tr>
    </thead>
    <tbody>

        <tr>
            <td colspan="8">Nenhuma lançamento encontrado</td>
        </tr>
    </tbody>
</table>



<?php } else { ?>

        <table class="table table-bordered " id="divLancamentos">
          <thead>
            <tr style="backgroud-color: #2D335B">
              <th>ID</th>
              <th>Tipo</th>
              <th>Cliente / Fornecedor</th>
              <th>Descrição</th>
              <th>Vencimento</th>
              <th>Status</th>
              <th>Valor</th>
			  <th>Ações</th>
              
            </tr>
          </thead>
          <tbody>
			        <?php 
        $totalReceita = 0;
		$totalComissao = 0;
        $totalDespesa = 0;
        $saldo = 0;
		$dataAtual = date('d/m/Y');
		$operador = $this->session->userdata('nome');
        foreach ($transacoes as $r) {
            $vencimento = date(('d/m/Y'),strtotime($r->data_vencimento));
            if($r->baixado == 0){$status = 'Pendente';}else{ $status = 'Pago';};
            if($r->tipo == 'receita'){ $label = 'success'; $totalReceita += $r->valor;} else{$label = 'important'; $totalDespesa += $r->valor;}
			//if($r->tipo == 'receita'){ $label = 'success'; $totalComissao += $r->comissao;} else{$label = 'important'; $totalDespesa += $r->comissao;}
			$boss = $r->cliente_fornecedor;
            if ($status == "Pago") {
                        echo '<tr class="success">';
                   
          } else {
                        echo '<tr class="error">';
                    }
            echo '<td>'.$r->idLancamentos.'</td>';
            echo '<td><span class="label label-'.$label.'">'.ucfirst($r->tipo).'</span></td>';
            echo '<td>'.$r->cliente_fornecedor.'</td>';
            //if ($r->pc_id == 0  or  $r->parcelas == 0){	
			echo '<td>'.$r->descricao.'</td>';		
			//}else{
           // echo '<td>'.$r->descricao.' - Parc: '.$r->pc_id. "/" .$r->parcelas.'</td>';
			//}
            echo '<td>'.$vencimento.'</td>';   
            echo '<td>'.$status.'</td>';
            echo '<td> R$ '.number_format($r->valor,2,',','.').'</td>';
            //echo '<td>'.$r->comissao.'</td>';
            echo '<td>';
            /* if($this->permission->checkPermission($this->session->userdata('permissao'),'eLancamento')){
                echo '<a href="#modalEditar" style="margin-right: 1%" data-toggle="modal" role="button" idLancamento="'.$r->idLancamentos.'" descricao="'.$r->descricao.'" valor="'.$r->valor.'" comissao="'.$r->comissao.'" vencimento="'.date('d/m/Y',strtotime($r->data_vencimento)).'" pagamento="'.date('d/m/Y', strtotime($r->data_pagamento)).'" baixado="'.$r->baixado.'" cliente="'.$r->cliente_fornecedor.'" formaPgto="'.$r->forma_pgto.'" pcontas="'.$r->pcontas.'" categoria="'.$r->categoria.'" observacao="'.$r->observacao.'" tipo="'.$r->tipo.'" class="btn btn-info tip-top editar" title="Editar Lançamento"><i class="icon-pencil icon-white"></i></a>'; 
            } */
            
            if($this->permission->checkPermission($this->session->userdata('permissao'),'dLancamento')){
                echo '<a href="#modalExcluir" data-toggle="modal" role="button" idLancamento="'.$r->idLancamentos.'" class="btn btn-danger tip-top excluir" title="Excluir Lançamento"><i class="icon-trash icon-white"></i></a>'; 
            }
                     
            echo '</td>';
            echo '</tr>';
        }?>
			        <tr>
			            
			        </tr>
			    </tbody>
			    <tfoot>
			    	<tr>
			    		<td colspan="6" style="text-align: right; color: green"> <strong>Total:</strong></td>
			    		<td colspan="2" style="text-align: left; color: green"><strong>R$ <?php echo number_format($totalReceita,2,',','.') ?></strong></td>
			    	</tr>

			    </tfoot>
			</table>
  
		<?php } ?>	
    </div> 
 </div> 
 </div>




<!-- Modal Excluir lançamento-->
<div id="modalExcluir" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">MapOS - Excluir Lançamento</h3>
  </div>
  <div class="modal-body">
    <h5 style="text-align: center">Deseja realmente excluir esse lançamento?</h5>
    <input name="id" id="idExcluir" type="hidden" value="" />
  </div>
  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true" id="btnCancelExcluir">Cancelar</button>
    <button class="btn btn-danger" id="btnExcluir">Excluir Lançamento</button>
  </div>
</div>




<!-- Modal -->
<div id="modal-excluir" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <form action="<?php echo base_url() ?>index.php/vendas/excluir" method="post">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h5 id="myModalLabel">Excluir Venda</h5>
        </div>
        <div class="modal-body">
            <input type="hidden" id="idVenda" name="id" value="" />
            <h5 style="text-align: center">Deseja realmente excluir esta Venda?</h5>
        </div>
        <div class="modal-footer">
            <button class="btn" data-dismiss="modal" aria-hidden="true">Cancelar</button>
            <button class="btn btn-danger">Excluir</button>
        </div>
    </form>
</div>

		


<!-- Modal -->
<div id="modal-excluir" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <form action="<?php echo base_url() ?>index.php/vendas/excluir" method="post" >
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h5 id="myModalLabel">Excluir Venda</h5>
  </div>
  <div class="modal-body">
    <input type="hidden" id="idVenda" name="id" value="" />
    <h5 style="text-align: center">Deseja realmente excluir esta Venda?</h5>
  </div>
  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Cancelar</button>
    <button class="btn btn-danger">Excluir</button>
  </div>
  </form>
</div>
</div>
 
 <script src="<?php echo base_url()?>js/jquery.validate.js"></script>
 <script src="<?php echo base_url();?>js/maskmoney.js"></script>
 <script type="text/javascript">
 
   $(document).on('click', 'a', function(event) {
        
        var venda = $(this).attr('venda');
        $('#idVenda').val(venda);

    });
	
	$(document).on('click', '.excluir', function(event) {
      $("#idExcluir").val($(this).attr('idLancamento'));
    });
	
	
	$(document).on('click', '.editar', function(event) {
      $("#idEditar").val($(this).attr('idLancamento'));
      $("#descricaoEditar").val($(this).attr('descricao'));
      $("#fornecedorEditar").val($(this).attr('cliente'));
      $("#valorEditar").val($(this).attr('valor'));
	  $("#descontoEditar").val($(this).attr('valor_desconto'));
      $("#comissaoEditar").val($(this).attr('comissao'));
      $("#vencimentoEditar").val($(this).attr('vencimento'));
      $("#pagamentoEditar").val($(this).attr('pagamento'));
      $("#formaPgtoEditar").val($(this).attr('formaPgto'));
      $("#tipoEditar").val($(this).attr('tipo'));
      $("#observacaoEditar").val($(this).attr('observacao'));
      $("#pcontasEditar").val($(this).attr('pcontas'));
      $("#categoriaEditar").val($(this).attr('categoria'));
      $("#urlAtualEditar").val($(location).attr('href'));
      var baixado = $(this).attr('baixado');
      if(baixado == 1){
        $("#pagoEditar").prop('checked', true);
        $("#divPagamentoEditar").show();
      }
      else{
        $("#pagoEditar").prop('checked', false); 
        $("#divPagamentoEditar").hide();
      }
      

    });
	
 
 $(document).on('click', '#btnExcluir', function(event) {
        var id = $("#idExcluir").val();
    
        $.ajax({
          type: "POST",
          url: "<?php echo base_url();?>index.php/financeiro/excluirLancamento",
          data: "id="+id,
          dataType: 'json',
          success: function(data)
          {
            if(data.result == true){
                $("#btnCancelExcluir").trigger('click');
                $("#divLancamentos").html('<div class="progress progress-striped active"><div class="bar" style="width: 100%;"></div></div>');
                $("#divLancamentos").load( $(location).attr('href')+" #divLancamentos" );
                
            }
            else{
                $("#btnCancelExcluir").trigger('click');
                alert('Ocorreu um erro ao tentar excluir produto.');
            }
          }
        });
        return false;
    });
</script>

