<?php 
session_start();
include "Class/autoload.php";

if (isset($_REQUEST['act']) and $_REQUEST['act']=='useradd' ){
    // dd($_REQUEST);
    $login = filterdb($_REQUEST['login']);
    $fio = filterdb($_REQUEST['fio']);
    $name = filterdb($_REQUEST['name']);
    $lastname = filterdb($_REQUEST['lastname']);
    $email = filterdb($_REQUEST['email']);
    $phone = filterdb($_REQUEST['phone']);
    $work = filterdb($_REQUEST['work']);
    $bday = filterdb($_REQUEST['bday']);
    $address = filterdb($_REQUEST['address']);
    $zvanie = filterdb($_REQUEST['rank']);
    $role = filterdb($_REQUEST['role']);
    $login = filterdb($_REQUEST['login']);
    $pass = filterdb($_REQUEST['pass']);

    // Начинаем отправлять в базу

    $url = '217.11.177.108:9907/Fines/api/rbdaapi/index.php';
    $secret = 'shohinnewkey';
    $inputdate = date('ymdHis');
    $plate = filterdb($_REQUEST['plate']);
    $data = [
        "action"=>"adduser",
        "inputdate"=>$inputdate,
        "plate"=>$plate,
        "sign"=>md5('adduser'.$plate.$inputdate.$secret),
        "login"=>$login,
        "fio"=>$fio,
        "name"=>$name,
        "lastname"=>$lastname,
        "email"=>$email,
        "phone"=>$phone,
        "work"=>$work,
        "bday"=>$bday,
        "address"=>$address,
        "zvanie"=>$zvanie,
        "role"=>$role,
        "login"=>$login,
        "pass"=>$pass,
		"useradd"=>$_SESSION['username']
    ];
    $jsondata = json_encode($data);
    //echo $jsondata;
    $sendcurl = sendcurl($jsondata,$url);
    $arraydata = customjsondecode($sendcurl);     
    // dd($arraydata);         
    if (isset($arraydata['code']) && $arraydata['code'] == '103') {
        $_SESSION['exists'] = '1';
        $_SESSION['login'] = $login;
        $_SESSION['error'] = 'useradd';
        header("Location:../pages/userlist.php");
        return;
    }
    $_SESSION['exists'] = '0';
    $_SESSION['login'] = $login;
    $_SESSION['fio'] = $fio.' '.$name;
    $_SESSION['error'] = 'useradd';
    header("Location:../pages/userlist.php");





}

if (isset($_REQUEST['act']) and $_REQUEST['act']=='userupdate' ){
    // dd($_REQUEST);
    // return;
    $login = filterdb($_REQUEST['login']);
    $fio = filterdb($_REQUEST['fio']);
    $name = filterdb($_REQUEST['name']);
    $lastname = filterdb($_REQUEST['lastname']);
    $email = filterdb($_REQUEST['email']);
    $phone = filterdb($_REQUEST['phone']);
    $work = filterdb($_REQUEST['work']);
    $bday = filterdb($_REQUEST['bday']);
    $address = filterdb($_REQUEST['address']);
    $zvanie = filterdb($_REQUEST['rank']);
    $role = filterdb($_REQUEST['role']);
    $login = filterdb($_REQUEST['login']);
    $pass = filterdb($_REQUEST['pass']);

    // Начинаем отправлять в базу

    $url = '217.11.177.108:9907/Fines/api/rbdaapi/index.php';
    $secret = 'shohinnewkey';
    $inputdate = date('ymdHis');
    $plate = filterdb($_REQUEST['plate']);
    $data = [
        "action"=>"upduser",
        "inputdate"=>$inputdate,
        "plate"=>$plate,
        "sign"=>md5('upduser'.$plate.$inputdate.$secret),
        "login"=>$login,
        "fio"=>$fio,
        "name"=>$name,
        "lastname"=>$lastname,
        "email"=>$email,
        "phone"=>$phone,
        "work"=>$work,
        "bday"=>$bday,
        "address"=>$address,
        "zvanie"=>$zvanie,
        "role"=>$role,
        "login"=>$login,
        "pass"=>$pass,
		"useradd"=>$_SESSION['username']
    ];
    $jsondata = json_encode($data);
    //echo $jsondata;
    $sendcurl = sendcurl($jsondata,$url);
    $arraydata = customjsondecode($sendcurl);     
    // dd($arraydata);      
    if (isset($arraydata['code']) && $arraydata['code'] == '103') {
        $_SESSION['exists'] = '1';
        $_SESSION['login'] = $login;
        $_SESSION['error'] = 'userupdate';
        header("Location:../pages/userlist.php");
        return;
    }
    $_SESSION['exists'] = '0';
    $_SESSION['login'] = $login;
    $_SESSION['fio'] = $fio.' '.$name;
    $_SESSION['error'] = 'userupdate';
    header("Location:../pages/userlist.php");





}

