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

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

$record_type = "Add New Media Logo";
$script_name = "addeditmedia.php";
$redirect_script_name = "manage_media.php";
$table_name = "media_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/media-image");
           
        $width="100";
				$height="40";
				$source_folder=SITE_DIR_PATH."/uploaded-files/media-image/";
				$destination_folder=SITE_DIR_PATH."/uploaded-files/media-image/thumbs/";
				saveResizedPhoto($img, $width, $height, $source_folder, $destination_folder);


        }
      }

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

      $maxRes = mysqli_fetch_array(mysqli_query($dbconn, "select max(client_order) from `$table_name`"));
     $client_order = $maxRes[0] + 1;

      $sql = mysqli_query($GLOBALS['dbconn'],"insert into `$table_name` set name='$name',caption='$caption',client_order='$client_order',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,.webp 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/media-image/" . $filename;
    //     if (file_exists($file)) {
    //       unlink($file);
    //     }
    //   }

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

       

        $width="100";
				$height="40";
				$source_folder=SITE_DIR_PATH."/uploaded-files/media-image/";
				$destination_folder=SITE_DIR_PATH."/uploaded-files/media-image/thumbs/";

				saveResizedPhoto($img, $width, $height, $source_folder, $destination_folder);
  
        updateTable($table_name, " logo_image='$img' where id='$catID'");
 
}

  updateTable($table_name, " name='$name',caption='$caption' 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>Media Title *</label>
                      <div id="the-basics">
                        <input class="typeahead" type="text" name="name" value="<?=$res['name']?>" placeholder="Media Title" required>
                      </div>
                    </div>
                    <!-- <div class="col">
                      <label>Image Caption</label>
                      <div id="bloodhound">
                        <input class="typeahead" type="text"  name="caption" value="<?=$res['caption']?>" placeholder="Image Caption" >
                      </div>
                    </div> -->
                  </div>
                  <?php
          if ($res['id'] == "") { ?>
                  <div class="form-group">
                     <label for="exampleTextarea1">Media Image *</label>
                      <input type="file" class="dropify" name="clientimg" required>
                  </div>
          <?php } else{ ?>
            <div class="form-group row">
              <div class="col">
                  <label>Media Image</label>
                      <input type="file" class="dropify" name="clientimg">
             </div>
            <div class="col">
                <img src="<?=site_url."/uploaded-files/media-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;
?>

