<?php
// Create Database If not exists
$create_db_if_not_sql = "CREATE TABLE IF NOT EXISTS tt_info (
                      InfoID int(11) AUTO_INCREMENT,
                      InfoName varchar(225) COLLATE utf8_unicode_ci DEFAULT NULL,
                      InfoValue mediumtext COLLATE utf8_unicode_ci DEFAULT NULL,
                      TimeUP datetime NOT NULL DEFAULT current_timestamp(),
                      PRIMARY KEY  (InfoID))ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$run_create_db_if_not_sql = $conn->prepare($create_db_if_not_sql);
$run_create_db_if_not_sql->execute();

// Get Data For All Page
$get_bd_sql = "SELECT * from tt_info";
$get_dbinfo = $conn -> prepare($get_bd_sql);
$get_dbinfo->execute();
$get_dbinfo_data=$get_dbinfo->fetchAll(PDO::FETCH_OBJ);

$simple_array = json_decode(json_encode($get_dbinfo_data), true);
$advancenews = [];

foreach ($simple_array as $item) {
    $advancenews[$item['InfoName']] = $item['InfoValue'];
}


// Get Record By ID
function getAdvanceNews($field_id){
  global $conn ; // Import global variables, database connection and form data
  $get_bd_sql = "SELECT InfoValue from tt_info WHERE InfoName = '".$field_id."'";
  $get_dbinfo = $conn -> prepare($get_bd_sql);
  $get_dbinfo->execute();
  $get_dbinfo_data=$get_dbinfo->fetchAll(PDO::FETCH_OBJ);
  if($get_dbinfo->rowCount() > 0)
  {
    $simple_array = json_decode(json_encode($get_dbinfo_data), true);
    // Return Value From Database
    foreach ($simple_array  as $final_array) {
       foreach ($final_array as $final_result) {
         return $final_result; // Return The Value
       }
    }
  }else {
    return 'not_available_create_new';
  }
}

// Update Record (Safe)
function updateAdvanceNews($field_data, $field_id){
    global $conn; // PDO connection

    $entry_sql = "UPDATE tt_info SET InfoValue = :field_data WHERE InfoName = :field_id";
    $entry_doc = $conn->prepare($entry_sql);
    $entry_doc->execute([
        ':field_data' => $field_data,
        ':field_id' => $field_id
    ]);
}

// Add New Record
function addAdvanceNews($field_data, $field_id){
  global $conn ; // Import global variables, database connection and form data
  $entry_sql = "INSERT INTO tt_info ( InfoName, InfoValue) VALUES("."'".$field_id."', "."'".$field_data."')"; // Make  Sql query
  $entry_doc = $conn->prepare($entry_sql); // prepare sql query
  $entry_doc->execute(); // Insert data
  echo '<meta http-equiv="refresh" content="0">';
}

function handleAdvanceNews($field_type, $field_id, $field_data) {
    $retrive_db = getAdvanceNews($field_id);

    if (is_array($field_data)) {
      if ($field_type == 'ads') {
        // If field_data is an array, convert it to a string
        $field_data = implode('<MAHAMUDFUTUREIT>', $field_data);
      }else {
        // If field_data is an array, convert it to a string
        $field_data = implode(',', $field_data);
      }
    }

    if ($retrive_db == 'not_available_create_new') {
        // If not available then add new
        addAdvanceNews($field_data, $field_id);
    } else {
        $form_value = $retrive_db;
        // Update Old Datas
        if (isset($_POST['update'])) {
            updateAdvanceNews($field_data, $field_id);
            $form_value = $field_data; // Return The Value
        }
    }
    return $form_value;
}

function AdvanceNews($advancenews_data){
  global $conn, $site_domain ; // Import global variables, database connection and form data

  foreach ($advancenews_data['fields'] as $single_fields  ) {
    extract($single_fields);
    if ($field_type == 'button') {
      include "fields/button.php";
    }else {

      $form_value = handleAdvanceNews($field_type, $field_id, $field_data);
      include "fields/$field_type.php";
      $field_id = '';
      $field_type = '';
      $field_multiple = '';
      $field_title = '';
      $field_subtitle = '';
      $field_desc = '';
      $field_data = '';
      $field_required = '';
      $field_options = '';
      $field_f_type = '';
      $field_plc_img = '';
      $field_max = '';
      $field_image = '';
      $field_date_formate = '';
      $field_plc_img_size = '';
    }
  }
}
?>