if (isset($_REQUEST['act']) and $_REQUEST['act']=='addrole' ){
    // dd($_REQUEST);
    $name = filterdb($_REQUEST['name']);
    // Начинаем отправлять в базу

    $url = '217.11.177.108:9907/Fines/api/rbdaapi/index.php';
    $secret = 'shohinnewkey';
    $inputdate = date('ymdHis');
    $plate = filterdb($_REQUEST['plate']);
    $data = [
        "action"=>"addupdroles",
        "inputdate"=>$inputdate,
        "plate"=>$plate,
        "sign"=>md5('addupdroles'.$plate.$inputdate.$secret),
        "name"=>$name,
		"useradd"=>$_SESSION['username']
    ];
    $jsondata = json_encode($data);	
    $sendcurl = sendcurl($jsondata,$url);	 
    $arraydata = customjsondecode($sendcurl);
    
	
    dd($arraydata);
    //return;    
    if (isset($arraydata['code']) && $arraydata['code'] == '103') {
        $_SESSION['exists'] = '1';
        $_SESSION['login'] = $login;
        $_SESSION['error'] = 'addrole';
        //$_SESSION['dopinfo'] = $arraydata['sql'];
        header("Location:../pages/rolelist.php");
        return;
    }
    $_SESSION['exists'] = '0';
    $_SESSION['login'] = $login;
    $_SESSION['fio'] = $fio.' '.$name;
    $_SESSION['error'] = 'addrole';
	//$_SESSION['dopinfo'] = $arraydata['sql'];
    header("Location:../pages/rolelist.php");
	//print_r($_SESSION);
	//return;





}

if (isset($_REQUEST['act']) and $_REQUEST['act']=='updrole' ){
    dd($_REQUEST);
    $name = filterdb($_REQUEST['name']);
    $url = '217.11.177.108:9907/Fines/api/rbdaapi/index.php';
    $secret = 'shohinnewkey';
    $inputdate = date('ymdHis');
    $id = filterdb($_REQUEST['id']);
    $status = filterdb($_REQUEST['status']);
    $data = [
        "action"=>"addupdroles",
        "inputdate"=>$inputdate,
        "id"=>$id,
        "status"=>$status,
        "sign"=>md5('addupdroles'.$plate.$inputdate.$secret),
        "name"=>$name
    ];
    $jsondata = json_encode($data);
    $sendcurl = sendcurl($jsondata,$url);
    $arraydata = customjsondecode($sendcurl);
    // return;     
    // dd($arraydata);      
    if (isset($arraydata['code']) && $arraydata['code'] == '103') {
        $_SESSION['exists'] = '1';
        $_SESSION['login'] = $login;
        $_SESSION['error'] = 'updrole';
        header("Location:../pages/rolelist.php");
        return;
    }
    $_SESSION['exists'] = '0';
    $_SESSION['login'] = $login;
    $_SESSION['fio'] = $fio.' '.$name;
    $_SESSION['error'] = 'updrole';
    header("Location:../pages/rolelist.php");





}


/*if (isset($_REQUEST['act']) and $_REQUEST['act']=='addrole' ){
    // dd($_REQUEST);
    $name = filterdb($_REQUEST['name']);
    // Начинаем отправлять в базу

    $url = '217.11.177.108:9907/Fines/api/rbdaapi/index.php';
    $secret = 'shohinnewkey';
    $inputdate = date('ymdHis');
    $plate = filterdb($_REQUEST['plate']);
    $data = [
        "action"=>"addupdroles",
        "inputdate"=>$inputdate,
        "plate"=>$plate,
        "sign"=>md5('addupdroles'.$plate.$inputdate.$secret),
        "name"=>$name
    ];
    $jsondata = json_encode($data);
    $sendcurl = sendcurl($jsondata,$url);
    $arraydata = customjsondecode($sendcurl);
    // return;     
    // dd($arraydata);      
    if (isset($arraydata['code']) && $arraydata['code'] == '103') {
        $_SESSION['exists'] = '1';
        $_SESSION['login'] = $login;
        $_SESSION['error'] = 'addrole';
        header("Location:../pages/rolelist.php");
        return;
    }
    $_SESSION['exists'] = '0';
    $_SESSION['login'] = $login;
    $_SESSION['fio'] = $fio.' '.$name;
    $_SESSION['error'] = 'addrole';
    header("Location:../pages/rolelist.php");





}*/

