#include "PROTHEUS.CH"
#include "RWMAKE.CH"
#include "APWEBEX.CH"
#include "TOPCONN.CH"
#include "TBICONN.CH"
/*__________________________________________________________________________
�����������������������������������������������������������������������������
��+-----------------------------------------------------------------------+��
���Fun��o    � Oportunidade � Autor � Lucilene Mendes    � Data �23.08.24 ���
��+----------+------------------------------------------------------------���
���Descri��o � Grid com as oportunidades do vendedor.					  ���
��+-----------------------------------------------------------------------+��
�����������������������������������������������������������������������������
���������������������������������������������������������������������������*/
User Function Oportunidade()
Local cHtml
Local cLink		:= ""                      
Local cCombo	:= ""                      
Local cDataDe	:= ""
Local cDataAte	:= ""
Local cFiltDe	:= ""
Local cFilAte	:= ""                      
Local cLinkDet  := ""
Local aStatus:= {} 

Private cColunas:= ""
Private cItens	:= ""  
Private cTopo	:= ""  
Private cSite	:= "u_PortalLogin.apw"
Private cPagina	  := "Homologa��es"
Private cTitle	  := "" 
Private lTableTools:= .T.
Private lSidebar:= .F.
Private cCodLogin := ""
Private cVendLogin:= ""

