<?php

include_once("../include/main.inc.php");
check_session('MyCpanel', 'index.php');
?>
<script type="text/javascript">
function chk_valid_date() {
    if (document.bannerFrm.validFrom.value == 0) {
        alert("Please select Validity Period");
        return false;
    }
    if (document.bannerFrm.validTo.value == 0) {
        alert("Please select Validity Period");
        return false;
    }
}
</script>
<?php
################ Paging Starts From Here ##################
@extract($_REQUEST);
$start = (intval($start) < 0 or $start == "") ? 0 : $start;
$pagesize = intval($pagesize) == 0 ? $pagesize = 20 : $pagesize;
$res = getResult("admin_details", " where user_type='admin'");

$heading = "Manage News Media Logo";
$record_type = "News Media Logo";
$script_name = "addeditnewsmedia.php";
$redirect_script_name ="manage-news-media.php";
$table_name = "news_media_tbl";
####################################################


if ($action == "add_record" && $_FILES['banner']['name'] != "") {
  // $count_cat = mysqli_query($GLOBALS['dbconn'], "SELECT * FROM $table_name");
  // if (mysqli_num_rows($count_cat) >= $res['banner_count']) {
  //   $_SESSION['session_message'] = ' <div class="btn btn-danger"> ' . $record_type . ' count exceeds the allowed limit [' . $res[banner_count] . ']  </div>';
  // } else {
    if ($_FILES['banner']['name'] != "") {
      $file_extension = strtolower(getExtension($_FILES['banner']['name']));
      if ($file_extension != "jpg" && $file_extension != "gif" && $file_extension != "jpeg" && $file_extension != "png" && $file_extension != "webp") {
        $form_check_alert .= "<li>Please upload small image(.jpg,.gif,.png,.webp Format Only)</li>";
      }
    }
      if ($_FILES['banner']['name'] != "") {
        $img_name = "$personName";
        $img_name = str_replace(" ", "-", $img_name);
        $img1 = upload_file($img_name, "banner", "uploaded-files/news-media");

           //Thumb Cut
      $width="250";
      $height="70";

      $source_folder=SITE_DIR_PATH."/uploaded-files/news-media/";
      $destination_folder=SITE_DIR_PATH."/uploaded-files/news-media/thumbs/";
      saveResizedPhoto($img1, $width, $height, $source_folder, $destination_folder);
      }
  
      $maxRes=db_fetch_array(db_query("select max(bannerorder) from $table_name"));
			$bannerorder=$maxRes[0]+1;

      $sql = db_query("insert into $table_name set personName='$personName',bannerUrl='$bannerUrl', bannerImage='$img1', bannerorder='$bannerorder', recvDate=now()");
      // $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been added successfully  </div>';

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

if ($action == "update_record") {
  if ($_FILES['banner']['name'] != "") {
    $file_extension = strtolower(getExtension($_FILES['banner']['name']));
    if ($file_extension != "jpg" && $file_extension != "gif" && $file_extension != "jpeg" && $file_extension != "png" && $file_extension != "webp") {
      $form_check_alert .= "<li>Please upload small image(.jpg,.gif,.png,.webp Format Only)</li>";
    }
  }
  if (!strlen($form_check_alert) > 0) {
    if ($_FILES['banner']['name'] != "") {
      $res1 = getResult($table_name, " where slno='$sl'");
      $filenm1 = SITE_DIR_PATH . "/uploaded-files/news-media/" . $res1['bannerImage'];
      if (file_exists($filenm1)) {
        unlink($filenm1);
      }

      $img_name = "$personName";
        $img_name = str_replace(" ", "-", $img_name);
      $img1 = upload_file($img_name, "banner", "uploaded-files/news-media");

            //Thumb Cut
            $width="250";
            $height="70";

      $source_folder=SITE_DIR_PATH."/uploaded-files/news-media/";
      $destination_folder=SITE_DIR_PATH."/uploaded-files/news-media/thumbs/";
      saveResizedPhoto($img1, $width, $height, $source_folder, $destination_folder);
      
      updateTable($table_name, " bannerImage='$img1' where slno='$slno'");
    }

    $bannerUrl = $bannerUrl;
    updateTable($table_name, " personName='$personName',bannerUrl='$bannerUrl', recvDate=now() where slno='$slno'");
    // $_SESSION['session_message'] = $record_type . ' has been updated successfully';
    header("Location: " . SITE_ADMIN_URL . "/" . $redirect_script_name . "?start=" . $start . "&pagesize=" . $pagesize);
    exit;
  } else {

    $msg = html_head();
    $msg .= $form_check_alert;
    $msg .= html_foot();
  }
  
}
########################################################
admin_header();
$res = getResult($table_name, " where slno='$slno'");
?>
<div class="content-wrapper">
    <div class="page-header">
        <h3 class="page-title">
            <?=$heading;?>
            <?=$_SESSION['session_message'];?>
        </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>News/Media Title *</label>
                                <div id="the-basics">
                                    <input class="typeahead" type="text" name="personName"
                                        value="<?=$res['personName']?>" placeholder="News/Media Title" required>
                                </div>
                            </div>

                        </div>

                        <div class="form-group row">
                            <div class="col">
                                <label>Media Link</label>
                                <div id="the-basics">
                                    <input class="typeahead" type="text" name="bannerUrl"
                                        value="<?=$res['bannerUrl']?>" placeholder="Media Link" >
                                </div>
                            </div>

                        </div>


                        <?php
          if ($res['slno'] == "") { ?>
                        <div class="form-group">
                            <label for="exampleTextarea1">Media Logo * (117 X 43Px)</label>
                            <input type="file" class="dropify" name="banner" required>
                        </div>
                        <?php } else{ ?>
                        <div class="form-group row">
                            <div class="col">
                                <label>Media Logo (117 X 43 Px)</label>
                                <input type="file" class="dropify" name="banner">
                            </div>
                            <div class="col">
                                <img src="<?=site_url."/uploaded-files/news-media/thumbs/".$res['bannerImage']?>"
                                    alt="image" style="width: 60%;">
                            </div>
                        </div>
                        <?php   } ?>

                        <?php
          if ($res['slno'] == "") {
            ?>
                        <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; ?>?parentID=<?= $parentID; ?>&catID=<?= $catID; ?>'">Cancel</button>

                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- content-wrapper ends -->
<?php
admin_footer();
exit;
?>