<?php

namespace WHMCS\Module\Server\ProxyCheap;

use WHMCS\Database\Capsule as DB;

class ClientAreaActions
{
        public static function saveAuth($apiparams)
    {
        $data = file_get_contents('php://input');
        $data = json_decode($data, true);

        $input = [];
        $input['proxy_account_id'] = (int)$_GET['proxy_id'];
        $input['newAuthType'] = $data['auth'];
        
       // if($input['newAuthType']==='IP_WHITELIST')
        //{
            $input['ips'] = array_values($data['ipwhitelist']);
        //}

        $setWhitelist = (new \WHMCS\Module\Addon\ProxyCheap\Commands\GetAvailableAuths($apiparams, $input))->post();
        var_dump($setWhitelist);exit;
        logModuleCall('ProxyCheap', 'POST proxies/' . $input['proxy_account_id'] . '/change-authentication-type', $input, $setWhitelist);

        if ($setWhitelist) {
            ob_end_clean();
            die('success');
        } else
            die('false');
    }
    public static function saveWhitelist($apiparams)
    {
        $data = file_get_contents('php://input');
        $data = json_decode($data, true);

        $input = [];
        $input['proxy_account_id'] = (int)$_GET['proxy_id'];
        $input['ips'] = array_values($data['ipwhitelist']);
        $setWhitelist = (new \WHMCS\Module\Addon\ProxyCheap\Commands\WhitelistIP($apiparams, $input))->post();

        logModuleCall('ProxyCheap', 'POST proxies/' . $input['proxy_account_id'] . '/whitelist-ip', $input, $setWhitelist);

        if ($setWhitelist) {
            ob_end_clean();
            die('success');
        } else
            die('false');
    }

    public static function saveExtend($packageid)
    {
        $data = file_get_contents('php://input');

        $proxy_id = $_GET['proxy_id'];
        $service = $_GET['id'];

        $data = json_decode($data, true);

        $addon = DB::table('tbladdons AS a')
            ->join('tblmodule_configuration AS mc', 'mc.entity_id', '=', 'a.id')
            ->where('mc.entity_type', 'addon')->where('mc.value', 'ADDON_EXTENSION')
            ->where('mc.setting_name', 'configoption1')
            ->where('a.module', 'proxycheap')
            ->whereRaw("find_in_set('$packageid',a.packages)")
            ->first(['a.id', 'a.allowqty']);
        if ($addon) {
            $_SESSION['proxycheap']['addons'][$addon->id][] = ['addon_id' => $addon->id, 'proxy_id' => $proxy_id, 'months' => $data['months']];
            $_SESSION['cart']['addons'][] = ['id' => $addon->id, 'productid' => $service, 'qty' => (int)$data['months'], 'allowsQuantity' => $addon->allowqty];

            echo ('done');
            exit;
        }
        return;
    }

    public static function saveBuybw($packageid)
    {
        $data = file_get_contents('php://input');

        $service = $_GET['id'];

        $data = json_decode($data, true);

        $addon = DB::table('tbladdons AS a')
            ->join('tblmodule_configuration AS mc', 'mc.entity_id', '=', 'a.id')
            ->where('mc.entity_type', 'addon')->where('mc.value', 'ADDON_BANDWIDTH')
            ->where('mc.setting_name', 'configoption1')
            ->where('a.module', 'proxycheap')
            ->whereRaw("find_in_set('$packageid',a.packages)")
            ->first(['a.id', 'a.allowqty']);
        if ($addon) {
            $_SESSION['cart']['addons'][] = ['id' => $addon->id, 'productid' => $service, 'qty' => (int)$data['bandwidth'], 'allowsQuantity' => $addon->allowqty];

            echo ('done');
            exit;
        }
        return;
    }
}
