<?php
/*------------------------------------------------------------------------
# com_guru
# ------------------------------------------------------------------------
# author    iJoomla
# copyright Copyright (C) 2013 ijoomla.com. All Rights Reserved.
# @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# Websites: http://www.ijoomla.com
# Technical Support:  Forum - http://www.ijoomla.com/forum/index/
-------------------------------------------------------------------------*/


defined('_JEXEC') or die("Go away.");
$app = JFactory::getApplication();
$jinput = $app->input;
$doc = JFactory::getDocument();
$k = 0;
$task = $jinput->get("task", '');
$n = count($this->programs);
$programs = $this->programs;

require JPATH_ROOT . '/components/com_guru/helpers/helper.php';
$configs = guruHelper::getConfig();
$currency = $configs->get('currency');
$currencypos = $configs->get('currencypos');
$character = trim(JText::_("GURU_CURRENCY_" . $currency));
$tmpl = $jinput->get("tmpl", "");

if (trim($tmpl) != "") {
	$jinput->set("tmpl", "component");
}
$listDirn = "asc";
$listOrder = "ordering";
$saveOrderingUrl = 'index.php?option=com_guru&controller=guruPrograms&task=saveOrderAjax&tmpl=component';
JHtml::_('sortablelist.sortable', 'articleList', 'adminForm', strtolower($listDirn), $saveOrderingUrl);
JHtml::_('dropdown.init');

$data_post = $jinput->post->getArray();
$search_text = $app->getUserStateFromRequest('com_guru.list_program.search_text', "search_text", "", 'string');
$catid = $app->getUserStateFromRequest('com_guru.list_program.catid', "catid", "-1", 'int');;
$course_publ_status = $app->getUserStateFromRequest('com_guru.list_program.course_publ_status', 'course_publ_status', 'YN');
?>

<script type="text/javascript" id="load-core-js">
	// When get this page content through Ajax Request all js files that supposed to be loaded by controller, are not loaded, so we have to load them with javascript only in case there are not already been loaded 
	var element = document.getElementById('load-core-js');
	if (typeof Joomla == 'undefined') {
		document.write('<script src="components/com_guru/js/core.js"><\/script>');
	}
	element.parentNode.removeChild(element);
</script>

