<?php
// Product attribute
$product_name = RT_THEME_NAME;
$product_slug = RT_THEME_SLUG;
$product_url = RT_THEME_URL;
$product_doc = RT_THEME_DOC;
$product_version = RT_THEME_VERSION;

// Form value
$license_key = !empty($_POST['product_license_key']) ? sanitize_text_field($_POST['product_license_key']) : '';
$license_action = !empty($_POST['product_license_key']) ? sanitize_text_field($_POST['product_license_submit']) : '';

// API Manager
$license = new Retheme\Activation([
    'product_key' => $license_key,
]);

$manager = array('result' => '');
$expired_date = $license->get_expired_date('format');

if ($license_action) {
    if ($license_action == 'activate') {
        $manager = $license->activate();
    } else {
        $manager = $license->deactivate();
    }
}

?>
<div class="rta theme-panel">
    <section class="rta-site__header">
        <h1 class="rta-site__title"><?php echo $product_name ?> Lisensi</h1>
        <strong>Version <?php echo $product_version ?></strong>
    </section>

    <div class="rta-site__body">
        <?php if ($manager['result']) : ?>
            <?php if ($manager['result'] == 'error') : ?>
                <div class="update-message notice inline notice-warning notice-alt"><?php echo $manager['message'] ?></div>
            <?php else : ?>
                <div class="update-message notice inline notice-success notice-alt"><?php echo $manager['message'] ?></div>
            <?php endif; ?>
        <?php endif; ?>

        <div class="rta-panel" style="max-width: 650px">

            <div class="rta-panel__header">
                <h4 class="rta-panel__title">Lisensi

                    <?php if ($license->is_local()) : ?>
                        <span class="rta-badges rta-badges--info" style="float: right">Local Lisensi</span>
                    <?php elseif ($license->status() === 'active') : ?>
                        <span class="rta-badges rta-badges--success" style="float: right">Active</span>
                    <?php else : ?>
                        <span class="rta-badges rta-badges--warning" style="float: right"><?php echo ucwords($license->status()) ?></span>
                    <?php endif ?>

                </h4>

            </div>

            <div class="rta-panel__body">
                <div class="rta-form-wrapper">

                    <?php if ($license->is_local()) : ?>
                        <p>Anda sedang menggunakan theme ini di localhost server, semua fitur <strong>Premium</strong> akan aktif pada server lokal. Anda wajib memasukan lisensi ketika di hosting atau server online </p>
                    <?php elseif ($license->status() === 'active') : ?>
                        <p>Tema Anda berhasil di aktifkan, silakan cek <a href="<?php echo esc_url($product_doc) ?>" target="_blank">disini</a> untuk melihat panduan tema</p>
                    <?php elseif ($license->status() === 'trial') : ?>
                        <p>Saat ini Anda menggunakan tema Kapital versi uji coba (Trial).</p>
                    <?php elseif ($license->status() === 'expired') : ?>
                        <p>Lisensi tema Anda berakhir pada tanggal <strong><?php echo $expired_date ?></strong>, silakan lakukan perpanjangan lisensi untuk tetap mendapatkan update dan support.</p>
                    <?php else : ?>
                        <p>Masukan kunci lisensi Anda untuk dapat menggunakan semua fitur premium, update, dan support. <a target="_blank" href="<?php echo esc_url($product_url) ?>">Dapatkan lisensi disini</a></p>
                    <?php endif ?>

                    <?php if ($expired_date && $license->status() !== 'unregistered' && !$license->is_local()) : ?>
                        <p>Aktif Hingga: <?php echo $expired_date ?></p>
                    <?php endif ?>

                    <form action="#" method="post">
                        <div class="rta-form">
                            <label class="rta-form__label">Lisensi Key</label>

                            <?php if ($license->is_local()) : ?>
                                <input class="rta-form__input" id="product_license_key" name="product_license_key" type="password" value="xxxxxxxxxxxxxx" disabled />
                            <?php elseif ($license->is_premium() || $license->status() === 'trial expired') : ?>
                                <input class="rta-form__input" type="password" value="<?php echo get_option("{$product_slug}_key"); ?>" disabled />
                                <input class="rta-form__input" id="product_license_key" name="product_license_key" type="hidden" value="<?php echo get_option("{$product_slug}_key"); ?>" />
                            <?php else : ?>
                                <input class="rta-form__input" id="product_license_key" name="product_license_key" type="text" value="" required />
                            <?php endif ?>

                        </div>
                        <div css="rta-form">
                            <?php if ($license->is_local()) : ?>
                                <a class="button button-primary" href="<?php echo $product_url ?>" target="_blank">Dapatkan lisensi disini</a>
                            <?php elseif ($license->is_premium() || $license->status() === 'trial expired') : ?>
                                <button class="button button-primary" name="product_license_submit" type="submit" value="reset">Reset</button>
                            <?php else : ?>
                                <button class="button button-primary" name="product_license_submit" type="submit" value="activate">Aktifkan</button>
                            <?php endif ?>
                        </div>
                    </form>

                </div>

            </div>

        </div>

    </div>

</div>