<?php

$table_name = 'blog';
$redirection_page = "index.php?action=$page_name";
$action_name = "action=$page_name";
$type = $_REQUEST['type'];
$edit_id = $_REQUEST['edit_id'];
$delete_id = $_REQUEST['delete_id'];


// For Submitting The Form

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

  //File Upload Codes Starts Here
  $allow = array("jpg", "JPG", "jpeg", "JPEG", "gif", "GIF", "png", "PNG", "pdf", "PDF");
  //1st File
  if($_FILES['photo1']['name'] == "") {
    //echo "No Image"
  } else {

    $photo1=basename($_FILES['photo1']['name']);
    $extension = pathinfo($photo1, PATHINFO_EXTENSION);
    if(in_array($extension,$allow)){
      $target_path = "../mobile/Flutter/assets/uploads/blogs/";
      $photo1 = md5(rand() * time()).'.'.$extension;
      $target_path = $target_path . $photo1;
      move_uploaded_file($_FILES['photo1']['tmp_name'], $target_path);
      $sql = ($photo1!='')?" blog_img='$photo1' ". ',':'';
    }
  }
  

  $field1    =  $_POST['field1'];
  $field2    =  $_POST['field2'];
  $field3    =  $_POST['field3'];
  $field4    =  $_POST['field4'];
  $field5    =  $_POST['field5'];
  $field6    =  $_POST['field6'];
  $field7    =  $_POST['field7'];
  $field8    =  $_POST['field8'];
  $field9    =  $_POST['field9'];
  $field10    =  $_POST['field10'];


  if ($type == 'Edit') {

    
  $insert_bookings = "UPDATE `$table_name` SET
  $sql
  blog_title   = '" . addslashes($field1) . "',  
  blog_title_as   = '" . addslashes($field2) . "',  
  blog_title_hi   = '" . addslashes($field3) . "',  
  blog_title_bg   = '" . addslashes($field4) . "',  
  blog_desc   = '" . addslashes($field5) . "', 
  blog_desc_as   = '" . addslashes($field6) . "', 
  blog_desc_hi   = '" . addslashes($field7) . "', 
  blog_desc_bg   = '" . addslashes($field8) . "' 
  where id='" . $edit_id . "'";


    $sql_insert = $dbconn->prepare($insert_bookings);
    $sql_insert->execute();


    $message = "Details successfully updated.";
    $status = "success";
    header("Location: $redirection_page&id=$edit_id");
  } else {
    $insert_bookings = "INSERT `$table_name` SET
    $sql
  blog_title   = '" . addslashes($field1) . "',  
  blog_title_as   = '" . addslashes($field2) . "',  
  blog_title_hi   = '" . addslashes($field3) . "',  
  blog_title_bg   = '" . addslashes($field4) . "',  
  blog_desc   = '" . addslashes($field5) . "', 
  blog_desc_as   = '" . addslashes($field6) . "', 
  blog_desc_hi   = '" . addslashes($field7) . "', 
  blog_desc_bg   = '" . addslashes($field8) . "' ,
  status   = 'Active'";


    $sql_insert = $dbconn->prepare($insert_bookings);
    $sql_insert->execute();
    $myid = $dbconn->lastInsertId();

    $message = "Details successfully updated.";
    $status = "success";
    header("Location: $redirection_page&id=$myid");
  }
}

// For Displaying the table

if (isset($_GET['pageno'])) {
  $pageno = $_GET['pageno'];
} else {
  $pageno = 1;
}
$no_of_records_per_page = 7;
$offset = ($pageno - 1) * $no_of_records_per_page;


$select_enquiry = "SELECT * FROM $table_name where status = 'Active' order by id desc";
$sql = $dbconn->prepare($select_enquiry);
$sql->execute();


$total_rows = $sql->fetchColumn();
$total_pages = ceil($total_rows / $no_of_records_per_page);

$select_enquiry = "SELECT * FROM $table_name where status = 'Active' order by id desc LIMIT $offset, $no_of_records_per_page";
$sql = $dbconn->prepare($select_enquiry);
$sql->execute();
$wlvd = $sql->fetchAll(PDO::FETCH_OBJ);


$select_bookings = "SELECT * FROM `$table_name` WHERE id = '$edit_id'";
$sql1 = $dbconn->prepare($select_bookings);
$sql1->execute();
$wlvd1 = $sql1->fetchAll(PDO::FETCH_OBJ);
foreach ($wlvd1 as $rows1);


?>


