<?php
// initial constants
define('ADMIN_SELECTED_PAGE', 'plugins');

// includes and security
include_once('../../../core/includes/master.inc.php');
include_once(DOC_ROOT . '/' . ADMIN_FOLDER_NAME . '/_local_auth.inc.php');

// prepare payment days
$days = array();
for ($i = 1; $i <= 28; $i++)
{
    $date     = strtotime(date('Y-m-') . str_pad($i, 2, "0", STR_PAD_LEFT) . ' 00:00:00');
    $days[$i] = date('jS', $date);
}

// load plugin details
$pluginId = (int) $_REQUEST['id'];
$plugin   = $db->getRow("SELECT * FROM plugin WHERE id = " . (int) $pluginId . " LIMIT 1");
if (!$plugin)
{
    adminFunctions::redirect(ADMIN_WEB_ROOT . '/plugin_manage.php?error=' . urlencode('There was a problem loading the plugin details.'));
}
define('ADMIN_PAGE_TITLE', $plugin['plugin_name'] . ' Plugin Settings');

// pre-load countries
$countries = $db->getRows('SELECT iso_alpha2, name FROM plugin_reward_country_list ORDER BY name');

// pre-load ppd payout groups
$payout_groups = $db->getRows('SELECT id, group_label, payout_rate FROM plugin_reward_ppd_group ORDER BY id');

// prepare variables
$plugin_enabled    = (int) $plugin['plugin_enabled'];
$payment_lead_time = 60;
$payment_threshold = 50;
$payment_date      = '05';
$ppd_enabled       = 0;
$ppd_group_id      = array();
$ppd_min_file_size = 10;
$pps_enabled       = 0;
$user_percentage   = 30;

// load existing settings
if (strlen($plugin['plugin_settings']))
{
    $plugin_settings = json_decode($plugin['plugin_settings'], true);
    if ($plugin_settings)
    {
        
        $payment_lead_time = $plugin_settings['payment_lead_time'];
        $payment_threshold = $plugin_settings['payment_threshold'];
        $payment_date      = $plugin_settings['payment_date'];
        $ppd_enabled       = (int) $plugin_settings['ppd_enabled'];
        if($ppd_enabled == 1)
        {
            $ppd_min_file_size = (int) $plugin_settings['ppd_min_file_size'];
        }
        $pps_enabled       = (int) $plugin_settings['pps_enabled'];
        if($pps_enabled == 1)
        {
            $user_percentage   = $plugin_settings['user_percentage'];
        }
    }
}

