<?php

function add_offers($sub_category_id, $parent_category_id) {

    global $con;

    $con->query("INSERT INTO `offers` (`sub_category_id`,`parent_category_id`,`date_added`)VALUES ('$sub_category_id','$parent_category_id','" . date("Y-m-d H:i:s") . "')");

    return mysqli_insert_id($con);
}

function view_offers() {

    global $con;

    $query = $con->query("SELECT * FROM `offers` ORDER BY `id` DESC");
    $x=1;
    while ($row = mysqli_fetch_assoc($query)) {

        $offer_id = $row['id'];
        $sub_category_id = $row['sub_category_id'];
        $date = $row['date_added'];
        $parent_category_id=$row['parent_category_id'];

        ?>
        <tr class="gradeX">
            <td><?php echo $x; ?></td>
            <td><?php echo sub_category_data($sub_category_id)["sub_category_name"]; ?></td>
            <td><?php echo sub_category_data($sub_category_id)["sub_category_name_ar"]; ?></td>
            <td><a href="sub_category_view.php?parent_category_id=<?php echo $parent_category_id; ?>"><?php echo get_parent_cat_by_id($parent_category_id)['parent_category_name']; ?></a></td>
            <td><?php echo $date; ?></td>

            <td class="actions">
                <a href="offer_edit.php?offerId=<?php echo $offer_id ?>" class=""><i class="fa fa-pencil"></i></a>
                <a href="<?php echo $offer_id; ?>"  class="on-default remove-row" id="deleteParent"><i class="fa fa-trash-o"></i></a>

            </td>
        </tr>
        <?php
        $x++;
    }

    return mysqli_insert_id($con);
}

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

    include("../connection.php");

    $offers_id = $_POST['delete_offers'];

    $query = $con->query("DELETE FROM `offers` WHERE `id`='$offers_id'");

    if ($query) {

        echo get_success("Deleted successfully");
    }
}
?>
