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

defined('_JEXEC') or die('Restricted access');

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\HTML\HTMLHelper;
use BlackSheepResearch\Component\UserPoints\Site\UserPoints;
use BlackSheepResearch\Component\UserPoints\Site\Problems\QRCodeTrack;
use BlackSheepResearch\Component\UserPoints\Site\Problems\SQL;

// JHtml::_('behavior.formvalidation');
// JHtml::_('formbehavior.chosen', 'select');
?>
<script type="text/javascript">
    Joomla.submitbutton = function (task)
    {
        //if (task == 'cpanel' || document.formvalidator.isValid(document.id('couponcodes-form'))) {
        Joomla.submitform(task, document.getElementById('adminForm'));
        //}
        //else {
        //alert('<?php echo $this->escape(Text::_('JGLOBAL_VALIDATION_FORM_FAILED')); ?>');
        //}
    }
</script>
<div id="j-main-container">
    <form action="index.php?option=com_userpoints" method="post" name="adminForm" id="adminForm" class="form-validate">
        <div id="filter-bar" class="btn-toolbar">
            <table>
                <tr>
                    <td align="left" width="100%">&nbsp;
                        <?php //echo Text::_( 'Filter' ); ?>
                            <!--<input type="text" name="search" id="search" value="<?php echo @$this->lists['search']; ?>" class="text_area" onchange="document.adminForm.submit();" />
                            <button onclick="this.form.submit();"><?php echo Text::_('Go'); ?></button>
                            <button onclick="document.getElementById('search').value='';this.form.submit();"><?php echo Text::_('Reset'); ?></button>-->
                    </td>
                    <td nowrap="nowrap" align="right">
                        <select name="filter_category_id" class="inputbox" onchange="this.form.submit()">
                            <option value=""><?php echo Text::_('JOPTION_SELECT_CATEGORY'); ?></option>
                            <?php echo HTMLHelper::_('select.options', HTMLHelper::_('category.options', 'com_userpoints'), 'value', 'text', $this->lists['filter_category_id']); ?>
                        </select>
                        <?php
                        echo $this->lists['filter_state'];
                        ?>
                    </td>
                </tr>
            </table>
        </div>
        <div class="clearfix"> </div>
        <table class="table table-striped table-hover">
            <thead>
                <tr>
                    <th width="2%" class="title">
                        <?php echo Text::_('NUM'); ?>
                    </th>
                    <th width="3%" class="title">
                        <input type="checkbox" name="checkall-toggle" value="" title="<?php echo Text::_('JGLOBAL_CHECK_ALL'); ?>" onclick="Joomla.checkAll(this)" />
                    </th>
                    <th width="20%" class="title">
                        <?php echo Text::_('AUP_CODE'); ?>
                    </th>
                    <th width="10%" class="title" nowrap="nowrap">
                        <?php echo Text::_('AUP_POINTS'); ?>
                    </th>
                    <th width="10%" class="title" nowrap="nowrap">
                        <?php echo Text::_('JCATEGORY'); ?>
                    </th>				
                    <th width="15%" class="title" nowrap="nowrap">
                        <?php echo Text::_('AUP_EXPIRE'); ?>
                    </th>
                    <th class="title" >
                        <?php echo Text::_('AUP_DESCRIPTION'); ?>
                    </th>
                    <th width="5%" class="title" >
                        <?php echo Text::_('AUP_PUBLIC'); ?>
                    </th>
                    <th width="60" class="title" >
                        <?php echo Text::_('AUP_PRINTABLE'); ?>
                    </th>
                    <th width="50" class="title" >
                        <?php echo Text::_('AUP_SCANNED'); ?>
                    </th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                    <td colspan="15">
                        <?php echo $this->pagination->getListFooter(); ?>
                    </td>
                </tr>
            </tfoot>
            <tbody>
                <?php
                $db = SQL::getInstance();

                $k = 0;

                $img = '<img src="images/tick.png" border="0" alt="' . Text::_('AUP_PUBLIC') . '" /></a>';

                $img = '<img src="components/com_userpoints/assets/images/tick.png" border="0" alt="' . Text::_('AUP_PUBLIC') . '" /></a>';

                for ($i = 0, $n = count($this->couponcodes); $i < $n; $i++) {
                    $row = & $this->couponcodes[$i];

                    $link = 'index.php?option=com_userpoints&amp;task=editcoupon&amp;cid[]=' . $row->id . '';

                    $nullDate = SQL::nulldatetimeSQL();

                    // check if the coupon is already awarded		
                    $where = $row->public ? "keyreference LIKE '" . strtoupper($row->couponcode) . "##%'" : "keyreference='" . strtoupper($row->couponcode) . "'";
                    $query = "SELECT * FROM #__alpha_userpoints_details WHERE $where AND enabled='1' AND rule_id ='sysplgup_couponpointscodes'";
                    $db->setQuery($query);
                    $resultCoupons = $db->loadObjectList();
                    ?>
                    <tr class="<?php echo "row$k"; ?>">
                        <td>
    <?php echo $i + 1 + $this->pagination->limitstart; ?>
                        </td>
                        <td align="center">
    <?php
    echo HTMLHelper::_('grid.id', $i, $row->id);
    ?>
                        </td>
                        <td>					
    <?php
    if ($resultCoupons) {
        if (!$row->public) {
            echo "<b><s>" . Text::_(strtoupper($row->couponcode)) . "</s></b><br />";
        } else {
            if (UserPoints::getUser()->authorise('core.edit', 'com_userpoints')) {
                echo '<b><a href="' . $link . '">';
            }
            echo Text::_(strtoupper($row->couponcode));
            if (UserPoints::getUser()->authorise('core.edit', 'com_userpoints')) {
                echo '</a></b>';
            }
        }
        // show list user(s)
        echo '<br /><span class="small">' . Text::_('AUP_AWARDED') . ': ';
        foreach ($resultCoupons as $awardedcoupon) {
            echo '<br />&nbsp;&nbsp;-&nbsp;' . $awardedcoupon->referreid . '&nbsp;(' . $this->presentDate('date', $awardedcoupon->insert_date, Text::_('DATE_FORMAT_LC2')) . ')';
        }
        echo '</span>';
    } else {
        if (UserPoints::getUser()->authorise('core.edit', 'com_userpoints')) {
            echo '<a href="' . $link . '">';
        }

        echo Text::_(strtoupper($row->couponcode));

        if (UserPoints::getUser()->authorise('core.edit', 'com_userpoints')) {
            echo '</a>';
        }
    }
    ?>					
                        </td>
                        <td>
                            <div align="right">
    <?php echo $this->getFormattedPointsAdm($row->points); ?>
                            </div>
                        </td>
                        <td>
                            <div align="center">
    <?php echo Text::_($row->category_title); ?>
                            </div>
                        </td>				
                        <td>
                            <div align="center">
    <?php
    if ($row->expires == $nullDate) {
        echo '-';
    } else {
        echo $this->presentDate('date', $row->expires, Text::_('DATE_FORMAT_LC'));
    }
    ?>
                            </div>
                        </td>
                        <td>		
    <?php echo Text::_($row->description); ?>							
                        </td>
                        <td>
                            <div align="center">
    <?php
    $public = ( $row->public ) ? $img : '';
    echo $public;
    ?>
                            </div>						
                        </td>
                        <td>
    <?php
    if ($row->printable) {
    ?>
                            <a href="<?php echo $row->QR(250); ?>"><img src="<?php echo $row->QR(75); ?>" alt="" align="absmiddle" /></a>
    <?php
    }
    ?>
                        </td>
                        <td>
                            <?php
                            if ($row->printable) {
                                $resultQRstats = QRCodeTrack::countByFieldvalue('couponid', $row->id);
                                if ($resultQRstats) {
                                    $linkQRcodestats = 'index.php?option=com_userpoints&amp;task=qrcodestats&amp;id=' . $row->id . '';
                                    echo '<a href="' . $linkQRcodestats . '" title="' . Text::_('AUP_TRACK') . '">';
                                    echo $resultQRstats;
                                    echo '</a>';
                                } else {
                                    echo '0';
                                }
                            }
                            ?>
                        </td>				
                    </tr>
                            <?php
                            $k = 1 - $k;
                        }
                        ?>
            </tbody>
        </table>
        <input type="hidden" name="option" value="com_userpoints" />
        <input type="hidden" name="task" value="couponcodes" />
        <input type="hidden" name="table" value="alpha_userpoints_coupons" />
        <input type="hidden" name="redirect" value="couponcodes" />
        <input type="hidden" name="boxchecked" value="0" />
    </form>
