<?php
require_once('config.php');
$page_title = 'Edit Page '; // Custom Page Title

if (isset($_GET['page_id'])) {
  $page_id_get = $_GET['page_id'];
}else {
  $page_id_get = '';
}

if (isset($_POST['edit_page'])) {
  $data_for_update = array(
    "page_title"=>$_POST['page_title'],
    "page_information"=>$_POST['page_information'],
    "page_status"=>$_POST['page_status'],
    "page_lust_edit"=> date("Y-m-d H:i:s"),
    "page_url"=>$_POST['page_url'],
  );
  if (UpdateData('tt_page', 'id', $page_id_get, $data_for_update)) {
    $tt_alert = ttAlert('success', 'Page Update Successfully !');
  }else {
    $tt_alert = ttAlert('danger', 'Something went wrong to Update Page !');
  }
}

require_once('inc/header.php');
?>
<main id="main" class="main">
  <div class="row">
    <div class="col-6 align-items-center">
      <div class="pagetitle mb-2">
        <h1><?php echo $page_title ?></h1>
      </div>
    </div>
    <div class="col-6 text-end">
        <a href="page.php" class="btn btn-primary btn-sm"><i class="bi bi-grid"></i> All Page </a>
    </div>
  </div>
  <?php echo $tt_alert ?? ''; ?>

  <section class="section mt-4">
    <div class="row">
      <?php
      if ($page_id_get !='') {
        $all_page = showAllttdata("tt_page", "WHERE id = ".$page_id_get);
        if (!empty($all_page)) {
            foreach ($all_page as $this_page) ;
              ?>
              <div class="col-md-12">
                <div class="bg-white rounded-2 p-3">
                  <form class="" action="" method="post" enctype="multipart/form-data">
                    <div class="form-group mb-2">
                      <label>Page Title <span class="text-danger">*</span></label>
                      <input value="<?php echo $this_page['page_title'] ?>" name="page_title" type="text" class="form-control" placeholder="Title" required>
                    </div>
                    <div class="form-group mb-2">
                      <label>Page Content <span class="text-danger">*</span></label>
                      <textarea name="page_information" class="form-control editor" rows="3" required><?php echo $this_page['page_information'] ?></textarea>
                    </div>
                    <div class="form-group mb-2">
                      <label>Page Status</label>
                      <select name="page_status" id='output_category' class="form-control" required>
                        <option  selected value="published" <?php if($this_page['page_status'] == 'published'){   echo "selected";   } ?>>Published</option>
                        <option value="draft" <?php if($this_page['page_status'] == 'draft'){   echo "selected";   } ?>>Draft</option>
                      </select>
                    </div>
                    <div class="form-group">
                      <label>URL (Use only A-Z, a-z, 0-1) <a target="_blank" href="<?php echo $site_domain.$this_page['page_url'] ?>/">View Page</a> </label>
                      <input value="<?php echo $this_page['page_url'] ?>" name="page_url"  id="restrictedInput" oninput="restrictInput(this)" type="text" class="form-control" >
                     </div>
                    <button type="submit" class="mt-3 btn btn-primary" name="edit_page"> Save Changes</button>
                  </form>
                </div>
              </div>
              <?php
        }else {
          ?>
          <div class="col-md-12">
            <h5 class="text-center mb-3">Page Not Found !</h5>
          </div>
          <?php
        }
      }else {
        ?>
        <div class="col-md-12">
            <h5 class="text-center mb-3">Page Not Found !</h5>
        </div>
        <?php
      }
      ?>
    </div>
  </section>
</main>
<?php require_once('inc/footer.php'); ?>
