<%@ page contentType="text/html"%>
<%@ page import="javax.xml.parsers.DocumentBuilderFactory,javax.xml.parsers.DocumentBuilder,org.w3c.dom.*"%>
<%@page  import="ibase.dashboard.umpl.bean.MSRTerritoryWisePBPRBean"%>
<%@page  import="ibase.utility.CommonConstants"%>
<%@page  import="javax.naming.InitialContext"%>
<%@page  import="javax.naming.Context"%>
<%@page  import="org.w3c.dom.Node"%>
<%@page  import="org.w3c.dom.NodeList"%>
<%@page  import="ibase.webitm.utility.GenericUtility"%>
<%@page  import="java.util.Calendar"%>
<%@page  import="java.util.Enumeration"%>
<%@page  import="java.util.List"%>
<%@page  import="java.util.ArrayList"%>
<%@page  import="java.text.SimpleDateFormat"%>
<jsp:useBean id="msrTerritoryWisePBPRBean" scope="request" class="ibase.dashboard.umpl.bean.MSRTerritoryWisePBPRBean" />

<%!GenericUtility genericUtility = GenericUtility.getInstance();

	SimpleDateFormat simpleDateFormat=null;
	Calendar calendar = null;
	int month = 0;
	int preyear = 0;
	int  preyearVal = 0;
	int year = 0;
	int  curryearVal =0;
	String[] seclectYear = null;
	String[] seclectMonth = null;
	int lastDayOfMonth = 0;
	int todaysDay = 0;
	int day=1;
	Document dom = null;
	NodeList dtlList = null;
	Node parentNode = null;
	NodeList childNodeList = null;
	String fldValue = "";
	String descr="";    
    String terrDescr="";    
	String xmlString = "";
	String objName = "";	
	StringBuffer xmlData = null;		
	NodeList detailList = null;		
	NodeList territoryList = null;
	Node territoryNode = null;
	String territoryNodeName="";	
	NodeList spCodeList = null;
	Node spCodeNode = null;
	String spCodeNodeName = "";	
	NodeList spCodeChildList= null;
	Node spCodeChildNode = null;
	String spCodeChildNodeName = "";	
	int TerrFldWrkngDays=0;
	int TerrDoctorCall=0;
	double totalTargetDoct=0.0;
	double targetDoctor=0.0;
	double territoryCallAvg=0.0;
	String msrName="";
	int MSRFldWrkngDays=0;
	int MSRDoctorCall=0;
	double MSRCallAvg=0.0;		
	String  territory_loc = "";
	String  checkTerrLoc = "";	
	String  specialtyCode = "";
	String  checkTerrLoc1 = "";	
	String  specialtyCode1 = "";	
	String rptTitle = "";
	String srcJSPath = "";
	String srcImagesPath = "";	
	String frmDate = "";
	String toDate = "";
	String terrLocCodeXml = "";
	
	String pbPercent="";
	String prPercent="";
	String othrPercent="";
%>
<%
	response.setHeader("Expires", "Sat, 6 May 1995 12:00:00 GMT");
	response.setHeader("Cache-Control", "no-store, no-cache,must-revalidate");
	response.addHeader("Cache-Control", "post-check=0, pre-check=0");
	response.setHeader("Pragma", "no-cache");
	
%>
<%
	String userAgent = request.getHeader("user-agent");
	System.out.println("userAgent ==" + userAgent);
	if (userAgent.indexOf("Android") > 0 || userAgent.indexOf("iPhone") > 0 || userAgent.indexOf("iPad") > 0)
	{
		srcJSPath     = "/ibase/FusionChart/js/FusionCharts.js";
		srcImagesPath = "/ibase/FusionChart/images/Pie3D.swf";
             System.out.println("call when,calling like ipad,iPhone and Tab");
  
	} else
	{
		srcJSPath = "/ibase/FusionChart/js/FusionCharts.js";
		srcImagesPath = "/ibase/FusionChart/images/Pie3D.swf";
		System.out.println("srcJSPath =="+srcJSPath);
		System.out.println("srcImagesPath ==" +srcImagesPath);

	}