Web Extended Init cHtml Start U_inSite()

	If Empty(HttpSession->CodVend)
		cHtml:= '<META HTTP-EQUIV="Refresh" CONTENT="0 ; URL='+cSite+'">'	
		Return cHtml
	Else
		If !Empty(HttpSession->Superv) .and. HttpSession->Superv <> HttpSession->CodVend
			HttpSession->CodVend:= HttpSession->Superv
		Endif
	Endif
	
	cVendLogin := u_GetUsrPR()
	cCodLogin  := U_SetParPR(cVendLogin)

	// Pega do par�metro com o Titulo do Portal
	cTitle	:= SuperGetMV("PS_TITLE", .T., "Portal SMS")
	
	// Define a funcao a ser chama no link
	cSite	:= "u_SMSPortal.apw?PR="+cCodLogin
	
	// Monta o cabe�alho para a pagina
	cHeader := U_PSHeader(cTitle, cSite) 	
	
	//Fun��o que atualiza os menus
	cMenus := U_GetMenus(AllTrim(Upper(Procname())), cVendLogin)
	
	//Tratamento dos filtros
	If type("HttpPost->DataDe") <> "U"
		//Se vazio, usa as datas padr�o para evitar erro na query
		If Empty(HttpPost->DataDe) .or. Empty(HttpPost->DataAte)
			cDataDe:= dtos(date()-30)
			cDataAte:= dtos(date())
		Else
			cDataDe:= dtos(ctod(HttpPost->DataDe))
		    cDataAte:= dtos(ctod(HttpPost->DataAte))
		Endif
		//Atualiza as vari�veis no valor do filtro
		cFiltDe:= dtoc(stod(cDataDe))
		cFilAte:= dtoc(stod(cDataAte))    
	Else
	    //Vari�veis dos input dos filtros
		cFiltDe:= dtoc(date()-30)
		cFilAte:= dtoc(date())
		//Vari�veis de filtro da query
		cDataDe:= dtos(date()-30)
		cDataAte:= dtos(date())
	Endif
	    
	//Topo da janela
	//Bot�o incluir novo or�amento
	cTopo:= '<div class="row form-group">'
	If HttpSession->Tipo = 'S'
		cTopo+= '	<div class="col-sm-3">'
		cTopo+= '		<button class="btn btn-primary" id="btAddOpt" name="btAddOpt" onclick="javascript: location.href='+"'"+'u_AddOpt.apw?PR='+cCodLogin+"'"+';">'
		cTopo+= '		  <i class="fa fa-plus"></i> Nova Homologa��o</button>'
		cTopo+= '  	</div>'
    Endif

    //Filtros
    cTopo+= '	<div class="col-sm-9" align="'+Iif(HttpSession->Tipo = 'S',"right","left")+'">'
    cTopo+= '	<form name="formGrid" id="formGrid" method="POST" action="U_Oportunidade.apw?PR='+cCodLogin+'">'
  	cTopo+= '		<label class="col-md-2 control-label">In�cio De:</label>'
    cTopo+= '  		<div class="col-md-3">'
	cTopo+= '		 	<div class="input-group">'
    cTopo+= '    			<span class="input-group-addon">'
    cTopo+= '					<i class="fa fa-calendar"></i>'
    cTopo+= '			    </span>'   
    cTopo+= '				<input data-plugin-datepicker="" data-plugin-options='+"'"+'{"autoclose": "true", "language": "pt-BR",'
	cTopo+= '					"daysOfWeekDisabled": "","daysOfWeekHighlighted":"[0]"}'+"'"+' value="'+cFiltDe+'" ' 
    cTopo+= '					placeholder="__/__/____" id="datade" name="datade" class="form-control only-numbers" type="text">'
    cTopo+= '			</div>'
    cTopo+= '		</div>'       
  
    cTopo+= '		<label class="col-md-2 control-label">In�cio At�:</label>'
	cTopo+= '		<div class="col-md-3">'
	cTopo+= '			<div class="input-group">'
    cTopo+= '				<span class="input-group-addon">'
    cTopo+= '					<i class="fa fa-calendar"></i>'
    cTopo+= '				</span>'   
    cTopo+= '				<input data-plugin-datepicker="" data-plugin-options='+"'"+'{"autoclose": "true", "language": "pt-BR",'
	cTopo+= '					"daysOfWeekDisabled": "","daysOfWeekHighlighted":"[0]"}'+"'"+' value="'+cFilAte+'" '
    cTopo+= '					placeholder="__/__/____" id="dataate" name="dataate" class="form-control only-numbers" type="text">'
    cTopo+= '			</div>'
    cTopo+= '		</div>'       
    cTopo+= '		<button class="btn btn-primary" id="btFiltro" value="" onclick="this.value= '+"'"+'Aguarde...'+"'"+';this.disabled= '+"'"+'disabled'+"'"+';Filtro()" name="btFiltro">'
    cTopo+= '			<i class="fa fa-filter"></i> Filtrar</button>'  
    cTopo+= '	</form>'
	cTopo+= '	</div>'
	cTopo+= '</div>'
	 	
	cQry:= " Select DISTINCT AD1_FILIAL, AD1_NROPOR, AD1_DESCRI, AD1_DESCOP, AD1_CODCLI, AD1_LOJCLI, A1_NOME, A1_EMAIL, AD1_DTINI, AD1_DTFIM, AD1_HOMOLO, AD1_VEND, AD1_REGION, AD1_DESCRE, "
	cQry+= " AD1_DESCCI, AD1_UF, AD1_STATUS, AD1_REVISA, AD1.R_E_C_N_O_ RECAD1 "
	cQry+= " From "+RetSqlName("AD1")+" AD1 " 
	cQry+= " Inner Join "+RetSqlName("SA1")+" SA1 On A1_FILIAL = '"+xFilial("SA1")+"' AND A1_COD = AD1_CODCLI AND A1_LOJA = AD1_LOJCLI AND SA1.D_E_L_E_T_ = ' ' " 
	cQry+= " Where  AD1.D_E_L_E_T_ = ' ' " 
    /*
	If HttpSession->Tipo = 'S' //Supervisor acessa todos os or�amentos da sua equipe
    	cQry+= " AND AD1_VEND in "+FormatIn(HttpSession->Equipe,"|")+" "
    Else
		cQry+= " AND AD1_VEND = '"+cVendLogin+"' "
    Endif
	*/
    If !Empty(cDataAte)
    	cQry+= " And AD1_DTINI between '"+cDataDe+"' and '"+cDataAte+"' "
    Endif
    cQry+= " Order by AD1_DTINI, AD1_NROPOR, AD1_CODCLI, AD1_LOJCLI "
	If Select("QRY") > 0
		QRY->(dbCloseArea())
	Endif	
	APWExOpenQuery(ChangeQuery(cQry),'QRY',.T.)
	
	
	//Cabe�alho do grid
	cColunas+='<th>Filial</th>'
	cColunas+='<th>N�mero</th>'
	cColunas+='<th>Descri��o</th>'
	//cColunas+='<th>Complemento</th>'
	cColunas+='<th>Status</th>'
	cColunas+='<th>Revis�o</th>'
	cColunas+='<th>In�cio</th>'
	cColunas+='<th>Fim</th>'
	cColunas+='<th>Cliente</th>'
	cColunas+='<th>Nome</th>'
	cColunas+='<th>Cidade/UF</th>'
	cColunas+='<th>Regional</th>'
	If HttpSession->Tipo = 'S'
		cColunas+='<th>Vendedor</th>'
	Endif
	cColunas+='<th></th>'         
	
	aStatus:= RetSx3Box(Posicione('SX3',2,'AD1_STATUS','X3CBox()'),,,1)
	
	While QRY->(!Eof())
		//Atualiza os controles do grid
		cLink:= "U_MntOpt.apw?PR="+cCodLogin+"&rec="+cValtoChar(QRY->RECAD1)
		cLinkDet  := '"onclick="window.document.location='+"'"+cLink+"&opc=view'"+';"'
		cItens+='<tr>'
	    cItens+='	<td role="button" onclick="window.document.location='+"'"+cLink+"&opc=view'"+';">'+QRY->AD1_FILIAL+'</td>'
	    cItens+='	<td role="button" onclick="window.document.location='+"'"+cLink+"&opc=view'"+';">'+QRY->AD1_NROPOR+'</td>'
	    If (nSeek := Ascan(aStatus, { |x| x[ 2 ] == QRY->AD1_STATUS })) > 0
  			cCombo := AllTrim( aStatus[nSeek,3])	
  		Endif
	    cItens+='	<td role="button" onclick="window.document.location='+"'"+cLink+"&opc=view'"+';">'+Upper(QRY->AD1_DESCRI)+'</td>'
	    //cItens+='	<td role="button" onclick="window.document.location='+"'"+cLink+"&opc=view'"+';">'+QRY->AD1_DESCOP+'</td>'
	    cItens+='	<td role="button" onclick="window.document.location='+"'"+cLink+"&opc=view'"+';">'+cCombo+'</td>'
	    cItens+='	<td role="button" onclick="window.document.location='+"'"+cLink+"&opc=view'"+';">'+QRY->AD1_REVISA+'</td>'
	    cItens+='	<td role="button" onclick="window.document.location='+"'"+cLink+"&opc=view'"+';" data-order="'+QRY->AD1_DTINI+'">'+dtoc(stod(QRY->AD1_DTINI))+'</td>'
		cItens+='	<td role="button" onclick="window.document.location='+"'"+cLink+"&opc=view'"+';" data-order="'+QRY->AD1_DTFIM+'">'+dtoc(stod(QRY->AD1_DTFIM))+'</td>'
	    cItens+='	<td role="button" onclick="window.document.location='+"'"+cLink+"&opc=view'"+';">'+QRY->AD1_CODCLI+'/'+QRY->AD1_LOJCLI+'</td>'
	    cItens+='	<td role="button" onclick="window.document.location='+"'"+cLink+"&opc=view'"+';">'+Alltrim(QRY->A1_NOME)+'</td>'
	    cItens+='	<td role="button" onclick="window.document.location='+"'"+cLink+"&opc=view'"+';">'+Alltrim(QRY->AD1_DESCCI+" / "+QRY->AD1_UF)+'</td>'
	    cItens+='	<td role="button" onclick="window.document.location='+"'"+cLink+"&opc=view'"+';">'+Alltrim(QRY->AD1_DESCRE)+'</td>'
	    If HttpSession->Tipo = 'S'
	    	cItens+='	<td role="button" onclick="window.document.location='+"'"+cLink+"&opc=view'"+';">'+QRY->AD1_VEND+' - '+Posicione("SA3",1,xFilial("SA3")+QRY->AD1_VEND,"A3_NREDUZ")+'</td>'
	    
	    
			cItens+='	<td class="actions">' 
	    
	    	//cItens+=	' <a href="'+cLink+'&opc=copy" class="on-default" data-toggle="tooltip" data-original-title="Copiar Or�amento"><i class="fa fa-files-o"></i></a>'
			
			If QRY->AD1_VEND $ HttpSession->Equipe
				If QRY->AD1_STATUS $ "1/3"
					cItens+=' <a href="'+cLink+'&opc=edit" class="on-default" data-toggle="tooltip" data-original-title="Alterar Homologa��o"><i class="fa fa-pencil"></i></a>'
					If Empty(QRY->AD1_REVISA)
						cItens+=' <a href="'+cLink+'&opc=dele" class="on-default" data-toggle="tooltip" data-original-title="Excluir Homologa��o"><i class="fa fa-trash-o"></i></a>'		
					Endif
				EndIf
			EndIf
			
			cItens+='	</td>'
		Endif
	    cItens+='</tr>'
	 
		QRY->(dbSkip())
	End
    
	//Retorna o HTML para constru��o da p�gina 
	cHtml := H_SMSGrid()	
	
