<?php
$t = 'Продление услуги';
require_once '../../wcore/core.php';
require_once '../../wcore/head.php';
iank();
$id = (isset($_GET['id']) && !empty($_GET['id'])?intval($_GET['id']):0);
$sql = mysqli_query($mysqli,"SELECT * FROM `orders`,`servers` WHERE `oid` = '$id' AND `l_s` = `id` AND `uid` = '$ank->id' LIMIT 1");
if (mysqli_num_rows($sql) == 0){echo msg_err('err',$lang['tarifs_orders_exp_1']);require_once '../../wcore/foot.php';exit();}
$order = mysqli_fetch_object($sql);
if ($order->infinity == 1){
    echo msg_err('err',$lang['orders_infinity_error_exp']);require_once '../../wcore/foot.php';exit();
}
if (isset($_POST['ok'])){
    if ($ank->bits < $order->bits){
        echo msg_err('err',$lang['ord_t_err_bits']);require_once '../../wcore/foot.php';exit();
    }
    if ($_SESSION['csrf_token'] != antixs($_POST['csrf'])){echo msg_err('err',$lang['csrf']);require_once '../../wcore/foot.php';exit();}
    #var_dump($order);
    if (!file_exists(WCORE_ROOT.'/wcore/order_'.$order->order_init.'.php')){echo msg_err('err',$lang['ord_t_err_module']);require_once '../../wcore/foot.php';exit();}
    require_once '../../wcore/order_'.$order->order_init.'.php';
    if (!function_exists('order_'.$order->order_init)) {echo msg_err('err',$lang['ord_t_err_module_s2']);require_once '../../wcore/foot.php';exit();}
    $OI = 'order_'.$order->order_init;
        if ($order->type == 1){
            $_tarifs_type_list = 'vhost';
        } elseif ($order->type == 2){
            $_tarifs_type_list = 'ress';
        } elseif ($order->type == 3){
            $_tarifs_type_list = 'vds';
        } elseif ($order->type == 4){
            $_tarifs_type_list = 'dedic';
        } elseif ($order->type == 5){
            $_tarifs_type_list = 'domain';
        } else {
            $_tarifs_type_list = 'none';
        }
        
        $_unit_t = json_decode(base64_decode($order->data));
        $jcmd = json_decode($OI('on',$order->id,$_unit_t->login,$_tarifs_type_list), true);
        if ($jcmd['status'] == 'ok' || $jcmd['status'] == 'warn'){
            logFile('[ORDER] ['.$order->order_init.'] [SERVER:'.$order->id.'] [OK] Пользователю '.(!isset($jcmd['select'])?$order->name:'N/A').' продлена услуга на '.$order->id.' (Услуга №'.$order->oid.') ');
            mysqli_query($mysqli,"UPDATE `users` SET `bits`=`bits`-'$order->bits' WHERE `id` = '$ank->id'") or die("Ошибка запроса: ".mysqli_error($mysqli));
            $ord_time_upd= ($_tarifs_type_list=='domain'?31536000:2592000);
            mysqli_query($mysqli,"UPDATE `orders` SET `status`='2',`time`=`time`+'".($ord_time_upd)."' WHERE `oid` = '$order->oid'");
            echo msg_err(($jcmd['status']=='ok'?'suc':'warn'),$lang['tarifs_orders_exp_8']);require_once '../../wcore/foot.php';exit();
            
        } else {
            logFile('[ORDER] ['.$order->order_init.'] [SERVER:'.$order->id.'] [ERR] [Code: '.$jcmd['err'].'] '.$jcmd['errmsg'].' '.(isset($jcmd['syserr'])?'{Return: '.$syserr.'}':''));
            echo msg_err('err',$lang['tarifs_orders_exp_10']);require_once '../../wcore/foot.php';exit();
        }
} else {
    echo $twig->render('pages_orders_extension.tpl', array(
        'lang'  =>  $lang,
        'id'    =>  $id,
        'name'  =>  $order->nametar,
        'time'  =>  ptime($order->time),
        'bits'  =>  $order->bits,
        'abits' =>  $ank->bits,
        'deta'  =>  json_decode(base64_decode($order->data)),
        'csrf'  =>  _csrf(),
    )
    );
}
require_once '../../wcore/foot.php';
?>