%>
<html>
<head>
<script type="text/javascript" src="<%=srcJSPath%>"> </script>
<link rel="stylesheet" type="text/css"	  href="/ibase/dashboard/umpl/css/ui.dropdownchecklist.standalone.css"/>
<script type="text/javascript"	         src="/ibase/dashboard/umpl/jquery/jquery-1.6.1.min.js"></script>
<script type="text/javascript"	         src="/ibase/dashboard/umpl/jquery/jquery-ui-1.8.13.custom.min.js"></script>
<script type="text/javascript"	         src="/ibase/dashboard/umpl/jquery/ui.dropdownchecklist-1.4-min.js"></script>

<style type="text/css">
.combox {
	border-radius: 5px;
	width: 1000px;
	align: center;
	background-color: #F5F5F5;
}
.headerTable {
	height: 50px;
	width: 900px;
	border: 1;
	align: center;
}
.headerTr {
	background-color:2471C4;
	height: 30px;
	font-size: 10pt;
}
.detailTr {
	background-color: E0E9EB;
	height: 20px;
	font-size: 10pt;
}
</style>

<script type="text/javascript">

$(document).ready(function(){
	$("#territory_loc").dropdownchecklist({emptyText:"Please Select", width:250 });
 });
$(document).ready(function(){
	$("#acct_year").dropdownchecklist({emptyText: "Please Select", width: 100 });
 });
$(document).ready(function(){
	$("#acct_month").dropdownchecklist({emptyText: "Please Select", width: 100 });
 });
 
 $(document).ready(function(){
	$("#spl_code").dropdownchecklist({emptyText: "Please Select", width: 100 });
 });

function getData(territoryLoc,curryear,currmonth,specialtyCode)
{
	
	var splCode  = document.getElementById("spl_code");
	
       for (var m = 0; m < splCode.options.length; m++) 
            	 {
        	      var spCode = splCode.options[m].value;
        		   if(spCode==specialtyCode)
    				  {
						  
        			   splCode.options[m].selected = true;
    				  }
    		       }
	
	 var territoryList   = document.getElementById("territory_loc");
     
        for (var m1 = 0; m1 < territoryList.options.length; m1++) 
            	 {
        	     var terLoc = territoryList.options[m1].value;
        		   if(terLoc==territoryLoc)
    				  {
        			    territoryList.options[m1].selected = true;   			    
        			   
        			  }
    		       }
              
       var curryearList   = document.getElementById("acct_year");
        
        for (var m = 0; m < curryearList.options.length; m++) 
            	 {
					var checkyear = curryearList.options[m].value;
        		    if(checkyear==curryear)
    				  {
        			   curryearList.options[m].selected = true;
    				  }
    		       }
         
         var currmonthList   = document.getElementById("acct_month");
                 
          	for (var n = 0; n < currmonthList.options.length; n++) 
              	 {
          	      var checkmonth = currmonthList.options[n].value;
          		   if(checkmonth==currmonth)
      				  {
          			   currmonthList.options[n].selected = true;
      				  }
      		       }
}

function isTerritorySelected()
 {
	 if(document.getElementById("territory_loc").selectedIndex==-1)
     {
	        alert("Please select at least one Territory Location");
	        return false;
	 }
	 if(document.getElementById("spl_code").selectedIndex==-1)
     {
	        alert("Please select at least one Territory Location");
	        return false;
	 }
	 return true;
  }
</script>

</head>
<body>

