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



if(isset($_POST['type']) && $_POST['type']=='create_users'){
     try{
         
        $pdo->beginTransaction(); 
         
        $ids = $_POST['ids'];
        $name = $_POST['name'];
        $email = $_POST['email'];
        $role = $_POST['role'];
        $username = $_POST['username'];
        $password = $_POST['password'];
        $mobile = $_POST['mobile'];
        $address = $_POST['address'];
        $alter_mobile = $_POST['alter_mobile'];
        $emp_code = $_POST['employee_code'];
        $hrm_password = $_POST['hrm_password'];
        
        
        $branch_ids = '';
        if(isset($_POST['branch_ids'])){
            $branch_ids = implode(',',$_POST['branch_ids']);
        }
        
        
         
        if (isset($mobile) && $mobile!=""){
            $statement = $pdo->prepare("SELECT * FROM `tbl_users` WHERE mobile=?");
            $result = $statement->execute(array($mobile));
            $total = $statement->rowCount();    
            if($total>0){
                $return_arr['status']=400;
                $return_arr['error'] = true;
                $return_arr['message'] = "This Mobile Number is Already Existed!";
                print_r(json_encode($return_arr));
                return false;
                exit(); 
            }
        }
        
        if (isset($username) && $username!=""){
            $statement = $pdo->prepare("SELECT * FROM `tbl_users` WHERE username=?");
            $result = $statement->execute(array($username));
            $total = $statement->rowCount();    
            if($total>0){
                $return_arr['status']=400;
                $return_arr['error'] = true;
                $return_arr['message'] = "This User Name is Already Existed!";
                print_r(json_encode($return_arr));
                return false;
                exit(); 
            }
        }
        
        if (isset($emp_code) && $emp_code!=""){
            $statement = $pdo->prepare("SELECT * FROM `tbl_users` WHERE emp_code=?");
            $result = $statement->execute(array($emp_code));
            $total = $statement->rowCount();    
            if($total>0){
                $return_arr['status']=400;
                $return_arr['error'] = true;
                $return_arr['message'] = "This Employee Code is Already Existed!";
                print_r(json_encode($return_arr));
                return false;
                exit(); 
            }
        }
         
        $statement = $pdo->prepare("INSERT INTO `tbl_users`(`name`, `email`, `role`, `username`, `password`, 
                                    `mobile`, `address`, `alter_mobile`, `created_by`, `emp_code`,
                                    `hrm_password`, `branch_ids`) VALUES (?,?,?,?,?,
                                    ?,?,?,?,?,
                                    ?,?)");
        $result = $statement->execute(array($name, $email, $role, $username, $password, 
                                    $mobile, $address, $alter_mobile, $session_id, $emp_code,
                                    $hrm_password, $branch_ids));
         
         
    
        if($result){    
            $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();
        error_log($e);
        $return_arr['message']="$e";
        $return_arr['status']=500;
        }   
    echo json_encode($return_arr);
}










if(isset($_POST['type']) && $_POST['type']=='update_users'){
     try{
         
         $pdo->beginTransaction(); 
         
        $ids = $_POST['ids'];
        $name = $_POST['name'];
        $email = $_POST['email'];
        $role = $_POST['role'];
        $username = $_POST['username'];
        $password = $_POST['password'];
        $mobile = $_POST['mobile'];
        $address = $_POST['address'];
        $alter_mobile = $_POST['alter_mobile'];
        $emp_code = $_POST['employee_code'];
        $hrm_password = $_POST['hrm_password'];
        
        $branch_ids = '';
        if(isset($_POST['branch_ids'])){
            $branch_ids = implode(',',$_POST['branch_ids']);
        }
        
         
        if (isset($mobile) && $mobile!=""){
            $statement = $pdo->prepare("SELECT * FROM `tbl_users` WHERE mobile=? and id!=?");
            $result = $statement->execute(array($mobile, $ids));
            $total = $statement->rowCount();    
            if($total>0){
                $return_arr['status']=400;
                $return_arr['error'] = true;
                $return_arr['message'] = "This Mobile Number is Already Existed!";
                print_r(json_encode($return_arr));
                return false;
                exit(); 
            }
        }
        
        if (isset($username) && $username!=""){
            $statement = $pdo->prepare("SELECT * FROM `tbl_users` WHERE username=? and id!=?");
            $result = $statement->execute(array($username, $ids));
            $total = $statement->rowCount();    
            if($total>0){
                $return_arr['status']=400;
                $return_arr['error'] = true;
                $return_arr['message'] = "This User Name is Already Existed!";
                print_r(json_encode($return_arr));
                return false;
                exit(); 
            }
        }
        
        if (isset($emp_code) && $emp_code!=""){
            $statement = $pdo->prepare("SELECT * FROM `tbl_users` WHERE emp_code=? and id!=?");
            $result = $statement->execute(array($emp_code, $ids));
            $total = $statement->rowCount();    
            if($total>0){
                $return_arr['status']=400;
                $return_arr['error'] = true;
                $return_arr['message'] = "This Employee Code is Already Existed!";
                print_r(json_encode($return_arr));
                return false;
                exit(); 
            }
        }
        

         
        $statement = $pdo->prepare("UPDATE tbl_users SET 
                                    name=?, email=?, role=?, password=?, mobile=?, address=?, 
                                    username=?, `emp_code`=?, `hrm_password`=?, `branch_ids`=?, `alter_mobile`=?
                                    WHERE id=?");
        $result = $statement->execute(array($name, $email, $role, $password, $mobile, $address, 
                                    $username, $emp_code, $hrm_password, $branch_ids, $alter_mobile,
                                    $ids));
         
         
    
        if($result){    
            $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();
        error_log($e);
        $return_arr['message']="$e";
        $return_arr['status']=500;
        }   
    echo json_encode($return_arr);
}


?>