</div>
<!-- Modal Form Coupons Generator-->
<div class="modal hide fade" id="generatorModal" data-toggle="modal">
    <form action="index.php" method="post" name="adminFormGenerator" id="adminFormGenerator" autocomplete="off" style="margin:0;">
        <div class="modal-header">
            <button type="button" role="presentation" class="close" data-dismiss="modal">&times;</button>
            <h3><?php echo Text::_('AUP_COUPONS_GENERATOR'); ?></h3>
        </div>
        <div class="modal-body">
            <div class="form-horizontal">
                <fieldset class="adminform">
                    <div class="control-group">
                        <div class="control-label">
                            <span class="editlinktip hasTip" title="<?php echo Text::_('AUP_NUMBER'); ?>::<?php echo Text::_('AUP_NUMBER_COUPON'); ?>">
<?php echo Text::_('AUP_NUMBER'); ?>:
                            </span>
                        </div>
                        <div class="controls">
                            <input class="inputbox" type="text" name="numbercouponcode" id="numbercouponcode" size="20" maxlength="20" value="20" />
                        </div>
                    </div>
                    <div class="control-group">
                        <div class="control-label">
                            <span class="editlinktip hasTip" title="<?php echo Text::_('AUP_PREFIXE'); ?>::<?php echo Text::_('AUP_PREFIXE'); ?>">
