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

  $valid_extensions = array('jpeg', 'jpg', 'png', 'gif', 'bmp');

  if(isset($_FILES['itm_img'])){


    $sql_count="SELECT * FROM album_count";
    $result_count=mysqli_query($conn, $sql_count);
    $row_count= mysqli_fetch_array($result_count);

    $album_count = $row_count['count'];

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


    mkdir('../upload/albums/'.$album_count);
    $path = '../upload/albums/'.$album_count.'/';
    $img = htmlspecialchars(trim($_FILES['itm_img']['name']), ENT_QUOTES, 'UTF-8');
    $tmp = htmlspecialchars(trim($_FILES['itm_img']['tmp_name']), ENT_QUOTES, 'UTF-8');

    $album_des = $_POST['album_des'];

    $file_name = '../upload/albums/'.$album_count.'/'.$album_count.'.txt';

    $myfile = fopen($file_name , "w") or die("Unable to open file!");
    //$txt = "John Doe\n";
    fwrite($myfile, $album_des);
    fclose($myfile);

    // get uploaded file's extension
    $ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));

    // can upload same image using rand function
    $final_image = rand(1000,1000000).strtolower($img);

    // check's valid format
    if(in_array($ext, $valid_extensions))
    {
      $path = $path.$final_image; 
    //echo "img logo name".$final_image;
      if(move_uploaded_file($tmp,$path))
      {
    //echo "string";
        $sql_gal = "INSERT INTO wedding_gallery VALUES (NULL, '$album_count', '$final_image' , '$album_count')";
        $result_gal = mysqli_query($conn, $sql_gal);

        $sql = "INSERT INTO album SET title='$title', image='$final_image',folder_name='$album_count'";
        $result = mysqli_query($conn, $sql);
        if ($result) {
          echo "Data inserted";
        }
      }
    }
    else 
    {
      echo 'invalid';
    }
    $album_count2=$album_count+1;
    $sql_cinc = "UPDATE album_count SET count = '$album_count2'";
    $result_cinc = mysqli_query($conn, $sql_cinc);

  }

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

    $title=htmlspecialchars(trim($_POST['title_up']), ENT_QUOTES, 'UTF-8');
    $id = htmlspecialchars(trim($_POST['id_holder']), ENT_QUOTES, 'UTF-8');

    if (isset($_POST['selected_album'])) {
      $selected = 1;
    }else{
      $selected = 0;
    }

    $sql_cinc = "UPDATE album SET title = '$title', selected = '$selected' WHERE  folder_name = '$id'";
    $result_cinc = mysqli_query($conn, $sql_cinc);

    $album_des = $_POST['album_des_up'];

    $file_name = '../upload/albums/'.$id.'/'.$id.'.txt';

    $myfile = fopen($file_name , "w") or die("Unable to open file!");
    //$txt = "John Doe\n";
    fwrite($myfile, $album_des);
    fclose($myfile);
    
    if ($result_cinc) {
      echo "Data inserted";
    }else{
      echo "error";
    }

  }

  if (array_key_exists('load_item_up', $_POST)) {

    $data = array();
    
    $album = htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');

    $sql = "SELECT * FROM album WHERE folder_name = '$album'";
    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_assoc($result);

    $file_name = '../upload/albums/'.$album.'/'.$album.'.txt';
    
    if (file_exists($file_name)) {
      $myfile = fopen($file_name, "r");

      $itm_dis = fgets($myfile);

      $dis_arr = array();

      $dis_arr['description_m'] = $itm_dis;

      $data = array_merge($row, $dis_arr);
    }else{
      $data = array_merge($row, array('description_m' => ''));
    }
    
    echo json_encode($data);

  }

}
?>	
