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

$heading="Manage FAQ";
$record_type="FAQ";
$script_name="manage-faqs.php";
$redirect_script_name = "manage-faqs.php";
$table_name="faq_tbl";
####################################################

if($id=="add_record") {
  $question = str_ireplace("'", "&apos;", $question);
  $answer = str_ireplace("'", "&apos;", $answer);
		$sql=db_query("insert into $table_name set question='$question', answer='$answer', category_id='$category_id'");
		$_SESSION['session_message']='New '.$record_type.' has been added successfully';
		header("Location: ".SITE_ADMIN_URL."/".$script_name);
		exit;
}

if($id=="update_record") {
  $question = str_ireplace("'", "&apos;", $question);
  $answer = str_ireplace("'", "&apos;", $answer);
		updateTable($table_name," question='$question', answer='$answer' where slno='$recordID'");
		$_SESSION['session_message']=$record_type.' has been updated successfully';
		header("Location: ".SITE_ADMIN_URL."/".$script_name);
		exit;
}


if (isset($_POST['delete'])) {
  if (!empty($_POST['arr_id'])) {
      $catid = $_POST['arr_id'];
      foreach ($catid as $id) {
          mysqli_query($dbconn, "DELETE FROM $table_name WHERE slno='$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 where category_id='$category_id'");
?>
<?php
 $res=getResult($table_name," where slno='$_GET[recordID]'");
?>
<div class="content-wrapper">
<div class="page-header">
            <h3 class="page-title">Add New FAQS</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 New FAQS</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="">
				 <?php if($category_id!=null){ ?>
                  <div class="form-group">
                   <label>Category</label>
                    <input class="typeahead" type="text" name="" value="<?= get_category_name($category_id)?>" placeholder="Category Name">
              		</div>
					  <?php } ?>
                    <div class="form-group">
                      <label>Question</label>
                        <input class="typeahead" type="text"  name="question" value="<?=$res['question']?>" placeholder="Question" required>
                 	</div>
					 <div class="form-group">
                      <label>Answer</label>
                        <input class="typeahead" type="text"  name="answer" value="<?=$res['answer']?>" placeholder="Answer" required>
                 	</div>
                  <?php
          if($_GET[recordID] != Null) {
            ?>
            <button type="submit" class="btn btn-primary mr-2" name="add_record">Update FAQS</button>
	   		<input type="hidden" name="recordID" value="<?=$res['slno'];?>">
	   		<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; ?>?parentID=<?= $parentID; ?>&catID=<?= $catID; ?>'">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="<?admin_url?>welcome.php">Admin</a></li>
                <li class="breadcrumb-item active" aria-current="page"><?=$heading;?></li>
                </ol>
            </nav>
          </div>
          <div class="card">
            <div class="card-body">
              <div class="row">
                <div class="col-12">
                  <form method="Post" action="">
                  <div class="table-responsive">
                   <table id="order-listing" class="table table-bordered table-hover">
                      <thead>
                        <tr>
                          <th>
                            <div class="form-check mt-0">
										<label class="form-check-label">
										<input type="checkbox" class="form-check-input" name="check_all" id="check_all" value="1" onclick="checkall(this.form)">
										<i class="input-helper"></i>
										</label>
                                    </div>
                            </th>
							<th>S.No.</th>
							<?php if($category_id != 0){  ?>
		   					<th>Category</th><?php } ?>
                            <th>Question</th>
                            <th>Actions</th>
                        </tr>
                      </thead>
                      <tbody>
					  <?php
 $sl=$start+1;
 while($arr=mysqli_fetch_array($qry)) { ?>
 <tr>
                            <td><input type="checkbox" name="arr_id[]" value="<?= $arr['slno'] ?>"></td>
                            <td><?=$sl?></td>
							<?php if($category_id != 0){  ?>
		  					<td><?= get_category_name($category_id)?></td><?php } ?>
     <td><p class="user-dcs"><span><?=$arr['question'];?></span></p></td>
                            <td><p class="download-dcs">
							<a href="<?=$script_name;?>?category_id=<?= $category_id?>&id=edit_record&recordID=<?=$arr['slno'];?>"> <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>
                        </form
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <script language="javascript">

checked=false;
function checkall(frm1){
	var aa= frm1;
	if(checked == false){
		checked = true
	}
	else{
		checked = false
	}
	for (var i =0; i < aa.elements.length; i++){
		aa.elements[i].checked = checked;
	}
} 
function select_chk(){
var chks = document.getElementsByName('arr_id[]');
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;
?>