<?php echo Text::_('AUP_PREFIXE'); ?>:
                            </span>
                        </div>
                        <div class="controls">
                            <input class="inputbox" type="text" name="prefixcouponcode" id="prefixcouponcode" size="20" maxlength="20" value="ABC-" />
                        </div>
                    </div>
                    <div class="control-group">
                        <div class="control-label">
                            <span class="editlinktip hasTip" title="<?php echo Text::_('AUP_LENGTH'); ?>::<?php echo Text::_('AUP_NUMBER_RANDOM_CHARS'); ?>">
<?php echo Text::_('AUP_LENGTH'); ?>:
                            </span>
                        </div>
                        <div class="controls">
                            <select name="numrandomchars" id="numrandomchars">
                                <option value="0">0</option>
                                <option value="1">1</option>
                                <option value="2">2</option>
                                <option value="3">3</option>
                                <option value="4">4</option>
                                <option value="5">5</option>
                                <option value="6">6</option>
                                <option value="7">7</option>
                                <option value="8" selected>8</option>
                                <option value="9">9</option>
                                <option value="10">10</option>
                                <option value="12">12</option>
                            </select>
                        </div>
                    </div>


                    <!--<div class="control-group">
                                    <span class="editlinktip hasTip" title="<?php //echo Text::_( 'AUP_SUFFIXE' );  ?>::<?php //echo Text::_('AUP_SUFFIXE');  ?>">
<?php //echo Text::_( 'AUP_SUFFIXE' );  ?>:
                                    </span>
                            <label class="checkbox">
                            <div class="controls">
                            <input type="checkbox" class="checkbox" name="enabledincrement" id="enabledincrement" value="1" />
