<?php
session_start();

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

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

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

if(isset($_POST['save_promo']))
{
    $code=mysqli_real_escape_string($conn,trim($_POST['code']));
    $type=mysqli_real_escape_string($conn,$_POST['discount_type']);
    $value=(float)$_POST['discount_value'];

    if($code=="" || $value<=0)
    {
        $error="Please fill all required fields.";
    }
    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) VALUES('$code','$type','$value')");
            $success="Promo code added successfully.";
        }
    }
}

$promo=mysqli_query($conn,"SELECT * FROM promo_codes ORDER BY id DESC");

$customerPoints = mysqli_query($conn,"
SELECT
customer_name,
loyalty_points
FROM customers
ORDER BY loyalty_points DESC, customer_name ASC
LIMIT 5
");

$totalCustomers=mysqli_num_rows(mysqli_query($conn,"SELECT id FROM customers"));

?>

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

<div class="col-12">
<h3 class="fw-bold mb-4">
<i class="fa-solid fa-bullhorn text-primary"></i>
Marketing
</h3>
</div>

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

<div class="col-12">
<div class="alert alert-success rounded-4">
<?php echo $success; ?>
</div>
</div>

<?php } ?>

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

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

<?php } ?>

<div class="col-lg-6 mb-4">

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

<div class="card-header bg-white d-flex justify-content-between align-items-center">

<h5 class="mb-0">
<i class="fa-solid fa-ticket text-warning"></i>
Promo Codes
</h5>

<a
href="add_promo.php"
class="btn btn-sm btn-primary rounded-pill">

<i class="fa-solid fa-plus"></i>

New Code

</a>

</div>

<div class="card-body">

<table class="table align-middle">

<thead>

<tr>

<th>Code</th>
<th>Discount</th>
<th width="80">Action</th>

</tr>

</thead>

<tbody>

<?php

if(mysqli_num_rows($promo)>0)
{

while($row=mysqli_fetch_assoc($promo))
{

?>

<tr>

<td>

<span class="badge bg-light text-dark fs-6">

<?php echo htmlspecialchars($row['code']); ?>

</span>

</td>

<td>

<?php

if($row['discount_type']=="percent")
{

echo '<span class="badge bg-success">'.$row['discount_value'].' % OFF</span>';

}
else
{

echo '<span class="badge bg-primary">Rs. '.$row['discount_value'].' OFF</span>';

}

?>

</td>

<td>

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

<a
href="edit_promo.php?id=<?php echo $row['id']; ?>"
class="btn btn-sm btn-warning"
title="Edit">

<i class="fa-solid fa-pen"></i>

</a>

<a
href="delete_promo.php?id=<?php echo $row['id']; ?>"
class="btn btn-sm btn-danger"
title="Delete"
onclick="return confirm('Are you sure you want to delete this promo code?');">

<i class="fa-solid fa-trash"></i>

</a>

</div>

</td>

</tr>

<?php

}

}
else
{

?>

<tr>

<td colspan="3" class="text-center text-muted">

No Promo Codes Found

</td>

</tr>

<?php

}

?>

</tbody>

</table>

</div>

</div>

</div>
<div class="col-lg-6 mb-4">

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

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

<h5 class="mb-0">

<i class="fa-solid fa-gift text-warning"></i>

Loyalty Program

</h5>

</div>

<div class="card-body">

<div class="alert alert-success rounded-4 mb-4">

<i class="fa-solid fa-lightbulb text-warning"></i>

Every <strong>Rs.100</strong> = <strong>1 Loyalty Point</strong>

</div>

<h6 class="fw-bold mb-3">

Points Summary

</h6>

<div class="table-responsive">

<table class="table align-middle">

<tbody>

<?php