<script language="javascript" type="text/javascript">
	Joomla.submitbutton = function(pressbutton) {
		//function submitbutton(pressbutton) {
		var form = document.adminForm;
		if (pressbutton == 'duplicate') {
			if (form['boxchecked'].value == 0) {
				alert("<?php echo JText::_("GURU_PROGRAM_MAKESEL_JAVAMSG"); ?>");
			} else {
				Joomla.submitform(pressbutton);
			}
		} else if (pressbutton == 'remove') {
			var temp = confirm("<?php echo JText::_("GURU_REMOVE_ADMIN_COURSES"); ?>");
			if (temp == true) {
				Joomla.submitform(pressbutton);
			} else {
				return false;
			}
		} else {
			Joomla.submitform(pressbutton);
		}
	}

	function utf8_encode(argString) {
		var string = (argString + '');
		var utftext = "",
			start, end, stringl = 0;

		start = end = 0;
		stringl = string.length;
		for (var n = 0; n < stringl; n++) {
			var c1 = string.charCodeAt(n);
			var enc = null;
			if (c1 < 128) {
				end++;
			} else if (c1 > 127 && c1 < 2048) {
				enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
			} else {
				enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
			}
			if (enc !== null) {
				if (end > start) {
					utftext += string.slice(start, end);
				}
				utftext += enc;
				start = end = n + 1;
			}
		}
		if (end > start) {
			utftext += string.slice(start, stringl);
		}
		return utftext;
	}

	function selectCourseID(prod_id, prod_name) {
		var id_selected = parent.document.getElementById("id_selected").value;
		parent.document.getElementById('course_name_text_' + id_selected).innerHTML = prod_name;
		parent.document.getElementById('course_id' + id_selected).value = parseInt(prod_id);

		var str = parent.document.getElementById('courses_ids_code_generate').value;
		var promo_code = '|' + id_selected;
		var courses_ids_code_generate = parent.document.getElementById('courses_ids_code_generate');
		parent.document.getElementById('courses_ids_code_generate').value = (str.indexOf(promo_code) === -1) ? courses_ids_code_generate.value + "|" + id_selected : courses_ids_code_generate.value;

		showPlanForCourseInclude(id_selected, prod_id);

	}

	function getCourseCost(url, generate_id) {
		jQuery.ajax({
			url: url,
			method: 'get',
			asynchronous: 'true',
			success: function(html) {
				old_total = window.parent.document.getElementById('amount_hidden').value;
				new_total = parseFloat(old_total) + parseFloat(html);
				if (!isNaN(new_total)) {
					var character = "<?php //echo $character; 
										?>";
					parent.document.getElementById('total').value = parseFloat(new_total);
					parent.document.getElementById('amount_hidden').value = parseFloat(new_total).toFixed(2);
					parent.document.getElementById('total_column').innerHTML = character + " " + parseFloat(new_total).toFixed(2);
					parent.document.getElementById('amount').innerHTML = character + " " + parseFloat(new_total).toFixed(2);
					var promo_code = window.parent.document.getElementById('promocode').value;

					window.parent.changeAmount(generate_id);
					window.parent.setPromo(promo_code);
					window.parent.guruModal && window.parent.guruModal.closeAll();
				}
			},
		});
	}

	function showPlanForCourseInclude(generate_id, course_id) {
		var subscr_type = window.parent.document.getElementById('subscr_type_' + generate_id);
		var licences = window.parent.document.getElementById('licences_' + generate_id);

		if (subscr_type.style) {
			if (subscr_type.style.display == 'none') {
				subscr_type.style.display = '';
			}
		}
		if (licences.style) {
			if (licences.style.display == 'none') {
				licences.style.display = '';
			}
		}

		var url = "index.php?option=com_guru&controller=guruPrograms&tmpl=component&format=raw&task=getplans&course_id=" + course_id + "&gen_id=" + generate_id;

		jQuery.ajax({
			url: url,
			method: 'get',
			asynchronous: 'true',
			success: function(html) {
				nd = window.parent.document.getElementById('div_licences_select_' + generate_id);
				nd.innerHTML = html;
				var licences_select = parent.document.adminForm.licences_select;
				var licences_selectLength = (licences_select.length == 1) ? 1 : licences_select.length;
				parent.document.getElementById("nr_licenses").value = licences_selectLength;
				//set new value after upload a new course
				var url = "index.php?option=com_guru&controller=guruPrograms&task=getcoursecost&tmpl=component&format=raw&course_id=" + course_id;
				getCourseCost(url, generate_id);
			},
		});
	}
