<?php

/**** this script contains sql statements that manipulates the database*****/

if (!isset($_SESSION)) {
  session_start();
}
include_once('../../config/class.php');
include_once('../../config/config.php');
$con = new DB;

$action = '';
$error  = false;
if (isset($_POST['action'])) {
  $action = $_POST['action'];
}

//print_r($_POST);

//creates user story 
if (isset($_POST['create_story'])) {
  //echo "string";
  $err_msg = '';
  $succ_msg = '';
  $link = $con->get_random_string_max(60);
  $thumb = $_FILES['thumb_image']['name'];
  $image = $_FILES['blog_image']['name'];

  $thumb_ext = substr($thumb, strripos($thumb, '.'));
  $image_ext = substr($image, strripos($image, '.'));

  $thumb_basename = substr($thumb, 0, strripos($thumb, '.'));
  $image_basename = substr($image, 0, strripos($thumb, '.'));

  //give the images new names to avoid overwrite imagesy
  $new_thumb = date('ymdhis') . "_" . $thumb_basename . $thumb_ext;
  $new_image = date('ymdhis') . "_" . $image_basename . $image_ext;

  $allowed_file_types = array('.jpg', '.jpeg', '.png'); //allowed file types
  $thumb_type = $_FILES['thumb_image']['type'];
  $image_type = $_FILES['blog_image']['type'];


  //check if the uploaded file is indeed an images
  if (in_array(strtolower($thumb_ext), $allowed_file_types) && in_array(strtolower($image_ext), $allowed_file_types)) {
    $thumb_temp = $_FILES['thumb_image']['tmp_name'];
    $image_temp = $_FILES['blog_image']['tmp_name'];

    $thumb_folder = "../../../assets/uploads/stories/thumbs/";
    $image_folder = "../../../assets/uploads/stories/images/";
    //../../../assets/uploads/

    if (move_uploaded_file($thumb_temp, $thumb_folder . $new_thumb) && move_uploaded_file($image_temp, $image_folder . $new_image)) {
      $data = array(
        'link' => $link,
        'title' => filter_var($_POST['blog_title']),
        'details' => filter_var($_POST['blog_details']),
        'posted_by' => filter_var($_SESSION['user_id']),
        'thumb' => $new_thumb,
        'image' => $new_image
      );

      $query = $con->insert('success_stories', $data);
      if ($query) {
        $succ_msg = "The blog post has been added successfuly";
        echo '<div class="alert alert-error" style="background-color:rgba(223,240,216,.3); border-color:#d6e9c6; color:#3c763d;">
              <button type="button" class="close" data-dismiss="alert">×</button>
              <span class="icon icon-ok-circle"></span> The story has been added successfuly!
            </div>
            <script>               
               setTimeout(\' window.location.href = ""; \',6000);
              </script>
            ';
      }
    } else {
      echo $err_msg =  "There is an error uploading the image";
      echo '<div class="alert alert-error" style="background-color:rgba(242,222,222,.3); border-color:#ebccd1; color:#a94442;">
              <button type="button" class="close" data-dismiss="alert">×</button>
              <span class="fa fa-exclamation-circle"></span> There is an error uploading the image"
            </div>';
    }
  } else {
    $err_msg = "The uploaded file is not an image, make sure the image should be of type .jpeg, .png, .jpg";
    echo '<div class="alert alert-error" style="background-color:rgba(242,222,222,.3); border-color:#ebccd1; color:#a94442;">
              <button type="button" class="close" data-dismiss="alert">×</button>
              <span class="fa fa-exclamation-circle"></span> The uploaded file is not an image, make sure the image should be of type .jpeg, .png, .jpg
            </div>';
  }
}

//update story
if (isset($_POST['update_story'])) {
  $data = array(
    'title' => filter_var($_POST['title']),
    'details' => filter_var($_POST['details'])
  );

  $query = $con->update('success_stories', $data, array('story_id' => filter_var($_POST['story_id'])));
  if ($query) {
    $succ_msg = "The blog post has been added successfuly";
    echo '<div class="alert alert-error" style="background-color:rgba(223,240,216,.3); border-color:#d6e9c6; color:#3c763d;">
              <button type="button" class="close" data-dismiss="alert">×</button>
              <span class="icon icon-ok-circle"></span> The story has been updated successfuly!
            </div>
            <script>               
               setTimeout(\' window.location.href = ""; \',6000);
              </script>
            ';
  }
}

//create a news post
if (isset($_POST['create_blog'])) {

  $err_msg = '';
  $succ_msg = '';
  $link = $con->get_random_string_max(60);
  $thumb = $_FILES['thumb_image']['name'];
  $image = $_FILES['blog_image']['name'];

  $thumb_ext = substr($thumb, strripos($thumb, '.'));
  $image_ext = substr($image, strripos($image, '.'));

  $thumb_basename = substr($thumb, 0, strripos($thumb, '.'));
  $image_basename = substr($image, 0, strripos($thumb, '.'));

  //give the images new names to avoid overwrite imagesy
  $new_thumb = date('ymdhis') . "_" . $thumb_basename . $thumb_ext;
  $new_image = date('ymdhis') . "_" . $image_basename . $image_ext;

  $allowed_file_types = array('.jpg', '.jpeg', '.png'); //allowed file types
  $thumb_type = $_FILES['thumb_image']['type'];
  $image_type = $_FILES['blog_image']['type'];


  //check if the uploaded file is indeed an images
  if (in_array(strtolower($thumb_ext), $allowed_file_types) && in_array(strtolower($image_ext), $allowed_file_types)) {
    $thumb_temp = $_FILES['thumb_image']['tmp_name'];
    $image_temp = $_FILES['blog_image']['tmp_name'];

    $thumb_folder = "../../../assets/uploads/news/thumbs/";
    $image_folder = "../../../assets/uploads/news/images/";
    //../../../assets/uploads/

    if (move_uploaded_file($thumb_temp, $thumb_folder . $new_thumb) && move_uploaded_file($image_temp, $image_folder . $new_image)) {
      $data = array(
        'blog_category' => filter_var($_POST['category']),
        'blog_title' => filter_var($_POST['blog_title']),
        'blog_content' => filter_var($_POST['blog_details']),
        'posted_by' => filter_var($_SESSION['user_id']),
        'post_thumb' => $new_thumb,
        'post_image' => $new_image,
        'post_link'  => $link
      );

      $query = $con->insert('blog_posts', $data);
      if ($query) {
        $succ_msg = "The blog post has been added successfuly";
        echo '<div class="alert alert-error" style="background-color:rgba(223,240,216,.3); border-color:#d6e9c6; color:#3c763d;">
              <button type="button" class="close" data-dismiss="alert">×</button>
              <span class="icon icon-ok-circle"></span> The news post has been added successfuly!
            </div>
            <script>               
               setTimeout(\' window.location.href = ""; \',6000);
              </script>
            ';
      }
    } else {
      echo $err_msg =  "There is an error uploading the image";
      echo '<div class="alert alert-error" style="background-color:rgba(242,222,222,.3); border-color:#ebccd1; color:#a94442;">
              <button type="button" class="close" data-dismiss="alert">×</button>
              <span class="fa fa-exclamation-circle"></span> There is an error uploading the image"
            </div>';
    }
  } else {
    $err_msg = "The uploaded file is not an image, make sure the image should be of type .jpeg, .png, .jpg";
    echo '<div class="alert alert-error" style="background-color:rgba(242,222,222,.3); border-color:#ebccd1; color:#a94442;">
              <button type="button" class="close" data-dismiss="alert">×</button>
              <span class="fa fa-exclamation-circle"></span> The uploaded file is not an image, make sure the image should be of type .jpeg, .png, .jpg
            </div>';
  }
}

//create a news post
if (isset($_POST['create_event'])) {

  $err_msg = '';
  $succ_msg = '';
  $link = $con->get_random_string_max(60);
  $thumb = $_FILES['thumb_image']['name'];
  $image = $_FILES['blog_image']['name'];

  $thumb_ext = substr($thumb, strripos($thumb, '.'));
  $image_ext = substr($image, strripos($image, '.'));

  $thumb_basename = substr($thumb, 0, strripos($thumb, '.'));
  $image_basename = substr($image, 0, strripos($thumb, '.'));

  //give the images new names to avoid overwrite imagesy
  $new_thumb = date('ymdhis') . "_" . $thumb_basename . $thumb_ext;
  $new_image = date('ymdhis') . "_" . $image_basename . $image_ext;

  $allowed_file_types = array('.jpg', '.jpeg', '.png'); //allowed file types
  $thumb_type = $_FILES['thumb_image']['type'];
  $image_type = $_FILES['blog_image']['type'];


  //check if the uploaded file is indeed an images
  if (in_array(strtolower($thumb_ext), $allowed_file_types) && in_array(strtolower($image_ext), $allowed_file_types)) {
    $thumb_temp = $_FILES['thumb_image']['tmp_name'];
    $image_temp = $_FILES['blog_image']['tmp_name'];

    $thumb_folder = "../../../assets/uploads/events/thumbs/";
    $image_folder = "../../../assets/uploads/events/images/";
    //../../../assets/uploads/

    if (move_uploaded_file($thumb_temp, $thumb_folder . $new_thumb) && move_uploaded_file($image_temp, $image_folder . $new_image)) {
      $data = array(
        'venue' => filter_var($_POST['venue']),
        'title' => filter_var($_POST['title']),
        'details' => filter_var($_POST['details']),
        'posted_by' => filter_var($_SESSION['user_id']),
        'thumb' => $new_thumb,
        'image' => $new_image,
        'date'  => filter_var($_POST['date'])
      );

      $query = $con->insert('events', $data);
      if ($query) {
        $succ_msg = "A new event has been added successfuly";
        echo '<div class="alert alert-error" style="background-color:rgba(223,240,216,.3); border-color:#d6e9c6; color:#3c763d;">
              <button type="button" class="close" data-dismiss="alert">×</button>
              <span class="icon icon-ok-circle"></span> A new event has been added successfuly
            </div>
            <script>               
               setTimeout(\' window.location.href = ""; \',6000);
              </script>
            ';
      }
    } else {
      echo $err_msg =  "There is an error uploading the image";
      echo '<div class="alert alert-error" style="background-color:rgba(242,222,222,.3); border-color:#ebccd1; color:#a94442;">
              <button type="button" class="close" data-dismiss="alert">×</button>
              <span class="fa fa-exclamation-circle"></span> There is an error uploading the image"
            </div>';
    }
  } else {
    $err_msg = "The uploaded file is not an image, make sure the image should be of type .jpeg, .png, .jpg";
    echo '<div class="alert alert-error" style="background-color:rgba(242,222,222,.3); border-color:#ebccd1; color:#a94442;">
              <button type="button" class="close" data-dismiss="alert">×</button>
              <span class="fa fa-exclamation-circle"></span> The uploaded image is not an image, make sure the image should be of type .jpeg, .png, .jpg
            </div>';
  }
}

//update event
if (isset($_POST['update_event'])) {
  $data = array(
    'venue' => $con->clean($_POST['venue']),
    'title' => $con->clean($_POST['title']),
    'date' => $con->clean($_POST['date']),
    'details' => $con->clean($_POST['details'])
  );

  $query = $con->update('events', $data, array('event_id' => filter_var($_POST['event_id'])));
  if ($query) {
    $succ_msg = "The blog post has been added successfuly";
    echo '<div class="alert alert-error" style="background-color:rgba(223,240,216,.3); border-color:#d6e9c6; color:#3c763d;">
          <button type="button" class="close" data-dismiss="alert">×</button>
          <span class="icon icon-ok-circle"></span> The event has been updated successfuly!
        </div>
        <script>               
           setTimeout(\' window.location.href = ""; \',6000);
          </script>
        ';
  }else{
    echo '<div class="alert alert-error" style="background-color:rgba(223,240,216,.3); border-color:#d6e9c6; color:#3c763d;">
          <button type="button" class="close" data-dismiss="alert">×</button>
          <span class="icon icon-ok-circle"></span> Sorry there was an error updating the selected event!
        </div>
        <script>               
           setTimeout(\' window.location.href = ""; \',6000);
          </script>
        ';
  }
}

//update story
if (isset($_POST['update_post'])) {
  $data = array(
    'blog_category' => filter_var($_POST['category']),
    'blog_title' => filter_var($_POST['blog_title']),
    'blog_content' => filter_var($_POST['blog_details'])
  );

  $query = $con->update('blog_posts', $data, array('blog_id' => filter_var($_POST['post_id'])));
  if ($query) {
    $succ_msg = "The blog post has been added successfuly";
    echo '<div class="alert alert-error" style="background-color:rgba(223,240,216,.3); border-color:#d6e9c6; color:#3c763d;">
          <button type="button" class="close" data-dismiss="alert">×</button>
          <span class="icon icon-ok-circle"></span> The news post has been updated successfuly!
        </div>
        <script>               
           setTimeout(\' window.location.href = ""; \',6000);
          </script>
        ';
  }
}

//this shows the blog post to the public
if ($action == 'show_event') {
  $post_id = $_POST['post_id'];
  $array = array('status' => '1');
  $query = $con->update('events', $array, array('event_id' => $post_id));
  if ($query) {
    echo "1";
  }
}

//this hides the blog post to the public
if ($action == 'hide_event') {
  $post_id = $_POST['post_id'];
  $array = array('status' => '0');
  $query = $con->update('events', $array, array('event_id' => $post_id));
  if ($query) {
    echo "1";
  }
}

//this deletes the blog post
if ($action == 'delete_event') {
  $post_id = $_POST['post_id'];
  $query = $con->delete('events', array('event_id' => $post_id));
  if ($query) {
    echo "1";
  } else {
    echo "2";
  }
}

///////////////////////////////

//update story
if (isset($_POST['update_post'])) {
  $data = array(
    'blog_category' => filter_var($_POST['category']),
    'blog_title' => filter_var($_POST['blog_title']),
    'blog_content' => filter_var($_POST['blog_details'])
  );

  $query = $con->update('blog_posts', $data, array('blog_id' => filter_var($_POST['post_id'])));
  if ($query) {
    $succ_msg = "The blog post has been added successfuly";
    echo '<div class="alert alert-error" style="background-color:rgba(223,240,216,.3); border-color:#d6e9c6; color:#3c763d;">
          <button type="button" class="close" data-dismiss="alert">×</button>
          <span class="icon icon-ok-circle"></span> The news post has been updated successfuly!
        </div>
        <script>               
           setTimeout(\' window.location.href = ""; \',6000);
          </script>
        ';
  }
}

//this shows the blog post to the public
if ($action == 'show_post') {
  $post_id = $_POST['post_id'];
  $array = array('status' => '1');
  $query = $con->update('blog_posts', $array, array('blog_id' => $post_id));
  if ($query) {
    echo "1";
  }
}

//this hides the blog post to the public
if ($action == 'hide_post') {
  $post_id = $_POST['post_id'];
  $array = array('status' => '0');
  $query = $con->update('blog_posts', $array, array('blog_id' => $post_id));
  if ($query) {
    echo "1";
  }
}

//this deletes the blog post
if ($action == 'delete_post') {
  $post_id = $_POST['post_id'];
  $query = $con->delete('blog_posts', array('blog_id' => $post_id));
  if ($query) {
    echo "1";
  } else {
    echo "2";
  }
}


//delete success story
if (isset($_POST['action']) && $action == 'delete_story') {
  $story_id = $_POST['post_id'];
  $query = $con->delete('success_stories', array('story_id' => $story_id));
  if ($query) {
    echo "1";
  } else {
    echo "2";
  }
}

//this hides the story post to the public
if ($action == 'hide_story') {
  $post_id = $_POST['post_id'];
  $array = array('status' => '0');
  $query = $con->update('success_stories', $array, array('story_id' => $post_id));
  if ($query) {
    echo "1";
  }
}

//this shows the blog post to the public
if ($action == 'show_story') {
  $post_id = $_POST['post_id'];
  $array = array('status' => '1');
  $query = $con->update('success_stories', $array, array('story_id' => $post_id));
  if ($query) {
    echo "1";
  }
}

//add category
if (isset($_POST['create_category'])) {
  $link = $con->get_random_string_max(60);
  $data = array('category' => filter_var($_POST['category']), 'category_link' => $link);
  $query = $con->insert("blog_categories", $data);
  if ($query) {
    echo "1";
  } else {
    echo "2";
  }
}

//update category
if (isset($_POST['update_category'])) {
  $cat_id = filter_var($_POST['cat_id']);
  $data = array('category' => filter_var($_POST['category']));
  $query = $con->update("blog_categories", $data, array('category_id' => $cat_id));
  if ($query) {
    echo "1";
  } else {
    echo "2";
  }
}

//this deletes the blog post
if ($action == 'delete_category') {
  $cat_id = $_POST['cat_id'];
  $query = $con->delete('blog_categories', array('category_id' => $cat_id));
  if ($query) {
    echo "1";
  } else {
    echo "2";
  }
}

//add new vacancy
if (isset($_POST['add_vacancy'])) {
  $data = array(
    'job_category' => filter_var($_POST['category']),
    'closing_date' => filter_var($_POST['closing_date']),
    'job_title'    => filter_var($_POST['job_title']),
    'job_description' => filter_var($_POST['job_details']),
    'posted_by' => filter_var($_SESSION['user_id']),
    'link'  => $con->get_random_string_max(60)
  );

  if (empty($_POST['category'])) {
    echo "
      <div class=\"alert alert-warning\"> <button type=\"button\" class=\"close\" data-dismiss=\"alert\">×</button><span class=\"fa fa-exclamation-circle\"></span>&nbsp; Please select position category.</div>
    ";
    $error = true;
  } else if (empty($_POST['closing_date'])) {
    echo "
      <div class=\"alert alert-warning\"> <button type=\"button\" class=\"close\" data-dismiss=\"alert\">×</button><span class=\"fa fa-exclamation-circle\"></span>&nbsp; Please enter closing date.</div>
    ";
    $error = true;
  } else if (empty($_POST['job_title'])) {
    echo "
      <div class=\"alert alert-warning\"> <button type=\"button\" class=\"close\" data-dismiss=\"alert\">×</button><span class=\"fa fa-exclamation-circle\"></span>&nbsp; Please enter job title.</div>
    ";
    $error = true;
  } else if (empty($_POST['job_details'])) {
    echo "
      <div class=\"alert alert-warning\"> <button type=\"button\" class=\"close\" data-dismiss=\"alert\">×</button><span class=\"fa fa-exclamation-circle\"></span>&nbsp; Please enter the job description.</div>
          <script> 
    ";
    $error = true;
  }

  if ($error == false) {
    $query = $con->insert('vacancies', $data);
    if ($query) {
      echo "
              <div class=\"alert alert-success\"> <button type=\"button\" class=\"close\" data-dismiss=\"alert\">×</button><span class=\"fa fa-exclamation-circle\"></span>&nbsp; A vacancy has been saved!</div>
              <script>               
               setTimeout(' window.location.href = \"vacancies.php\"; ',6000);
              </script>";
    } else {
      echo "
          <div class=\"alert alert-warning\"> <button type=\"button\" class=\"close\" data-dismiss=\"alert\">×</button><span class=\"icon icon-ok-circle\"></span>&nbsp; There was an saving the vacancy, please try again.</div>
              <script> 
        ";
    }
  }
}

//print_r($_POST);die();

//update vacancy
if (isset($_POST['update_vacancy'])) {
  $id  =  filter_var($_POST['vacancy_id']);
  $data = array(
    'job_category' => filter_var($_POST['category']),
    'closing_date' => filter_var($_POST['closing_date']),
    'job_title'    => filter_var($_POST['job_title']),
    'job_description' => filter_var($_POST['job_details']),
    'posted_by' => filter_var($_SESSION['user_id'])
  );

  if (empty($_POST['category'])) {
    echo "
      <div class=\"alert alert-warning\"> <button type=\"button\" class=\"close\" data-dismiss=\"alert\">×</button><span class=\"fa fa-exclamation-circle\"></span>&nbsp; Please select position category.</div>
    ";
    $error = true;
  } else if (empty($_POST['closing_date'])) {
    echo "
      <div class=\"alert alert-warning\"> <button type=\"button\" class=\"close\" data-dismiss=\"alert\">×</button><span class=\"fa fa-exclamation-circle\"></span>&nbsp; Please enter closing date.</div>
    ";
    $error = true;
  } else if (empty($_POST['job_title'])) {
    echo "
      <div class=\"alert alert-warning\"> <button type=\"button\" class=\"close\" data-dismiss=\"alert\">×</button><span class=\"fa fa-exclamation-circle\"></span>&nbsp; Please enter job title.</div>
    ";
    $error = true;
  } else if (empty($_POST['job_details'])) {
    echo "
      <div class=\"alert alert-warning\"> <button type=\"button\" class=\"close\" data-dismiss=\"alert\">×</button><span class=\"fa fa-exclamation-circle\"></span>&nbsp; Please enter the job description.</div>
          <script> 
    ";
    $error = true;
  }

  if ($error == false) {
    $query = $con->update('vacancies', $data, array('vacancy_id' => $id));
    if ($query) {
      echo "
              <div class=\"alert alert-success\"> <button type=\"button\" class=\"close\" data-dismiss=\"alert\">×</button><span class=\"fa fa-exclamation-circle\"></span>&nbsp; A vacancy has been saved!</div>
              <script>               
               setTimeout(' window.location.href = \"vacancies.php\"; ',6000);
              </script>";
    } else {
      echo "
          <div class=\"alert alert-warning\"> <button type=\"button\" class=\"close\" data-dismiss=\"alert\">×</button><span class=\"icon icon-ok-circle\"></span>&nbsp; There was an saving the vacancy, please try again.</div>
              <script> 
        ";
    }
  }
}


//delete vacancy
if (isset($_POST['delete_vacancy'])) {
  $id = $_POST['vacancy_id'];

  $statement = "delete  from vacancies where vacancy_id=?";
  try {
    $sql   = $db->prepare($statement);
    $array = array($id);
    $query = $sql->execute($array);

    if ($query) {
      echo "1";
    } else {
      echo "2";
    }
  } catch (PDOException $e) {
    echo $e->getMessage();
  }
}

//delete_inquiry
if (isset($_POST['delete_inquiry'])) {
  $id = $_POST['id'];

  $statement = "delete from inquiries where link=?";
  try {
    $sql   = $db->prepare($statement);
    $array = array($id);
    $query = $sql->execute($array);

    if ($query) {
      echo "1";
    } else {
      echo "2";
    }
  } catch (PDOException $e) {
    echo $e->getMessage();
  }
}

//show vacancy
if (isset($_POST['show_vacancy'])) {
  $id = $_POST['vacancy_id'];

  $statement = "update vacancies set status='1' where vacancy_id=?";
  try {
    $sql   = $db->prepare($statement);
    $array = array($id);
    $query = $sql->execute($array);

    if ($query) {
      echo "1";
    } else {
      echo "2";
    }
  } catch (PDOException $e) {
    echo $e->getMessage();
  }
}

//hide vacancy
if (isset($_POST['hide_vacancy'])) {
  $id = $_POST['vacancy_id'];

  $statement = "update vacancies set status='0' where vacancy_id=?";
  try {
    $sql   = $db->prepare($statement);
    $array = array($id);
    $query = $sql->execute($array);

    if ($query) {
      echo "1";
    } else {
      echo "2";
    }
  } catch (PDOException $e) {
    echo $e->getMessage();
  }
}

//add tender file
if (isset($_POST['add_tender'])) {
  $err_msg = '';
  $succ_msg = '';
  $link = $con->get_random_string_max(60);
  $file = $_FILES['file']['name'];

  $file_ext = substr($file, strripos($file, '.'));

  $file_basename = substr($file, 0, strripos($file, '.'));

  //give the images new names to avoid overwrite imagesy
  $new_file = date('ymdhis') . "_" . $_POST['title'] . $file_ext;

  $allowed_file_types = array('.doc', '.docx', '.pdf', '.jpeg', '.jpg', '.png'); //allowed file types
  $file_type = $_FILES['file']['type'];


  //check if the uploaded file is indeed an images
  if (in_array($file_ext, $allowed_file_types)) {
    $file_temp = $_FILES['file']['tmp_name'];

    $folder = "../../../assets/uploads/tenders/";

    if (move_uploaded_file($file_temp, $folder . $new_file)) {
      $data = array(
        'resource_title' => filter_var($_POST['title']),
        'resource_file' => $new_file,
        'link' => $link
      );

      $query = $con->insert('resources', $data);
      if ($query) {
        echo '1';
      }
    } else {
      //echo $err_msg =  "There is an error uploading the image";
      echo '2';
    }
  } else {
    $err_msg = "The uploaded file is not an image, make sure the image should be of type .jpeg, .png, .jpg";
    echo '3';
  }
}

//delete tender file
if (isset($_POST['action']) && $action == 'delete_resource') {
  $id = $_POST['resource_id'];

  //get file name
  $row = $con->getRows('resources', array('where' => 'resource_id="' . $id . '"', 'return_type' => 'single'));
  //$folder = "../../../assets/uploads/directory/";

  $file = "../../../assets/uploads/tenders/" . $row['resource_file'];


  $delete = $con->delete('resources', array('resource_id' => $id));
  if ($delete) {
    echo "1";
    unlink($file);
  } else {
    echo "2";
  }
}

//add knowledge_management
if (isset($_POST['add_knowledge'])) {
  $err_msg = '';
  $succ_msg = '';
  $file = $_FILES['file']['name'];
  $link = $con->get_random_string_max(60);

  $file_ext = substr($file, strripos($file, '.'));

  $file_basename = substr($file, 0, strripos($file, '.'));

  //give the images new names to avoid overwrite imagesy
  $new_file = date('ymdhis') . "_" . $_POST['title'] . $file_ext;

  $allowed_file_types = array('.doc', '.docx', '.pdf', '.ppt', '.pptx', '.xls', '.xlsx', '.jpeg', '.jpg', '.png'); //allowed file types
  $file_type = $_FILES['file']['type'];


  //check if the uploaded file is indeed an images
  if (in_array($file_ext, $allowed_file_types)) {
    $file_temp = $_FILES['file']['tmp_name'];

    $folder = "../../../assets/uploads/knowledge_management/";

    if (move_uploaded_file($file_temp, $folder . $new_file)) {
      $data = array(
        'title' => filter_var($_POST['title']),
        'file' => $new_file,
        'link' => $link,
        'posted_by' => $_SESSION['user_id']
      );

      $query = $con->insert('publications', $data);
      if ($query) {
        echo '1';
      }
    } else {
      //echo $err_msg =  "There is an error uploading the image";
      echo '2';
    }
  } else {
    $err_msg = "The uploaded file is not an image, make sure the image should be of type .jpeg, .png, .jpg";
    echo '3';
  }
}

/*********sacco statistics***************/
//add tender file
if (isset($_POST['add_stat'])) {
  $err_msg = '';
  $succ_msg = '';
  $link = $con->get_random_string_max(60);
  $file = $_FILES['file']['name'];

  $file_ext = substr($file, strripos($file, '.'));

  $file_basename = substr($file, 0, strripos($file, '.'));

  //give the images new names to avoid overwrite imagesy
  $new_file = date('ymdhis') . "_" . $_POST['title'] . $file_ext;

  $allowed_file_types = array('.doc', '.docx', '.pdf', '.jpeg', '.jpg', '.png'); //allowed file types
  $file_type = $_FILES['file']['type'];


  //check if the uploaded file is indeed an images
  if (in_array($file_ext, $allowed_file_types)) {
    $file_temp = $_FILES['file']['tmp_name'];

    $folder = "../../../assets/uploads/stats/";

    if (move_uploaded_file($file_temp, $folder . $new_file)) {
      $data = array(
        'title' => filter_var($_POST['title']),
        'type'  => filter_var($_POST['type']),
        'file' => $new_file
      );

      $query = $con->insert('sacco_statistics', $data);
      if ($query) {
        echo '1';
      }
    } else {
      echo '2';
    }
  } else {
    echo '3';
  }
}

//add_ann_report
if (isset($_POST['add_ann_report'])) {
  $err_msg = '';
  $succ_msg = '';
  $link = $con->get_random_string_max(60);
  $file = $_FILES['file']['name'];

  $file_ext = substr($file, strripos($file, '.'));

  $file_basename = substr($file, 0, strripos($file, '.'));

  //give the images new names to avoid overwrite imagesy
  $new_file = date('ymdhis') . "_" . $_POST['title'] . $file_ext;

  $allowed_file_types = array('.doc', '.docx', '.pdf', '.jpeg', '.jpg', '.png','.xls', '.xlsx', '.csv'); //allowed file types
  $file_type = $_FILES['file']['type'];


  //check if the uploaded file is indeed an images
  if (in_array($file_ext, $allowed_file_types)) {
    $file_temp = $_FILES['file']['tmp_name'];

    $folder = "../../../assets/uploads/reports/";

    if (move_uploaded_file($file_temp, $folder . $new_file)) {
      $data = array(
        'title' => filter_var($_POST['title']),
        'file' => $new_file
      );

      $query = $con->insert('report', $data);
      if ($query) {
        echo '1';
      }
    } else {
      echo '2';
    }
  } else {
    echo '3';
  }
}

//delete report
if (isset($_POST['action']) && $action == 'delete_report') {
  $id = $_POST['id'];

  //get file name
  $row = $con->getRows('report', array('where' => 'report_id="' . $id . '"', 'return_type' => 'single'));
  //$folder = "../../../assets/uploads/directory/";

  $file = "../../../assets/uploads/reports/" . $row['file'];


  $delete = $con->delete('report', array('report_id' => $id));
  if ($delete) {
    echo "1";
    unlink($file);
  } else {
    echo "2";
  }
}

//delete tender file
if (isset($_POST['action']) && $action == 'delete_stat') {
  $id = $_POST['resource_id'];

  //get file name
  $row = $con->getRows('sacco_statistics', array('where' => 'stat_id="' . $id . '"', 'return_type' => 'single'));
  //$folder = "../../../assets/uploads/directory/";

  $file = "../../../assets/uploads/stats/" . $row['file'];


  $delete = $con->delete('sacco_statistics', array('stat_id' => $id));
  if ($delete) {
    echo "1";
    unlink($file);
  } else {
    echo "2";
  }
}

/**end */

//delete publication file
if (isset($_POST['action']) && $action == 'delete_knowledge') {
  $id = $_POST['resource_id'];

  //get file name
  $row = $con->getRows('publications', array('where' => 'link="' . $id . '"', 'return_type' => 'single'));

  $file = "../../../assets/uploads/knowledge_management/" . $row['file'];


  $delete = $con->delete('publications', array('link' => $id));
  if (!empty($delete)) {
    echo "1";
    unlink($file);
  } else {
    echo "2";
  }
}

//add newsletter file
if (isset($_POST['add_newsletter'])) {
  $err_msg = '';
  $succ_msg = '';
  $file = $_FILES['file']['name'];
  $link = $con->get_random_string_max(60);

  $file_ext = substr($file, strripos($file, '.'));

  $file_basename = substr($file, 0, strripos($file, '.'));

  //give the images new names to avoid overwrite imagesy
  $new_file = date('ymdhis') . "_" . $_POST['title'] . $file_ext;

  $allowed_file_types = array('.doc', '.docx', '.pdf', '.ppt', '.pptx', '.xls', '.xlsx'); //allowed file types
  $file_type = $_FILES['file']['type'];


  //check if the uploaded file is indeed an images
  if (in_array($file_ext, $allowed_file_types)) {
    $file_temp = $_FILES['file']['tmp_name'];

    $folder = "../../../assets/uploads/newsletters/";

    if (move_uploaded_file($file_temp, $folder . $new_file)) {
      $data = array(
        'title' => filter_var($_POST['title']),
        'file' => $new_file,
        'link' => $link,
        'posted_by' => $_SESSION['user_id']
      );

      $query = $con->insert('newsletters', $data);
      if ($query) {
        echo '1';
      }
    } else {
      //echo $err_msg =  "There is an error uploading the image";
      echo '2';
    }
  } else {
    $err_msg = "The uploaded file is not an image, make sure the image should be of type .jpeg, .png, .jpg";
    echo '3';
  }
}

//delete newsletter file
if (isset($_POST['action']) && $action == 'delete_newsletter') {
  $id = $_POST['resource_id'];

  //get file name
  $row = $con->getRows('newsletters', array('where' => 'link="' . $id . '"', 'return_type' => 'single'));
  $file = "../../../assets/uploads/newsletters/" . $row['file'];


  $delete = $con->delete('newsletters', array('link' => $id));
  if ($delete) {
    echo "1";
    unlink($file);
  } else {
    echo "2";
  }
}


//update Password
if (isset($_POST['update_password'])) {
  $oldpass = filter_var($_POST['oldpass']);
  $newpass = filter_var($_POST['newpass']);
  $repass  = filter_var($_POST['repass']);
  $user    = $_SESSION['user_id'];

  //checks if the current password entered is correct;
  $query = $db->query("select password from system_users where user_id='$user'");
  if ($query) {
    $row = $query->fetch();
    if ($row['password'] != sha1($oldpass)) {
      echo "2";
    } else {
      //checks if the new password entered is the same with the re entered password
      if ($newpass != $repass) {
        echo "3";
      } else {
        $sql = "update system_users set password=? where user_id='$user' ";
        try {
          $prepare = $db->prepare($sql);
          $array   = array(sha1($newpass));
          $execute = $prepare->execute($array);

          if ($execute) {
            echo "1";
          } else {
            echo "4";
          }
        } catch (PDOException $e) {
          //$e->getMessage();
        }
      }
    }
  }
}

//share_newsletter

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

  /***echo "string";
  //print_r($_FILES);
  $from_email         = 'wezmhango@hotmail.com'; //from mail, sender email address
  $recipient_email    = 'wezmhango@hotmail.com'; //recipient email address

  //Load POST data from HTML form
  $sender_name    = $_POST["title"]; //sender name
  $reply_to_email = "wezmhango@hotmail.com"; //sender email, it will be used in "reply-to" header
  $subject        = $_POST["title"]; //subject for the email
  $message        = $_POST["message"]; //body of the email


  /*Always remember to validate the form fields like this
    if(strlen($sender_name)<1)
    {
        die('Name is too short or empty!');
    }
    

  //Get uploaded file data using $_FILES array
  $tmp_name    = $_FILES['my_file']['tmp_name']; // get the temporary file name of the file on the server
  $name        = $_FILES['my_file']['name'];  // get the name of the file
  $size        = $_FILES['my_file']['size'];  // get size of the file for size validation
  $type        = $_FILES['my_file']['type'];  // get type of the file
  $error       = $_FILES['my_file']['error']; // get the error (if any)

  //validate form field for attaching the file
  if ($error > 0) {
    die('Upload error or No files uploaded');
  }

  //read from the uploaded file & base64_encode content
  $handle = fopen($tmp_name, "r");  // set the file handle only for reading the file
  $content = fread($handle, $size); // reading the file
  fclose($handle);                  // close upon completion

  $encoded_content = chunk_split(base64_encode($content));

  $boundary = md5("random"); // define boundary with a md5 hashed value

  //header
  $headers = "MIME-Version: 1.0\r\n"; // Defining the MIME version
  $headers .= "From:" . $from_email . "\r\n"; // Sender Email
  $headers .= "Reply-To: " . $reply_to_email . "\r\n"; // Email address to reach back
  $headers .= "Content-Type: multipart/mixed;"; // Defining Content-Type
  $headers .= "boundary = $boundary\r\n"; //Defining the Boundary

  //plain text
  $body = "--$boundary\r\n";
  $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
  $body .= "Content-Transfer-Encoding: base64\r\n\r\n";
  $body .= chunk_split(base64_encode($message));

  //attachment
  $body .= "--$boundary\r\n";
  $body .= "Content-Type: $type; name=" . $name . "\r\n";
  $body .= "Content-Disposition: attachment; filename=" . $name . "\r\n";
  $body .= "Content-Transfer-Encoding: base64\r\n";
  $body .= "X-Attachment-Id: " . rand(1000, 99999) . "\r\n\r\n";
  $body .= $encoded_content; // Attaching the encoded file with email

  $sentMailResult = mail($recipient_email, $subject, $body, $headers);

  if ($sentMailResult) {
    echo "File Sent Successfully.";
    unlink($name); // delete the file after attachment sent.
  } else {
    die("Sorry but the email could not be sent. Please go back and try again!");
  } */
}

/****************sacco*********************/
//add sacco
if (isset($_POST['save_sacco'])) {
  $data = array(
    'sacco_name'  => $con->clean($_POST['sacco']),
    'region_id'   => $con->clean($_POST['region']),
    'location'    => $con->clean($_POST['location']),
    'email'       => $con->clean($_POST['email']),
    'phone'       => $con->clean($_POST['phone']),
    'contact_person'  => $con->clean($_POST['contact']),
    'url'         => $con->clean($_POST['url']),
    'address'     => $con->clean($_POST['address'])
  );
  $save = $con->insert('saccos', $data);
  if (!empty($save)) {
    echo "1";
  } else {
    echo "2";
  }
}

//delete sacco
if (isset($_POST['action']) && $action == 'delete_sacco') {
  $delete = $con->delete('saccos', array('sacco_id' => $_POST['id']));
  if ($delete) {
    echo "1";
  } else {
    echo "2";
  }
}

//update sacco
if (isset($_POST['update_sacco'])) {
  $data = array(
    'sacco_name'  => $con->clean($_POST['sacco']),
    'region_id'   => $con->clean($_POST['region']),
    'location'    => $con->clean($_POST['location']),
    'email'       => $con->clean($_POST['email']),
    'phone'       => $con->clean($_POST['phone']),
    'contact_person'  => $con->clean($_POST['contact']),
    'url'         => $con->clean($_POST['url']),
    'address'     => $con->clean($_POST['address'])
  );
  $save = $con->update('saccos', $data, array('sacco_id' => $_POST['id']));
  if (!empty($save)) {
    echo "1";
  } else {
    echo "2";
  }
}
/*****************************************************************************/
/****************management/leadership******************/
//add member
if (isset($_POST['add_member'])) {

  $file = $_FILES['file']['name'];
  $file_ext = substr($file, strripos($file, '.'));
  $file_basename = substr($file, 0, strripos($file, '.'));

  //give the images new names to avoid overwrite imagesy
  $new_file = time() . "_" . $_POST['name'] . $file_ext;

  $allowed_file_types = array('.jpeg', '.jpg', '.png'); //allowed file types
  $file_type = $_FILES['file']['type'];


  //check if the uploaded file is indeed an images
  if (in_array($file_ext, $allowed_file_types)) {
    $file_temp = $_FILES['file']['tmp_name'];

    $folder = "../../../assets/uploads/leadership/";

    if (move_uploaded_file($file_temp, $folder . $new_file)) {
      $data = array(
        'title' => filter_var($_POST['title']),
        'name'  => $con->clean($_POST['name']),
        'type'  => $con->clean($_POST['type']),
        'facebook' => filter_var($_POST['fb']),
        'twitter'  => $con->clean($_POST['twitter']),
        'linkedin'  => $con->clean($_POST['linkedin']),
        'image' => $new_file
      );

      $query = $con->insert('leadership', $data);
      if ($query) {
        echo '1';
      }
    } else {
      echo '2';
    }
  } else {
    echo '3';
  }
}

//update member
if (isset($_POST['update_member'])) {
  $image = $con->clean($_POST['current_image']);

  if(!empty($_FILES['image']['name'])){
    $file = $_FILES['image']['name'];
    $file_ext = substr($file, strripos($file, '.'));
    $file_basename = substr($file, 0, strripos($file, '.'));

    //give the images new names to avoid overwrite imagesy
    $new_file = time() . "_" . $_POST['name'] . $file_ext;

    $allowed_file_types = array('.jpeg', '.jpg', '.png'); //allowed file types
    $file_type = $_FILES['image']['type'];

    //check if the uploaded file is indeed an images
    if (!in_array(strtolower($file_ext), $allowed_file_types)) {
      echo '3';
      exit();
    }

    $file_temp = $_FILES['image']['tmp_name'];
    $folder = "../../../assets/uploads/leadership/";

    if (move_uploaded_file($file_temp, $folder . $new_file)) {
      $image = $new_file;     
      unlink('../../../assets/uploads/leadership/'.$_POST['current_image']); 
    }

  }
  
  $data = array(
    'title' => filter_var($_POST['title']),
    'name'  => $con->clean($_POST['name']),
    'facebook' => filter_var($_POST['fb']),
    'twitter'  => $con->clean($_POST['twitter']),
    'linkedin'  => $con->clean($_POST['linkedin']),
    'image' => $image
  );

  $query = $con->update('leadership', $data, array('leader_id'=>$con->clean($_POST['member_id'])));
  if ($query) {
    echo '1'; 
  }
  else {
    echo '2';
  }
  
}


//delete member
if (isset($_POST['action']) && $action == 'delete_member') {
  $id = $_POST['id'];

  //get file name
  $row = $con->getRows('leadership', array('where' => 'leader_id="' . $id . '"', 'return_type' => 'single'));
  $file = "../../../assets/uploads/leadership/" . $row['image'];


  $delete = $con->delete('leadership', array('leader_id' => $id));
  if ($delete) {
    echo "1";
    unlink($file);
  } else {
    echo "2";
  }
}

/********************Projects********************/
//create a news post
if (isset($_POST['create_project'])) {

  $err_msg = '';
  $succ_msg = '';
  $link = $con->get_random_string_max(60);
  $thumb = $_FILES['thumb_image']['name'];
  $image = $_FILES['blog_image']['name'];

  $thumb_ext = substr($thumb, strripos($thumb, '.'));
  $image_ext = substr($image, strripos($image, '.'));

  $thumb_basename = substr($thumb, 0, strripos($thumb, '.'));
  $image_basename = substr($image, 0, strripos($thumb, '.'));

  //give the images new names to avoid overwrite imagesy
  $new_thumb = time() . "_" . $thumb_basename . $thumb_ext;
  $new_image = time() . "_" . $image_basename . $image_ext;

  $allowed_file_types = array('.jpg', '.jpeg', '.png'); //allowed file types
  $thumb_type = $_FILES['thumb_image']['type'];
  $image_type = $_FILES['blog_image']['type'];


  //check if the uploaded file is indeed an images
  if (in_array(strtolower($thumb_ext), $allowed_file_types) && in_array(strtolower($image_ext), $allowed_file_types)) {
    $thumb_temp = $_FILES['thumb_image']['tmp_name'];
    $image_temp = $_FILES['blog_image']['tmp_name'];

    $thumb_folder = "../../../assets/uploads/projects/thumbs/";
    $image_folder = "../../../assets/uploads/projects/images/";
    //../../../assets/uploads/

    if (move_uploaded_file($thumb_temp, $thumb_folder . $new_thumb) && move_uploaded_file($image_temp, $image_folder . $new_image)) {
      $data = array(
        'title' => filter_var($_POST['title']),
        'details' => filter_var($_POST['details']),
        'posted_by' => filter_var($_SESSION['user_id']),
        'thumb' => $new_thumb,
        'image' => $new_image
      );

      $query = $con->insert('projects', $data);
      if ($query) {
        $succ_msg = "The project has been added successfuly";
        echo '<div class="alert alert-error" style="background-color:rgba(223,240,216,.3); border-color:#d6e9c6; color:#3c763d;">
              <button type="button" class="close" data-dismiss="alert">×</button>
              <span class="icon icon-ok-circle"></span> The project has been added successfuly!
            </div>
            <script>               
               setTimeout(\' window.location.href = ""; \',6000);
              </script>
            ';
      }
    } else {
      //echo $err_msg =  "There is an error uploading the image";
      echo '<div class="alert alert-error" style="background-color:rgba(242,222,222,.3); border-color:#ebccd1; color:#a94442;">
              <button type="button" class="close" data-dismiss="alert">×</button>
              <span class="fa fa-exclamation-circle"></span> There is an error uploading the image"
            </div>';
    }
  } else {
    $err_msg = "The uploaded file is not an image, make sure the image should be of type .jpeg, .png, .jpg";
    echo '<div class="alert alert-error" style="background-color:rgba(242,222,222,.3); border-color:#ebccd1; color:#a94442;">
              <button type="button" class="close" data-dismiss="alert">×</button>
              <span class="fa fa-exclamation-circle"></span> The uploaded file is not an image, make sure the image should be of type .jpeg, .png, .jpg
            </div>';
  }
}

//update project details
if (isset($_POST['update_project'])) {
  $data = array(
    'title' => filter_var($_POST['title']),
    'details' => filter_var($_POST['details']),
    'active' => filter_var($_POST['status'])
  );

  $query = $con->update('projects', $data, array('project_id' => $_POST['id']));
  if ($query) {
    $succ_msg = "The project has been added successfuly";
    echo '<div class="alert alert-error" style="background-color:rgba(223,240,216,.3); border-color:#d6e9c6; color:#3c763d;">
              <button type="button" class="close" data-dismiss="alert">×</button>
              <span class="icon icon-ok-circle"></span> The project has been updated successfuly!
            </div>
            <script>               
               setTimeout(\' window.location.href = ""; \',6000);
              </script>
            ';
  } else {
    //echo $err_msg =  "There is an error uploading the image";
    echo '<div class="alert alert-error" style="background-color:rgba(242,222,222,.3); border-color:#ebccd1; color:#a94442;">
              <button type="button" class="close" data-dismiss="alert">×</button>
              <span class="fa fa-exclamation-circle"></span> There is an error updating the project"
            </div>';
  }
}

//show project
if (isset($_POST['action']) && $_POST['action'] == 'show_project') {
  $show = $con->update('projects', array('status' => 1), array('project_id' => $_POST['id']));
  if (!empty($show)) {
    echo "1";
  }
}

//hide project
if (isset($_POST['action']) && $_POST['action'] == 'hide_project') {
  $hide = $con->update('projects', array('status' => 0), array('project_id' => $_POST['id']));
  if (!empty($hide)) {
    echo "1";
  }
}

//delete project
if (isset($_POST['action']) && $_POST['action'] == 'delete_project') {
  $id = $_POST['id'];

  //get file name
  $row = $con->getRows('projects', array('where' => 'project_id="' . $id . '"', 'return_type' => 'single'));
  $thumb = "../../../assets/uploads/projects/thumbs/" . $row['thumb'];
  $image = "../../../assets/uploads/projects/images/" . $row['image'];


  $delete = $con->delete('projects', array('project_id' => $id));
  if ($delete) {
    echo "1";
    unlink($thumb);
    unlink($image);
  } else {
    echo "2";
  }
}

/********************faqs********************/
//add faqs
if (isset($_POST['add_faq'])) {
  $data = array(
    'question'  => $con->clean($_POST['question']),
    'answer'    => $con->clean($_POST['answer'])
  );
  $save = $con->insert('faqs', $data);
  if (!empty($save)) {
    echo "1";
  } else {
    echo "2";
  }
}

//delete faq
if (isset($_POST['action']) && $_POST['action'] == 'delete_faq') {
  $id = $_POST['id'];
  $delete = $con->delete('faqs', array('faq_id' => $id));
  if ($delete) {
    echo "1";
  } else {
    echo "2";
  }
}


//services
//add service
if (isset($_POST['create_service'])) {

  $image = $_FILES['image']['name'];

  $image_ext = substr($image, strripos($image, '.'));

  $image_basename = substr($image, 0, strripos($image, '.'));

  //give the images new names to avoid overwrite imagesy
  $new_image = time() . "_" . $image_basename . $image_ext;

  $allowed_file_types = array('.jpg', '.jpeg', '.png'); //allowed file types
  $image_type = $_FILES['image']['type'];


  //check if the uploaded file is indeed an images
  if (in_array(strtolower($image_ext), $allowed_file_types)) {
    $image_temp = $_FILES['image']['tmp_name'];

    $image_folder = "../../../assets/uploads/services/";

    if (move_uploaded_file($image_temp, $image_folder . $new_image)) {
      $data = array(
        'title' => filter_var($_POST['title']),
        'details' => filter_var($_POST['details']),
        'posted_by' => filter_var($_SESSION['user_id']),
        'image' => $new_image
      );

      $query = $con->insert('services', $data);
      if ($query) {
        $succ_msg = "The project has been added successfuly";
        echo '<div class="alert alert-error" style="background-color:rgba(223,240,216,.3); border-color:#d6e9c6; color:#3c763d;">
              <button type="button" class="close" data-dismiss="alert">×</button>
              <span class="icon icon-ok-circle"></span> The service has been added successfuly!
            </div>
            <script>               
               setTimeout(\' window.location.href = ""; \',6000);
              </script>
            ';
      }
    } else {
      //echo $err_msg =  "There is an error uploading the image";
      echo '<div class="alert alert-error" style="background-color:rgba(242,222,222,.3); border-color:#ebccd1; color:#a94442;">
              <button type="button" class="close" data-dismiss="alert">×</button>
              <span class="fa fa-exclamation-circle"></span> There is an error uploading the image"
            </div>';
    }
  } else {
    $err_msg = "The uploaded file is not an image, make sure the image should be of type .jpeg, .png, .jpg";
    echo '<div class="alert alert-error" style="background-color:rgba(242,222,222,.3); border-color:#ebccd1; color:#a94442;">
              <button type="button" class="close" data-dismiss="alert">×</button>
              <span class="fa fa-exclamation-circle"></span> The uploaded file is not an image, make sure the image should be of type .jpeg, .png, .jpg
            </div>';
  }
}


//show service
if (isset($_POST['action']) && $_POST['action'] == 'show_service') {
  //echo print_r($_POST);
  $show = $con->update('services', array('status' => 1), array('service_id' => $_POST['id']));
  if (!empty($show)) {
    echo "1";
  }
}

//hide service
if (isset($_POST['action']) && $_POST['action'] == 'hide_service') {
  $hide = $con->update('services', array('status' => 0), array('service_id' => $_POST['id']));
  if (!empty($hide)) {
    echo "1";
  }
}

//delete service
if (isset($_POST['action']) && $_POST['action'] == 'delete_service') {
  $id = $_POST['id'];

  //get file name
  $row = $con->getRows('services', array('where' => 'service_id="' . $id . '"', 'return_type' => 'single'));
  $image = "../../../assets/uploads/services/" . $row['image'];


  $delete = $con->delete('services', array('service_id' => $id));
  if ($delete) {
    echo "1";
    unlink($image);
  } else {
    echo "2";
  }
}

//update service
if (isset($_POST['update_service'])) {
  $data = array(
    'title' => filter_var($_POST['title']),
    'details' => filter_var($_POST['details']),
  );
  $update = $con->update('services', $data, array('service_id' => $_POST['service_id']));
  if (!empty($update)) {
    $succ_msg = "The project has been added successfuly";
    echo '<div class="alert alert-error" style="background-color:rgba(223,240,216,.3); border-color:#d6e9c6; color:#3c763d;">
              <button type="button" class="close" data-dismiss="alert">×</button>
              <span class="icon icon-ok-circle"></span> The service has been updated successfuly!
            </div>
            <script>               
               setTimeout(\' window.location.href = ""; \',6000);
              </script>
            ';
  } else {
    //echo $err_msg =  "There is an error uploading the image";
    echo '<div class="alert alert-error" style="background-color:rgba(242,222,222,.3); border-color:#ebccd1; color:#a94442;">
              <button type="button" class="close" data-dismiss="alert">×</button>
              <span class="fa fa-exclamation-circle"></span> There is an error updating the service"
            </div>';
  }
}
