<?php 
ob_start();
session_start();
include "../inc/config.php";
$today_date = date('Y-m-d');
//$session_id = $_SESSION['users']['id'];

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_location'){
     try{
         
        $pdo->beginTransaction(); 
        
            $name = $_POST['name'];
            $status = $_POST['status'];
           
        
        $statement = $pdo->prepare("INSERT INTO `tbl_material_type`(`name`, `status`) VALUES (?,?)");
    	$result = $statement->execute(array($name, $status));

        if($result){
            $last_inserted_id = $pdo->lastInsertId();
            $pdo->commit();
            $return_arr['message']='Material Type Details 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_location'){
     try{
         
        $pdo->beginTransaction(); 
        
        
            $ids = $_POST['id'];
            $name = $_POST['name'];
            $status = $_POST['status'];
            
       
       
        $statement = $pdo->prepare("UPDATE `tbl_material_type` SET  `name`=?, `status`=? WHERE id=?");
       	$result = $statement->execute(array($name, $status,$ids));

    
        if($result){
            
            $pdo->commit();
            $return_arr['message']='Material Type Details 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);
}

?>