<?php
session_start();
ob_start();
require_once("./connection/setting_jaldhi_class.php");
$info = new clsSettings();
$connect = $info->funConnect();

$urlToken = $_REQUEST['urlToken'];	
//$urlToken = 'NnVYMVU5ZDc4ZTRnTnhJY0h1K1lxdz09OjrXPTzuUIL33wERd2xY2MG5';	

$url = 'https://dev.arkratos.com/api/logisticAndShippingLogin';
$data = array('userToken'=>$urlToken);
$postdata = json_encode($data);


$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$reportresult = curl_exec($ch);
if(!$reportresult){die("Connection Failure");}
curl_close($ch);
//print_r($reportresult);die;
$resultdata = json_decode($reportresult, true);
$loginid = '';
if($resultdata['success']== 'TRUE'):
    //echo $resultdata['success'];die;
    $mcompanyid = '';
    $sql="SELECT * FROM main_company WHERE MCOMPANYID='".$resultdata['data']['client_id']."' ";
    $result=mysql_query($sql) or die(mysql_error());
	$count=mysql_num_rows($result);
	
	if($count==1):
	    $rows = mysql_fetch_assoc($result);
		$mcompanyid = $rows['MCOMPANYID'];
	    $sql1 = "update main_company set COMPANY_NAME='".addslashes(htmlentities(strip_tags($resultdata['data']['client_name'])))."', ADDRESS='".addslashes(htmlentities(strip_tags($resultdata['data']['client_address'])))."', EMAIL_ID='".$resultdata['data']['user_email']."' where MCOMPANYID='".$rows['MCOMPANYID']."'";
		mysql_query($sql1) or die("Sorry, Request can't be fulfilled. Something went wrong. Please contact administrator.");
		mysql_query("commit");
	else:
		$sql1 = "insert into main_company (MCOMPANYID, COMPANY_NAME, ADDRESS, EMAIL_ID) values('".$resultdata['data']['client_id']."', '".addslashes(htmlentities(strip_tags($resultdata['data']['client_name'])))."', '".addslashes(htmlentities(strip_tags($resultdata['data']['client_address'])))."', '".$resultdata['data']['user_email']."')";
		mysql_query($sql1) or die("Sorry, Request can't be fulfilled. Something went wrong. Please contact administrator.");
		mysql_query("commit");
		$sql2 = "select max(MCOMPANYID) as maxid from main_company";
		$result2 = mysql_query($sql2);
		$rows2 = mysql_fetch_assoc($result2);
		$mcompanyid = $rows2['maxid'];
	endif;
	
	if($mcompanyid!=''):
		$loginid = '';
	    $sql="SELECT * FROM login WHERE MCOMPANYID='".$mcompanyid."' and EMAILID='".$resultdata['data']['user_email']."' ";
		$result=mysql_query($sql) or die(mysql_error());
		$count=mysql_num_rows($result);
		
		if($count==1):
			$rows = mysql_fetch_assoc($result);
			$loginid = $rows['LOGINID'];
			$sql1 = "update login set CONTACT_PERSON='".addslashes(htmlentities(strip_tags($resultdata['data']['user_name'])))."', ADDRESS='".addslashes(htmlentities(strip_tags($resultdata['data']['client_address'])))."', EMAILID='".$resultdata['data']['user_email']."', USER_TYPE='internal_user', STATUS=1, USERNAME='".$resultdata['data']['user_email']."', LOGIN_STATUS='ONLINE'  where LOGINID='".$rows['LOGINID']."'";
			mysql_query($sql1) or die("Sorry, Request can't be fulfilled. Something went wrong. Please contact administrator1.");
			mysql_query("commit");
		else:
			$sql1 = "insert into login (MCOMPANYID, CONTACT_PERSON, ADDRESS, EMAILID, USER_TYPE, STATUS, USERNAME, LOGIN_STATUS) values('".$mcompanyid."', '".addslashes(htmlentities(strip_tags($resultdata['data']['user_name'])))."', '".addslashes(htmlentities(strip_tags($resultdata['data']['client_address'])))."', '".$resultdata['data']['user_email']."', 'internal_user', 1, '".$resultdata['data']['user_email']."', 'ONLINE')";
			mysql_query($sql1) or die("Sorry, Request can't be fulfilled. Something went wrong. Please contact administrator1.");
			mysql_query("commit");
			$sql2 = "select max(LOGINID) as maxid from login";
			$result2 = mysql_query($sql2);
			$rows2 = mysql_fetch_assoc($result2);
			$loginid = $rows2['maxid'];
		endif;
		//echo $loginid;die;
		if($loginid!='' && $mcompanyid!=''):
			$del_query = "delete FROM user_rights WHERE LOGINID=".$loginid;
			mysql_query($del_query) or die($del_query);	
			mysql_query("commit");
			
			for($i=1;$i<=19;$i++)
			{
				$link_auth = '1,2,3,4';
				if($link_auth != "")
				{
					for($k=1;$k<=19;$k++)
					{
						$sql3 = "INSERT into user_rights (LOGINID,MODULEID,LINKID,LINK_AUTH) values ('".$loginid."','".$k."','".$i."','".$link_auth."')";
						mysql_query($sql3)  or die("Sorry, Request can't be fulfilled. Something went wrong. Please contact administrator2.");
						mysql_query("commit");
					}
				}
			}
			
			$uid  = $_SESSION['uid'] = $loginid;
			$user_name = $_SESSION['username']  = $resultdata['data']['user_email'];
			$user_display = $_SESSION['display'] = addslashes(htmlentities(strip_tags($resultdata['data']['user_name'])));
			$_SESSION['gender'] = 1;
			$_SESSION['iutype'] = 'internal_user';
			$_SESSION['company']  = $mcompanyid;
			
			header("Location:../internal-user/index.php");
			exit;
		endif;
	endif;
endif;
ob_end_flush();
?>