<?php

/*
 * @component UserPoints
 * @copyright Copyright (C) 2015-2022 Bernard Gilly - Adrien Roussel - Martin Brampton
 * @license : GNU/GPL
 * @Website : https://remository.com
 */

namespace BlackSheepResearch\Component\UserPoints\Administrator\Viewers;

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Toolbar\Toolbar;
use Joomla\CMS\Toolbar\ToolbarHelper;
use Joomla\CMS\Pagination\Pagination;
use Joomla\CMS\HTML\HTMLHelper;
use BlackSheepResearch\Component\UserPoints\Site\UserPoints;
use BlackSheepResearch\Component\UserPoints\Site\WebAssetManager;

class CouponcodesHTML extends AdminHTML {

    function displaylist($tpl = null) {
        $logo = '<img src="' . Uri::root() . 'administrator/components/com_userpoints/assets/images/icon-48-userpoints.png" />&nbsp;&nbsp;';
        ToolbarHelper::title($logo . 'UserPoints :: ' . Text::_('AUP_COUPON_CODES'), 'thememanager');
        $this->getCpanelToolbar();
        if (UserPoints::getUser()->authorise('core.edit', 'com_userpoints')) {
            ToolbarHelper::editList('editcoupon');
        }
        if (UserPoints::getUser()->authorise('core.create', 'com_userpoints')) {
            ToolbarHelper::addNew('editcoupon');
        }
        if (UserPoints::getUser()->authorise('core.delete', 'com_userpoints')) {
            ToolbarHelper::custom('deletecoupon', 'delete.png', 'delete.png', Text::_('AUP_DELETE'));
        }
        ToolbarHelper::divider();
        //ToolbarHelper::custom( 'qrcodestats', 'qrcode-stats.png', 'qrcode-stats.png', Text::_('AUP_QRCODE'), false );
        $bar = Toolbar::getInstance('toolbar');

        HTMLHelper::_('bootstrap.modal', 'collapseModal');
        $title = Text::_('AUP_GENERATOR');
        $dhtml = "<button data-toggle=\"modal\" data-target=\"#generatorModal\" class=\"btn btn-small\">
					<i class=\"icon-checkbox-partial\" title=\"$title\"></i>
					$title</button>";
        $bar->appendButton('Custom', $dhtml, 'coupongenerator');

        //$bar->appendButton( 'Popup', 'upload', Text::_('AUP_GENERATOR'), 'index.php?option=com_userpoints&task=coupongenerator&layout=modal&tmpl=component', 800, 560, 0, 0, 'window.top.location.reload(true);document.location.reload(true);' );

        if (UserPoints::getUser()->authorise('core.create', 'com_userpoints')) {
            ToolbarHelper::custom('exportcoupons', 'upload.png', 'upload.png', Text::_('AUP_EXPORT'), false);
        }

        $this->getPrefHelpToolbar();

        $this->isIEAdm();

        $this->pagination = new Pagination($this->total, $this->limitstart, $this->limit);

        parent::display($tpl);
    }

    function edit_coupon($tpl = null) {
        Factory::getApplication()->input->set('hidemainmenu', 1);
        $logo = '<img src="' . Uri::root() . 'administrator/components/com_userpoints/assets/images/icon-48-userpoints.png" />&nbsp;&nbsp;';
        ToolbarHelper::title($logo . 'UserPoints :: ' . Text::_('AUP_COUPON_CODES'), 'thememanager');
        $this->getCpanelToolbar();

        if ($this->row->printable) {
            $bar = Toolbar::getInstance('toolbar');
            $bar->appendButton('Popup', 'print', Text::_('AUP_PRINT'), 'index.php?option=com_userpoints&task=printcoupon&id=' . $this->row->id . '&layout=modal&tmpl=component', 740, 480, 0, 0);
            ToolbarHelper::divider();
        }
        if (UserPoints::getUser()->authorise('core.edit.state', 'com_userpoints')) {
            ToolbarHelper::apply('applycoupon');
            ToolbarHelper::save('savecoupon');
        }

        ToolbarHelper::cancel('cancelcoupon');
        $this->getPrefHelpToolbar();
        //JHTML::_('behavior.mootools');
        //JHTML::_('behavior.calendar');
        //$document->addScriptDeclaration("window.addEvent('domready', function(){ var JTooltips = new Tips($$('.hasTip'), { maxTitleChars: 50, fixed: false}); });");

        parent::display("form");
    }

    function displayQRcodestats($tpl = null) {
        $logo = '<img src="' . Uri::root() . 'administrator/components/com_userpoints/assets/images/icon-48-userpoints.png" />&nbsp;&nbsp;';

        ToolbarHelper::title($logo . 'UserPoints :: ' . Text::_('AUP_QRCODE'), 'thememanager');

        $this->getCpanelToolbar();
        ToolbarHelper::back();
        $this->getPrefHelpToolbar();

        $this->pagination = new Pagination($this->total, $this->limitstart, $this->limit);

        parent::display('qrstats');
    }

    function generate_coupon($tpl = null) {
        parent::display("generator");
    }

    function print_coupon($tpl = null) {
        WebAssetManager::getInstance('com_userpoints')->useStyle('print_coupon.styles');

        parent::display("print");
    }

}
