<?php
session_start();

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

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

if(!isset($_GET['id']) || !is_numeric($_GET['id']))
{
    header("Location: index.php");
    exit;
}

$id=(int)$_GET['id'];

$getPromo=mysqli_query($conn,"
SELECT *
FROM promo_codes
WHERE id='$id'
LIMIT 1
");

if(mysqli_num_rows($getPromo)==0)
{
    header("Location: index.php");
    exit;
}

$promo=mysqli_fetch_assoc($getPromo);

$error="";

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

    $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
    {

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

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

            mysqli_query($conn,"
            UPDATE promo_codes
            SET
            code='$code',
            discount_type='$discount_type',
            discount_value='$discount_value',
            status='$status'
            WHERE id='$id'
            ");

            $_SESSION['success']="Promo Code Updated 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-pen-to-square text-primary"></i>

Edit 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
value="<?php echo htmlspecialchars($promo['code']); ?>">

</div>

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

<label class="form-label">Discount Type</label>

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

<option value="percent" <?php if($promo['discount_type']=="percent") echo "selected"; ?>>
Percentage
</option>

<option value="flat" <?php if($promo['discount_type']=="flat") echo "selected"; ?>>
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
value="<?php echo $promo['discount_value']; ?>">

</div>

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

<label class="form-label">Status</label>

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

<option value="1" <?php if($promo['status']==1) echo "selected"; ?>>
Active
</option>

<option value="0" <?php if($promo['status']==0) echo "selected"; ?>>
Inactive
</option>

</select>

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

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

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

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

Update 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"; ?>