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


if(isset($_POST['type']) && $_POST['type']=='create_category'){
     try{
         
         $pdo->beginTransaction(); 
         
        // Details Here
        $name = $_POST['name'];
        $stone_cost = $_POST['stone_cost'];
        $category_hsn_code = $_POST['category_hsn_code'];
        $product_ids = $_POST['product_ids'];
        $status = $_POST['status'];
        
         
        $statement = $pdo->prepare("INSERT INTO `tbl_category`(`name`, `stone_cost`, `category_hsn_code`, `product_ids`, `status`) VALUES (?,?,?,?,?)");
        $result = $statement->execute(array($name, $stone_cost, $category_hsn_code, $product_ids, $status));
         
        if($result){    
            $pdo->commit();
            $return_arr['message']='Category 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_category'){
     try{
         
         $pdo->beginTransaction(); 
         
        // Details Here
        $ids = $_POST['ids'];
        $name = $_POST['name'];
        $stone_cost = $_POST['stone_cost'];
        $category_hsn_code = $_POST['category_hsn_code'];
        $product_ids = $_POST['product_ids'];
        $status = $_POST['status'];
        
        
         
        $statement = $pdo->prepare("UPDATE `tbl_category` SET `name`=?, `status`=?, `stone_cost`=?, `category_hsn_code`=?, `product_ids`=? WHERE id=?");
        $result = $statement->execute(array($name, $status, $stone_cost, $category_hsn_code, $product_ids, $ids));
         
        if($result){    
            $pdo->commit();
            $return_arr['message']='Category 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);
}

if(isset($_POST['type']) && $_POST['type']=='create_sub_category'){
     try{
         
         $pdo->beginTransaction(); 
         
        // Details Here
        $sub_category_name = $_POST['sub_category_name'];
        $sub_category_hsn_code = $_POST['sub_category_hsn_code'];
        $category_id = $_POST['category_id'];
         
        $statement = $pdo->prepare("INSERT INTO `tbl_sub_category`(`name`, `category_id`, `sub_category_hsn_code`, `created_by`, `updated_by`) VALUES (?,?,?,?,?)");
        $result = $statement->execute(array($sub_category_name, $category_id, $sub_category_hsn_code, $session_id, $session_id));
         
        if($result){    
            $pdo->commit();
            $return_arr['message']='Sub Category 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_sub_category'){
     try{
         
         $pdo->beginTransaction(); 
         
        // Details Here
        $ids = $_POST['ids'];
        $sub_category_name = $_POST['sub_category_name'];
        $sub_category_hsn_code = $_POST['sub_category_hsn_code'];
        $category_id = $_POST['category_id'];
        $status = $_POST['status'];
         
        $statement = $pdo->prepare("UPDATE `tbl_sub_category` SET `name`=?, `category_id`=?, `updated_by`=?, `updated_at`=?, `status`=?, `sub_category_hsn_code`=? WHERE id=?");
        $result = $statement->execute(array($sub_category_name, $category_id, $session_id, $updated_at, $status, $sub_category_hsn_code, $ids));
         
        if($result){    
            $pdo->commit();
            $return_arr['message']='Category 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);
}

if(isset($_POST['type']) && $_POST['type']=='create_gst'){
     try{
         
         $pdo->beginTransaction(); 
         
        // Details Here
        $name = $_POST['name'];
        $value = $_POST['value'];
        $status = $_POST['status'];
        
        $statement = $pdo->prepare("SELECT * FROM tbl_gst WHERE gst=?");
		$statement->execute(array($value));
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalData = $statement->rowCount();
		if($totalData>0){
		    $return_arr['status']=400;
            $return_arr['error'] = true;
            $return_arr['message'] = "This GST is Already Existed!";
            print_r(json_encode($return_arr));
            return false;
            exit(); 
		}
         
        $statement = $pdo->prepare("INSERT INTO `tbl_gst`(`name`, `gst`, `status`) VALUES (?,?,?)");
        $result = $statement->execute(array($name, $value, $status));
         
        if($result){    
            $pdo->commit();
            $return_arr['message']='GST 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_gst'){
     try{
         
         $pdo->beginTransaction(); 
         
        // Details Here
        $ids = $_POST['ids'];
        $name = $_POST['name'];
        $value = $_POST['value'];
        $status = $_POST['status'];
        
        $statement = $pdo->prepare("SELECT * FROM tbl_gst WHERE gst=? and id!=?");
		$statement->execute(array($value, $ids));
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalData = $statement->rowCount();
		if($totalData>0){
		    $return_arr['status']=400;
            $return_arr['error'] = true;
            $return_arr['message'] = "This GST is Already Existed!";
            print_r(json_encode($return_arr));
            return false;
            exit(); 
		}
         
        $statement = $pdo->prepare("UPDATE `tbl_gst` SET `name`=?, `gst`=?, `status`=? WHERE id=?");
        $result = $statement->execute(array($name, $value, $status, $ids));
         
        if($result){    
            $pdo->commit();
            $return_arr['message']='GST 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);
}

if(isset($_POST['type']) && $_POST['type']=='create_purity'){
     try{
         
         $pdo->beginTransaction(); 
         
        // Details Here
        $purity_name = $_POST['purity_name'];
        $purity_value = $_POST['purity_value'];
        $product_ids = $_POST['product_ids'];
        $short_name = $_POST['short_name'];
         
        $statement = $pdo->prepare("INSERT INTO `tbl_ids_purity`(`name`, `product_ids`, `value`, `created_by`, `short_name`) VALUES (?,?,?,?,?)");
        $result = $statement->execute(array($purity_name, $product_ids, $purity_value, $session_id, $short_name));
        
        
        $purity_id = $pdo->lastInsertId(); 
        
        
        /*$statement = $pdo->prepare("INSERT INTO `tbl_prices`(`product_id`, `purity_id`, `amount`, 
                                    `created_by`,`created_at`, `updated_at`) VALUES (?,?,?,
                                    ?,?,?)");
                                    
        $result = $statement->execute(array($product_ids, $purity_id, 0, 
                                    $session_id, $updated_at, $updated_at));*/
        
        
         
        if($result){    
            $pdo->commit();
            $return_arr['message']='Purity 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_purity'){
     try{
         
         $pdo->beginTransaction(); 
         
        // Details Here
        $ids = $_POST['ids'];
        $name = $_POST['name'];
        $product_id = $_POST['product_id'];
        $value = $_POST['value'];
        $status = $_POST['status'];
        $short_name = $_POST['short_name'];
         
        $statement = $pdo->prepare("UPDATE `tbl_ids_purity` SET `name`=?, `product_ids`=?, `value`=?, `updated_by`=?, `updated_at`=?, `status`=?, `short_name`=? WHERE id=?");
        $result = $statement->execute(array($name, $product_id, $value, $session_id, $updated_at, $status, $short_name, $ids));
         
        if($result){    
            $pdo->commit();
            $return_arr['message']='Purity 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);
}

if(isset($_POST['type']) && $_POST['type']=='create_wastage'){
     try{
         
         $pdo->beginTransaction(); 
         
        // Details Here
        $name = $_POST['name'];
        $category_id = $_POST['category'];
        $percentage = $_POST['percentage'];
        
       $touch = isset($_POST['touch']) ? $_POST['touch'] : '' ;
       $per_gram_va = isset($_POST['per_gram_va']) ? $_POST['per_gram_va'] : '' ;
      
        $statement = $pdo->prepare("INSERT INTO `tbl_wastage`(`name`, `category_id`, `percentage`, `touch`, `per_gram_va`) VALUES (?,?,?,?,?)");
        $result = $statement->execute(array($name, $category_id, $percentage, $touch, $per_gram_va));
         
        if($result){    
            $pdo->commit();
            $return_arr['message']='Wastage 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_wastage'){
     try{
         
         $pdo->beginTransaction(); 
         
        // Details Here
        $ids = $_POST['ids'];
        $name = $_POST['name'];
        $category_id = $_POST['category_id'];
        $value = $_POST['value'];
        $status = $_POST['status'];
        
        $touch = isset($_POST['touch']) ? $_POST['touch'] : '' ;
        $per_gram_va = isset($_POST['per_gram_va']) ? $_POST['per_gram_va'] : '' ;
         
        $statement = $pdo->prepare("UPDATE `tbl_wastage` SET `name`=?, `category_id`=?, `percentage`=?, `status`=?, `touch`=?, `per_gram_va`=? WHERE id=?");
        $result = $statement->execute(array($name, $category_id, $value, $status, $touch, $per_gram_va, $ids));
         
        if($result){    
            $pdo->commit();
            $return_arr['message']='Wastage 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);
}


if(isset($_POST['field_type']) && $_POST['field_type']=='create_model'){
     try{
         
        $pdo->beginTransaction(); 
         
        // Details Here
        $category_id = $_POST['category_id'];
        $subcategory_id = $_POST['subcategory_id'];
        $model_name = $_POST['model_name'];
        $model_hsn_code = $_POST['model_hsn_code'];
        $status = 1;
        $created_at = date('Y-m-d H:i:s');
         
        $statement = $pdo->prepare("INSERT INTO `tbl_models`(`model_name`, `model_hsn_code`, `category_id`, `subcategory_id`, `status`, `created_at`) VALUES (?,?,?,?,?,?)");
        $result = $statement->execute(array($model_name, $model_hsn_code, $category_id, $subcategory_id, $status, $created_at));
         
        if($result){    
            $pdo->commit();
            $return_arr['message']='Model 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['field_type']) && $_POST['field_type']=='update_model'){
    
     try{
      
        $pdo->beginTransaction(); 
         
        // Details Here
        $ids = $_POST['row_id'];
        $category_id = $_POST['category_id'];
        $subcategory_id = $_POST['subcategory_id'];
        $model_name = $_POST['model_name'];
        $model_hsn_code = $_POST['model_hsn_code'];
        
         
        $statement = $pdo->prepare("UPDATE `tbl_models` SET `model_name`=?, `model_hsn_code`=?, `category_id`=?, `subcategory_id`=?  WHERE id=?");
        $result = $statement->execute(array($model_name, $model_hsn_code, $category_id, $subcategory_id, $ids));
         
        if($result){    
            $pdo->commit();
            $return_arr['message']='Model 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);
}


?>