// handle page submissions
if (isset($_REQUEST['submitted']))
{
    // get variables
    $plugin_enabled    = (int) $_REQUEST['plugin_enabled'];
    $plugin_enabled    = $plugin_enabled != 1 ? 0 : 1;
    $payment_lead_time = (int) $_REQUEST['payment_lead_time'];
    $payment_lead_time = $payment_lead_time > 365 ? 365 : $payment_lead_time;
    $payment_threshold = (float) str_replace(array(SITE_CONFIG_COST_CURRENCY_SYMBOL, '$', '£'), '', $_REQUEST['payment_threshold']);
    $payment_threshold = $payment_threshold < 0 ? 0 : $payment_threshold;
    $payment_date      = $_REQUEST['payment_date'];
    $ppd_enabled       = (int) $_REQUEST['ppd_enabled'];
    if($ppd_enabled == 1)
    {
        $ppd_min_file_size      = (int)$_REQUEST['ppd_min_file_size'];
        if (isset($_REQUEST['ppd_group_id']))
        {
            $ppd_group_id = $_REQUEST['ppd_group_id'];
        }
    }
    
    $pps_enabled       = (int) $_REQUEST['pps_enabled'];
    if($pps_enabled == 1)
    {
        $user_percentage   = (int) $_REQUEST['user_percentage'];
        $user_percentage   = $user_percentage > 100 ? 100 : $user_percentage;
    }

    // validate submission
    if (_CONFIG_DEMO_MODE == true)
    {
        adminFunctions::setError(adminFunctions::t("no_changes_in_demo_mode"));
    }
    else
    {
        // validate ppd groups, payouts and countries
        $currentRow = 1;
        $oneBlank   = null;
        foreach ($payout_groups AS $payout_group)
        {
            $group_id = $payout_group['id'];
            if (isset($_REQUEST['group_name_id_' . $group_id]))
            {
                // pickup variables
                $group_name         = $_REQUEST['group_name_id_' . $group_id];
                $payout_rate        = $_REQUEST['payout_rate_id_' . $group_id];
                $selected_countries = $_REQUEST['selected_countries_id_' . $group_id];

                if (strlen($group_name) == 0)
                {
                    adminFunctions::setError(translate::getTranslation('rewards_plugin_error_please_enter_group_name_for_row', 'Please enter a group name for row [[[ROW_NUMBER]]]', 1, array('ROW_NUMBER' => $currentRow)));
                }
                elseif (strlen($payout_rate) == 0)
                {
                    adminFunctions::setError(translate::getTranslation('rewards_plugin_error_please_enter_payout_rate_for_row', 'Please enter the payout rate for row [[[ROW_NUMBER]]]', 1, array('ROW_NUMBER' => $currentRow)));
                }
                elseif (COUNT($selected_countries) == 0)
                {
                    // allow for 1 row with blank coutries for 'default' group.
                    if ($oneBlank == null)
                    {
                        $oneBlank = translate::getTranslation('rewards_plugin_error_please_select_at_least_1_country_for_row', 'Please select at least 1 country for row [[[ROW_NUMBER]]]', 1, array('ROW_NUMBER' => $currentRow));
                    }
                    else
                    {
                        if ($oneBlank != -1)
                        {
                            adminFunctions::setError($oneBlank);
                            $oneBlank = -1;
                        }
                        adminFunctions::setError(translate::getTranslation('rewards_plugin_error_please_select_at_least_1_country_for_row', 'Please select at least 1 country for row [[[ROW_NUMBER]]]', 1, array('ROW_NUMBER' => $currentRow)));
                    }
                }
            }

            $currentRow++;
        }
    }

    // update the settings
    if (adminFunctions::isErrors() == false)
    {
        // compile new settings
        $settingsArr                      = array();
        $settingsArr['payment_lead_time'] = $payment_lead_time;
        $settingsArr['payment_threshold'] = number_format($payment_threshold, 2);
        $settingsArr['payment_date']      = $payment_date;
        $settingsArr['ppd_enabled']       = (int) $ppd_enabled;
        if($ppd_enabled == 1)
        {
            $settingsArr['ppd_min_file_size'] = (int)$ppd_min_file_size;
        }
        $settingsArr['pps_enabled']       = (int) $pps_enabled;
        if($pps_enabled == 1)
        {
            $settingsArr['user_percentage']   = $user_percentage;
        }
        $settings                         = json_encode($settingsArr);

        if($ppd_enabled == 1)
        {
            foreach ($payout_groups AS $payout_group)
            {
                $group_id = $payout_group['id'];
                if (isset($_REQUEST['group_name_id_' . $group_id]))
                {
                    // pickup variables
                    $group_name         = $_REQUEST['group_name_id_' . $group_id];
                    $payout_rate        = $_REQUEST['payout_rate_id_' . $group_id];
                    $selected_countries = $_REQUEST['selected_countries_id_' . $group_id];

                    // update group info
                    $dbUpdate              = new DBObject("plugin_reward_ppd_group", array("group_label", "payout_rate"), 'id');
                    $dbUpdate->group_label = $group_name;
                    $dbUpdate->payout_rate = $payout_rate;
                    $dbUpdate->id          = $group_id;
                    $dbUpdate->update();

                    // set group countries
                    $db->query('DELETE FROM plugin_reward_ppd_group_country WHERE group_id = ' . (int) $group_id);
                    if (COUNT($selected_countries))
                    {
                        foreach ($selected_countries AS $selected_country)
                        {
                            $dbInsert               = new DBObject("plugin_reward_ppd_group_country", array("group_id", "country_code"));
                            $dbInsert->group_id     = (int) $group_id;
                            $dbInsert->country_code = $selected_country;
                            $dbInsert->insert();
                        }
                    }
                }
            }
        }

        // update the plugin
        $dbUpdate                  = new DBObject("plugin", array("plugin_enabled", "plugin_settings"), 'id');
        $dbUpdate->plugin_enabled  = $plugin_enabled;
        $dbUpdate->plugin_settings = $settings;
        $dbUpdate->id              = $pluginId;
        $dbUpdate->update();

        adminFunctions::redirect(ADMIN_WEB_ROOT . '/plugin_manage.php?se=1');
    }
}

// page header
include_once(ADMIN_ROOT . '/_header.inc.php');
?>

<script>
    $(function() {
        // formvalidator
        $("#userForm").validationEngine();

        // date picker
        $("#expiry_date").datepicker({
            "dateFormat": "dd/mm/yy"
        });
        
        showHidePPS();
        showHidePPD();
    });
    
    function showHidePPS()
    {
        if($('#pps_enabled').val() == 1)
        {
            $('.ppsOptions').show();
        }
        else
        {
            $('.ppsOptions').hide();
        }
    }
    
    function showHidePPD()
    {
        if($('#ppd_enabled').val() == 1)
        {
            $('.ppdOptions').show();
        }
        else
        {
            $('.ppdOptions').hide();
        }
    }
