<?php 
ob_start();
session_start();
include "../inc/config.php";
include "../inc/session.php";


function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[random_int(0, $charactersLength - 1)];
    }
    return $randomString;
}



if(isset($_POST['type']) && $_POST['type']=='create_users'){
     try{
         
        $pdo->beginTransaction(); 
        
        
       $branch_ids = '';
       $client_ids = '';
       if(isset($_POST['branch_ids'])){
           $branch_ids = implode(',',$_POST['branch_ids']);
       }
       if(isset($_POST['client_ids'])){
           $client_ids = implode(',',$_POST['client_ids']);
       }
       
        $name = $_POST['name'];
        $username = $_POST['username'];
        $password = $_POST['password'];
        $email = $_POST['email'];
        $address = $_POST['address'];
        $status = $_POST['status'];
        $user_type = $_POST['user_type'];
        $role = $_POST['role'];
        
        
        if(isset($email) && $email!=""){
            $statementsss = $pdo->prepare("SELECT * FROM `tbl_users` WHERE email=?");
            $resultsss = $statementsss->execute(array($email));
            $totalsss = $statementsss->rowCount();    
            if($totalsss>0){
                $return_arr['status']=400;
                $return_arr['error'] = true;
                $return_arr['message'] = "This Email is Already Existed!";
                print_r(json_encode($return_arr));
                return false;
                exit(); 
            }
        }
       
       
       
        $statement = $pdo->prepare("INSERT INTO tbl_users (`name`, `username`, `password`, `email`, `address`,
                            		`status`, `created_by`, `branch_ids`, `client_ids`,
                            		`user_type_id`, `role_id`) VALUES (?,?,?,?,?,
                            		?,?,?,?,
                            		?,?)");
		$result = $statement->execute(array($name, $username, $password, $email, $address,
                            		$status, $session_id, $branch_ids, $client_ids,
                            		$user_type, $role));
        
            

        if($result){
            $last_inserted_id = $pdo->lastInsertId();
            
            $statement = $pdo->prepare("UPDATE tbl_users SET `username`=? WHERE id=?");
		    $result = $statement->execute(array($last_inserted_id, $last_inserted_id));
            
            $pdo->commit();
            $return_arr['message']='User Added Successfully...';
            $return_arr['status']=200;

        }else{
            $return_arr['message']='Something Went Wrong..';
            $return_arr['status']=400;
        }
            
            
             
     }catch(PDOException $e){
        $pdo->rollBack();
        $return_arr['message']="$e";
        $return_arr['status']=500;
        }   
    echo json_encode($return_arr);
}


if(isset($_POST['type']) && $_POST['type']=='update_users'){
     try{
         
        $pdo->beginTransaction(); 
        
        
       $branch_ids = '';
       $client_ids = '';
       if(isset($_POST['branch_ids'])){
           $branch_ids = implode(',',$_POST['branch_ids']);
       }
       if(isset($_POST['client_ids'])){
           $client_ids = implode(',',$_POST['client_ids']);
       }
       
        $ids = $_POST['ids'];
        $name = $_POST['name'];
        $username = $_POST['username'];
        $password = $_POST['password'];
        $email = $_POST['email'];
        $address = $_POST['address'];
        $status = $_POST['status'];
        $user_type = $_POST['user_type'];
        $role = $_POST['role'];
        
        
        if(isset($email) && $email!=""){
            $statementsss = $pdo->prepare("SELECT * FROM `tbl_users` WHERE email=? and id!=?");
            $resultsss = $statementsss->execute(array($email, $ids));
            $totalsss = $statementsss->rowCount();    
            if($totalsss>0){
                $return_arr['status']=400;
                $return_arr['error'] = true;
                $return_arr['message'] = "This Email is Already Existed!";
                print_r(json_encode($return_arr));
                return false;
                exit(); 
            }
        }
       
       
        if($ids!=1){
        $statement = $pdo->prepare("UPDATE tbl_users SET `name`=?, `password`=?, `email`=?, `address`=?,
                            		`status`=?, `updated_by`=?, `branch_ids`=?, `client_ids`=?,
                            		`user_type_id`=?, `role_id`=? WHERE id=?");
		$result = $statement->execute(array($name, $password, $email, $address,
                            		$status, $session_id, $branch_ids, $client_ids,
                            		$user_type, $role, $ids));
        }
        else if($ids==1){
        $statement = $pdo->prepare("UPDATE tbl_users SET `name`=?, `password`=?, `email`=? WHERE id=?");
		$result = $statement->execute(array($name, $password, $email, $ids));
        }
            

        if($result){
            $last_inserted_id = $ids;
            $pdo->commit();
            $return_arr['message']='User Updated Successfully...';
            $return_arr['status']=200;

        }else{
            $return_arr['message']='Something Went Wrong..';
            $return_arr['status']=400;
        }
            
            
             
     }catch(PDOException $e){
        $pdo->rollBack();
        $return_arr['message']="$e";
        $return_arr['status']=500;
        }   
    echo json_encode($return_arr);
}

?>