<%
	try
	{
		simpleDateFormat = new SimpleDateFormat("dd-MMM-yy");
		calendar         = Calendar.getInstance();
		preyear          = calendar.get(Calendar.YEAR) - 1;
		preyearVal       = preyear;
		year             = calendar.get(Calendar.YEAR);	
		curryearVal      = year;
		month            = calendar.get(Calendar.MONTH)+1 ;
		
		String currDate = simpleDateFormat.format(calendar.getTime());
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		toDate  = simpleDateFormat.format(calendar.getTime());	
		
		calendar       = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		frmDate        = simpleDateFormat.format(calendar.getTime());
		todaysDay      = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
		lastDayOfMonth = Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH);
		
		//new code added by Birendra Pandey It is VIMP
		seclectYear     = request.getParameterValues("acct_year");
		seclectMonth    = request.getParameterValues("acct_month");		
		
		if(seclectYear!=null && seclectMonth!=null){
			
			 year        = Integer.parseInt(seclectYear[0]);
			 month       = Integer.parseInt(seclectMonth[0]);	
			 
			//ADD-------1St of month and last of month				  
			  calendar =Calendar.getInstance();
		      calendar.set(year, month-1, day);
		      frmDate = simpleDateFormat.format(calendar.getTime());			     
		      calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		      toDate = simpleDateFormat.format(calendar.getTime());
		     
		   //End of -------1St of month and last of month
		     			
			currDate =seclectMonth[0]+"-"+seclectYear[0];
		  
		}
		
		rptTitle      = "MSR Territory Wise PB/PR Report For " + currDate;
		terrLocCodeXml = msrTerritoryWisePBPRBean.getTerritoryLocXmlData();
	     
%>
<form method="post" action="MSRTWisePBPRReport.jsp"	name="MSRTWisePBPRReport">

 <table align="center">
	<tr>
		<td class="title"><%=rptTitle%></td>
	</tr>
</table>
<table class="combox">
	<tr>
		<td>Territory: 
		<select id="territory_loc" name="territory_loc" size="10">
			<%
			dom = genericUtility.parseString(terrLocCodeXml);

			 if (dom != null)
					{
			%>
			<option value="allIndia">All India</option>
			<%
				      dtlList = dom.getElementsByTagName("Detail");
						for (int cntr = 0; cntr < dtlList.getLength(); cntr++)
						{
							parentNode = dtlList.item(cntr);
							childNodeList = parentNode.getChildNodes();
							if (childNodeList.getLength() > 0)
							{
								fldValue = childNodeList.item(0).getTextContent();
								descr = childNodeList.item(1).getTextContent();
			%>
			<option value="<%=fldValue%>"><%=descr%></option>
			<%
							}
						}
					}
			%>			
		</select>
		</td>
		<td>
		
		</td>		
		<td>Speciality: <select id="spl_code" name="spl_code" size="5">
		    <option value="ALL">ALL</option>
            <option value="PAED">PAED</option>
            <option value="GP">GP</option>  
            <option value="GYNC">GYNC</option> 
            <option value="ENT">ENT</option>          
            </select></td>                        
		<td>Year: <select id="acct_year" name="acct_year" size="5">
            <option value="<%=curryearVal%>"><%=curryearVal%></option>
            <option value="<%=preyearVal%>"><%=preyearVal%></option>
           
            </select></td>
		<td>Month: <select id="acct_month" name="acct_month" size="5">
            <option value="1">JAN</option>
            <option value="2">FEB</option>
            <option value="3">MAR</option>
            <option value="4">APR</option>
            <option value="5">MAY</option>
            <option value="6">JUN</option>
            <option value="7">JUL</option>
            <option value="8">AUG</option>
            <option value="9">SEP</option>
            <option value="10">OCT</option>
            <option value="11">NOV</option>
            <option value="12">DEC</option>            
            </select></td>	
		  
		<td><input type="submit" name="showChart" value="View Report" 	onclick="return isTerritorySelected();" /></td>
	</tr>
