<?php
session_start();
include_once "../config/database.php";

if(!isset($_SESSION['user_id'])){
    header("Location: /beauty_pos/login.php");
    exit;
}

$error="";
$success="";

if(isset($_POST['submit'])){

    $brand_name = trim(mysqli_real_escape_string($conn,$_POST['brand_name']));

    if($brand_name==""){
        $error="Brand name is required.";
    }else{

        $check=mysqli_query($conn,"
        SELECT id
        FROM brands
        WHERE brand_name='$brand_name'
        ");

        if(mysqli_num_rows($check)>0){

            $error="Brand already exists.";

        }else{

            $insert=mysqli_query($conn,"
            INSERT INTO brands(brand_name)
            VALUES('$brand_name')
            ");

            if($insert){

                header("Location: /beauty_pos/brands/index.php?success=added");
                exit;

            }else{

                $error="Failed to add brand.";

            }

        }

    }

}

include_once "../includes/header.php";
include_once "../includes/sidebar.php";
?>

<div class="overlay" id="overlay"></div>

<div class="main-content">

<?php include_once "../includes/topbar.php"; ?>

<div class="container-fluid">

<div class="card-box rounded-4 shadow p-4">

<div class="d-flex justify-content-between align-items-center mb-4">

<h4>Add Brand</h4>

<a href="/beauty_pos/brands/index.php" class="btn btn-secondary">
Back
</a>

</div>

<?php if($error!=""){ ?>

<div class="alert alert-danger">
<?php echo $error; ?>
</div>

<?php } ?>

<form method="POST">

<div class="row g-3">

<div class="col-md-12">

<label class="form-label">
Brand Name
</label>

<input
type="text"
name="brand_name"
class="form-control"
required
>

</div>

<div class="col-md-12">

<button
type="submit"
name="submit"
class="btn btn-primary">

Save Brand

</button>

</div>

</div>

</form>

</div>

</div>

<?php include_once "../includes/footer.php"; ?>

</div>