<?php defined('BASEPATH') OR exit('No direct script access allowed');

$base_url = base_url();
$version = OB_VERSION;

$min = '.min';
$min = '';

// $licKey = get_user_data("licenseKey", true);
$licenseData = get_user_data("licenseData", true);
$licData = null;
$hasSalesRep = false;
$isBranch = false;

if (!empty($licenseData))
{
	$daysLeft = getExpireDue($licenseData["expiredOn"]);

	$licData = array(
		"expiredOn" => $licenseData['expiredOn'],
		"expiredOnF" => $licenseData['expiredOnF'],
		"isExpired" => $licenseData['isExpired'],
		"plan" => $licenseData['plan'],
		"isDemo" => $licenseData['isDemo'],
		"isTrial" => $licenseData['isTrial'],
		"isFree" => $licenseData['isFree'],
		"isPro" => $licenseData['isPro'],
		"daysLeft" => $daysLeft,
		"isStandard" => $licenseData["isStandard"],
		"isEnterprise" => $licenseData["isEnterprise"]
	);

	$licData["isExpired"] = canSave() ? false : true;

	if (!empty($licenseData["isDemo"]) || !empty($licenseData["isTrial"]) || !empty($licenseData["isStandard"]) || !empty($licenseData["isEnterprise"]))
	{
		$hasSalesRep = $licenseData["hasSalesRep"];
		$isBranch = $licenseData["allowBranch"];
	}
	else if (!empty($licenseData["isPro"]) && $licenseData["allowBranch"])
	{
		$isBranch = $licenseData["allowBranch"];
	}
}
/*
if ($isBranch)
{
	$isBranch = sal_mod_active(BRANCH_MOD);
}
*/
$q = '?v=' . OB_VERSION;
if (ENVIRONMENT == 'development')
{
	$q .= '&t=' . time();
	$min = '';
}

$is_logged_in = $this->session->userdata('is_logged_in') ? true : false;
$is_activated = get_user_data("device_activated", true);
$licmanager = get_user_data("licmanager", true);
$licmanager = empty($licmanager) ? false : true;

$countryCode = null;
$cId = null;
$cName = "";
if ($is_logged_in)
{
	$countryCode = get_company("countryCode", true);
	$cId = get_company("cId", false);
	$cName = get_company("name");
}

if (!$countryCode)
{
	$countryCode = "IN";
}
$localE = "en-" . $countryCode;

$socket_url = $this->config->item("socket_url");
$fullSearch = $this->config->item("fullSearch");

$dateFormat = get_company("dateFormat");

if ($dateFormat)
{
	$dateFormats = array(
		"dd/mm/yyyy" => "dd/MM/yyyy",
		"mm/dd/yyyy" => "MM/dd/yyyy",
		"dd-mm-yyyy" => "dd-MM-yyyy",
		"dd-mmm-yyyy" => "dd-MMM-yyyy",
		"mmm dd, yyyy" => "MMM dd, yyyy"
	);
	$dateFormat = $dateFormats[$dateFormat];
}

if ($is_logged_in && !$licmanager) {
	$currency  = get_company("currency");
	$this->config->load('currency_symbols','true');
	$currency_symbol = $this->config->item('currency');
}
$q = '?v=' . OB_VERSION;


// <base href="< ? php echo site_url(); ? >">
echo '// OutputBooks-v' . $version . PHP_EOL;

?>
window.OBooks = window.OBooks || {};
console.log("%cOutput Books v<?php echo $version; ?>", "color:#FFF;padding:3px 6px;border-radius:10px 0px;background-color:#2185d0;font-size:18px;");
<?php
	if ($is_logged_in && ENVIRONMENT == 'development')
	{
		$style = '\', "color:#2185d0;font-size:12px;font-weight:bold;");';
		echo 'console.log(\'%c' . json_encode(array("c" => $cId, "u" => get_user_data("id"), "pU" => get_company("primUserid"), "e" => get_user_data("email"))) . $style . PHP_EOL;
		echo 'console.log(\'%c' . site_url() . $style . PHP_EOL;
	}