if (isset($_REQUEST['act']) and $_REQUEST['act']=='updrank' ){
    dd($_REQUEST);
    $name = filterdb($_REQUEST['name']);
    $url = '217.11.177.108:9907/Fines/api/rbdaapi/index.php';
    $secret = 'shohinnewkey';
    $inputdate = date('ymdHis');
    $id = filterdb($_REQUEST['id']);
    $status = filterdb($_REQUEST['status']);
    $data = [
        "action"=>"addupdrank",
        "inputdate"=>$inputdate,
        "id"=>$id,
        "status"=>$status,
        "sign"=>md5('addupdrank'.$plate.$inputdate.$secret),
        "name"=>$name
    ];
    $jsondata = json_encode($data);
    $sendcurl = sendcurl($jsondata,$url);
    $arraydata = customjsondecode($sendcurl);
    // return;     
    // dd($arraydata);      
    if (isset($arraydata['code']) && $arraydata['code'] == '103') {
        $_SESSION['exists'] = '1';
        $_SESSION['login'] = $login;
        $_SESSION['error'] = 'updrank';
        header("Location:../pages/ranklist.php");
        return;
    }
    $_SESSION['exists'] = '0';
    $_SESSION['login'] = $login;
    $_SESSION['fio'] = $fio.' '.$name;
    $_SESSION['error'] = 'updrank';
    header("Location:../pages/ranklist.php");





}

if (isset($_REQUEST['act']) and $_REQUEST['act']=='addrank' ){
    // dd($_REQUEST);
    $name = filterdb($_REQUEST['name']);
    // Начинаем отправлять в базу

    $url = '217.11.177.108:9907/Fines/api/rbdaapi/index.php';
    $secret = 'shohinnewkey';
    $inputdate = date('ymdHis');
    $plate = filterdb($_REQUEST['plate']);
    $data = [
        "action"=>"addupdrank",
        "inputdate"=>$inputdate,
        "plate"=>$plate,
        "sign"=>md5('addupdrank'.$plate.$inputdate.$secret),
        "name"=>$name
    ];
    $jsondata = json_encode($data);
    $sendcurl = sendcurl($jsondata,$url);
    $arraydata = customjsondecode($sendcurl);
    // return;     
    // dd($arraydata);      
    if (isset($arraydata['code']) && $arraydata['code'] == '103') {
        $_SESSION['exists'] = '1';
        $_SESSION['login'] = $login;
        $_SESSION['error'] = 'addrank';
        header("Location:../pages/ranklist.php");
        return;
    }
    $_SESSION['exists'] = '0';
    $_SESSION['login'] = $login;
    $_SESSION['fio'] = $fio.' '.$name;
    $_SESSION['error'] = 'addrank';
    header("Location:../pages/ranklist.php");





}


if (isset($_REQUEST['act']) and $_REQUEST['act']=='access' ){
    $data = file_get_contents("php://input");
    $accessarray = ($_REQUEST['access']); 
    $access = '';
    foreach($accessarray as $item){
        $access.=$item.',';
    }   
    $access = strtoupper(substr($access, 0, -1));
    
    $id = filterdb($_REQUEST['id']);
    // Начинаем отправлять в базу

    $url = '217.11.177.108:9907/Fines/api/rbdaapi/index.php';
    $secret = 'shohinnewkey';
    $inputdate = date('ymdHis');
    $plate = @filterdb($_REQUEST['plate']);
    $data = [
        "action"=>"setaccess",
        "inputdate"=>$inputdate,
        "id"=>$id,
        "access"=>$access,
        "sign"=>md5('setaccess'.$inputdate.$secret),
    ];
    $jsondata = json_encode($data);
    $sendcurl = sendcurl($jsondata,$url);
    $arraydata = customjsondecode($sendcurl);
    //dd($arraydata);         
   if (isset($arraydata['code']) && $arraydata['code'] == '103') {
        $_SESSION['exists'] = '1';
        $_SESSION['login'] = $login;
        $_SESSION['error'] = 'access';
        header("Location:../pages/rolelist.php");
        return;
    }
    $_SESSION['exists'] = '0';
    $_SESSION['login'] = $login;
    $_SESSION['error'] = 'access';
    header("Location:../pages/rolelist.php");





}

if (isset($_REQUEST['act']) and $_REQUEST['act']=='auth' ){
    $login = filterdb($_REQUEST['login']);
    $password = filterdb($_REQUEST['password']);
    // Начинаем отправлять в базу
    $url = '217.11.177.108:9907/Fines/api/rbdaapi/index.php';
    $secret = 'shohinnewkey';
    $inputdate = date('ymdHis');
    $plate = filterdb($_REQUEST['plate']);
    $data = [
        "action"=>"auth",
        "inputdate"=>$inputdate,
        "login"=>$login,
        "pass"=>$password,
        "sign"=>md5('auth'.$inputdate.$secret),
    ];
    $jsondata = json_encode($data);
    echo $sendcurl = sendcurl($jsondata,$url);
    $arraydata = customjsondecode($sendcurl);
    // dd($arraydata);         
    // return;
    if (isset($arraydata['code']) && $arraydata['code'] == '103') {
        $_SESSION['failauth'] = '1';
        $_SESSION['auth'] = false;
        header("Location:../index.php");
        return;
    }


    $_SESSION['username'] = $arraydata['login'];
    $_SESSION['rank'] = $arraydata['rankname'];
    $_SESSION['name'] = $arraydata['name'];
    $_SESSION['fio'] = $arraydata['surname'];
    $_SESSION['dostup'] = $arraydata['access'];
    $_SESSION['auth'] = true;
    
    header("Location:../pages/dashboard.php");





}

