<?php
include_once("../include/main.inc.php");
check_session('MyCpanel','index.php');
################ Paging Starts From Here ##################
@extract($_REQUEST);
$start = (intval($start)<0 or $start=="")?0:$start;
$pagesize = intval($pagesize)==0?$pagesize=20:$pagesize;

$heading="Manage Tutorial PDF";
$record_type="Tutorial PDF";
$script_name="manage_tutorialpdf.php";
$table_name="tutorialspdf_tbl";
####################################################
if ($id == "add_record" && $_FILES['pdf_file']['name'] != "") {
  $qry1= db_query("select * from $table_name where pdf_file='$link'");
  if(NumRows($qry1)>0){
  $_SESSION['session_message']=$record_type.' Already Exists';
  } 
else{
  if ($_FILES['pdf_file']['name'] != "") {
    $file_extension = strtolower(getExtension($_FILES['pdf_file']['name']));
    if ($file_extension != "jpg" && $file_extension != "pdf" && $file_extension != "jpeg" && $file_extension != "png") {
      $form_check_alert .= '<div class="btn btn-danger">Please upload small image(.jpg,.gif,.png Format Only) </div>';
    }
  }
  if (!strlen($form_check_alert) > 0) {
    if ($_FILES['pdf_file']['name'] != "") {
      $img_name = generate_unique_code('tutorialpdf_');
      $img1 = upload_file($img_name, "pdf_file", "uploaded-files/tutorial/pdf");
    }
    if ($_FILES['thumb_img']['name'] != "") {
        $img_name2 = generate_unique_code('tutorialpdf_thumb_img_');
        $img2 = upload_file($img_name2, "thumb_img", "uploaded-files/tutorial/pdf/thumb");
      }
    $sql = db_query("insert into $table_name set title='$title',pdf_file='$img1',thumbnail_img='$img2'");
    $_SESSION['session_message'] = '<div class="btn btn-success">New ' . $record_type . ' has been added successfully </div>';

    header("Location: " . SITE_ADMIN_URL . "/" . $script_name);
    exit;
  } else {
    $msg = html_head();
    $msg .= $form_check_alert;
    $msg .= html_foot();
  }
}
}
if($id=="update_record") {
  if ($_FILES['pdf_file']['name'] != "") {
    $file_extension = strtolower(getExtension($_FILES['pdf_file']['name']));
    if ($file_extension != "jpg" && $file_extension != "gif" && $file_extension != "jpeg" && $file_extension != "png") {
      $form_check_alert .= "<li>Please upload small image(.jpg,.gif,.png Format Only)</li>";
    }
  }
	if(!strlen($form_check_alert)>0) {
    if ($_FILES['pdf_file']['name'] != "") {
      $res1 = getResult($table_name, " where id='$recordID'");
      $filenm1 = SITE_DIR_PATH . "/uploaded-files/tutorial/pdf/" . $res1['pdf_file'];
      if (file_exists($filenm1)) {
        unlink($filenm1);
      }

      $img_name = generate_unique_code('tutorialpdf_');
      $img1 = upload_file($img_name, "pdf_file", "uploaded-files/tutorial/pdf");
      updateTable($table_name, " pdf_file='$img1' where id='$recordID'");
    }
    if ($_FILES['thumb_img']['name'] != "") {
        $res1 = getResult($table_name, " where id='$recordID'");
        $filenm1 = SITE_DIR_PATH . "/uploaded-files/tutorial/pdf/" . $res1['thumb_img'];
        if (file_exists($filenm1)) {
          unlink($filenm1);
        }
  
        $img_name = generate_unique_code('tutorialpdf_thumb_img_');
        $img1 = upload_file($img_name, "thumb_img", "uploaded-files/tutorial/pdf/thumb");
        updateTable($table_name, " thumbnail_img='$img1' where id='$recordID'");
      }

		mysqli_query($dbconn,"UPDATE `$table_name` SET  title='$title' where id='$recordID'");
		$_SESSION['session_message']=$record_type.' has been updated successfully';
		header("Location: ".SITE_ADMIN_URL."/".$script_name);
		exit;
	}
	else {
		
		$msg=html_head();
		$msg.=$form_check_alert;
		$msg.=html_foot();
	}	
}

if (isset($_POST['delete'])) {
	if (!empty($_POST['cat_arr'])) {
		$catid = $_POST['cat_arr'];
		foreach ($catid as $id) {
			mysqli_query($dbconn, "DELETE FROM $table_name WHERE id = '$id'");
		}
		$_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been deleted successfully</div>';
	} else {
		$_SESSION['session_message'] = '<div class="btn btn-danger">No Category selected</div>';
	}
	header("Location: ".SITE_ADMIN_URL."/".$script_name);
	exit;
  }
  ########################################################
  admin_header("");
  $qry= db_query("select * from $table_name");
  ?>
