<?php

/*
 * ==========================================================
 * ADMIN_CODE.PHP
 * ==========================================================
 *
 * Admin area. © 2022-2024 cryptopayment.network. All rights reserved.
 *
 */

function crpay_box_admin() {
    crpay_colors_admin();
    crpay_load_custom_js_css();
    $is_shop = defined('CRPAY_SHOP');
    $css = crpay_is_rtl(crpay_language(true)) ? ' crpay-rtl' : '';
    if (!crpay_ve_box()) {
        return;
    }
    crpay_version_updates();
    if ($is_shop) {
        crpay_shop_db_update();
    }
    if (crpay_is_agent()) {
        $css .= ' crpay-agent';
    }
    ?>
    <div class="crpay-main crpay-admin crpay-area-transactions<?php echo ($css); ?>">
        <div class="crpay-sidebar">
            <div>
                <img class="crpay-logo" src="<?php echo (CRPAY_CLOUD ? CLOUD_LOGO : (crpay_settings_get('logo-admin') ? crpay_settings_get('logo-url', CRPAY_URL . 'media/logo.webp') : CRPAY_URL . 'media/logo.webp')); ?>" />
                <img class="crpay-logo-icon" src="<?php echo (CRPAY_CLOUD ? CLOUD_ICON : (crpay_settings_get('logo-admin') ? crpay_settings_get('logo-icon-url', CRPAY_URL . 'media/icon-18x18.png') : CRPAY_URL . 'media/icon-18x18.png')); ?>" />
            </div>
            <div class="crpay-nav">
                <div id="transactions" class="crpay-active">
                    <i class="crpay-icon-shuffle"></i>
                    <span>
                        <?php crpay_e('Transactions'); ?>
                    </span>
                </div>
                <div id="checkouts">
                    <i class="crpay-icon-automation"></i>
                    <span>
                        <?php crpay_e('Checkouts'); ?>
                    </span>
                </div>
                <div id="balances">
                    <i class="crpay-icon-bar-chart"></i>
                    <span>
                        <?php crpay_e('Balances'); ?>
                    </span>
                </div>
                <?php
                if (!crpay_is_agent()) {
                    echo '<div id="settings"><i class="crpay-icon-settings"></i><span>' . (crpay_('Settings')) . '</span></div>';
                }
                if ($is_shop) {
                    echo '<div id="analytics"><i class="crpay-icon-calendar"></i><span>' . (crpay_('Analytics')) . '</span></div>';
                }
                if (CRPAY_CLOUD) {
                    echo '<div id="account"><i class="crpay-icon-user"></i><span>' . (crpay_('Account')) . '</span></div>';
                }
                ?>
            </div>
            <div class="crpay-bottom">
                <div id="crpay-request-payment" class="crpay-btn">
                    <?php crpay_e('Request a payment'); ?>
                </div>
                <div id="crpay-create-checkout" class="crpay-btn">
                    <?php crpay_e('Create checkout'); ?>
                </div>
                <div id="crpay-save-settings" class="crpay-btn">
                    <?php crpay_e('Save settings'); ?>
                </div>
                <div class="crpay-mobile-menu">
                    <i class="crpay-icon-menu"></i>
                    <div class="crpay-flex">
                        <div class="crpay-link" id="crpay-logout">
                            <?php crpay_e('Logout'); ?>
                        </div>
                        <div id="crpay-version">
                            <?php echo (CRPAY_VERSION); ?>
                        </div>
                        <a class="crpay-btn-icon" href="<?php echo (CRPAY_CLOUD ? CLOUD_DOCS : 'https://cryptopayment.network/docs'); ?>" target="_blank">
                            <i class="crpay-icon-help"></i>
                        </a>
                    </div>
                </div>
            </div>
        </div>
        <div class="crpay-body">
            <main>
                <div data-area="transactions" class="crpay-active">
                    <div class="crpay-nav-wide">
                        <div class="crpay-input crpay-search">
                            <input type="text" id="crpay-search-transactions" class="crpay-search-input" name="crpay-search" placeholder="<?php crpay_e('Search all transactions'); ?>" autocomplete="false" />
                            <input type="text" class="crpay-hidden" />
                            <i class="crpay-icon-search"></i>
                        </div>
                        <div id="crpay-download-transitions" class="crpay-btn-icon">
                            <i class="crpay-icon-download"></i>
                        </div>
                        <?php crpay_admin_filters(); ?>
                        <div id="crpay-filters" class="crpay-btn-icon">
                            <i class="crpay-icon-filters"></i>
                        </div>
                    </div>
                    <hr />
                    <table id="crpay-table-transactions" class="crpay-table">
                        <thead>
                            <tr>
                                <th data-field="id"></th>
                                <th data-field="date">
                                    <?php crpay_e('Date'); ?>
                                </th>
                                <th data-field="from">
                                    <?php crpay_e('From'); ?>
                                </th>
                                <th data-field="to">
                                    <?php crpay_e('To'); ?>
                                </th>
                                <th data-field="status">
                                    <?php crpay_e('Status'); ?>
                                </th>
                                <th data-field="amount">
                                    <?php crpay_e('Amount'); ?>
                                </th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
                <div data-area="checkouts" class="crpay-loading">
                    <table id="crpay-table-checkouts" class="crpay-table">
                        <tbody></tbody>
                    </table>
                    <div id="crpay-checkouts-form">
                        <form>
                            <div class="crpay-info"></div>
                            <div class="crpay-top">
                                <div id="crpay-checkouts-list" class="crpay-btn crpay-btn-border">
                                    <i class="crpay-icon-back"></i>
                                    <?php crpay_e('Checkouts list'); ?>
                                </div>
                            </div>
                            <div id="crpay-checkout-title" class="crpay-input">
                                <span>
                                    <?php crpay_e('Title'); ?>
                                </span>
                                <input type="text" required />
                            </div>
                            <div id="crpay-checkout-description" class="crpay-input">
                                <span>
                                    <?php crpay_e('Description'); ?>
                                </span>
                                <input type="text" />
                            </div>
                            <div class="crpay-flex">
                                <div id="crpay-checkout-price" data-type="select" class="crpay-input">
                                    <span>
                                        <?php crpay_e('Price'); ?>
                                    </span>
                                    <input type="number" />
                                </div>
                                <div id="crpay-checkout-currency" data-type="select" class="crpay-input">
                                    <select>
                                        <option value="" selected>Default</option>
                                        <option value="crypto"><?php crpay_e('Cryptocurrency'); ?></option>
                                        </option>
                                        <option value="AED">United Arab Emirates Dirham</option>
                                        <option value="AFN">Afghan Afghani</option>
                                        <option value="ALL">Albanian Lek</option>
                                        <option value="AMD">Armenian Dram</option>
                                        <option value="ANG">Netherlands Antillean Guilder</option>
                                        <option value="AOA">Angolan Kwanza</option>
                                        <option value="ARS">Argentine Peso</option>
                                        <option value="AUD">Australian Dollar</option>
                                        <option value="AWG">Aruban Florin</option>
                                        <option value="AZN">Azerbaijani Manat</option>
                                        <option value="BAM">Bosnia-Herzegovina Convertible Mark</option>
                                        <option value="BBD">Barbadian Dollar</option>
                                        <option value="BDT">Bangladeshi Taka</option>
                                        <option value="BGN">Bulgarian Lev</option>
                                        <option value="BHD">Bahraini Dinar</option>
                                        <option value="BIF">Burundian Franc</option>
                                        <option value="BMD">Bermudan Dollar</option>
                                        <option value="BND">Brunei Dollar</option>
                                        <option value="BOB">Bolivian Boliviano</option>
                                        <option value="BRL">Brazilian Real</option>
                                        <option value="BSD">Bahamian Dollar</option>
                                        <option value="BTN">Bhutanese Ngultrum</option>
                                        <option value="BWP">Botswanan Pula</option>
                                        <option value="BYN">Belarusian Ruble</option>
                                        <option value="BZD">Belize Dollar</option>
                                        <option value="CAD">Canadian Dollar</option>
                                        <option value="CDF">Congolese Franc</option>
                                        <option value="CHF">Swiss Franc</option>
                                        <option value="CLF">Chilean Unit of Account (UF)</option>
                                        <option value="CLP">Chilean Peso</option>
                                        <option value="CNH">Chinese Yuan (Offshore)</option>
                                        <option value="CNY">Chinese Yuan</option>
                                        <option value="COP">Colombian Peso</option>
                                        <option value="CRC">Costa Rican Colón</option>
                                        <option value="CUC">Cuban Convertible Peso</option>
                                        <option value="CUP">Cuban Peso</option>
                                        <option value="CVE">Cape Verdean Escudo</option>
                                        <option value="CZK">Czech Republic Koruna</option>
                                        <option value="DJF">Djiboutian Franc</option>
                                        <option value="DKK">Danish Krone</option>
                                        <option value="DOP">Dominican Peso</option>
                                        <option value="DZD">Algerian Dinar</option>
                                        <option value="EGP">Egyptian Pound</option>
                                        <option value="ERN">Eritrean Nakfa</option>
                                        <option value="ETB">Ethiopian Birr</option>
                                        <option value="EUR">Euro</option>
                                        <option value="FJD">Fijian Dollar</option>
                                        <option value="FKP">Falkland Islands Pound</option>
                                        <option value="GBP">British Pound Sterling</option>
                                        <option value="GEL">Georgian Lari</option>
                                        <option value="GGP">Guernsey Pound</option>
                                        <option value="GHS">Ghanaian Cedi</option>
                                        <option value="GIP">Gibraltar Pound</option>
                                        <option value="GMD">Gambian Dalasi</option>
                                        <option value="GNF">Guinean Franc</option>
                                        <option value="GTQ">Guatemalan Quetzal</option>
                                        <option value="GYD">Guyanaese Dollar</option>
                                        <option value="HKD">Hong Kong Dollar</option>
                                        <option value="HNL">Honduran Lempira</option>
                                        <option value="HRK">Croatian Kuna</option>
                                        <option value="HTG">Haitian Gourde</option>
                                        <option value="HUF">Hungarian Forint</option>
                                        <option value="IDR">Indonesian Rupiah</option>
                                        <option value="ILS">Israeli New Sheqel</option>
                                        <option value="IMP">Manx pound</option>
                                        <option value="INR">Indian Rupee</option>
                                        <option value="IQD">Iraqi Dinar</option>
                                        <option value="IRR">Iranian Rial</option>
                                        <option value="ISK">Icelandic Króna</option>
                                        <option value="JEP">Jersey Pound</option>
                                        <option value="JMD">Jamaican Dollar</option>
                                        <option value="JOD">Jordanian Dinar</option>
                                        <option value="JPY">Japanese Yen</option>
                                        <option value="KES">Kenyan Shilling</option>
                                        <option value="KGS">Kyrgystani Som</option>
                                        <option value="KHR">Cambodian Riel</option>
                                        <option value="KMF">Comorian Franc</option>
                                        <option value="KPW">North Korean Won</option>
                                        <option value="KRW">South Korean Won</option>
                                        <option value="KWD">Kuwaiti Dinar</option>
                                        <option value="KYD">Cayman Islands Dollar</option>
                                        <option value="KZT">Kazakhstani Tenge</option>
                                        <option value="LAK">Laotian Kip</option>
                                        <option value="LBP">Lebanese Pound</option>
                                        <option value="LKR">Sri Lankan Rupee</option>
                                        <option value="LRD">Liberian Dollar</option>
                                        <option value="LSL">Lesotho Loti</option>
                                        <option value="LYD">Libyan Dinar</option>
                                        <option value="MAD">Moroccan Dirham</option>
                                        <option value="MDL">Moldovan Leu</option>
                                        <option value="MGA">Malagasy Ariary</option>
                                        <option value="MKD">Macedonian Denar</option>
                                        <option value="MMK">Myanma Kyat</option>
                                        <option value="MNT">Mongolian Tugrik</option>
                                        <option value="MOP">Macanese Pataca</option>
                                        <option value="MRU">Mauritanian Ouguiya</option>
                                        <option value="MUR">Mauritian Rupee</option>
                                        <option value="MVR">Maldivian Rufiyaa</option>
                                        <option value="MWK">Malawian Kwacha</option>
                                        <option value="MXN">Mexican Peso</option>
                                        <option value="MYR">Malaysian Ringgit</option>
                                        <option value="MZN">Mozambican Metical</option>
                                        <option value="NAD">Namibian Dollar</option>
                                        <option value="NGN">Nigerian Naira</option>
                                        <option value="NIO">Nicaraguan Córdoba</option>
                                        <option value="NOK">Norwegian Krone</option>
                                        <option value="NPR">Nepalese Rupee</option>
                                        <option value="NZD">New Zealand Dollar</option>
                                        <option value="OMR">Omani Rial</option>
                                        <option value="PAB">Panamanian Balboa</option>
                                        <option value="PEN">Peruvian Nuevo Sol</option>
                                        <option value="PGK">Papua New Guinean Kina</option>
                                        <option value="PHP">Philippine Peso</option>
                                        <option value="PKR">Pakistani Rupee</option>
                                        <option value="PLN">Polish Zloty</option>
                                        <option value="PYG">Paraguayan Guarani</option>
                                        <option value="QAR">Qatari Rial</option>
                                        <option value="RON">Romanian Leu</option>
                                        <option value="RSD">Serbian Dinar</option>
                                        <option value="RUB">Russian Ruble</option>
                                        <option value="RWF">Rwandan Franc</option>
                                        <option value="SAR">Saudi Riyal</option>
                                        <option value="SBD">Solomon Islands Dollar</option>
                                        <option value="SCR">Seychellois Rupee</option>
                                        <option value="SDG">Sudanese Pound</option>
                                        <option value="SEK">Swedish Krona</option>
                                        <option value="SGD">Singapore Dollar</option>
                                        <option value="SHP">Saint Helena Pound</option>
                                        <option value="SLL">Sierra Leonean Leone</option>
                                        <option value="SOS">Somali Shilling</option>
                                        <option value="SRD">Surinamese Dollar</option>
                                        <option value="SSP">South Sudanese Pound</option>
                                        <option value="STD">São Tomé and Príncipe Dobra (pre-2018)</option>
                                        <option value="STN">São Tomé and Príncipe Dobra</option>
                                        <option value="SVC">Salvadoran Colón</option>
                                        <option value="SYP">Syrian Pound</option>
                                        <option value="SZL">Swazi Lilangeni</option>
                                        <option value="THB">Thai Baht</option>
                                        <option value="TJS">Tajikistani Somoni</option>
                                        <option value="TMT">Turkmenistani Manat</option>
                                        <option value="TND">Tunisian Dinar</option>
                                        <option value="TOP">Tongan Pa'anga</option>
                                        <option value="TRY">Turkish Lira</option>
                                        <option value="TTD">Trinidad and Tobago Dollar</option>
                                        <option value="TWD">New Taiwan Dollar</option>
                                        <option value="TZS">Tanzanian Shilling</option>
                                        <option value="UAH">Ukrainian Hryvnia</option>
                                        <option value="UGX">Ugandan Shilling</option>
                                        <option value="USD">United States Dollar</option>
                                        <option value="UYU">Uruguayan Peso</option>
                                        <option value="UZS">Uzbekistan Som</option>
                                        <option value="VEF">Venezuelan Bolívar Fuerte (Old)</option>
                                        <option value="VES">Venezuelan Bolívar Soberano</option>
                                        <option value="VND">Vietnamese Dong</option>
                                        <option value="VUV">Vanuatu Vatu</option>
                                        <option value="WST">Samoan Tala</option>
                                        <option value="XAF">CFA Franc BEAC</option>
                                        <option value="XAG">Silver Ounce</option>
                                        <option value="XAU">Gold Ounce</option>
                                        <option value="XCD">East Caribbean Dollar</option>
                                        <option value="XDR">Special Drawing Rights</option>
                                        <option value="XOF">CFA Franc BCEAO</option>
                                        <option value="XPD">Palladium Ounce</option>
                                        <option value="XPF">CFP Franc</option>
                                        <option value="XPT">Platinum Ounce</option>
                                        <option value="YER">Yemeni Rial</option>
                                        <option value="ZAR">South African Rand</option>
                                        <option value="ZMW">Zambian Kwacha</option>
                                        <option value="ZWL">Zimbabwean Dollar</option>
										<?php
    $cryptocurrencies = crpay_crypto_name();
    $code = '';
    foreach ($cryptocurrencies as $key => $value) {
        $code .= '<option value="' . (strtoupper($key)) . '">' . ($value[1]) . '</option>';
    }
    echo $code;
    ?>
</select>
</div>
</div>
<div id="crpay-checkout-type" data-type="select" class="crpay-input">
    <span>
        <?php crpay_e('Type'); ?>
    </span>
    <select>
        <option value="I" selected>
            <?php crpay_e('Inline'); ?>
        </option>
        <option value="L">
            <?php crpay_e('Link'); ?>
        </option>
        <option value="P">
            <?php crpay_e('Popup'); ?>
        </option>
        <option value="H">
            <?php crpay_e('Hidden'); ?>
        </option>
    </select>
</div>
<div id="crpay-checkout-redirect" class="crpay-input">
    <span>
        <?php crpay_e('Redirect URL'); ?>
    </span>
    <input type="url" />
</div>
<div id="crpay-checkout-external_reference" class="crpay-input">
    <span>
        <?php crpay_e('External reference'); ?>
    </span>
    <input type="text" />
</div>
<div id="crpay-checkout-hide_title" class="crpay-input">
    <span>
        <?php crpay_e('Hide title'); ?>
    </span>
    <input type="checkbox" />
</div>
<?php
if (defined('CRPAY_SHOP')) {
	$checkout_area = crpay_shop_checkout_area();
    echo $checkout_area;
}
if (defined('CRPAY_WP')) {
    echo '<div id="crpay-checkout-shortcode" class="crpay-input"><span>Shortcode</span><div></div><i class="crpay-icon-copy crpay-clipboard crpay-toolip-cnt"><span class="crpay-toolip">' . (crpay_('Copy to clipboard')) . '</span></i></div>';
}
if (crpay_settings_get('url-rewrite-checkout') || CRPAY_CLOUD) {
    echo '<div id="crpay-checkout-slug" class="crpay-input"><span>' . (crpay_('Slug')) . '</span><input type="text" /></div>';
}
?>
<div id="crpay-checkout-embed-code" class="crpay-input">
    <span>
        <?php crpay_e('Embed code'); ?>
    </span>
    <div></div>
    <i class="crpay-icon-copy crpay-clipboard crpay-toolip-cnt">
        <span class="crpay-toolip">
            <?php crpay_e('Copy to clipboard'); ?>
        </span>
    </i>
</div>
<div id="crpay-checkout-payment-link" class="crpay-input">
    <span>
        <?php crpay_e('Payment link'); ?>
    </span>
    <div></div>
    <i class="crpay-icon-copy crpay-clipboard crpay-toolip-cnt">
        <span class="crpay-toolip">
            <?php crpay_e('Copy to clipboard'); ?>
        </span>
    </i>
</div>
<div class="crpay-bottom">
    <div id="crpay-save-checkout" class="crpay-btn">
        <?php crpay_e('Save checkout'); ?>
    </div>
    <a id="crpay-delete-checkout" class="crpay-btn-icon crpay-btn-red">
        <i class="crpay-icon-delete"></i>
    </a>
</div>
</form>
</div>
</div>
<div data-area="balance">
    <div>
        <div id="crpay-balance-total" class="crpay-title"></div>
        <div class="crpay-text">
            <?php crpay_e('Available balance'); ?>
        </div>
    </div>
    <table id="crpay-table-balances" class="crpay-table">
        <thead>
            <tr>
                <th data-field="cryptocurrency">
                    <?php crpay_e('Crypto currency'); ?>
                </th>
                <th data-field="balance">
                    <?php crpay_e('Balance'); ?>
                </th>
            </tr>
        </thead>
        <tbody></tbody>
    </table>
</div>
<div data-area="settings" class="crpay-loading">
    <?php crpay_settings_populate(); ?>
</div>
 
</main>
</div>
<div id="crpay-card" class="crpay-info-card"></div>
</div>
<div id="crpay-lightbox">
    <div>
        <div class="crpay-top">
            <div class="crpay-title"></div>
            <div class="crpay-flex">
                <div class="crpay-lightbox-buttons crpay-flex"></div>
                <div id="crpay-lightbox-close" class="crpay-btn-icon crpay-btn-red">
                    <i class="crpay-icon-close"></i>
                </div>
            </div>
        </div>
        <div id="crpay-lightbox-main" class="crpay-scrollbar"></div>
    </div>
    <span></span>
</div>
<div id="crpay-lightbox-loading" class="crpay-loading"></div>
<form id="crpay-upload-form" action="#" method="post" enctype="multipart/form-data">
    <input type="file" name="files[]" />
</form>
<?php } ?>

