<html style="overflow:hidden;">
  <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <style>
    table {
  font-family: arial, sans-serif;
  border-collapse: none;
  width: 80%;
  
}

td, th {
  border: 0px solid #dddddd;
  padding: 8px;
}
#widget_Frame_Popup{
	height:10px;
    overflow: hidden;

}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://live.zwidgets.com/js-sdk/1.0.6/ZohoEmbededAppSDK.min.js"></script>
     <link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet" />
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
	<script src="https://live.zwidgets.com/js-sdk/1.0.5/ZohoEmbededAppSDK.min.js"></script>
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

  </head>
  <body>
 
<div>
	<table style="margin-left:auto;margin-right:auto;">
   <tr>
      <td>Select Stage:</td>
      <td><select id="test" name="test" onchange="fncall()">
       <option disabled selected value> -- select an option -- </option>
      </select>
	  </td>
    </tr>
  
   <tr>
      <td>Select Buy/Sell:</td>
      <td><select id="Type" name="Type" onchange="fncall()">
       <option disabled selected value> -- select an option -- </option>
      <option value="Buy">Buy</option>
        <option value="Sell">Sell</option>
      </select>
	  </td>
    </tr>
  </table>


</div>
	<script type="text/javascript">
	function fncall()
	{
console.log("ON CHANGE FUNCTION CALLED ");
	 selected_value=document.getElementById("test").value;
	 console.log("Selected value :",selected_value);
	selected_type=document.getElementById("Type").value;
	 console.log("Selected value :",selected_type);
	 criteria= "(Account_Name:equals:"+selected_value+")";
	 ZOHO.CRM.API.searchRecord({Entity:"Accounts",Type:"criteria",Query:criteria})
.then(async function(data){
   
    acc_det= data['data'];
     console.log("acc_det",acc_det);
      for (let acc of acc_det )
      {
    States_Serviced=acc['States_Serviced'];
    Counties_Serviced=acc['Counties_Serviced'];
    console.log("state and county serviced :",States_Serviced);
     console.log("state and county serviced :",Counties_Serviced);
   if (selected_type=="Buy")
  {
   console.log("Buy");
   var config={
  Entity:"Deals",
  APIData:{
        "id": EntityId,
        "State_Serviced": States_Serviced,
        "Title_Search_Company":selected_value,
        "Counties_Serviced":Counties_Serviced,
        "Title_Escrow_Company":""
      
  }
}
  }
  else if (selected_type=="Sell")
  {
  console.log("Sell");
   var config={
  Entity:"Deals",
  APIData:{
        "id": EntityId,
        "State_Serviced": States_Serviced,
        "Search_Specialist":selected_value,
        "Counties_Serviced":Counties_Serviced,
        "Title_Escrow_Company1":""
      
  }
}
  }
  

 
ZOHO.CRM.API.updateRecord(config)
.then(function(data){
    
    suc_data= data['data'];
   
    code=suc_data[0];
    succ_code=code['code'];
     console.log(succ_code)
     if (succ_code== 'SUCCESS')
     {
     alert("Title Search Specialist updated successfully");
     }
     else
     {
     console.log("Update fail ");
     }
})
     }
    });
	}
  
  function title()
  {
      console.log("Testing in ttle fn");
      var statelist= [{"STATE":"Alabama","SHORTCODE":"AL"},{"STATE":"Alaska","SHORTCODE":"AK"},{"STATE":"American Samoa","SHORTCODE":"AS"},{"STATE":"Arizona","SHORTCODE":"AZ"},{"STATE":"Arkansas","SHORTCODE":"AR"},{"STATE":"California","SHORTCODE":"CA"},{"STATE":"Colorado","SHORTCODE":"CO"},{"STATE":"Connecticut","SHORTCODE":"CT"},{"STATE":"Delaware","SHORTCODE":"DE"},{"STATE":"District Of Columbia","SHORTCODE":"DC"},{"STATE":"Florida","SHORTCODE":"FL"},{"STATE":"Georgia","SHORTCODE":"GA"},{"STATE":"Guam","SHORTCODE":"GU"},{"STATE":"Hawaii","SHORTCODE":"HI"},{"STATE":"Idaho","SHORTCODE":"ID"},{"STATE":"Illinois","SHORTCODE":"IL"},{"STATE":"Indiana","SHORTCODE":"IN"},{"STATE":"Iowa","SHORTCODE":"IA"},{"STATE":"Kansas","SHORTCODE":"KS"},{"STATE":"Kentucky","SHORTCODE":"KY"},{"STATE":"Louisiana","SHORTCODE":"LA"},{"STATE":"Maine","SHORTCODE":"ME"},{"STATE":"Maryland","SHORTCODE":"MD"},{"STATE":"Massachusetts","SHORTCODE":"MA"},{"STATE":"Michigan","SHORTCODE":"MI"},{"STATE":"Minnesota","SHORTCODE":"MN"},{"STATE":"Mississippi","SHORTCODE":"MS"},{"STATE":"Missouri","SHORTCODE":"MO"},{"STATE":"Montana","SHORTCODE":"MT"},{"STATE":"Nebraska","SHORTCODE":"NE"},{"STATE":"NEVADA","SHORTCODE":"NV"},{"STATE":"NEW HAMPSHIRE","SHORTCODE":"NH"},{"STATE":"NEW JERSEY","SHORTCODE":"NJ"},{"STATE":"WYOMING","SHORTCODE":"WY"},{"STATE":"TENNESSEE","SHORTCODE":"TN"},{"STATE":"Nevada","SHORTCODE":"NV"},{"STATE":"New Mexico","SHORTCODE":"NM"},{"STATE":"New York","SHORTCODE":"NY"},{"STATE":"North Carolina","SHORTCODE":"NC"},{"STATE":"Arizona","SHORTCODE":"AZ"},{"STATE":"North Dakota","SHORTCODE":"ND"},{"STATE":"California","SHORTCODE":"CA"},{"STATE":"Ohio","SHORTCODE":"OH"},{"STATE":"Oklahoma","SHORTCODE":"OK"},{"STATE":"OREGON","SHORTCODE":"OR"},{"STATE":"Pennsylvania","SHORTCODE":"PA"},{"STATE":"South Carolina","SHORTCODE":"SC"},{"STATE":"South Dakota","SHORTCODE":"SD"},{"STATE":"Texas","SHORTCODE":"TX"},{"STATE":"Utah","SHORTCODE":"UT"},{"STATE":"Virginia","SHORTCODE":"VA"},{"STATE":"Washington","SHORTCODE":"WA"},{"STATE":"Vermont","SHORTCODE":"VT"},{"STATE":"West Virginia","SHORTCODE":"WV"}]

  var prop_county;
  var prop_state;
  	ZOHO.CRM.API.getRecord({Entity:"Deals",RecordID:EntityId})
.then( async function(data){
    console.log("Deal Data",data)
    deal_data= data['data'];
    deal_dt=deal_data[0];
    Property_State=deal_dt['Property_State'];
    console.log("State Property_State :",Property_State);
    for (let statesmap of statelist)
 {
 	if(Property_State == statesmap.SHORTCODE)
 	{
 		prop_state = statesmap.STATE;
 	
 		
	break;
	}
	else
	{
		console.log(statesmap.SHORTCODE);
 		
	}
	}
	
    Property_County=deal_dt['Property_County'];
    Property_County_name=Property_County['name'];
    console.log("Property_County :",Property_County_name);
    console.log("Deal data check :", deal_data);
   prop_county=Property_County_name;
ZOHO.CRM.API.searchRecord({Entity:"Accounts",Type:"criteria",Query:"(Account_Type:equals:Title Search)"})
.then(async function(data){
    console.log(data);
    fetchacc=data['data'];
    drp_list=[];
    console.log('fetchacc Data:', fetchacc);
    for (let rec of fetchacc )
    {
    acc_name= rec['Account_Name'];
     console.log('acc_name :', acc_name);
     drp_list.push(acc_name);
    }
    var docfrag = document.createDocumentFragment();
     
    console.log('Drp list :'+drp_list );
    for (let op of drp_list)
    {
     docfrag.appendChild(new Option(op, op));
        
    }
    var select = document.getElementById("test");
select.appendChild(docfrag);
    
});
});
		//	resolve(data);
  }
 ZOHO.embeddedApp.on("PageLoad",function(data)
   {
  ZOHO.CRM.UI.Resize({height:"20px",width:"40%"}).then(function(data){
	console.log(data);
 
}); 
console.log('entity data',data);
    EntityId=data.EntityId[0];
	console.log('EntityId ',EntityId);
acc= title();
console.log('Lead Data:', acc);

   


   })
   ZOHO.embeddedApp.init();
   </script>
	</body>
	


</html>