</script>

<div class="row clearfix">
    <div class="col_12">
        <div class="sectionLargeIcon" style="background: url(../assets/img/icons/128px.png) no-repeat;"></div>
        <div class="widget clearfix">
            <h2>Plugin Settings</h2>
            <div class="widget_inside">
                <?php echo adminFunctions::compileNotifications(); ?>
                <form method="POST" action="settings.php" name="pluginForm" id="pluginForm" autocomplete="off">
                    <div class="clearfix col_12">
                        <div class="col_4">
                            <h3>Plugin State</h3>
                            <p>Whether the rewards program is available.</p>
                        </div>
                        <div class="col_8 last">
                            <div class="form">
                                <div class="clearfix alt-highlight">
                                    <label>Plugin Enabled:</label>
                                    <div class="input">
                                        <select name="plugin_enabled" id="plugin_enabled" class="medium validate[required]">
                                            <?php
                                            $enabledOptions = array(0 => 'No', 1 => 'Yes');
                                            foreach ($enabledOptions AS $k => $enabledOption)
                                            {
                                                echo '<option value="' . $k . '"';
                                                if ($plugin_enabled == $k)
                                                {
                                                    echo ' SELECTED';
                                                }
                                                echo '>' . $enabledOption . '</option>';
                                            }
                                            ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="clearfix col_12">
                        <div class="col_4">
                            <h3>Outpayment Calculations</h3>
                            <p>Payment lead times, minimum payment threshold and the monthly payout date.</p>
                        </div>
                        <div class="col_8 last">
                            <div class="form">
                                <div class="clearfix">
                                    <label>Payout Lead Time:</label>
                                    <div class="input"><input id="payment_lead_time" name="payment_lead_time" type="text" class="small validate[required]" value="<?php echo adminFunctions::makeSafe($payment_lead_time); ?>"/>&nbsp;days</div>
                                </div>
                                <div class="clearfix alt-highlight">
                                    <label>Min Payout Threshold:</label>
                                    <div class="input"><input id="payment_threshold" name="payment_threshold" type="text" class="small validate[required]" value="<?php echo adminFunctions::makeSafe($payment_threshold); ?>"/>&nbsp;<?php echo SITE_CONFIG_COST_CURRENCY_SYMBOL; ?></div>
                                </div>
                                <div class="clearfix">
                                    <label>Monthly Payout Date:</label>
                                    <div class="input">
                                        <select name="payment_date" id="payment_date">
                                            <?php
                                            foreach ($days AS $k => $day)
                                            {
                                                $k = str_pad($k, 2, "0", STR_PAD_LEFT);
                                                echo '<option value="' . $k . '"';
                                                if ($k == $payment_date)
                                                {
                                                    echo ' SELECTED';
                                                }
                                                echo '>' . $day . '</option>';
                                            }
                                            ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="clearfix col_12">
                        <div class="col_4">
                            <h3>PPS Settings</h3>
                            <p>If PPS (pay per sale) is enabled, what percentage the user earns for each upgrade.</p>
                        </div>
                        <div class="col_8 last">
                            <div class="form">
                                <div class="clearfix alt-highlight">
                                    <label>Enable PPS Scheme:</label>
                                    <div class="input">
                                        <select name="pps_enabled" id="pps_enabled" class="medium validate[required]" onChange="showHidePPS(); return false;">
                                            <?php
                                            $options = array(0 => 'No', 1 => 'Yes');
                                            foreach ($options AS $k => $option)
                                            {
                                                echo '<option value="' . $k . '"';
                                                if ($pps_enabled == $k)
                                                {
                                                    echo ' SELECTED';
                                                }
                                                echo '>' . $option . '</option>';
                                            }
                                            ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="clearfix ppsOptions">
                                    <label>Payout Percentage:</label>
                                    <div class="input"><input id="user_percentage" name="user_percentage" type="text" class="small validate[required]" value="<?php echo adminFunctions::makeSafe($user_percentage); ?>"/>&nbsp;%</div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="clearfix col_12">
                        <div class="col_4">
                            <h3>PPD Settings</h3>
                            <p>If PPD (pay per download) is enabled, manage any group names, outpayment rates and the applicable countries within each.</p>
                        </div>
                        <div class="col_8 last">
                            <div class="form">
                                <div class="clearfix alt-highlight">
                                    <label>Enable PPD Scheme:</label>
                                    <div class="input">
                                        <select name="ppd_enabled" id="ppd_enabled" class="medium validate[required]" onChange="showHidePPD(); return false;">
                                            <?php
                                            $options = array(0 => 'No', 1 => 'Yes');
                                            foreach ($options AS $k => $option)
                                            {
                                                echo '<option value="' . $k . '"';
                                                if ($ppd_enabled == $k)
                                                {
                                                    echo ' SELECTED';
                                                }
                                                echo '>' . $option . '</option>';
                                            }
                                            ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="clearfix ppdOptions">
                                    <label>Minimum Filesize:</label>
                                    <div class="input">
                                        <input id="ppd_min_file_size" name="ppd_min_file_size" type="text" class="small validate[required]" value="<?php echo adminFunctions::makeSafe($ppd_min_file_size); ?>"/>&nbsp;MB
                                        <br/>
                                        <div style="color: #777; font-size: 11px; padding-top: 2px;">Files below this size won't be counted in the PPD scheme. Set to 0 (zero) to ignore.</div>
                                    </div>
                                </div>
                                <?php
                                $tracker = 0;
                                foreach ($payout_groups AS $payout_group)
                                {
                                    // load existing countries
                                    $group_countries   = $db->getRows('SELECT country_code FROM plugin_reward_ppd_group_country WHERE group_id = ' . (int) $payout_group['id']);
                                    $group_country_arr = array();
                                    if ($group_countries)
                                    {
                                        foreach ($group_countries AS $group_country)
                                        {
                                            $group_country_arr[] = $group_country['country_code'];
                                        }
                                    }

                                    $group_id    = $payout_group['id'];
                                    $group_name  = $payout_group['group_label'];
                                    $payout_rate = $payout_group['payout_rate'];

                                    // pickup existing submit
                                    if (COUNT($ppd_group_id))
                                    {
                                        $group_name        = $_REQUEST['group_name_id_' . $group_id];
                                        $payout_rate       = $_REQUEST['payout_rate_id_' . $group_id];
                                        $group_country_arr = $_REQUEST['selected_countries_id_' . $group_id];
                                    }

                                    $rowClass = '';
                                    if ($tracker % 2 == 0)
                                    {
                                        $rowClass = ' alt-highlight';
                                    }
                                    $tracker++;
                                    ?>
                                    <div class="clearfix<?php echo $rowClass; ?> ppdOptions">
                                        <div>
                                            <div style='float: right; width: 60%;'>
                                                <label style='float: left; margin-right: 10px;'>Applies to Countries:</label>
                                                <div>
                                                    <select multiple name="selected_countries_id_<?php echo $payout_group['id']; ?>[]" id="selected_countries" class="xlarge">
                                                        <?php
                                                        foreach ($countries AS $k => $country)
                                                        {
                                                            echo '<option value="' . $country['iso_alpha2'] . '"';
                                                            if (in_array($country['iso_alpha2'], $group_country_arr))
                                                            {
                                                                echo ' SELECTED';
                                                            }
                                                            echo '>' . $country['name'] . '</option>';
                                                        }
                                                        ?>
                                                    </select>
                                                    <br/>
                                                    <div style="color: #777; font-size: 11px; padding-top: 2px; float: right;">Hold ctrl &amp; click to select multiple.</div>
                                                </div>
                                            </div>
                                            <div style='float: left;'>
                                                <label style='margin-right: 18px;'>Group Name:</label>
                                                <input id="group_name" name="group_name_id_<?php echo $payout_group['id']; ?>" type="text" class="small validate[required]" value="<?php echo adminFunctions::makeSafe($group_name); ?>"/>
                                            </div>
                                            <div style='float: left;'>
                                                <label style='margin-right: 18px;'>Payment Per 1,000:</label>
                                                <input id="payout_rate" name="payout_rate_id_<?php echo $payout_group['id']; ?>" type="text" class="small validate[required]" value="<?php echo adminFunctions::makeSafe($payout_rate); ?>"/>&nbsp;<?php echo SITE_CONFIG_COST_CURRENCY_SYMBOL; ?>
                                            </div>
                                            <input name="ppd_group_id[]" type="hidden" value="<?php echo $payout_group['id']; ?>"/>
                                        </div>
                                    </div>
                                    <?php
                                }
                                ?>
                            </div>
                        </div>
                    </div>

                    <div class="clearfix col_12">
                        <div class="col_4">&nbsp;</div>
                        <div class="col_8 last">
                            <div class="clearfix">
                                <div class="input no-label">
                                    <input type="submit" value="Submit" class="button blue">
                                    <input type="reset" value="Reset" class="button grey">
                                </div>
                            </div>
                        </div>
                    </div>

                    <input name="submitted" type="hidden" value="1"/>
                    <input name="id" type="hidden" value="<?php echo $pluginId; ?>"/>
                </form>
            </div>
        </div>   
    </div>
</div>

<?php
include_once(ADMIN_ROOT . '/_footer.inc.php');
?>