</table> 
<%
		checkTerrLoc    = request.getParameter("territory_loc");
		specialtyCode   = request.getParameter("spl_code");		
		terrDescr =request.getParameter("terrDescr");
		
		//This is main section for executiing or display chart 
		if((checkTerrLoc!=null && checkTerrLoc.trim().length() > 0 ) && (specialtyCode!=null && specialtyCode.trim().length() > 0))
		{
			xmlString = msrTerritoryWisePBPRBean.getMSRWisePBPRDetail(frmDate,toDate,this.checkTerrLoc,this.specialtyCode);
		}
		
		System.out.println("xmlString  in jsp ==>" + xmlString);
		
		xmlData = new StringBuffer("<chart caption='");	
		xmlData.append(rptTitle);
		xmlData.append("' showPercentageValues='1'  showLabels='1' showLegend='1'>" );
		if (xmlString != null && xmlString.trim().length() > 0)
		{
			dom = genericUtility.parseString(xmlString);
			
			if(dom !=null )
			{	
				// start of Target Doctors Data	calcualtion		
				detailList = dom.getElementsByTagName("Detail1");
				if(detailList.item(0).getFirstChild() != null)
				{
					territoryList = dom.getElementsByTagName("territory");
					
					for (int cntr = 0; cntr < territoryList.getLength(); cntr++)
					{
						territoryNode = territoryList.item(cntr);
						territoryNodeName=territoryNode.getAttributes().item(0).getNodeValue();
						spCodeList = territoryNode.getChildNodes();						
						targetDoctor=0.0;
						for (int nodCtr = 0; nodCtr < spCodeList.getLength(); nodCtr++)
						{
							spCodeNode = spCodeList.item(nodCtr);
							spCodeNodeName = spCodeNode.getNodeName();
							spCodeChildList = spCodeNode.getChildNodes();
						    for(int chldNodCtr = 0; chldNodCtr < spCodeChildList.getLength(); chldNodCtr++)
						    {
								spCodeChildNode = spCodeChildList.item(chldNodCtr);
								spCodeChildNodeName=spCodeChildNode.getNodeName();
								if(spCodeChildNodeName.equals("msr_name"))
								{
									msrName=(spCodeChildNode.getTextContent());
								}
								if(spCodeChildNodeName.equals("target_doct"))
								{
									targetDoctor=Double.parseDouble(spCodeChildNode.getTextContent());
								}							
																				
							}	
							totalTargetDoct	= totalTargetDoct+targetDoctor	;																					
						}
										
					  }
				    }
				  
				    // end of Target Doctors Data	calcualtion				    	
			        // start of PB Doctors Data	calcualtion		
							
				            detailList = dom.getElementsByTagName("Detail2");
							if(detailList.item(0).getFirstChild() != null)
							{
								territoryList = dom.getElementsByTagName("territory_pb");
							    int pbCodeCount=0;
							    		
								for (int cntr = 0; cntr < territoryList.getLength(); cntr++)
								{
									territoryNode = territoryList.item(cntr);
									territoryNodeName=territoryNode.getAttributes().item(0).getNodeValue();
									spCodeList = territoryNode.getChildNodes();						
									
									for (int nodCtr = 0; nodCtr < spCodeList.getLength(); nodCtr++)
									{
										pbCodeCount++;
										spCodeNode = spCodeList.item(nodCtr);
										spCodeNodeName = spCodeNode.getNodeName();
										spCodeChildList = spCodeNode.getChildNodes();
									    for(int chldNodCtr = 0; chldNodCtr < spCodeChildList.getLength(); chldNodCtr++)
									    {
											spCodeChildNode = spCodeChildList.item(chldNodCtr);
											spCodeChildNodeName=spCodeChildNode.getNodeName();
											if(spCodeChildNodeName.equals("msr_name_pb"))
											{
												msrName=(spCodeChildNode.getTextContent());
											}																								
										}																					
									}	
																	
								  }	
								  				 
								pbPercent=String.format("%.2f",(Double.parseDouble(String.valueOf(pbCodeCount))/totalTargetDoct));
							    xmlData.append("<set value='"+pbPercent+"' label='PB Doctors,"+pbCodeCount+"' color='429EAD'  />");
							  }							   
							   
							  
				           // End of PB Doctors Data	calcualtion	
				           
				           // start of PR Doctors Data	calcualtion		
							
				            detailList = dom.getElementsByTagName("Detail3");
							if(detailList.item(0).getFirstChild() != null)
							{
								territoryList = dom.getElementsByTagName("territory_pr");
								int prCodeCount=0;
								for (int cntr = 0; cntr < territoryList.getLength(); cntr++)
								{
									territoryNode = territoryList.item(cntr);
									territoryNodeName=territoryNode.getAttributes().item(0).getNodeValue();
									spCodeList = territoryNode.getChildNodes();						
									
									for (int nodCtr = 0; nodCtr < spCodeList.getLength(); nodCtr++)
									{
										prCodeCount++;
										spCodeNode = spCodeList.item(nodCtr);
										spCodeNodeName = spCodeNode.getNodeName();
										spCodeChildList = spCodeNode.getChildNodes();
									    for(int chldNodCtr = 0; chldNodCtr < spCodeChildList.getLength(); chldNodCtr++)
									    {
											spCodeChildNode = spCodeChildList.item(chldNodCtr);
											spCodeChildNodeName=spCodeChildNode.getNodeName();
											if(spCodeChildNodeName.equals("msr_name_pr"))
											{
												msrName=(spCodeChildNode.getTextContent());
											}																								
										}																					
									}									
								  }
								 
								  prPercent=String.format("%.2f",(Double.parseDouble(String.valueOf(prCodeCount))/totalTargetDoct));
								 xmlData.append("<set value='"+prPercent+"' label='PR Doctors,"+prCodeCount+"' color='4249AD'/>");
							  }
							    
							   
							  
				           // End of PR Doctors Data	calcualtion	
				           
				           // start of Others Doctors Data	calcualtion		
							
				            detailList = dom.getElementsByTagName("Detail3");
							if(detailList.item(0).getFirstChild() != null)
							{
								territoryList = dom.getElementsByTagName("territory_oth");
					            int othrCodeCount=0;
								for (int cntr = 0; cntr < territoryList.getLength(); cntr++)
								{
									territoryNode = territoryList.item(cntr);
									territoryNodeName=territoryNode.getAttributes().item(0).getNodeValue();
									//out.println("territoryNodeName:"+territoryNodeName);
									spCodeList = territoryNode.getChildNodes();						
									
									for (int nodCtr = 0; nodCtr < spCodeList.getLength(); nodCtr++)
									{
										othrCodeCount++;
										spCodeNode = spCodeList.item(nodCtr);
										spCodeNodeName = spCodeNode.getNodeName();
										spCodeChildList = spCodeNode.getChildNodes();
									    for(int chldNodCtr = 0; chldNodCtr < spCodeChildList.getLength(); chldNodCtr++)
									    {
											spCodeChildNode = spCodeChildList.item(chldNodCtr);
											spCodeChildNodeName=spCodeChildNode.getNodeName();
											if(spCodeChildNodeName.equals("msr_name_oth"))
											{
												msrName=(spCodeChildNode.getTextContent());
											}																								
										}																					
									}									
								  }
								   
								   othrPercent=String.format("%.2f",(Double.parseDouble(String.valueOf(othrCodeCount))/totalTargetDoct));
						           xmlData.append("<set value='"+othrPercent+"' label='Other Doctors,"+othrCodeCount+"' color='AD42A2'/>");
							  }							   
							   
							  
				           // End of Others Doctors Data	calcualtion	
				  
			}				
		}
		xmlData.append("</chart>");
		System.out.println("xmlDataString----->"+xmlData.toString());
		xmlString=xmlData.toString();
		xmlData.setLength(0);
		
	    detailList=null;
	    territoryList=null;
	    spCodeList=null;
	    spCodeChildList=null;
		territoryNode=null;
		spCodeNode=null;
		spCodeChildNode=null;	
		
 %>

<div id="chartContainer"></div>	
		
		<script type="text/javascript"> 			
		         FusionCharts.setCurrentRenderer('javascript');
		         var myChart = new FusionCharts("/ibase/FusionChart/images/Pie3D.swf", "myChartId", "750", "750", "0", "1");
		         myChart.setXMLData("<%=xmlString%>"); 		            
		         myChart.render("chartContainer");
		           
        </script>
      
<%
	} catch (Exception ex)
	{
		System.out.println("Exception : ITMWizardHandlerServlet sMessageArguments(String,wizardBean) :" + ex); 
	}
	
prPercent="";
othrPercent="";
pbPercent="";	
totalTargetDoct=0.0;	
	
%>
<script>
var curryear       ="<%=this.year%>";
var currmonth      ="<%=this.month%>";
var territoryLoc   ="<%=this.checkTerrLoc%>";
var specialtyCode  ="<%=this.specialtyCode%>";
window.onload =getData(territoryLoc,curryear,currmonth,specialtyCode);
</script>
</form>
</body>
</html>