if (isset($_REQUEST['act']) and $_REQUEST['act']=='exit' ){
    session_destroy();
    $_SESSION['auth']= false;
    header("Location:../index.php");
}

if (isset($_REQUEST['act']) and $_REQUEST['act']=='proceeddisput' ){
    //dd($_REQUEST);
    //return;
    $orderid = filterdb($_REQUEST['orderid']);
    $reason = filterdb($_REQUEST['reason']);
    $comment = filterdb($_REQUEST['comment']);
    $inspector = filterdb($_REQUEST['inspector']);
    $phone = filterdb($_REQUEST['phone']);
    $plate = filterdb($_REQUEST['plate']);
    $inspector = filterdb($_REQUEST['inspector']);
    if(isset($_REQUEST['accept'])){$type='1';}
    if(isset($_REQUEST['decline'])){$type='0';}
    // Начинаем отправлять в базу
    $url = '217.11.177.108:9907/Fines/api/rbdaapi/index.php';
    $secret = 'shohinnewkey';
    $inputdate = date('ymdHis');
    $plate = filterdb($_REQUEST['plate']);
    $data = [
        "action"=>"disput",
        "inputdate"=>$inputdate,
        "orderid"=>$orderid,
        "reason"=>$reason,
        "comment"=>$comment,
        "inspector"=>$inspector,
        "phone"=>$phone,
        "plate"=>$plate,
        "type"=>$type,
        "sign"=>md5('disput'.$inputdate.$secret),
    ];
    $jsondata = json_encode($data);
    $sendcurl = sendcurl($jsondata,$url);
    $arraydata = customjsondecode($sendcurl);
    // dd($arraydata);
    // return;
    if (isset($arraydata['code']) && $arraydata['code'] == '103') {
        $_SESSION['failauth'] = '1';
        $_SESSION['auth'] = false;
        header("Location:../disputfines.php");
        return;
    }

    $_SESSION['failauth'] = '1';
    $_SESSION['auth'] = true;
    
    if($type=='1'){
        header("Location:../pages/acceptdisput.php");
    }
    else{

        header("Location:../pages/declinedisput.php");
    }
    
   




}

if (isset($_REQUEST['act']) and $_REQUEST['act']=='autiddisput' ){
    //dd($_REQUEST);
    //return;
    $orderid = filterdb($_REQUEST['orderid']);
    $reason = filterdb($_REQUEST['reason']);
    $comment = filterdb($_REQUEST['comment']);
    $inspector = filterdb($_REQUEST['inspector']);
    $phone = filterdb($_REQUEST['phone']);
    $plate = filterdb($_REQUEST['plate']);
    $inspector = filterdb($_REQUEST['inspector']);
    if(isset($_REQUEST['accept'])){$type='1';}
    if(isset($_REQUEST['decline'])){$type='0';}
    // Начинаем отправлять в базу
    $url = '217.11.177.108:9907/Fines/api/rbdaapi/index.php';
    $secret = 'shohinnewkey';
    $inputdate = date('ymdHis');
    $plate = filterdb($_REQUEST['plate']);
    $data = [
        "action"=>"disputaudit",
        "inputdate"=>$inputdate,
        "orderid"=>$orderid,
        "reason"=>$reason,
        "comment"=>$comment,
        "inspector"=>$inspector,
        "phone"=>$phone,
        "plate"=>$plate,
        "type"=>$type,
        "sign"=>md5('disputaudit'.$inputdate.$secret),
    ];
    $jsondata = json_encode($data);
    $sendcurl = sendcurl($jsondata,$url);
    $arraydata = customjsondecode($sendcurl);
    dd($arraydata);
    return;
    if (isset($arraydata['code']) && $arraydata['code'] == '103') {
        $_SESSION['failauth'] = '1';
        $_SESSION['auth'] = false;
        header("Location:../disputfines.php");
        return;
    }

    $_SESSION['failauth'] = '1';
    $_SESSION['auth'] = true;
    
    if($type=='1'){
        header("Location:../pages/acceptdisput.php");
    }
    else{

        header("Location:../pages/declinedisput.php");
    }
    
   




}
?>