<?php
/**
 * Debug HTML – Terminal UI Linux
 * Safe for WordPress Admin
 */

global $opt_themes;

if ( empty($opt_themes['debug_on']) ) {
    return;
}

if ( empty($data['title_GP']) || is_wp_error($data['title_GP']) ) {
    echo defined('NO_ID') ? NO_ID : '';
    return;
} 

global $opt_themes;  
$urlX	= $url;
$parse	= parse_url($urlX);
$urlX1	= preg_replace("/^([a-zA-Z0-9].*\.)?([a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z.]{2,})$/", '$2', $parse['host']);
$language	= $opt_themes['apk_language']; 
$arr['languages']	= $language;	
?>

<div id="terminal-debug">

    <!-- TERMINAL HEADER --> 
    <!-- TERMINAL BODY -->
    <div class="terminal-body">

        <?php if (!empty($data['GP_ID'])): ?>
        <section class="terminal-section">
            <div class="terminal-prompt">$ fetch <a href='https://play.google.com/store/apps/details?id=<?php echo $data['GP_ID']; ?>' target="_blank" style="color:#7ee787" >playstore</a></div>

            <div class="terminal-grid">
                <?php
                $map = [
                    'PACKAGE'   => 'GP_ID',
                    'APP'       => 'title_GP',
                    'VERSION'   => 'version_GP',
                    'SIZE'      => 'sizes_GP',
                    'CATEGORY'  => 'genres_GP',
                    'DEVELOPER' => 'developers_GP',
                    'INSTALLS'  => 'installs_GP',
                    'UPDATED'   => 'updates_GP',
                    'REQUIRES'  => 'requires_GP',
                    'RATED'     => 'rated_GP',
                    'VOTES'     => 'ratings_GP',
                    'YOUTUBE'     => 'youtube_GP',
                    'PAID'     => 'paid_GP1',
                    'PAID#2'     => 'paid_GP2',
                ];

                foreach ($map as $label => $key):
                    if (!empty($data[$key])):
                ?>
                    <div>
                        <label><?= esc_html($label) ?></label>
                        <span><?= esc_html($data[$key]) ?></span>
                    </div>
                <?php endif; endforeach; ?>
            </div>
        </section>
        <?php endif; ?>

        <?php
        $blocks = [
            'summary.txt'   => 'desck_GP',
            'whats-new.log' => 'whatnews_GP',
            'article.md'    => 'articlebody_GP',
        ];

        foreach ($blocks as $file => $key):
            if (!empty($data[$key])):
        ?>
        <section class="terminal-section">
            <div class="terminal-prompt">$ cat <?= esc_html($file) ?></div>
            <pre class="terminal-pre"><?= esc_html(trim(strip_tags($data[$key]))) ?></pre>
        </section>
        <?php endif; endforeach; ?>

		<section class="terminal-section">
            <div class="terminal-prompt">$ fetch <a href="<?php echo esc_url($url); ?>" target="_blank" style="color:#7ee787" ><?php echo esc_html($urlX1); ?></a></div>

            <div class="terminal-grid">
				<?php
                $fields = [
                    'Title'       => $data['title'] ?? '',
                    'App Name'    => $data['app_name'] ?? '',
                    'Version'     => $data['version_sc'] ?? '',
                    'Genre'       => $data['genre_sc'] ?? '',
                    'Size'        => $data['size_sc'] ?? '',
                    'Developer'   => $data['developer_sc'] ?? '',
                    'Updated'     => $data['updated_sc'] ?? '',
                ];

                foreach ($fields as $label => $value):
                    if ( empty($value) ) continue;
                ?>				 
				<div>
				<label><?= esc_html($label) ?></label>
				<span><?php echo esc_html($value); ?></span>
				</div> 
				<?php endforeach; ?>
			
            </div>
        </section>		
		
		 <?php
        $blocks2 = [
			'short-mods.txt'  => 'mods', 
            'full-mod.txt'   => 'full_mod',
            'article-content.md' => 'article_content', 
        ];

       foreach ($blocks2 as $file => $key):
            if (!empty($data[$key])):
                ?>		 
        <section class="terminal-section">
            <div class="terminal-prompt">$ <?= esc_html($file) ?></div>
            <pre class="terminal-pre"><?= esc_html(trim(strip_tags($data[$key]))) ?></pre>
        </section>
        <?php endif; endforeach; ?>		 

		
		<?php
		/**
		 * DOWNLOAD LINKS → TERMINAL STYLE (LIKE desck_GP)
		 */

		$download_blocks = [
			'download-page.html' => $data['download_links_page'] ?? '',
			'download-main.url'  => $data['downloadlink'] ?? '',
		];

		foreach ( $download_blocks as $file => $value ) :
			if ( empty($value) ) continue;
		?>
		<section class="terminal-section">
			<div class="terminal-prompt">$ cat <?php echo esc_html($file); ?></div>
			<pre class="terminal-pre"><?php
				echo esc_html(
					strtoupper(str_replace(['-', '.'], [' ', ''], $file)) . "\n" .
					"URL  : " . trim($value) . "\n" .
					str_repeat('-', 40)
				);
			?></pre>
		</section>
		<?php endforeach; ?>


		<?php
		/**
		 * DOWNLOAD LINKS → TERMINAL STYLE (LIKE desck_GP)
		 */

		if ( ! empty($data['downloadlinks_gma']) && is_array($data['downloadlinks_gma']) ) {

			$output = [];
			$count  = count($data['downloadlinks_gma']);

			for ($i = 0; $i < $count; $i++) {

				// NAME wajib
				if ( empty($data['name_downloadlinks_gma'][$i]) ) {
					continue;
				}

				$name = $data['name_downloadlinks_gma'][$i];

				// LINK
				if ( ! empty($data['downloadlink_gma'][$i]) ) {
					$link = $data['downloadlink_gma'][$i];
				} elseif ( ! empty($data['downloadlinks_gma'][$i]) ) {
					$link = $data['downloadlinks_gma'][$i];
				} else {
					$link = '-';
				}

				// APK
				$apk  = ! empty($data['apklink'][$i]) ? $data['apklink'][$i] : '';

				// SIZE
				$size = ! empty($data['size_downloadlinks_gma'][$i])
					? wp_strip_all_tags($data['size_downloadlinks_gma'][$i])
					: '';

				// TYPE
				if ( ! empty($data['type_downloadlink_gma'][$i]) ) {
					$type = $data['type_downloadlink_gma'][$i];
				} elseif ( ! empty($data['types_downloadlinks_gma'][$i]) ) {
					$type = $data['types_downloadlinks_gma'][$i];
				} else {
					$type = '';
				}

				// NOTE
				$note = ! empty($data['notes_downloadlinks_gma'][$i])
					? $data['notes_downloadlinks_gma'][$i]
					: '';

				// BUILD TEXT BLOCK
				$block  = "DOWNLOAD #" . ($i + 1) . "\n";
				$block .= "NAME  : {$name}\n";
				$block .= "LINK  : {$link}\n";

				if ($apk)  $block .= "APK   : {$apk}\n";
				if ($size) $block .= "SIZE  : {$size}\n";
				if ($type) $block .= "TYPE  : {$type}\n";
				if ($note) $block .= "NOTE  : {$note}\n";

				$block .= str_repeat('-', 40);

				$output[] = $block;
			}

			if ( ! empty($output) ) :
		?>
		<section class="terminal-section">
			<div class="terminal-prompt">$ cat download_links.txt</div>
			<pre class="terminal-pre"><?php echo esc_html(implode("\n", $output)); ?></pre>
		</section>
		<?php
			endif;
		}
		?>
		
		<?php
		/**
		 * COVER → TERMINAL STYLE (LIKE desck_GP)
		 */

		if ( ! empty($data['cover']) ) :
		?>
		<section class="terminal-section">
			<div class="terminal-prompt">$ cat cover.txt</div>
			<pre class="terminal-pre"><?php
				echo esc_html(
					"BACKGROUND COVER\n" .
					"URL  : " . trim($data['cover']) . "\n" .
					str_repeat('-', 40)
				);
			?></pre>
		</section>
		<?php endif; ?>


		<?php
		/**
		 * IMAGES GP → TERMINAL STYLE (LIKE desck_GP)
		 */

		if ( ! empty($data['images_GP']) && is_array($data['images_GP']) ) {

			$images = [];
			$no = 1;

			foreach ( $data['images_GP'] as $img ) {
				if ( empty($img) ) continue;

				$images[] = sprintf(
					"IMAGE #%d\nURL   : %s\n%s",
					$no,
					$img,
					str_repeat('-', 40)
				);

				$no++;
			}

			if ( ! empty($images) ) :
		?>
		<section class="terminal-section">
			<div class="terminal-prompt">$ cat gallery_images.txt</div>
			<pre class="terminal-pre"><?php echo esc_html(implode("\n", $images)); ?></pre>
		</section>
		<?php
			endif;
		}
		?>

		
        <div class="terminal-footer">
            root@parser:~$ <span class="blink">_</span>
        </div>
 
    </div>