?>
(function(_OBooks){
	function loadJS(FILE_URL, async) {
		var scriptEle = document.createElement("script");

		scriptEle.setAttribute("src", FILE_URL);
		scriptEle.setAttribute("type", "text/javascript");
		scriptEle.setAttribute("async", async);

		document.body.appendChild(scriptEle);

		// success event
		scriptEle.addEventListener("load", function(){
			console.log("File loaded : ", FILE_URL);
		});

		// error event
		scriptEle.addEventListener("error", function(ev) {
			console.log("Error on loading file : ", FILE_URL, ev);
		});
	}
	function loadCSS(FILE_URL) {
		var scriptEle = document.createElement("link");

		scriptEle.setAttribute("href", FILE_URL);
		scriptEle.setAttribute("rel", "stylesheet");

		document.body.appendChild(scriptEle);
	}

	function resizeIframe(obj) {obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';}
<?php
	if (!empty($socket_url))
	{
		echo 'loadJS("' . $base_url . 'assets/js/sockjs.min.js", true);' . PHP_EOL;
	}

	$mod_codes = explode(',',get_company('salModCodes'));//Item master ecom tab load issue due to summernote js not loaded

	if(in_array('EC', $mod_codes))
	{
		echo 'loadJS("' . $base_url . 'assets/js/summernote-lite.min.js", true);' . PHP_EOL;
	}

	if ($is_logged_in && (strpos($this->uri->uri_string() , "location/trackLocation") !== false))
	{
		echo 'loadJS("' . $base_url . 'assets/js/OpenLayers.js", true);' . PHP_EOL;
		echo 'loadCSS("' . $base_url . 'assets/css/leaflet.css");' . PHP_EOL;
		echo 'loadJS("' . $base_url . 'assets/js/leaflet.js", true);' . PHP_EOL;
		// echo 'loadCSS("' . $base_url . 'assets/css/leaflet-routing-machine.css");' . PHP_EOL;
		// echo 'loadJS("' . $base_url . 'assets/js/AnimatedMarker.js", true);' . PHP_EOL;
		echo 'loadJS("' . $base_url . 'assets/js/snakeAnim.js", true);' . PHP_EOL;
		echo 'loadJS("' . $base_url . 'assets/js/route.js", true);' . PHP_EOL;
	}
?>
	_OBooks.version = "<?php echo OB_VERSION; ?>";

	_OBooks.iDBv = 4;	// Changing this version will force delete the indexedDB sync.

	if (_OBooks.version != _OBooks.Storage.get('version'))
	{
		_OBooks.Storage.clear();
		_OBooks.Storage.set('version', _OBooks.version);
	}

	_OBooks.base_url = "<?php echo base_url(); ?>";
	_OBooks.site_url = "<?php echo site_url(); ?>";
	_OBooks.socket_url = "<?php echo !empty($socket_url) ? $socket_url : ''; ?>";
	_OBooks.print_socket_url = "<?php echo !empty($print_socket_url) ? $print_socket_url : ''; ?>";
	_OBooks.time = <?php echo time(); ?>;
	Object.defineProperty(_OBooks, "isCloud", {value:<?php echo isCloud() ? "true" : "false" ; ?>,writable:false});
<?php
if ($is_logged_in && !$licmanager)
{
?>
	_OBooks.pageLimit = <?php echo $this->config->item('page_limit'); ?>;
	_OBooks.syncLimit = <?php echo $this->config->item('sync_limit'); ?>;
	// _OBooks.OutputObo = <?php echo $this->config->item('sync_limit'); ?>;
	
	_OBooks.pageLimitBatchStock = <?php echo $this->config->item('batch_stk_page_limit'); ?>;
	_OBooks.fyStart = <?php echo get_company("fyStart") ? get_company("fyStart") : 4; ?>;
	_OBooks.countryCode = "<?php echo $countryCode; ?>";
	_OBooks.localE = "<?php echo $localE; ?>";
	_OBooks.dateFormat = "<?php echo !empty($dateFormat) ? $dateFormat : 'dd/MM/y'; ?>";
	_OBooks.OutputSMS = <?php echo defined("OutputSMS") ? 1 : 0; ?>;
	_OBooks.OutputObo = <?php echo defined("OutputObo") ? 1 : 0; ?>;
	_OBooks.myl = <?php echo isMyl() ?>;
	// _OBooks.myl = 0;
	_OBooks.currFraction = (<?php echo !empty($currFraction) ? $currFraction : (!empty($currency_symbol[$currency]['fraction']) ? $currency_symbol[$currency]['fraction'] : 2);  ?>-0) || 2;
	_OBooks.rateFraction = (<?php echo !empty($rateFraction) ? $rateFraction : 2; ?>-0) || 2;
	_OBooks.currCode = "<?php echo !empty($currency) ? $currency : '' ?>";
	_OBooks.currSign = "<?php echo !empty($currency_symbol[$currency]['sign']) ? $currency_symbol[$currency]['sign'] : '' ?>";
	Object.defineProperty(_OBooks, "roleCode", {value:"<?php echo get_user_data('roleCode')?get_user_data('roleCode'):'A'; ?>",writable:false});
	// "BSICI"
	_OBooks.fullSearch = !!<?php echo empty($fullSearch) ? 0 : 1; ?>;
	_OBooks.hasSerialAttr = !!<?php echo get_company('hasSerialAttr') == true ? 1 : 0; ?>;
<?php
}

if (!empty($licData))
{
	$dbLog = getBackupLog();
	echo "_OBooks.lastBackup = " . (empty($dbLog) ? 'null' : json_encode($dbLog)) . ";";
?>
	Object.defineProperty(_OBooks, "isExpired", {value:<?php echo $licData['isExpired'] ? "true" : "false"; ?>,writable:false});
	Object.defineProperty(_OBooks, "isActivated", {value:<?php echo $is_activated ? "true" : "false"; ?>,writable:false});
	Object.defineProperty(_OBooks, "isTrial", {value:<?php echo $licData['isTrial'] ? "true" : "false"; ?>,writable:false});
<?php
	echo "_OBooks.licData = " . json_encode($licData) . ";";
}
?>
	Object.defineProperty(_OBooks, "issrep", {value:<?php echo $hasSalesRep ? "true" : "false"; ?>,writable:false});
	Object.defineProperty(_OBooks, "isbl", {value:<?php echo $isBranch ? "true" : "false"; ?>,writable:false});

})(window.OBooks);
<?php

if ($is_logged_in && $cId)
{
	$clientJs = FCPATH . "assets/js/client.js";
	if (@file_exists($clientJs))
	{
		require_once($clientJs);
	}

	$clientJs = FCPATH . "assets/js/" . $cId . "-client.js";
	if (@file_exists($clientJs))
	{
		require_once($clientJs);
	}
}


