<?php
/**
 * Filename: update.button.php
 * Created : EzLinkTools.Com <marcus@blogprofitnetwork.com> (https://EzLinkTools.Com)
 * Copyright 2020 - EzLinkTools.Com <marcus@blogprofitnetwork.com> (https://EzLinkTools.Com)
 */
defined('_IN_MAIN_MODULE') or die ('ACCESS DENIED');

include_once ('pages/header.part.php');

$buttonid		= intval($_GET['id']);
$button			= $buttonVoodooDatabase->getButtonById($buttonid);
$buttonOptions	= get_option($buttonVoodooDatabase->tableButtonOptions .'_'. $buttonid);
$groupid		= $button['groupid'];
$groupOptions	= get_option($buttonVoodooDatabase->tableGroupOptions .'_'. $groupid);

if (0 == count($button)) {
	echo '<script type="text/javascript">window.location.href="'. admin_url('admin.php?page='. BUTTONVOODOO_CODE) .'";</script>';
}
?>

	<div class="ui-tabs">
		<ul class="ui-tabs-nav">
			<li><a href="#manage">Update <?php echo $button['title']; ?></a></li>
		</ul>



		<!-- manage -->
		<div id="manage">

			<!-- holder - addnew -->
			<div id="holder-addnew">
				<form name="addNewButtonForm" id="addNewButtonForm" method="POST" action="">

					<div class="row cell">
						<h3>Update <?php echo $button['title']; ?></h3> <hr />
					</div>


					<div class="row cells2">
						<div class="cell">
							<div class="input-control text full-size">
								<label for="button-title">Title</label>
								<input type="text" name="button-title" id="button-title" placeholder="Enter title" value="<?php echo $button['title']; ?>" />
							</div>
						</div>
					</div>

					<div class="row cells2">
						<div class="cell">
							<div class="input-control text full-size">
								<label for="button-url">URL</label>
								<input type="text" name="button-url" id="button-url" placeholder="Enter URL" value="<?php echo $button['url']; ?>" />
							</div>
						</div>
					</div>

					<div class="row cells2">
						<div class="cell">
							<input type="hidden" name="button-icon" id="button-icon" value="<?php echo $button['icon']; ?>" />

							<button name="cmdUploadIcon" id="cmdUploadIcon" class="button button-secondary">
								<span class="dashicons dashicons-upload"></span> Upload Icon
							</button>

							<div id="holder-icon"><img src="<?php echo $button['icon']; ?>" width="150: height=150" /></div>

						</div>
					</div>

					<div class="row cells2">
						<div class="cell">
							<div>
								<label for="button-icon-enable" class="input-control checkbox adjust-top">
									<input type="checkbox" name="button-icon-enable" id="button-icon-enable" <?php echo ($buttonOptions['icon']['enable'] == BUTTONVOODOO_ENABLED) ? 'checked="checked"' : ''; ?> />
									<span class="check"></span>
									<span class="caption">Display Icon?</span>
								</label>
							</div>
						</div>
					</div>

					<div class="row cells2">
						<div class="cell">
							<div class="input-control select full-size">
								<label for="button-icon-location">Display Icon Where</label>
								<select name="button-icon-location" id="button-icon-location" style="height:38px;">
									<option value="left" <?php selected($buttonOptions['icon']['location'], 'left'); ?>>Left</option>
									<option value="right" <?php selected($buttonOptions['icon']['location'], 'right'); ?>>Right</option>
								</select>
							</div>
						</div>
					</div>

					<div class="row cells2">
						<div class="cell">
							<div class="input-control select full-size">
								<label for="button-icon-type">Display Icon As</label>
								<select name="button-icon-type" id="button-icon-type" style="height:38px;">
									<option value="square" <?php selected($buttonOptions['icon']['type'], 'square'); ?>>Square</option>
									<option value="round" <?php selected($buttonOptions['icon']['type'], 'round'); ?>>Round</option>
								</select>
							</div>
						</div>
					</div>


					<div class="row cells2">
						<div class="cell">
							<div>
								<label for="button-button-bg-override" class="input-control checkbox adjust-top">
									<input type="checkbox" name="button-button-bg-override" id="button-button-bg-override" <?php echo ($buttonOptions['button']['bg']['override'] == BUTTONVOODOO_ENABLED) ? 'checked="checked"' : ''; ?> />
									<span class="check"></span>
									<span class="caption">Override Background Color ( Default: <?php echo $groupOptions['button']['bg']; ?>)</span>
								</label>

								<div id="holder-button-button-bg-override" class="input-control text full-size" style="margin-top:5px; display:none;">
									<input type="text" name="button-button-bg" id="button-button-bg" placeholder="Enter background color" value="<?php echo $buttonOptions['button']['bg']['color']; ?>" />
								</div>
							</div>
						</div>
					</div>


					<div class="row cells2">
						<div class="cell">
							<div>
								<label for="button-button-color-override" class="input-control checkbox adjust-top">
									<input type="checkbox" name="button-button-color-override" id="button-button-color-override" <?php echo ($buttonOptions['button']['color']['override'] == BUTTONVOODOO_ENABLED) ? 'checked="checked"' : ''; ?> />
									<span class="check"></span>
									<span class="caption">Override Background Text Color( Default: <?php echo $groupOptions['button']['color']; ?>)</span>
								</label>

								<div id="holder-button-button-color-override" class="input-control text full-size" style="margin-top:5px; display:none;">
									<input type="text" name="button-button-color" id="button-button-color" placeholder="Enter text color" value="<?php echo $buttonOptions['button']['color']['color']; ?>" />
								</div>
							</div>
						</div>
					</div>


					<div class="row cell">
						<button name="cmdUpdateButton" id="cmdUpdateButton" class="button button-primary text-bold">
							<span class="icon mif-checkmark"></span> Update <?php echo $button['title']; ?>
						</button>
						<a href="<?php echo admin_url('admin.php?page='. BUTTONVOODOO_CODE .'&action=manage-buttons&id='. $groupid); ?>" class="button button-secondary fg-red text-bold">
							<span class="icon mif-cross"></span> Cancel
						</a>

						<div id="dialog-manage" style="margin-left:10px;"></div>
					</div>
				</form>
			</div>
			<!-- // holder - addnew -->

		</div>
		<!-- // manage -->
	</div>


	<script type="text/javascript">
		var manageJs = jQuery.noConflict();
		manageJs(function($) {


			//* manage - upload icon
			$('#cmdUploadIcon').click(function() {
				tb_show('Upload Icon', 'media-upload.php?type=image&TB_iframe=true&post_id=0', false);

				window.restore_send_to_editor = window.send_to_editor;
				window.send_to_editor = function(html) {
					var imgUrl = $('<div>'+ html +'</div>').find('img').attr('src');
					$('#button-icon').val(imgUrl);
					$('#holder-icon').fadeIn();
					$('#holder-icon > img').attr('src', imgUrl).attr('width', 150).attr('height', 150);

					tb_remove();
				}
				return false;
			});


			//* manage - background override
			$('#button-button-bg-override').on('change', function() {
				var $this	= $(this),
					$holder	= $('#holder-button-button-bg-override');

				if ($this.is(':checked')) {
					$holder.fadeIn();
				}
				else {
					$holder.fadeOut();
				}

				return false;
			});

			if ($('#button-button-bg-override').is(':checked')) {
				$('#holder-button-button-bg-override').fadeIn();
			}
			else {
				$('#holder-button-button-bg-override').fadeOut();
			}


			//* manage - color override
			$('#button-button-color-override').on('change', function() {
				var $this	= $(this),
					$holder	= $('#holder-button-button-color-override');

				if ($this.is(':checked')) {
					$holder.fadeIn();
				}
				else {
					$holder.fadeOut();
				}

				return false;
			});

			if ($('#button-button-color-override').is(':checked')) {
				$('#holder-button-button-color-override').fadeIn();
			}
			else {
				$('#holder-button-button-color-override').fadeOut();
			}

			//* manage -submit
			$('#cmdUpdateButton').click(function() {
				var $title			= $('#button-title'),
					$url			= $('#button-url'),

					$icon			= $('#button-icon'),
					$iconEnable		= $('#button-icon-enable'),
					$iconLocation	= $('#button-icon-location'),
					$iconType		= $('#button-icon-type'),

					$bgOverride			= $('#button-button-bg-override'),
					$bgOverrideColor	= $('#button-button-bg'),

					$colorOverride		= $('#button-button-color-override'),
					$colorOVerrideColor	= $('#button-button-color'),

					$dialog			= $('#dialog-manage'),

					$data			= {
						action			: '<?php echo BUTTONVOODOO_CODE; ?>-update-button',
						id				: <?php echo $buttonid; ?>,
						title			: $title.val(),
						url				: $url.val(),

						icon			: $icon.val(),
						iconEnable		: $iconEnable.is(':checked'),
						iconLocation	: $iconLocation.val(),
						iconType		: $iconType.val(),

						bgOverride			: $bgOverride.is(':checked'),
						bgOverrideColor		: $bgOverrideColor.val(),

						colorOverride		: $colorOverride.is(':checked'),
						colorOverrideColor	: $colorOVerrideColor.val()
					};

				$dialog.empty().append('<span class="icon mif-spinner2 mif-ani-spin"></span> Processing data... Please wait!');
				$.post('<?php echo admin_url('admin-ajax.php'); ?>', $data, function(res) {
					if (res != 'done') {
						$dialog.empty().append('<span class="icon mif-warning"></span> There was problem processing your data. Please try again!');
					}
					else {
						$dialog.empty().append('<span class="icon mif-checkmark"></span> Data has been successfully processed.');
						window.location.href = '<?php echo admin_url('admin.php?page='. BUTTONVOODOO_CODE .'&action=manage-buttons&id='. $groupid); ?>';
					}
				});

				return false;
			});


			//* manage - update status
			$('#manage-table').on('click', '*[id^=cmdUpdateButtonStatus]', function() {
				var $this	= $(this),
					$id		= $this.attr('rel'),
					$span	= $this.children('span'),

					$data	= {
						action	: '<?php echo BUTTONVOODOO_CODE; ?>-update-button-status',
						id		: $id
					};

				$span.removeClass('dashicons dashicons-visibility').addClass('icon mif-spinner2 mif-ani-spin');
				$.post('<?php echo admin_url('admin-ajax.php'); ?>', $data, function(res) {
					$span.removeClass('icon mif-spinner2 mif-ani-spin').addClass('dashicons dashicons-visibility');
					if ($span.hasClass('fg-green')) {
						$span.removeClass('fg-green').addClass('fg-red');
					}
					else {
						$span.removeClass('fg-red').addClass('fg-green');
					}
				});

				return false;
			});


			//* manage - reset stat
			$('#manage-table').on('click', '*[id^=cmdResetButtonStat]', function() {
				var $this		= $(this),
					$id			= $this.attr('rel'),
					$span		= $this.children('span'),

					$clicks		= $('#holder-clicks-'+ $id),

					$data		= {
						action	: '<?php echo BUTTONVOODOO_CODE; ?>-reset-button-stat',
						id		: $id
					},

					q			= confirm('Are you sure you want to reset this item?');

				if (q) {
					$span.removeClass('dashicons dashicons-update').addClass('icon mif-spinner2 mif-ani-spin');
					$.post('<?php echo admin_url('admin-ajax.php'); ?>', $data, function(res) {
						$span.removeClass('icon mif-spinner2 mif-ani-spin').addClass('dashicons dashicons-update');

						$clicks.empty().append(0);
					});
				}

				return false;
			});


			//* manage - delete
			$('#manage-table').on('click', '*[id^=cmdDeleteButton]', function() {
				var $this   = $(this),
					$id     = $this.attr('rel'),
					$span	= $this.children('span'),
					$tr     = $this.closest('tr'),

					$data   = {
						action  : '<?php echo BUTTONVOODOO_CODE; ?>-delete-button',
						id      : $id
					},

					q       = confirm('Are you sure you want to delete this item?');

				if (q) {
					$span.removeClass('dashicons dashicons-trash').addClass('icon mif-spinner2 mif-ani-spin');
					$.post('<?php echo admin_url('admin-ajax.php'); ?>', $data, function() {
						$span.removeClass('icon mif-spinner2 mif-ani-spin').addClass('icon mif-checkmark');
						$tr.slideUp();
					});
				}

				return false;
			});
		});
	</script>

<?php
include_once ('pages/footer.part.php');