Web Extended End

Return (cHTML) 

// Montar o formul�rio para mandar por e-mail
static function montarForm(cTitulo, cSubm)
Local cRet:=""
Local cEmail:= ""

cRet+= '<!-- Modal Form -->'+CRLF
cRet+= '         <div id="modalEmail" class="modal-block modal-block-primary mfp-hide">'+CRLF
cRet+= '            <section class="panel">'+CRLF
cRet+= '                <header class="panel-heading">'+CRLF
cRet+= '                    <h2 class="panel-title">Enviar '+cTitulo+' por e-mail</h2>'+CRLF
cRet+= '                </header>'+CRLF
cRet+= '                <div class="panel-body">'+CRLF
cRet+= '                    <form id="formEmail" class="form-horizontal mb-lg" novalidate="novalidate">'+CRLF
cRet+= '                        <span><i>Para enviar para mais de um destinat�rio, separe os e-mails com ;</i></span>'+CRLF
cRet+= '                        <br><br>'+CRLF
cRet+= '                        <div class="form-group">'+CRLF
cRet+= '                            <label class="col-sm-3 control-label">E-mail</label>'+CRLF
cRet+= '                            <div class="col-sm-9">'+CRLF
cRet+= '                                <input type="hidden" name="nrdoc"/>'+CRLF
cRet+= '                                <input type="hidden" name="formDest" value="'+cSubm+'"/>'+CRLF
cRet+= '                                <input type="email" name="email" class="form-control" placeholder="Insira seu email..." required/>'+CRLF
cRet+= '                            </div>'+CRLF
cRet+= '                        </div>'+CRLF
cRet+= '                    </form>'+CRLF
cRet+= '                </div>'+CRLF
cRet+= '                <footer class="panel-footer">'+CRLF
cRet+= '                    <div class="row">'+CRLF
cRet+= '                        <div class="col-md-12 text-right">'+CRLF
cRet+= '                            <button class="btn btn-primary modal-confirm" onclick="javascript:enviarEmail();">Enviar</button>'+CRLF
cRet+= '                            <button class="btn btn-default modal-dismiss" onclick="javascript:fecharEmail();">Cancelar</button>'+CRLF
cRet+= '                        </div>'+CRLF
cRet+= '                    </div>'+CRLF
cRet+= '                </footer>'+CRLF
cRet+= '            </section>'+CRLF
cRet+= '         </div>'+CRLF
	
return cRet