</script>
<?php if ($task == "selectCourse") {
} else {
	$font = "";
	$input = "";
}
?>
<div id="editcell">


	<form action="<?php echo JRoute::_('index.php?option=com_guru&controller=guruPrograms') ?>" id="adminForm" name="adminForm" method="post" style=" <?php //echo $font; 
																															?> ">
		<table style="width: 78%;" cellpadding="5" cellspacing="5" bgcolor="#FFFFFF">
			<tr>
				<td>
					<input style=" <?php echo $input; ?>" type="text" name="search_text" value="<?php echo htmlspecialchars($search_text) ?>" />
					<input class="btn btn-primary" type="submit" name="submit_search" value="<?php echo JText::_('GURU_SEARCHTXT'); ?>" />
				</td>
				<td>
					<?php
					$lists['treecateg'] = $this->list_all2(1, "catid", 0, $catid); ?>
				</td>
				<?php
				if ($task != "selectCourse") {
				?>
					<td>
						<select onchange="document.adminForm.submit()" name="course_publ_status">
							<?php
							$pb = $course_publ_status;
							?>
							<option <?php if ($pb == 'YN') {
										echo "selected='selected'";
									} ?> value="YN"><?php echo JText::_("GURU_ALLYN"); ?></option>
							<option <?php if ($pb == 'Y') {
										echo "selected='selected'";
									} ?> value="Y"><?php echo JText::_("GURU_PUBLISHED"); ?></option>
							<option <?php if ($pb == 'N') {
										echo "selected='selected'";
									} ?> value="N"><?php echo JText::_("GURU_UNPUBLISHED"); ?></option>
						</select>
					</td>
				<?php
				}
				?>
			<tr>
		</table>

		<div class="clearfix"></div>
		<?php
		if ($task != "selectCourse") {
		?>
			<div class="well well-minimized">
				<?php echo JText::_("GURU_COURSES_SETTINGS_DESCRIPTION"); ?>
			</div>
		<?php } ?>

		<table class="table table-bordered table-striped adminlist" id="articleList" >
			<thead>

				<tr>
					<?php
					if ($task != "selectCourse") {
					?>

						<th>
							<?php echo JHtml::_('grid.sort', '<i class="icon-menu-2"></i>', 'a.ordering', $listDirn, $listOrder, null, 'asc', 'JGRID_HEADING_ORDERING'); ?>
						</th>
						<th width="5">
							<input type="checkbox" onclick="Joomla.checkAll(this)" name="toggle" value="" />
							<span class="lbl"></span>
						</th>
					<?php
					}
					?>
					<th width="20">
						<?php echo JText::_('GURU_ID'); ?>
					</th>
					<th>
						<?php echo JText::_('GURU_COURSE_TREE'); ?>
					</th>

					<?php
					if ($task != "selectCourse") {
					?>
						<th> <?php echo JText::_('GURU_EDIT_INFO'); ?></th>
						<th>
							<?php echo JText::_('GURU_PROGRAM_CAT'); ?>
						</th>
						<th>
							<?php echo JText::_('GURU_TREECUSTOMERS'); ?>
						</th>
					<?php
					} else {
					?>
						<th>
							<?php echo JText::_('GURU_PROGRAM_CAT'); ?>
						</th>
					<?php
					}

					if ($task != "selectCourse") {
					?>

						<th>
							<?php echo JText::_('GURU_PUBLISHED'); ?>
						</th>
						<th>
							<?php echo JText::_('GURU_STATUS'); ?>
						</th>

					<?php
					}
					?>
				</tr>
			</thead>

			<tbody>

				<?php

				for ($i = 0; $i < $n; $i++) :
					$program = $this->programs[$i];
					$id = $program->id;
					$checked = JHTML::_('grid.id', $i, $id);
					if (isset($checked_array_expld["0"])) {
						$checked = $checked_array_expld["0"];
					}

					$link = JRoute::_("index.php?option=com_guru&controller=guruPrograms&task=edit&cid[]=" . $id);
					// $published = JHTML::_('grid.published', $program, $i);

					$tick = JURI::root() . 'administrator/components/com_guru/images/tick.png';
					$tick_x = JURI::root() . 'administrator/components/com_guru/images/publish_x.png';
					$img = $program->published ? $tick : $tick_x;
					$btnTask = $program->published ? 'unpublish' : 'publish';
					$action = $program->published ? JText::_('JLIB_HTML_UNPUBLISH_ITEM') : JText::_('JLIB_HTML_PUBLISH_ITEM');

					$published = '<a href="#" onclick="return Joomla.listItemTask(\'cb' . $i . '\',\'' . '' . $btnTask . '\')" title="' . $action . '">'
						. '<img src="'.$img.'" /></a>';

					// we find how many days has a program
					$howManyDays = guruAdminModelguruProgram::getpdays($id);
					$categoryObj = "";
					if ($program->catid > 0) {
						$categoryObj = guruAdminModelguruProgram::getProgramCategory($program->catid);
						$linkcat = JRoute::_("index.php?option=com_guru&controller=guruPcategs&task=edit&cid[]=" . $categoryObj->id);
						$link_cat_for_program = '<a class="a_guru" href="' . $linkcat . '" >' . $categoryObj->name . '</a>';
					} else {
						$link_cat_for_program = '<span class="muted">' . JText::_('GURU_CATEGORY_SEARCH') . '</span>';
					}

					if ($task == "selectCourse") {
						if ($categoryObj != "") {
							$link_cat_for_program = $categoryObj->name;
						}
					}

					$howManyStudCourse = guruAdminModelguruProgram::getStudentsNumber($id);
					$link_nb_stud = JRoute::_("index.php?option=com_guru&controller=guruPrograms&task=show&pid=" . $id);
					$link_stud_nb_of_course = '<a class="a_guru" href="' . $link_nb_stud . '" >' . $howManyStudCourse . '</a>';

				?>
					<tr class="row<?php echo $k; ?>">
						<?php
						if ($task != "selectCourse") {
						?>
							<td>
								<span class="sortable-handler active" style="cursor: move;">
									<i class="icon-menu"></i>
								</span>
								<input type="text" class="width-20 text-area-order " value="<?php echo $program->ordering; ?>" size="5" name="order[]" style="display:none;">
							</td>
						<?php } ?>
						<?php
						if ($task != "selectCourse") {
							echo "<td>" . $checked . "<span class=\"lbl\"></span></td>";
						}
						?>
						<td>
							<?php echo $id; ?>
						</td>
						<td>
							<?php
							if ($task == "selectCourse") {
							?>
								<a class="a_guru" onclick="selectCourseID('<?php echo $id; ?>', '<?php echo trim(str_replace("'", "&acute;", $program->name)); ?>')">
									<?php echo $program->name . " (" . $howManyDays->how_many . ")"; ?>
								</a>
							<?php
							} else {
							?>
								<a class="a_guru" href="index.php?option=com_guru&controller=guruDays&pid=<?php echo $id; ?>">
									<?php echo $program->name . " (" . $howManyDays->how_many . ")"; ?>
								</a>
							<?php
							}
							?>
						</td>
						<?php
						if ($task != "selectCourse") {
						?>
							<td>
								<a class="a_guru" href="<?php echo $link; ?>"><?php echo JText::_('edit'); ?></a>
							</td>
						<?php
						}
						?>
						<td>
							<?php echo $link_cat_for_program; ?>
						</td>

						<?php
						if ($task != "selectCourse") {
						?>
							<td align="center">
								<?php echo $link_stud_nb_of_course; ?>
							</td>
						<?php
						}

						if ($task != "selectCourse") {
						?>
							<td align="center">
								<?php echo $published; ?>
							</td>
							<td align="center">
								<?php
								if ($program->status == "0") { // in pending
								?>
									<a title="Unpublish Item" onclick="return Joomla.listItemTask('cb<?php echo $i; ?>','approve')" href="#">
										<img src="<?php echo JURI::root(); ?>administrator/components/com_guru/images/publish_x.png">
									</a>
								<?php
								} elseif ($program->status == "1") { // approved
								?>
									<a title="Unpublish Item" onclick="return Joomla.listItemTask('cb<?php echo $i; ?>','pending')" href="#">
										<img src="<?php echo JURI::root(); ?>administrator/components/com_guru/images/tick.png">
									</a>
								<?php
								}
								?>
							</td>

						<?php
						}
						?>
					</tr>


				<?php
					$k = 1 - $k;
				endfor;


				?>
			</tbody>


		</table>
		<?php
		if ($task == "selectCourse") {
		?>
			<style>
				.pagination {
					height: 0px !important;
				}
			</style>
		<?php
		}
		?>
		<div class="pagination-toolbar" style="display: flex; justify-content: space-between;">
			<div>
				<?php echo $this->pagination->getLimitBox(); ?>
			</div>
			<?php echo $this->pagination->getListFooter(); ?>
		</div>
		<input type="hidden" name="filter_order" value="<?php echo $listOrder; ?>" />
		<input type="hidden" name="filter_order_Dir" value="<?php echo $listDirn; ?>" />
		<?php echo JHtml::_('form.token'); ?>
		<input type="hidden" name="option" value="com_guru" />
		<input type="hidden" name="task" value="<?php echo $task; ?>" />
		<input type="hidden" name="boxchecked" value="0" />
		<input type="hidden" name="controller" value="guruPrograms" />

		<?php
		if ($task == "selectCourse") {
		?>
			<input type="hidden" name="tmpl" value="component" />
		<?php
		}
		?>
	</form>
</div>
