<?php
require_once('config.php');

// Delete Page
if (isset($_GET['delid'])) {
  $id_for_delete = $_GET['delid'] ;
  if (deleteData('mahamudfutureit_video', 'ID', $id_for_delete)){
    header("Location:video.php");
    exit;
  }else {
    $tt_alert = ttAlert('danger', 'Something went wrong to delete   !');
  }
}

// Add Video
if (isset($_POST['add_video'])) {
  $post_form_data = array(
    "VideoTitle"=>$_POST['VideoTitle'],
    "FeaturedImage"=>$_POST['FeaturedImage'],
    "VideoContent"=>$_POST['VideoContent'],
    "Description"=>$_POST['Description'],
    "Time"=> date("Y-m-d H:i:s"),
  );
  // Insert Database
  $insert_now = InsertData('mahamudfutureit_video', $post_form_data);
  if ($insert_now) {
    $tt_alert = ttAlert('success', 'Video Added !');
  }else {
    $tt_alert = ttAlert('danger', 'Something went wrong to add Video !');
  }
}

//  Edit Video
if (isset($_GET['video_id'])) {
  $video_id_get = $_GET['video_id'];
  $get_videos = showAllttdata("mahamudfutureit_video", "WHERE ID = ".$video_id_get);
  if (!empty($get_videos)) {
    $the_video = $get_videos['0'];

    // Update Video
    if (isset($_POST['update_video'])) {
      $data_for_update = array(
        "VideoTitle"=>$_POST['VideoTitle'],
        "FeaturedImage"=>$_POST['FeaturedImage'],
        "VideoContent"=>$_POST['VideoContent'],
        "Description"=>$_POST['Description'],
      );
      if (UpdateData('mahamudfutureit_video', 'ID', $video_id_get, $data_for_update)) {
        header("Location:video.php?video_id=".$video_id_get);
      }else {
        $tt_alert = ttAlert('danger', 'Something went wrong to Update Video !');
      }
    }
  }
}else {
    $the_video =  [];
}

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="video.php" class="btn btn-primary btn-sm"><i class="bi bi-grid"></i> Add Video </a>
    </div>
  </div>
  <?php echo $tt_alert ?? ''; ?>
  <section class="section mt-4">
    <div class="row">
      <div class="col-md-6">
        <div class="bg-white rounded-2 p-3">
          <?php
          if (!empty($the_video)) {
            ?>
            <form class="" action="" method="post" enctype="multipart/form-data">
              <div class="btn btn-outline-warning text-center w-100 mb-2 ">
                <strong>Your Are Editing Video</strong>
              </div>
              <div class="form-group mb-2">
                <label>Video Title <span class="text-danger">*</span> </label>
                <input value="<?php echo $the_video['VideoTitle'] ?>" name="VideoTitle" type="text" class="form-control" placeholder="Title" required>
              </div>
                <div class='mt-1'>
                  <div class='position-relative'>
                    <label>Video Thumbnail <span class="text-danger">*</span> </label>
                    <img class='img-fluid w-100 bg-white border rounded-2' id='VideoImage_Preview'
                         src='<?php echo $the_video['FeaturedImage'] ?>'
                         style='aspect-ratio: 1280 / 720; object-fit: cover; width: 100%; height: auto; max-height: 720px;'>
                  </div>
                  <div class='m-0 input-group my-2'>
                    <input id='VideoImage' value="<?php echo $the_video['FeaturedImage'] ?>" name='FeaturedImage' type='url' class='form-control rounded-0 border-success' placeholder='Select file'>
                    <a href='#' class='addMediaButton input-group-text border-success bg-success text-white rounded-0' data-media_type='' data-bs-toggle='modal' data-bs-target='#SelectMedia' data-target='#VideoImage'>
                      <i class='bi bi-upload'></i>
                    </a>
                  </div>
                </div>
                <div class="form-group mb-2">
                  <label>Video Link  <small>(Use Facebook/Youtube Video Link  )</small> </label>
                  <input name="VideoContent" class="form-control" value="<?php echo $the_video['VideoContent'] ?>">
                </div>
              <div class="form-group mb-2">
                <label>Video Description</label>
                <textarea name="Description" class="form-control" rows="3"><?php echo $the_video['Description'] ?></textarea>
              </div>
              <button type="submit" class="mt-3 btn btn-primary" name="update_video"> Update Video</button>
            </form>
            <?php
          }else {
            ?>
            <form class="" action="" method="post" enctype="multipart/form-data">
              <h4>Add New Video </h4>
              <div class="form-group mb-2">
                <label>Video Title <span class="text-danger">*</span> </label>
                <input name="VideoTitle" type="text" class="form-control" placeholder="Title" required>
              </div>
                <div class='mt-1'>
                  <div class='position-relative'>
                    <label>Video Thumbnail <span class="text-danger">*</span> </label>
                    <img class='img-fluid w-100 bg-white border rounded-2' id='VideoImage_Preview'
                         src='assets/img/placeholder/photo-placeholder.png'
                         style='aspect-ratio: 1280 / 720; object-fit: cover; width: 100%; height: auto; max-height: 720px;'>
                  </div>
                  <div class='m-0 input-group my-2'>
                    <input id='VideoImage' name='FeaturedImage' type='url' class='form-control rounded-0 border-success' value='' placeholder='Select file'>
                    <a href='#' class='addMediaButton input-group-text border-success bg-success text-white rounded-0' data-media_type='' data-bs-toggle='modal' data-bs-target='#SelectMedia' data-target='#VideoImage'>
                      <i class='bi bi-upload'></i>
                    </a>
                  </div>
                </div>
                <div class="form-group mb-2">
                  <label>Video Link/Iframe (Use Video Link or Iframe)</label>
                  <textarea name="VideoContent" class="form-control" rows="3"> </textarea>
                </div>
              <div class="form-group mb-2">
                <label>Video Description</label>
                <textarea name="Description" class="form-control" rows="3"> </textarea>
              </div>
              <button type="submit" class="mt-3 btn btn-primary" name="add_video"> Publish Video</button>
            </form>
            <?php
          }
          ?>
        </div>
      </div>
      <div class="col-md-6">
        <div class="bg-white rounded-2 ">
          <table  class="tt_datatable" style="width:100%">
            <thead>
                <tr>
                    <th>All Video</th>
                </tr>
            </thead>
            <tbody>
            <?php
            $tt_web_page = showAllttdata("mahamudfutureit_video", " ORDER BY  ID  DESC ");
            if (!empty($tt_web_page)) {
              foreach ($tt_web_page as $the_page) {
                ?>
                <tr>
                    <td>
                      <div class="d-flex align-items-center">
                        <img class="rounded-2 me-2" height="70px"  width="124px" src="<?php echo $the_page['FeaturedImage'] ?>" alt="<?php echo $the_page['VideoTitle'] ?>">
                        <h6>
                          <a target="_blank" href="<?php echo $site_domain ?>video/<?php echo $the_page['ID'] ?>/"><?php echo $the_page['VideoTitle'] ?></a>
                          <div class="action-btn mt-2">
                            <a class="bg-primary" href="video.php?video_id=<?php echo $the_page['ID'] ?>"><i class="bi bi-pencil"></i></a>
                            <a class="bg-danger" data-bs-toggle="modal" data-bs-target="#delete_pop<?php echo $the_page['ID'] ?>" href="#"><i class="bi bi-trash3"></i></a>
                          </div>
                        </h6>
                      </div>
                    </td>
                </tr>
                <?php
                ttPopaction('delete_pop'.$the_page['ID'],'video.php?delid='.$the_page['ID']);
              }
            }
            ?>
            </tbody>
          </table>
        </div>

      </div>
    </div>
  </section>
</main>
<?php require_once('inc/footer.php'); ?>
