<?php
    include('../session_timeout.php');
    include "../../db/connection.php";

    $login_user_id = $_SESSION['skybots_user_id'];
    $login_role = $_SESSION['skybots_role'];

    $type = $_POST['type'];

    date_default_timezone_set("Asia/Kolkata");
    $currentdatetime = date("Y-m-d H:i:s");
    $currentdate = date("Y-m-d");
    $currenttime = date("H-i-s");

    if ($type == 'add_brand') {
        $errors = [];

        $brand_id       = trim($_POST['brand_id'] ?? '');
        $brand_type       = trim($_POST['brand_type'] ?? '');

        if ($brand_type === '') { $errors[] = 'brand_type'; }

        if (!empty($errors)) {
            echo json_encode(['status' => 'error', 'errors' => $errors]);
            exit;
        }

        // --- Check duplicate ---
        $check_sql = "SELECT id 
                        FROM brand 
                        WHERE deletion = 0 
                            AND brand = ?";
        $stmt_check = $conn->prepare($check_sql);
        $stmt_check->bind_param("s", $brand_type);
        $stmt_check->execute();
        $result_check = $stmt_check->get_result();

        if ($result_check->num_rows > 0) {
            echo json_encode(['status' => 'exists', 'message' => 'Already Available..!']);
            exit;
        }

        // --- Insert new record ---
        $insert_sql = "INSERT INTO brand (brand_id, brand, added_by, added_time)
                        VALUES ('NA', ?, ?, ?)";
        $stmt_insert = $conn->prepare($insert_sql);
        $stmt_insert->bind_param(
            "sss",
            $brand_type,
            $login_user_id,
            $currentdatetime
        );

        if ($stmt_insert->execute()) {
            $last_id = $conn->insert_id;
            $brand_id = 'brand_' . $last_id;
            
            $update_sql = "UPDATE brand 
                        SET brand_id = ? 
                        WHERE id = ?";
            $stmt_update = $conn->prepare($update_sql);
            $stmt_update->bind_param("si",$brand_id, $last_id);
            $stmt_update->execute();

            echo json_encode(['status' => 'success', 'code' => 11]);
        } else {
            echo json_encode(['status' => 'error', 'message' => $conn->error, 'code' => 00]);
        }

        $stmt_insert->close();
        $stmt_check->close();
    }
    else if ($type == 'edit_brand') {
        $errors = [];

        $brand_id       = trim($_POST['brand_id'] ?? '');
        $brand_type       = trim($_POST['brand_type'] ?? '');
        
        if ($brand_type === '') { $errors[] = 'brand_type'; }

        if (!empty($errors)) {
            echo json_encode(['status' => 'error', 'errors' => $errors]);
            exit;
        }
            
        $update_sql = "UPDATE brand 
                    SET brand = ? 
                    WHERE brand_id = ?";
        $stmt_update = $conn->prepare($update_sql);
        $stmt_update->bind_param("ss",$brand_type, $brand_id);
        if ($stmt_update->execute()) {
            echo json_encode(['status' => 'success', 'code' => 11]);
        } else {
            echo json_encode(['status' => 'error', 'message' => $conn->error, 'code' => 00]);
        }
        
    }
    else if ($type == 'delete_brand') {
        $id = $_POST['id'];
        $sql = "UPDATE brand SET deleted_by = '$login_user_id', deleted_time = '$currentdatetime', deletion = 1 WHERE brand_id = '$id'";
        if ($conn->query($sql) === TRUE) {
            echo "11";
        } else {
            echo $conn->error;
        }
    } 
?>