</div>

<style>
#terminal-debug {background: #0d1117;color: #c9d1d9;font-family: JetBrains Mono, monospace;border-radius: 10px;margin: 20px 0;box-shadow: 0 20px 50px rgba(0,0,0,.6);overflow: visible;}.terminal-header {background: #161b22;padding: 10px;display: flex;align-items: center;}.dot {width: 12px;height: 12px;border-radius: 50%;margin-right: 6px;}.red {background: #ff5f56;}.yellow {background: #ffbd2e;}.green {background: #27c93f;}.terminal-title {margin-left: 12px;font-size: 12px;color: #8b949e;}.terminal-body {padding: 15px;max-height: none;overflow: visible;}.terminal-section {margin-bottom: 25px;}.terminal-prompt {color: #7ee787;margin-bottom: 8px;}.terminal-grid {display: grid;grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));gap: 8px;}.terminal-grid div {background: #010409;border: 1px solid #21262d;padding: 6px 10px;border-radius: 6px;}.terminal-grid label {font-size: 10px;color: #8b949e;display: block;}.terminal-pre {background: #010409;border: 1px solid #21262d;border-radius: 6px;padding: 12px;white-space: pre-wrap;line-height: 1.6;color: #fff;}.terminal-list {list-style: none;padding: 0;margin: 0;}.terminal-list li {padding: 6px 0;border-bottom: 1px dashed #21262d;}.terminal-footer {padding-top: 10px;color: #7ee787;}.blink {animation: blink 1s infinite;}@keyframes blink {50% {opacity: 0;}}
</style>