if(mysqli_num_rows($customerPoints)>0)
{

while($customer=mysqli_fetch_assoc($customerPoints))
{

?>

<tr>

<td>

<?php echo htmlspecialchars($customer['customer_name']); ?>

</td>

<td class="text-end">

<span class="badge rounded-pill bg-danger">

<i class="fa-solid fa-star text-warning"></i>

<?php echo (int)$customer['loyalty_points']; ?> pts

</span>

</td>

</tr>

<?php

}

}
else
{

?>

<tr>

<td class="text-center text-muted">

No Customer Data

</td>

</tr>

<?php

}

?>

</tbody>

</table>

</div>

</div>

</div>

</div>

</div>

<div class="row">

<div class="col-12">

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

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

<h5 class="mb-0">

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

Send Campaign

</h5>

</div>

<div class="card-body">

<form
method="POST"
action="send_campaign.php">

<div class="row g-3">

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

<label class="form-label">

Target Audience

</label>

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

<option value="All Customers">

All Customers

</option>

<option value="Premium Customers">

Premium Customers

</option>

<option value="Recent Customers">

Recent Customers

</option>

</select>

</div>

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

<label class="form-label">

Channel

</label>

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

<option value="SMS">

SMS

</option>

<option value="Email">

Email

</option>

<option value="WhatsApp">

WhatsApp

</option>

</select>

</div>

<div class="col-12">

<label class="form-label">

Message

</label>

<textarea
name="message"
rows="5"
class="form-control"
required>Dear {name}, enjoy 20% OFF this weekend. Use promo code GLOW20 🌸</textarea>

</div>

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

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

<div class="card-body">

<h6>Total Customers</h6>

<h3 class="fw-bold">

<?php echo $totalCustomers; ?>

</h3>

</div>

</div>

</div>

<div class="col-md-6 d-flex align-items-end justify-content-end">

<button
type="submit"
name="send_campaign"
class="btn btn-primary px-5 rounded-pill">

<i class="fa-solid fa-paper-plane"></i>

Send Campaign

</button>

</div>

</div>

</form>

</div>

</div>

</div>

</div>
<div class="row mt-4">

<div class="col-12">

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

<div class="card-header bg-white d-flex justify-content-between align-items-center">

<h5 class="mb-0">

<i class="fa-solid fa-clock-rotate-left text-success"></i>

Campaign History

</h5>

</div>

<div class="card-body">

<div class="table-responsive">

<table class="table table-hover align-middle">

<thead>

<tr>

<th width="80">#</th>

<th>Audience</th>

<th>Channel</th>

<th>Message</th>

<th>Status</th>

<th>Date</th>

</tr>

</thead>

<tbody>

<?php

$history=mysqli_query($conn,"SELECT * FROM campaigns ORDER BY id DESC");

if(mysqli_num_rows($history)>0)
{

$no=1;

while($row=mysqli_fetch_assoc($history))
{

?>

<tr>

<td><?php echo $no++; ?></td>

<td>

<?php echo htmlspecialchars($row['audience']); ?>

</td>

<td>

<?php

if($row['channel']=="SMS")
{

echo '<span class="badge bg-primary">SMS</span>';

}
elseif($row['channel']=="Email")
{

echo '<span class="badge bg-success">Email</span>';

}
else
{

echo '<span class="badge bg-dark">WhatsApp</span>';

}

?>

</td>

<td>

<?php echo nl2br(htmlspecialchars($row['message'])); ?>

</td>

<td>

<?php

if($row['status']=="Sent")
{

echo '<span class="badge bg-success">Sent</span>';

}
else
{

echo '<span class="badge bg-warning text-dark">Pending</span>';

}

?>

</td>

<td>

<?php echo date("d M Y h:i A",strtotime($row['created_at'])); ?>

</td>

</tr>

<?php

}

}
else
{

?>

<tr>

<td colspan="6" class="text-center text-muted py-4">

No Campaign History Found.

</td>

</tr>

<?php

}

?>

</tbody>

</table>

</div>

</div>

</div>

</div>

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