<div class="page-title">
    <h1>
        <?php echo $this->user->lang("Affiliate Settings - Custom Links"); ?>
    </h1>
    <p class="page-subtitle">
        <?php echo $this->user->lang('To track affiliate hits on your website, paste the code below on every page you\'d like to track. Your Clientexec installation automatically tracks hits.'); ?>
    </p>
</div>

<div class="cm-card">
    <div class="cm-card-header">
        <?php echo $this->user->lang("Tracking Code"); ?>
    </div>

    <div class="cm-card-body">
        <div class="code-block">
            <button id="copy-btn" onclick="copyCode()" title="Copy to clipboard">
                <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24"
                     fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                    <rect x="9" y="2" width="6" height="4" rx="1"/>
                    <path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2"/>
                </svg>
                <span id="copy-label"><?php echo $this->user->lang("Copy"); ?></span>
            </button>

            <pre><code id="tracking-code"><?php echo $this->html; ?></code></pre>
        </div>
    </div>
</div>

<script data-cfasync="false">
    function copyCode() {
        const code = document.getElementById('tracking-code').innerText;
        const btn = document.getElementById('copy-btn');
        const label = document.getElementById('copy-label');

        function showSuccess() {
            label.textContent = 'Copied!';
            btn.classList.add('is-copied');
            setTimeout(() => {
                label.textContent = 'Copy';
                btn.classList.remove('is-copied');
            }, 2000);
        }

        if (navigator.clipboard && window.isSecureContext) {
            navigator.clipboard.writeText(code).then(showSuccess);
        } else {
            const textarea = document.createElement('textarea');
            textarea.value = code;
            textarea.style.position = 'fixed';
            textarea.style.opacity = '0';
            document.body.appendChild(textarea);
            textarea.focus();
            textarea.select();
            document.execCommand('copy');
            document.body.removeChild(textarea);
            showSuccess();
        }
    }
</script>