<?php
include 'connect.php';
date_default_timezone_set("Asia/Bangkok");
if (isset($_REQUEST['setprepare'])) {

    $date = $_REQUEST['timecycle'];
    if ($date < date('Y-m-d')) {
        $date = date('Y-m-d');
    }

    if ($_REQUEST['type_pay'] == 1) {
        $step = 2;
    } else {
        $step = 3;
    }
    $sql = $con->prepare('select lot from lot where lot >= ? order by lot asc LIMIT ' . $step);
    $sql->execute(array($date));
    $next_lot='';
    foreach ($sql as $c) {
        $next_lot = $c['lot'];
    }

    $sql = $con->prepare('delete from prepare_payment where userid=? and user=? and ip=? and status=?');
    $sql->execute(array($_REQUEST['userid'],$_REQUEST['user'],$_REQUEST['ip'],'รอชำระ'));

    $sql = $con->prepare('insert into prepare_payment(ip,dbuser,dbpass,status,userid,user,amount,tbuser,timecycle,next_lot,isue_date,priroot,subdomain) values (?,?,?,?,?,?,?,?,?,?,?,?,?)');
    $sql->execute(array($_REQUEST['ip'], $_REQUEST['dbuser'], $_REQUEST['dbpass'], 'รอชำระ', $_REQUEST['userid'], $_REQUEST['user'], $_REQUEST['amount'], $_REQUEST['tbuser'],$_REQUEST['timecycle'],$next_lot,date('Y-m-d H:i:s'),$_REQUEST['priroot'],$_REQUEST['subdomain']));
    echo $_REQUEST['ip'];
}

if(isset($_REQUEST['setprepare_del']))
{
    $sql = $con->prepare('delete from prepare_payment where userid=? and user=? and ip=? and status=?');
    $sql->execute(array($_REQUEST['userid'],$_REQUEST['user'],$_REQUEST['ip'],'รอชำระ'));

}
