<?php
session_start();

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

include_once "../config/database.php";

$error="";

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

    $code=mysqli_real_escape_string($conn,trim($_POST['code']));
    $discount_type=mysqli_real_escape_string($conn,$_POST['discount_type']);
    $discount_value=(float)$_POST['discount_value'];
    $status=(int)$_POST['status'];

    if($code=="")
    {
        $error="Promo code is required.";
    }
    elseif($discount_value<=0)
    {
        $error="Discount value must be greater than zero.";
    }
    else
    {

        $check=mysqli_query($conn,"
        SELECT id
        FROM promo_codes
        WHERE code='$code'
        ");

        if(mysqli_num_rows($check)>0)
        {
            $error="Promo code already exists.";
        }
        else
        {

            mysqli_query($conn,"
            INSERT INTO promo_codes
            (
                code,
                discount_type,
                discount_value,
                status
            )
            VALUES
            (
                '$code',
                '$discount_type',
                '$discount_value',
                '$status'
            )
            ");

            $_SESSION['success']="Promo Code Added Successfully.";

            header("Location: index.php");
            exit;

        }

    }

}

?>

<?php include_once "../includes/header.php"; ?>
<?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 py-4">

<div class="row justify-content-center">

<div class="col-xl-7 col-lg-8 col-md-10">

<div class="card rounded-4 shadow border-0">

<div class="card-header bg-white">

<h4 class="mb-0">

<i class="fa-solid fa-ticket text-primary"></i>

Add Promo Code

</h4>

</div>

<div class="card-body">

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

<div class="alert alert-danger rounded-3">

<?php echo $error; ?>

</div>

<?php } ?>

<form method="POST">

<div class="row g-3">

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

<label class="form-label">

Promo Code

</label>

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

</div>

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

<label class="form-label">

Discount Type

</label>

<select
name="discount_type"
class="form-select"
required>

<option value="percent">

Percentage

</option>

<option value="flat">

Flat Amount

</option>

</select>

</div>

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

<label class="form-label">

Discount Value

</label>

<input
type="number"
step="0.01"
min="1"
name="discount_value"
class="form-control"
required>

</div>

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

<label class="form-label">

Status

</label>

<select
name="status"
class="form-select">

<option value="1">

Active

</option>

<option value="0">

Inactive

</option>

</select>

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

<div class="d-flex gap-2">

<button
type="submit"
name="save"
class="btn btn-primary rounded-pill px-4">

<i class="fa-solid fa-floppy-disk"></i>

Save Promo

</button>

<a
href="index.php"
class="btn btn-secondary rounded-pill px-4">

<i class="fa-solid fa-arrow-left"></i>

Back

</a>

</div>

</div>

</div>

</form>

</div>

</div>

</div>

</div>

</div>

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