<?php
include_once("../include/main.inc.php");
check_session('MyCpanel', 'index.php');

@extract($_REQUEST);
if (empty($catID)) {
  $heading = "Add New Client Testimonial Video";
} else {
  $heading = "Edit Client Testimonial Video";
}
$res = getResult("admin_details", " where user_type='admin'");

$record_type = "Add New Client Testimonial Video";
$script_name = "addedittestimonialvideo.php";
$redirect_script_name = "manage-testimonial-video.php";
$table_name = "client_testi_video_tbl";
#################### Add Record ##################
if ($action == "add_record" && $name != "") {
 
  $numCnt = getCount($table_name, " where name='$name'");
    if ($numCnt > 0) {
      $_SESSION['session_message'] = '<div class="btn btn-warning">' . $record_type . ' already exist  </div>';
    } else{
     
    
      if ($_FILES['clientimg']['name']!=" ") {
        $file_extension = strtolower(getExtension($_FILES['clientimg']['name']));
        if ($_FILES['clientimg']['name'] != "") {
          $Client = str_replace(" ", "-", $name);
          $img_name = $Client;
          $img = upload_file($img_name, "clientimg", "uploaded-files/client-image");
           
        $width="119";
				$height="33";
				$source_folder=SITE_DIR_PATH."/uploaded-files/client-image/";
				$destination_folder=SITE_DIR_PATH."/uploaded-files/client-image/thumbs/";
				saveResizedPhoto($img, $width, $height, $source_folder, $destination_folder);


        }
      }
      $caption_text = htmlentitiesDecode($caption_text);
  
      // echo "insert into `$table_name` set name='$name',caption='$caption',logo_image='$img'";
      // exit;

    //  echo  "insert into `$table_name` set name='$name',caption='$caption',caption_text='$caption_text',logo_image='$img'";
    //  exit;

      $sql = mysqli_query($GLOBALS['dbconn'],"insert into `$table_name` set name='$name',caption='$caption_text',caption_text='$data',logo_image='$img'");
      //$_SESSION['session_message'] ='<div class="btn btn-success">' . $name . ' has been added successfully  </div> ';

      header("Location: " . SITE_ADMIN_URL . "/" . $redirect_script_name);
      exit;
    }
  
}
################ Update Record ##################

if ($action == "update_record") {

  $numCnt = getCount($table_name, " where name='$name' and id!='$catID'");
  if ($numCnt > 0) {
    $_SESSION['session_message'] = '<div class="btn btn-warning">' . $record_type . ' already exist  </div>';
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
  }

  if ($_FILES['clientimg']['name'] != "") {
    
    $file_extension = strtolower(getExtension($_FILES['clientimg']['name']));
    if ($file_extension != "jpg" && $file_extension != "gif" && $file_extension != "jpeg" && $file_extension != "png" && $file_extension != "webp") {
      $_SESSION['session_message'] = '<div class="btn btn-warning"> Please upload image (.jpg,.gif,.png Format Only) </div>';
      header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
      exit;
    }
    if ($_FILES['clientimg']['name'] != "") {
      $categoryRes = getResult($table_name, " where id='$catID'", "logo_image");

      $filename = $categoryRes['logo_image'];
      if ($filename != "") {
        $file = site_url . "/uploaded-files/client-image/" . $filename;
        if (file_exists($file)) {
          unlink($file);
        }

        $Client = str_replace(" ", "-", $name);
        $img_name = $Client;
   
        $img = upload_file($img_name, "clientimg", "uploaded-files/client-image");

        $width="119";
				$height="33";
				$source_folder=SITE_DIR_PATH."/uploaded-files/client-image/";
				$destination_folder=SITE_DIR_PATH."/uploaded-files/client-image/thumbs/";
				saveResizedPhoto($img, $width, $height, $source_folder, $destination_folder);
  
        updateTable($table_name, " logo_image='$img' where id='$catID'");
  }

    
    }
}


$caption_text = htmlentitiesDecode($caption_text);
  updateTable($table_name, " name='$name',caption='$caption',caption_text='$caption_text' where id='$catID'");
  $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been added successfully  </div>';
   
  header("Location: " . SITE_ADMIN_URL . "/" . $redirect_script_name);
  exit;
}
########################################################
admin_header();
$res = getResult($table_name, " where id='$catID'");
?>
           <div class="content-wrapper">
          <div class="page-header">
            <h3 class="page-title">
			<?=$heading;?>
            </h3>
            <nav aria-label="breadcrumb">
              <ol class="breadcrumb">
			  <li class="nav-item d-none d-lg-flex">
            <a class="nav-link" href="<?=$redirect_script_name?>">
              <span class="btn btn-primary">Go Back</span>
            </a>
          </li>
              </ol>
            </nav>
          </div>
          <div class="row">
          
            <div class="col-12 grid-margin stretch-card">
              <div class="card">
                <div class="card-body">
                  <form class="forms-sample" method="POST" action=""  enctype="multipart/form-data">
                  <div class="form-group row">
                    <div class="col">
                      <label>Client Name *</label>
                      <div id="the-basics">
                        <input class="typeahead" type="text" name="name" value="<?=$res['name']?>" placeholder="Client Name" required>
                      </div>
                    </div>
                    <div class="col">
                      <label>YT Video ID (eg. LahsUNxFvuU)</label>
                      <div id="bloodhound">
                        <input class="typeahead" type="text"  name="caption" value="<?=$res['caption']?>" placeholder="YT Video ID" required>
                      </div>
                    </div>
                  </div>

                  <div class="form-group">
                      <label>Video Caption Text</label>
                      <div id="bloodhound">
                        <input class="typeahead" type="text"  name="caption_text" value="<?=$res['caption_text']?>" placeholder="Video Caption" >
                      </div>
                    </div>
                  <!-- <?php
          if ($res['id'] == "") { ?>
                  <div class="form-group">
                     <label for="exampleTextarea1">Trusted Client Logo *</label>
                      <input type="file" class="dropify" name="clientimg">
                  </div>
          <?php } else{ ?>
            <div class="form-group row">
              <div class="col">
                  <label>Trusted Client Logo</label>
                      <input type="file" class="dropify" name="clientimg">
             </div>
            <div class="col">
                <img src="<?=site_url."/uploaded-files/client-image/".$res['logo_image']?>" alt="image" style="width: 60%;">
            </div>
          </div>
       <?php   } ?> -->
                  <?php
          if ($res['id'] == "") {
            ?>
            <button type="submit" class="btn btn-primary mr-2" name="add_record">Submit</button>
            <input type="hidden" name="action" value="add_record">
    <?php
          } else {
            ?>
              <button type="submit" class="btn btn-primary mr-2" name="update_record">Update</button>
              <input type="hidden" name="action" value="update_record">
    <?php
          }
          ?>
          <button class="btn btn-light" onclick="javascript: window.location.href = '<?=SITE_ADMIN_URL;?>/<?= $redirect_script_name; ?>'">Cancel</button>
                  </form>
                </div>
              </div>
            </div>
          </div>
        </div>
        <!-- content-wrapper ends -->
<?php
admin_footer();
exit;
?>

