<?php
$page_title = (isset($_GET['subscription_id'])) ? 'Edit Subscription' : 'Add Subscription';
include("includes/header.php");
require_once("thumbnail_images.class.php");

if (isset($_POST['submit']) && isset($_GET['add'])  && PURCHASE == '') {
    if (
        empty(validate_input($_POST['name'])) ||
        empty(validate_input($_POST['duration'])) ||
        empty(validate_input($_POST['plan_type'])) ||
        empty(validate_input($_POST['productCategory'])) ||
        empty(validate_input($_POST['price'])) ||
        empty(validate_input($_POST['plan_desc'], true)) ||
        empty(validate_input($_POST['playstore_id'])) ||
        empty(validate_input($_POST['appstore_id']))
    ) {
        $_SESSION['msg'] = "15";
        $_SESSION['class'] = 'error';
        header("Location:add_subscription.php?add=yes");
        exit;
    }
    $data = array(
        'plan_name'  =>  validate_input($_POST['name']),
        'plan_duration'  =>  validate_input($_POST['duration']),
        'plan_type'  =>  validate_input($_POST['plan_type']),
        'productCategory'  =>  validate_input($_POST['productCategory']),
        'plan_price'  =>  validate_input($_POST['price']),
        'plan_description'  =>  validate_input($_POST['plan_desc'], true),
        'playstore_id'  =>  validate_input($_POST['playstore_id']),
        'appstore_id'  =>  validate_input($_POST['appstore_id']),
    );

    $qry = Insert('tbl_subscription', $data);

    $_SESSION['msg'] = "10";
    header("Location:manage_subscription.php");
    exit;
}
$count = 0;
if (isset($_GET['subscription_id'])) {
    $qry = "SELECT * FROM tbl_subscription where id='" . $_GET['subscription_id'] . "'";
    $result = mysqli_query($mysqli, $qry);
    $row = mysqli_fetch_assoc($result);
    $count = $result->num_rows;
}
if (isset($_POST['submit']) and isset($_POST['subscription_id']) && empty($_POST['PURCHASE'])) {
    if (
        empty(validate_input($_POST['name'])) ||
        empty(validate_input($_POST['duration'])) ||
        empty(validate_input($_POST['plan_type'])) ||
        empty(validate_input($_POST['productCategory'])) ||
        empty(validate_input($_POST['price'])) ||
        empty(validate_input($_POST['plan_desc'], true)) ||
        empty(validate_input($_POST['playstore_id'])) ||
        empty(validate_input($_POST['appstore_id']))
    ) {
        $_SESSION['msg'] = "15";
        $_SESSION['class'] = 'error';
        header("Location:add_subscription.php?subscription_id=" . $_GET['subscription_id']);
        exit;
    }
    $data = array(
        'plan_name'  =>  validate_input($_POST['name']),
        'plan_duration'  =>  validate_input($_POST['duration']),
        'plan_type'  =>  validate_input($_POST['plan_type']),
        'productCategory'  =>  validate_input($_POST['productCategory']),
        'plan_price'  =>  validate_input($_POST['price']),
        'plan_description'  =>  validate_input($_POST['plan_desc'], true),
        'playstore_id'  =>  validate_input($_POST['playstore_id']),
        'appstore_id'  =>  validate_input($_POST['appstore_id']),
    );
    $edit =  Update('tbl_subscription', $data, "WHERE id = '" . $_POST['subscription_id'] . "'");

    $_SESSION['msg'] = "11";
    header("Location:manage_subscription.php");
    exit;
}
?>
<main id="main" class="main">
    <section class="section">
        <div class="row  g-3">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-body g-3"><br>
                        <div class="pagetitle mb-5">
                            <h1><?php _e($page_title); ?></h1>

                        </div><!-- End Page Title -->
                        <!-- Floating Labels Form -->
                        <form class="row g-3" action="" name="addeditcategory" method="post" enctype="multipart/form-data">
                            <br><br>
                            <input type="hidden" name="subscription_id" value="<?php if ($count > 0) {
                                                                                    _e($row['id']);
                                                                                } ?>">
                            <div class="col-12">
                                <label class="form-label">Plan Name</label>
                                <input type="text" class="form-control" id="name" name="name" value="<?php if ($count > 0) {
                                                                                                            _e($row['plan_name']);
                                                                                                        } ?>" class="form-control">
                            </div>
                            <div class="col-12">
                                <label class="form-label">Duration</label>
                                <input type="number" class="form-control" id="duration" name="duration" value="<?php if ($count > 0) {
                                                                                                                    _e($row['plan_duration']);
                                                                                                                } ?>" class="form-control">
                            </div>
                            <div class="col-12" id="subtitle_type">
                                <label class="form-label">Plan Type</label>
                                <select name="plan_type" id="plan_type" class="form-control">
                                    <option value="" selected="" disabled="">--Select Type--</option>
                                    <option value="days" <?php if ($count > 0) { if($row['plan_type'] == "days"){  echo "selected"; } } ?>>Days</option>
                                    <option value="months" <?php if ($count > 0) { if($row['plan_type'] == "months"){ echo "selected"; } } ?>>Months</option>
                                    <option value="years" <?php if ($count > 0) { if($row['plan_type'] == "years"){  echo "selected"; } } ?>>Years</option>
                                </select>
                            </div>
                            <div class="col-12" id="subtitle_type">
                                <label class="form-label">Product Category</label>
                                <select name="productCategory" id="plan_type" class="form-control">
                                    <option value="" selected="" disabled="">--Select Type--</option>
                                    <option value="nonSubscription" <?php if ($count > 0) { if($row['productCategory'] == "nonSubscription"){  echo "selected"; } } ?>>Non Subscription</option>
                                    <option value="subscription" <?php if ($count > 0) { if($row['productCategory'] == "subscription"){ echo "selected"; } } ?>>Subscription</option>
                                </select>
                            </div>
                            <div class="col-12">
                                <label class="form-label">Price</label>
                                <input type="text" class="form-control" id="price" name="price" value="<?php if ($count > 0) {
                                                                                                            _e($row['plan_price']);
                                                                                                        } ?>" class="form-control">
                            </div>
                            <div class="col-12">
                                <label class="form-label">Plan Description</label>
                                <textarea name="plan_desc" id="plan_desc" class="form-control">
                                <?php if ($count > 0) {
                                    _e($row['plan_description'], true);
                                } ?>
                                </textarea>
                                <script>
                                    CKEDITOR.replace('plan_desc');
                                </script>
                            </div>
                            <div class="col-12">
                                <label class="form-label">Play Store Id</label>
                                <input type="text" class="form-control" id="playstore_id" name="playstore_id" value="<?php if ($count > 0) {
                                                                                                                            _e($row['playstore_id']);
                                                                                                                        } ?>" class="form-control">
                            </div>
                            <div class="col-12">
                                <label class="form-label">App Store Id</label>
                                <input type="text" class="form-control" id="appstore_id" name="appstore_id" value="<?php if ($count > 0) {
                                                                                                                        _e($row['appstore_id']);
                                                                                                                    } ?>" class="form-control">
                            </div>
                            <div class="text-center">
                                <button type="submit" name="submit" class="btn button btn-primary <?php _e(PURCHASE); ?>">Save</button>
                                <button type="reset" class="btn btn-secondary">Reset</button>
                            </div>
                        </form>
                    </div>
                </div>

            </div>
        </div>
    </section>

</main><!-- End #main -->
<?php include("includes/footer.php"); ?>