<?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 `servers`,`tarifs` WHERE `servers`.`id`=`tarifs`.`sid` AND `tarifs`.`tid` = '$id' LIMIT 1");
if (mysqli_num_rows($sql) == 0){echo msg_err('err',$lang['ord_t_na']);require_once '../../wcore/foot.php';exit();}
$tarifs = mysqli_fetch_object($sql);
if (isset($_POST['ok'])){
    if ($_SESSION['csrf_token'] != antixs($_POST['csrf'])){echo msg_err('err',$lang['csrf']);require_once '../../wcore/foot.php';exit();}
    if ($tarifs->bits > $ank->bits){echo msg_err('err',$lang['ord_t_err_bits']);require_once '../../wcore/foot.php';exit();}
    if (!file_exists(WCORE_ROOT.'/wcore/order_'.$tarifs->order_init.'.php')){echo msg_err('err',$lang['ord_t_err_module']);require_once '../../wcore/foot.php';exit();}
    require_once '../../wcore/order_'.$tarifs->order_init.'.php';
    if (!function_exists('order_'.$tarifs->order_init)) {echo msg_err('err',$lang['ord_t_err_module_s2']);require_once '../../wcore/foot.php';exit();}
    $OI = 'order_'.$tarifs->order_init;
    if (empty($_POST['domain'])){
        echo msg_err('err','Необходимо заполнить поле с доменом.');
        require_once '../../wcore/foot.php';
        exit();
    }
    if (mysqli_num_rows(mysqli_query($mysqli,"SELECT * FROM `tld24_profile` WHERE `uid` = '$ank->id'")) == 0){
        echo msg_err('err','Необходимо заполнить профайл для выполнения данного действия');
        require_once '../../wcore/foot.php';
        exit();
    }
    $bj = json_decode(base64_decode($tarifs->deskpack));
    $jcmd = json_decode($OI('CheckDomain',9,antixs($_POST['domain'].'.'.$bj->zone)), true);
    if (!isset($jcmd['avail']) || $jcmd['avail'] == 0){
        echo msg_err('err','Домен "'.antixs($_POST['domain'].'.'.$bj->zone).'" для регистрации занят');
        require_once '../../wcore/foot.php';
        exit();
    } 
    
    $profile_id = mysqli_fetch_object(mysqli_query($mysqli,"SELECT * FROM `tld24_profile` WHERE `uid` = '$ank->id'"));
    $jcmd = json_decode($OI('RegisterDomain',9,antixs($_POST['domain'].'.'.$bj->zone),'ns1=ns1.'.domain.'&ns2=ns2.'.domain.'&profileid='.$profile_id->pid), true);
    var_dump($jcmd);
    if ($jcmd['status'] == 'err'){
        $syserr = (isset($jcmd['syserr'][0])?$jcmd['syserr'][0]:false);
        logFile('[ORDER] ['.$tarifs->order_init.'] [SERVER:'.$tarifs->id.'] [ERR] [Code: '.$jcmd['err'].'] '.$jcmd['errmsg'].' '.(isset($jcmd['syserr'])?'{Return: '.$syserr.'}':''));
        echo msg_err('err',$lang['ord_t_err_module_s3']);require_once '../../wcore/foot.php';exit();
    } else {
        logFile('[ORDER] ['.$tarifs->order_init.'] [SERVER:'.$tarifs->id.'] [OK] Услуга обработана '.$jcmd['login'].' на '.$jcmd['url'].' ');
        $data = base64_encode(json_encode(array(
	        'profileid' 	=> 	$jcmd['profileid'],
	        'domain' 	    => 	$jcmd['domain'],
	        'packname'      =>  $tarifs->preset,
	        'tarifs_id'     =>	$tarifs->tid,
	        'server_id'     =>	$tarifs->sid,
	        'order_init'    =>  $tarifs->order_init,
        )));
        $motime = time()+31536000; # 1 месяц
        mysqli_query($mysqli,"INSERT INTO `orders`(`uid`, `nametar`, `data`, `status`, `time`, `bits`,`l_s`) VALUES ('$ank->id','$tarifs->namepack','$data','1','$motime','$tarifs->bits','$tarifs->sid')")or die("Ошибка запроса: ".mysqli_error($mysqli));
        mysqli_query($mysqli,"UPDATE `users` SET `bits`=`bits`-'$tarifs->bits' WHERE `id` = '$ank->id'") or die("Ошибка запроса: ".mysqli_error($mysqli));

        echo msg_err('suc',$lang['ord_t_suc_buy']);require_once '../../wcore/foot.php';exit();
    }
} else {
    echo $twig->render('page_modules_order_buy_domain.tpl', array(
    'lang'  =>  $lang,
    'id'    =>  $id,
    'tarifs'=>  $tarifs,
    'desc'  =>  json_decode(base64_decode($tarifs->deskpack)),
    'csrf'  =>  _csrf(),
    )
    );
}
require_once '../../wcore/foot.php';
?>