<?php

session_name('super_admin_session');

if (!is_dir(dirname(__DIR__) . '/session_data/super')) { mkdir(dirname(__DIR__) . '/session_data/super', 0700, true); } session_save_path(dirname(__DIR__) . '/session_data/super');

session_set_cookie_params(['path' => '/', 'httponly' => true, 'samesite' => 'Strict']);

session_start();

error_reporting(0);

include('includes/dbconnection.php');
require_once 'includes/session_term_helper.php';
$stResult = resolveCurrentSessionTerm($con);
$currentSessionName = $stResult['session_name'];
$currentTermName    = $stResult['term_name'];




if (strlen($_SESSION['sid']) == 0) {

    header('location:logout.php');

}



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

    $code = strtoupper(substr(sha1(time()), 0, 8)); // Generate a random discount code

    $discountAmount = filter_input(INPUT_POST, 'discount_amount', FILTER_VALIDATE_FLOAT);

    $startDate = $_POST['start_date'];

    $expiryDate = $_POST['expiry_date'];



    // Validate input

    if ($discountAmount <= 0 || strtotime($startDate) >= strtotime($expiryDate)) {

        $message = "Invalid discount amount or date range.";

    } else {

        // Insert into database using prepared statement

        $stmt = $con->prepare("INSERT INTO discount_codes (code, discount_amount, start_date, expiry_date) VALUES (?, ?, ?, ?)");

        $stmt->bind_param("sdss", $code, $discountAmount, $startDate, $expiryDate);



        if ($stmt->execute()) {

            $message = "Discount code generated successfully! Code: <strong>$code</strong>";

        } else {

            $message = "Error generating discount code. Please try again.";

        }

        $stmt->close();

    }

}

?>

  <!DOCTYPE html>

  <html>

  <?php @include("includes/head.php"); ?>

  <body class="hold-transition sidebar-mini layout-fixed">

    <div class="wrapper">

      <!-- Navbar -->

      <?php @include("includes/header.php"); ?>

      <!-- /.navbar -->



      <!-- Main Sidebar Container -->

      <?php @include("includes/sidebar.php"); ?>



      <!-- Content Wrapper. Contains page content -->

      <div class="content-wrapper">

        <!-- Content Header (Page header) -->

         <section class="content-header premium-header">

    <div class="container-fluid">

        <div class="header-glass-card">

            <div class="row align-items-center">

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

                    <div class="title-section">

                        <div class="icon-box bg-primary-gradient shadow-sm">

                            <i class="fas fa-tags"></i>

                        </div>

                        <div>

                            <h1 class="premium-title text-primary">

                                Discount Management

                                <span class="premium-subtitle text-secondary">Add Discount</span>

                            </h1>
                            <div class="mt-1">
                              <?php if ($currentSessionName): ?>
                                <span class="badge badge-primary mr-1">
                                  <i class="fas fa-calendar-alt"></i>
                                  <?= htmlspecialchars($currentSessionName) ?>
                                </span>
                              <?php endif; ?>
                              <?php if ($currentTermName): ?>
                                <span class="badge badge-success">
                                  <i class="fas fa-bookmark"></i>
                                  <?= htmlspecialchars($currentTermName) ?>
                                </span>
                              <?php else: ?>
                                <span class="badge badge-warning">
                                  <i class="fas fa-exclamation-triangle"></i> No active term
                                </span>
                              <?php endif; ?>
                            </div>

                        </div>

                    </div>

                </div>

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

                    <nav aria-label="breadcrumb">

                        <ol class="breadcrumb premium-breadcrumb">

                            <li class="breadcrumb-item"><a href="dashboard.php" class="text-primary">Home</a></li>

                            <li class="breadcrumb-item active" aria-current="page">Add Discount</li>

                        </ol>

                    </nav>

                </div>

            </div>

        </div>

    </div>

</section>



        <!-- Main content -->

        <section class="content">

          <div class="container-fluid">

            <div class="row ">

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

                <!-- general form elements -->

                <div class="card card-primary">

                  <div class="card-header">

                    <h5 class="card-title font-weight-bold"><i class="fas fa-tags"></i> Generate Discount Code</h5>

                  </div>

                  <!-- /.card-header -->

                  <!-- form start -->

                  <?php if (isset($message)): ?>

                        <div class="alert alert-info"><?php echo $message; ?></div>

                    <?php endif; ?>

                    <form method="POST" action="">

                    <div class="card-body">

                      <span style="color: brown"><h5>Discount Details</h5></span>

                      <hr>





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

                            <label for="discount_amount">Discount Percentage (%):</label>

                            <input type="text"  class="form-control" id="discount_amount" name="discount_amount" placeholder="Enter discount percentage" required>

                        </div>

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

                            <label for="start_date">Start Date:</label>

                            <input type="datetime-local" class="form-control" id="start_date" name="start_date" required>

                        </div>

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

                            <label for="expiry_date">Expiry Date:</label>

                            <input type="datetime-local" class="form-control" id="expiry_date" name="expiry_date" required>

                        </div>

                    

                        <div class="text-center">

                        <button type="submit" name="generate_discount" class="btn btn-primary"> <i class="fas fa-check-circle"></i> Generate Discount Code</button>

                        </div>

                      </div>

                    </form>



                </div>

                <!-- /.card -->

              </div>

              <!-- /.col -->

            </div>

            <!-- /.row -->

          </div>

          <!-- /.container-fluid -->

        </section>

        <!-- /.content -->

      </div>

      <!-- /.content-wrapper -->

      <?php @include("includes/footer.php"); ?>



    </div>



    <!-- ./wrapper -->

    <?php @include("includes/foot.php"); ?>



   

  </body>

  </html>



