<?php
    include '../db/db.php';
    session_start();
    $log=htmlspecialchars(trim($_SESSION['user']), ENT_QUOTES, 'UTF-8');
    if (empty($log)) {
         
      $redirectUrl = "index.php";
      echo "<script type=\"text/javascript\">";
      echo "window.location.href = '$redirectUrl'";
      echo "</script>";
         
    }else{

      $sqlu="SELECT * FROM details_of_users WHERE user='$log' and val='1'";
      $resultu=mysqli_query($conn, $sqlu);
      $countu=mysqli_num_rows($resultu);
      
    if($countu==1){

      $sqlc="SELECT * FROM details_of_users where user='$log'";
      $resultc=mysqli_query($conn, $sqlc);
      while ($rowc = mysqli_fetch_array($resultc)){
        $name=$rowc['id'];
      }

    } else{
      $redirectUrl = "index.php";
      echo "<script type=\"text/javascript\">";
      echo "window.location.href = '$redirectUrl'";
      echo "</script>";

    }}

    if(isset($_POST['daterange']))
    {
      $date = htmlspecialchars(trim($_POST['daterange']), ENT_QUOTES, 'UTF-8');

      $date1=substr($date, 0, 10);
      $date2=substr($date, 13, 23);

      $itm = htmlspecialchars(trim($_POST['item']), ENT_QUOTES, 'UTF-8');


      $sql = mysqli_query($conn, "INSERT INTO special_offers SET item = '$itm' , s_date = '$date1' , e_date = '$date2'"); 
      
      if ($sql) {
        $response['status'] = 'ok';
      }else{
        $response['status'] = 'error';
      }
      
      echo json_encode($response);


    }


    if (array_key_exists('delete_item', $_POST)) {
      //echo "in";
      $id = htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');
      //echo $id;
      $sql = mysqli_query($conn, "DELETE FROM special_offers WHERE id=$id"); 
      if ($sql) {
        $response = 'ok';
      }else{
        $response = 'error';
      }
      
      echo json_encode($response);
      //echo "In";
    }

    if (array_key_exists('load_item_up', $_POST)) {
      $sql = "SELECT * FROM special_offers WHERE id = '".htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8')."'";
    //echo $sql;
      $result = mysqli_query($conn, $sql);
      $row = mysqli_fetch_assoc($result);

      echo json_encode($row);

    }

        if (array_key_exists('del_cat', $_POST)) {
      /*echo $_POST['uname'];*/
      $id = htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');

      $sql = mysqli_query($conn, "DELETE FROM cat WHERE id=$id"); 
      
      if ($sql) {
        $response['status'] = 'ok';
      }else{
        $response['status'] = 'error';
      }
      
      echo json_encode($response);
      //echo "In";
    }

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

      $id_holder = htmlspecialchars(trim($_POST['id_holder']), ENT_QUOTES, 'UTF-8');
      $date = htmlspecialchars(trim($_POST['up_daterange']), ENT_QUOTES, 'UTF-8');

      $date1=substr($date, 0, 10);
      $date2=substr($date, 13, 23);

      echo $sql2 = "UPDATE special_offers SET s_date = '$date1' , e_date = '$date2'  WHERE id= '$id_holder'";
      
      $result2 = mysqli_query($conn, $sql2);

      if ($result2) {
        echo "Data inserted without image";
      }

    }

?>