<?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_intersect_key($licenseData, array_flip(array(
		"expiredOn", "expiredOnF", "isExpired",
		"plan", "isDemo", "isTrial", "isFree", "isPro", "isStandard", "isEnterprise",
		"maxUsers", "maxTrans", "hasEcommerce", "hasProject", "hasSalesRep"
	)));
	$licData["daysLeft"] = $daysLeft;
	// "maxTrans" => !empty($licenseData['maxTrans']) ? $licenseData['maxTrans'] : 0,

	$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');
}

?><!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Output Books</title>
		<base href="<?php echo site_url(); ?>">
		<meta name="robots" content="noindex" />
		<meta name="viewport" content="width=device-width,initial-scale=1" />
		<link rel="icon" type="image/x-icon" href="favicon.ico" />

		<link href="<?php echo $base_url; ?>assets/semantic/semantic.min.css" rel="stylesheet"/>
		<link href="<?php echo $base_url; ?>assets/semantic/calendar/calendar.min.css" rel="stylesheet"/>
		<!-- link href="<?php echo $base_url; ?>assets/app/styles.bundle.css?v=<?php echo $version ?>" rel="stylesheet"/ -->
		<link href="<?php echo $base_url; ?>assets/js/jqueryui/jquery-ui.min.css" rel="stylesheet"/>
		<link href="<?php echo $base_url; ?>assets/css/main.css<?php echo $q ?>" rel="stylesheet"/>
		<link href="<?php echo $base_url; ?>assets/css/extra_colors.css<?php echo $q ?>" rel="stylesheet"/>
		<link href="<?php echo $base_url; ?>assets/css/site.css<?php echo $q ?>" rel="stylesheet"/>

		<script src="<?php echo $base_url; ?>assets/js/jquery/jquery-1.11.0.min.js"></script>
		<script src="<?php echo $base_url; ?>assets/js/jquery/jquery.plugin.min.js"></script>
		<script src="<?php echo $base_url; ?>assets/js/jqueryui/jquery-ui.min.js"></script>
		<script src="<?php echo $base_url; ?>assets/semantic/semantic.js"></script>
		<script src="<?php echo $base_url; ?>assets/semantic/calendar/calendar.min.js"></script>
		<script src="<?php echo $base_url; ?>assets/js/dateentry/jquery.dateentry.min.js"></script>
		<script src="<?php echo $base_url; ?>assets/js/datetimeentry/jquery.datetimeentry.js"></script>
		<script src="<?php echo $base_url; ?>assets/google/charts/loader.js"></script>
		<script src="<?php echo $base_url; ?>assets/js/tinycolor.js"></script>
		<!-- <script src="<?php echo $base_url; ?>assets/js/websocket-printer.js"></script> -->
		<script src="<?php echo $base_url; ?>assets/js/dateformat<?php echo $min; ?>.js<?php echo $q; ?>"></script>
<?php if (!empty($socket_url)) { ?>
		<script src="<?php echo $base_url; ?>assets/js/sockjs.min.js"></script>
<?php }
		$salModCodes = get_company('salModCodes');
		$mod_codes = explode(',', $salModCodes ? $salModCodes : "");//Item master ecom tab load issue due to summernote js not loaded
		if(in_array('EC', $mod_codes)){
?>
		<script src="<?php echo $base_url; ?>assets/js/summernote-lite.min.js"></script>
<?php
		}
?>		<script type="text/javascript">
			// $.fn.modal.settings.className.scrolling = "";

			window.OBooks = window.OBooks || {};

			console.log("%cOutput Books v<?php echo $version . " - " . OB_RELEASE_DATE; ?>", "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;
	echo 'console.log(\'%c' . site_url() . $style;
}
?>			(function(_OBooks){
				_OBooks.version = "<?php echo OB_VERSION; ?>";

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

				if (_OBooks.version != localStorage.getItem('ob_version'))
				{
					function removeAllExcept() {
						let valuesToKeep = {};

						// Loop through all keys in localStorage
						for (let i = 0; i < localStorage.length; i++) {
							let key = localStorage.key(i);

							// Keep keys that are in the exceptKeys list or contain '*Columns'
							if (key.includes('Columns')) {
								valuesToKeep[key] = localStorage.getItem(key);
							}
						}

						// Clear all localStorage items
						localStorage.clear();

						// Restore the kept keys
						Object.keys(valuesToKeep).forEach(key => {
							localStorage.setItem(key, valuesToKeep[key]);
						});
					}
					// Example usage:
					removeAllExcept();

					localStorage.setItem('ob_version', _OBooks.version);
					// Comment this line to delete & sync local indexed db on version update.
					localStorage.setItem('obiDBv', _OBooks.iDBv);
				}
				_OBooks.mainbId = "<?php echo get_company('mainbId'); ?>";
				_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, "isDev", {value:<?php echo ENVIRONMENT == 'development' ? "true" : "false"; ?>,writable:false});
				Object.defineProperty(_OBooks, "isCloud", {value:<?php echo isCloud() ? "true" : "false" ; ?>,writable:false});
				Object.defineProperty(_OBooks, "isAppOB", {value:<?php echo defined("APP_EXP") ? "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 isset($currFraction) ? $currFraction : (!empty($currency_symbol[$currency]['fraction']) ? $currency_symbol[$currency]['fraction'] : 2);  ?>-0);
				_OBooks.rateFraction = (<?php echo isset($rateFraction) ? $rateFraction : 2; ?>-0);
				_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);
			function resizeIframe(obj) {obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';}
<?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);
		}
	}
?>		</script>

		<script src="<?php echo $base_url; ?>assets/js/helper<?php echo $min; ?>.js<?php echo $q; ?>"></script>
		<script src="<?php echo $base_url; ?>assets/js/gridify<?php echo $min; ?>.js<?php echo $q; ?>"></script>
		<script src="<?php echo $base_url; ?>assets/js/omessage.js<?php echo $q; ?>"></script>
<?php
		if ($is_logged_in && (strpos($this->uri->uri_string() , "location/trackLocation") !== false))
		{
?>			<script src="<?php echo $base_url; ?>assets/js/OpenLayers.js"></script>
			<link href="<?php echo $base_url; ?>assets/css/leaflet.css<?php echo $q ?>" rel="stylesheet" />
			<script src="<?php echo $base_url; ?>assets/js/leaflet.js"></script>
		<!--	<link href="<?php echo $base_url; ?>assets/css/leaflet-routing-machine.css<?php echo $q ?>" rel="stylesheet"/> -->
		<!--	<script src="<?php echo $base_url; ?>assets/js/AnimatedMarker.js"></script> -->
			<script src="<?php echo $base_url; ?>assets/js/snakeAnim.js"></script>
			<script src="<?php echo $base_url; ?>assets/js/route.js"></script>
<?php
		}
?>
	</head>
	<body class="">
		<div class="ui segment" id="loader" style="display:none;">
			<div class="ui active dimmer">
				<div class="ui massive text loader">Loading</div>
			</div><p></p><p></p><p></p>
		</div>