<?php
$res = getResult($table_name," where id='$_GET[recordID]'");
?>
<div class="content-wrapper">
    <div class="page-header">
        <h3 class="page-title">Add Tutorial Pdf</h3>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="<?admin_url?>welcome.php">Admin</a></li>
                <li class="breadcrumb-item active" aria-current="page">Add Tutorial Pdf</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">
                            <label>PDF Title</label>
                            <input class="typeahead" type="text" name="title" value="<?=$res['title']?>" placeholder="PDF Title" required>
                        </div>
                        <?php
          if($res['pdf_file'] != Null) {
            ?>
                        <div class="form-group">
                            <label>PDF File</label>
                            <input class="dropify" type="file" name="pdf_file">
                            <a href="" class="href">View PDF</a>
                        </div>
                        <?php
          } else {
            ?>
                        <div class="form-group">
                            <label>PDF File</label>
                            <input class="dropify" type="file" name="pdf_file" required>
                        </div>
                        <?php
          }
          ?>
                        <?php
          if($res['thumbnail_img'] != Null) {
            ?>
                        <div class="form-group row">
                            <div class="col">
                                <label>Thumbnail Image</label>
                                <div id="the-basics">
                                    <input class="dropify" type="file" name="thumb_img">
                                </div>
                            </div>
                            <div class="col">
                                <div id="bloodhound">
                                    <img src="<?=site_url."/uploaded-files/tutorial/pdf/thumb/".$res['thumbnail_img']?>" alt="<?=$res['title']?>" title="<?=$res['title']?>" style="height:240px">
                                </div>
                            </div>
                        </div>
                        <?php
          } else {
            ?>
                        <div class="form-group">
                            <label>Thumbnail Image</label>
                            <input class="dropify" type="file" name="thumb_img" required>
                        </div>
                        <?php
          }
          ?>
                        <?php
          if($_GET[recordID] != Null) {
            ?>
                        <button type="submit" class="btn btn-primary mr-2" name="add_record">Update</button>
                        <input type="hidden" name="recordID" value="<?=$res['id'];?>">
                        <input type="hidden" name="id" value="update_record">
                        <?php
          } else {
            ?>
                        <button type="submit" class="btn btn-primary mr-2" name="submit">Submit</button>
                        <input type="hidden" name="id" value="add_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 class="page-header">
        <h3 class="page-title">
            <?=$heading;?>
        </h3>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="welcome.php">Admin</a></li>
                <li class="breadcrumb-item active" aria-current="page">Manage Video Gallery</li>
            </ol>
        </nav>
    </div>
    <div class="card">
        <div class="card-body">
            <div class="row">
                <div class="col-12">
                    <form action="" method="post">
                        <div class="table-responsive">
                            <table id="order-listing" class="table table-bordered table-hover">
                                <thead>
                                    <tr>
                                        <th class="_reab">
                                            <div class="form-check mt-0">
                                                <label class="form-check-label">
                                                    <input type="checkbox" class="form-check-input">
                                                    <i class="input-helper"></i>
                                                </label>
                                            </div>
                                        </th>
                                        <th>S.No.</th>
                                        <th>PDF Title</th>
                                        <th>Thumbnail Image</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
 $sl=$start+1;
 while($arr=mysqli_fetch_array($qry)) { ?>
                                    <tr>
                                        <td>
                                            <div class="form-check mt-0"><label class="form-check-label"><input type="checkbox" class="form-check-input" name="cat_arr[]" value="<?= $arr['id'] ?>"><i class="input-helper"></i></label>
                                            </div>
                                        </td>
                                        <td><?=$sl?></td>
                                        <td><p class="ordern"><?=$arr['title'];?></td>
                                        <td>
                                            <img src="<?=site_url?>/uploaded-files/tutorial/pdf/thumb/<?=$arr['thumbnail_img'];?>" alt="<?=$arr['title'];?>" title="<?=$arr['title'];?>">
                                        </td>
                                        <td><p class="download-dcs">
                                            <a href="<?=SITE_ADMIN_URL?>/manage_tutorialpdf.php?recordID=<?=$arr['id'];?>">
                                            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-edit-3">
                                                        <path d="M12 20h9"></path>
                                                        <path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z"></path>
                                                </svg>
                                            </a>
                                            </p>
                                        </td>
                                    </tr>

                                    <?php  $sl++; }?>

                                </tbody>
                            </table>
                            <div class="btn-grp">
                                <button type="submit" name="delete" onclick="return select_chk()" class="btn btn-danger btn-icon-text btn-sm">
                                    <i class="fas fa-trash-alt mr-1"></i>Delete
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script language="javascript">
    function select_chk() {
        var chks = document.getElementsByName('cat_arr[]');
        var hasChecked = false;
        for (var i = 0; i < chks.length; i++) {
            if (chks[i].checked) {
                hasChecked = true;
                break;
            }
        }
        if (hasChecked == false) {
            alert("Please Select At Least One.");
            return false;
        }
    }

</script>
<?php
admin_footer();
exit;
?>