<?php echo Text::_('AUP_ENABLED_INCREMENT'); ?>
                            </div>
                            </label>
                    </div>-->					
                    <div class="control-group">
                        <div class="control-label">
                            <span class="editlinktip hasTip" title="<?php echo Text::_('AUP_ENABLED_INCREMENT'); ?>::<?php echo Text::_('AUP_ENABLED_INCREMENT'); ?>">
<?php echo Text::_('AUP_ENABLED_INCREMENT'); ?>:
                            </span>
                        </div>
                        <div class="controls">
                            <fieldset id="jform_public" class="radio btn-group"><?php echo $this->lists['enabledincrement']; ?></fieldset>
                        </div>
                    </div>
                    <div class="control-group">
                        <div class="control-label">
                            <span class="editlinktip hasTip" title="<?php echo Text::_('AUP_DESCRIPTION'); ?>::<?php echo Text::_('AUP_DESCRIPTION'); ?>">
<?php echo Text::_('AUP_DESCRIPTION'); ?>:
                            </span>
                        </div>
                        <div class="controls">
                            <input class="inputbox" type="text" name="description" id="description" size="100" maxlength="255" value="" />
                        </div>
                    </div>
                    <div class="control-group">
                        <div class="control-label">
                            <span class="editlinktip hasTip" title="<?php echo Text::_('AUP_POINTS'); ?>::<?php echo Text::_('AUP_POINTS'); ?>">
<?php echo Text::_('AUP_POINTS'); ?>:
                            </span>
                        </div>
                        <div class="controls">
                            <input class="inputbox" type="text" name="points" id="points" size="20" value="" />
                        </div>
                    </div>
                    <div class="control-group">
                        <div class="control-label">
                            <span class="editlinktip hasTip" title="<?php echo Text::_('AUP_EXPIRE'); ?>::<?php echo Text::_('AUP_EXPIRE'); ?>">
<?php echo Text::_('AUP_EXPIRE'); ?>:
                            </span>
                        </div>
                        <div class="controls">
                                <?php echo HTMLHelper::_('calendar', '', 'expires', 'expires', '%Y-%m-%d %H:%M:%S', array('class' => 'inputbox', 'size' => '20', 'maxlength' => '19')); ?>
                        </div>
                    </div>
                    <div class="control-group">
                        <div class="control-label">
                            <span class="editlinktip hasTip" title="<?php echo Text::_('AUP_PUBLIC'); ?>::<?php echo Text::_('AUP_PUBLIC'); ?>">
<?php echo Text::_('AUP_PUBLIC'); ?>:
                            </span>
                        </div>
                        <div class="controls">
                            <fieldset id="jform_public" class="radio btn-group"><?php echo $this->lists['public']; ?></fieldset>
                        </div>
                    </div>
                    <div class="control-group">
                        <div class="control-label">
                            <span class="editlinktip hasTip" title="<?php echo Text::_('AUP_PRINTABLE'); ?>::<?php echo Text::_('AUP_PRINTABLE'); ?>">
<?php echo Text::_('AUP_PRINTABLE'); ?>:
                            </span>
                        </div>
                        <div class="controls">
                            <fieldset id="jform_printable" class="radio btn-group"><?php echo $this->lists['printable']; ?></fieldset>
                        </div>
                    </div>
                </fieldset>
                <input type="hidden" name="option" value="com_userpoints" />
                <input type="hidden" name="couponcode" value=""/>
                <input type="hidden" name="task" value="savecoupongenerator"/>	
            </div>
        </div>
        <div class="modal-footer">
            <button class="btn" type="button" data-dismiss="modal">
<?php echo Text::_('Cancel'); ?>
            </button>
            <button class="btn btn-primary" type="submit" onclick="submit('savecoupongenerator');">
                <?php echo Text::_('Save'); ?>
            </button>

        </div>
    </form>
</div>