<div class="container-fluid">
  <div class="content-wrapper">
    <div class="container-fluid">
      <!-- Breadcrumb-->
      <div class="row pt-2 pb-2">
        <div class="col-sm-9">
          <h4 class="page-title"><?php echo $page_name; ?></h4>
          <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="index.php?action=Welcome">Dashboard</a></li>
            <li class="breadcrumb-item active" aria-current="page"><a href="index.php?action=<?php echo $page_name; ?>"><?php echo $page_name; ?></a></li>
          </ol>
        </div>
        <div class="col-sm-3">
          <?php include('plugins/alert.php'); ?>
          <!-- Button trigger modal -->
        </div>
      </div>
      <!-- End Breadcrumb-->
      <?php if ($type == 'Delete') {

        $delete_statement = "UPDATE `$table_name` SET
      status   = 'Inactive'
      where id='" . $delete_id . "'";


        $sql_insert = $dbconn->prepare($delete_statement);
        $sql_insert->execute();


        $message = "Details successfully updated.";
        $status = "success";
        header("Location: $redirection_page&id=$delete_id");
      } else {
      ?>
        <div class="row">
          <div class="col-lg-12">
            <div class="card">
              <div class="card-header"><i class="fa fa-user"></i>
                <?php
                if ($type == 'Edit') {
                  echo ("You Are Editing A Record");
                } else {
                  echo "You Are Adding A New Record";
                }; ?>
              </div>
              <div class="card-body">
                <div class="table-responsive">

                  <form accept-charset="UTF-8" id="formID" class="m-t-30" method="POST" action="" enctype="multipart/form-data">
                    <div class="row">
                      <div class="col-md-12">
                        <div class="form-group">
                          <label for="inv_phone">Blog Title(English) </label>
                          <input name="field1" type="text" class="form-control" id="field1" value="<?php echo $rows1->blog_title; ?>">
                          <small id="emailHelp" class="form-text text-muted"></small>
                        </div>
                      </div>
                      <div class="col-md-12">
                        <div class="form-group">
                          <label for="inv_phone">Blog Title(Assamese) </label>
                          <input name="field2" type="text" class="form-control" id="field1" value="<?php echo $rows1->blog_title_as; ?>">
                          <small id="emailHelp" class="form-text text-muted"></small>
                        </div>
                      </div>
                      <div class="col-md-12">
                        <div class="form-group">
                          <label for="inv_phone">Blog Title(Hindi) </label>
                          <input name="field3" type="text" class="form-control" id="field1" value="<?php echo $rows1->blog_title_hi; ?>">
                          <small id="emailHelp" class="form-text text-muted"></small>
                        </div>
                      </div>
                      
                      <div class="col-md-12">
                        <div class="form-group">
                          <label for="inv_phone">Blog Title(Bengali) </label>
                          <input name="field4" type="text" class="form-control" id="field1" value="<?php echo $rows1->blog_title_bg; ?>">
                          <small id="emailHelp" class="form-text text-muted"></small>
                        </div>
                      </div>
                      
                      <div class="col-md-12">
                        <div class="form-group">
                          <label for="inv_phone">Blog Description (English) </label>
                          <textarea class="form-control" id="editor" name="field5"><?php echo $rows1->blog_desc; ?></textarea>
                          <small id="emailHelp" class="form-text text-muted"></small>
                        </div>
                      </div>
                      
                      <div class="col-md-12">
                        <div class="form-group">
                          <label for="inv_phone">Blog Description (Assamese) </label>
                          <textarea class="form-control" id="editor2" name="field6"><?php echo $rows1->blog_desc_as; ?></textarea>
                          <small id="emailHelp" class="form-text text-muted"></small>
                        </div>
                      </div>
                      
                      
                      <div class="col-md-12">
                        <div class="form-group">
                          <label for="inv_phone">Blog Description (Hindi) </label>
                          <textarea class="form-control" id="editor3" name="field7"><?php echo $rows1->blog_desc_hi; ?></textarea>
                          <small id="emailHelp" class="form-text text-muted"></small>
                        </div>
                      </div>
                      
                      
                      <div class="col-md-12">
                        <div class="form-group">
                          <label for="inv_phone">Blog Description (Hindi) </label>
                          <textarea class="form-control" id="editor4" name="field8"><?php echo $rows1->blog_desc_bg; ?></textarea>
                          <small id="emailHelp" class="form-text text-muted"></small>
                        </div>
                      </div>
                      
                    </div>

                    <div class="col-md-12">
                      <div class="form-group">
                      <label for="inv_phone">Featured Image</label> <a href="https://flaticon.com" target="_blank"  style="color:red"> Download icons from Here </a>
                       <input name="photo1" type="file" id="photo1" class="form-control">
                      <small id="emailHelp" class="form-text text-muted"></small> 
                      </div>
                    </div>


                    <button type="submit" name="submit" value="Submit" class="btn btn-primary btn-lg btn-block waves-effect waves-light m-1">Submit</button>

                  </form>


                </div>
              </div>
            </div>
          </div>

           
        </div>

      <?php
      };
      ?>


    </div><!-- End Row-->


  </div><!--End content-wrapper-->
  <!--Start Back To Top Button-->
  <a href="javaScript:void();" class="back-to-top"><i class="fa fa-angle-double-up"></i> </a>
  <!--End Back To Top Button-->