<?php function crpay_admin_filters($show_all_statues = true) { ?>
<div class="crpay-nav-filters">
    <div class="crpay-input">
        <input class="crpay-filter-date" placeholder="<?php crpay_e('Start date...'); ?>" type="text" readonly />
        <input class="crpay-filter-date-2" placeholder="<?php crpay_e('End date...'); ?>" type="text" readonly />
    </div>
    <div class="crpay-filter-status crpay-select crpay-right">
        <p>
            <?php crpay_e($show_all_statues ? 'All statuses' : 'Completed'); ?>
        </p>
        <ul>
            <?php
            if ($show_all_statues) {
                echo ' <li data-value="" class="crpay-active">' . (crpay_('All statuses')) . '</li>';
            }
            ?>
            <li data-value="C" <?php echo  ( $show_all_statues ) ? '' : ' class="crpay-active"'; ?>>
                <?php crpay_e('Completed'); ?>
            </li>
            <li data-value="P">
                <?php crpay_e('Pending'); ?>
            </li>
            <li data-value="R">
                <?php crpay_e('Refunded'); ?>
            </li>
        </ul>
    </div>
    <div class="crpay-filter-cryptocurrency crpay-select crpay-right">
        <p>
            <?php crpay_e('All currencies'); ?>
        </p>
        <ul>
            <li data-value="" class="crpay-active">
                <?php crpay_e('All currencies'); ?>
            </li>
            <?php
            $cryptocurrencies = crpay_crypto_name(false, true);
            $currencies = crpay_db_get('SELECT cryptocurrency FROM crpay_transactions GROUP BY cryptocurrency UNION SELECT currency FROM crpay_transactions GROUP BY currency', false);
			$code = '';
            if (is_array($currencies) || is_countable($currencies)) {
                $count = $currencies;
            } else {
                $count = [];
            }
			for ($i = 0; $i < count($count); $i++) {
				$currency = $currencies[$i]['cryptocurrency'];
				if (!in_array($currency, ['stripe', 'verifone', 'paypal'])) {
					$code .= '<li data-value="' . ($currency) . '">' . (isset($cryptocurrencies[$currency]) ? $cryptocurrencies[$currency][1] . ' ' . crpay_crypto_get_network($currency, true, true) : strtoupper($currency)) . '</li>';
				}
			}
			
			// Escape the entire HTML string before outputting it
			echo $code;
			
            ?>
        </ul>
    </div>
    <div class="crpay-filter-checkout crpay-select crpay-right">
        <p>
            <?php crpay_e('All checkouts'); ?>
        </p>
        <ul>
            <li data-value="" class="crpay-active">
                <?php crpay_e('All checkouts'); ?>
            </li>
            <?php
            $checkouts = crpay_checkout_get();
			$code = '';
            if (is_array($checkouts) || is_countable($checkouts)) {
                $count_checkouts = $checkouts;
            } else {
                $count_checkouts = [];
            }
			for ($i = 0; $i < count($count_checkouts); $i++) {
				$code .= '<li data-value="' . ($checkouts[$i]['id']) . '">' . ($checkouts[$i]['title']) . '</li>';
			}
			

			
			// Escape the entire HTML string before outputting it
			echo $code;
			
            ?>
        </ul>
    </div>
</div>
<?php } ?>