<?php
require_once("header.php");
require_once("menu.php");


if(isset($_REQUEST['edit']))
{
    $faq_id = base64_decode($_REQUEST['edit']);
    $ex = $obj->get_faq_id($faq_id);
    $row = mysqli_fetch_array($ex);
}
if(isset($_POST['update']))
{   
  $faq_info['faq_faq_menu_id'] = $_POST['faq_faq_menu_id'];
  $faq_info['faq_id'] = $_POST['faq_id'];
   foreach ($_POST['faq_title'] as $key => $value) {
    $faq_info['faq_title'] = $_POST['faq_title'][$key];
           $faq_info['faq_desc'] = $_POST['faq_desc'][$key];

 $ex = $obj->update_faq_info($faq_info);
  }
    echo '<script>window.location="faq.php"</script>';
}

if(isset($_POST['submit']))
{
  $faq_info['faq_faq_menu_id'] = $_POST['faq_faq_menu_id'];
 foreach ($_POST['faq_title'] as $key => $value) {
          $faq_info['faq_title'] = $_POST['faq_title'][$key];
           $faq_info['faq_desc'] = $_POST['faq_desc'][$key];
          
 $ex = $obj->add_faq_info($faq_info);
  }
 
        
    echo '<script>window.location="faq.php"</script>';
}

?>

<div id="layoutSidenav_content">
<main class="mt-4">
    <div class="container-fluid">
        
        <div class="card mb-4">
            <div class="card-header">
                <i class="fas fa-check-square mr-1"></i>Faq Content
                <a href="faq.php" class="btn btn-primary btn-sm float-right">Back</a>
            </div>
            <div class="card-body">
                <form method="post" id="users" enctype="multipart/form-data">
                   <input type="hidden" name="faq_id" value="<?php echo $row['faq_id'];?>">
                
                     <div class="form-group">

                        <select name="faq_faq_menu_id" placeholder="Category" class="form-control" required="">
                <option value="">Select Category</option>
                 <?php
                  $ex = $obj->view_faqmenu();
                  while($crow=mysqli_fetch_array($ex))
                  {
                  ?>
                  <option <?php echo $row['faq_menu_id'] == $crow['faq_menu_id'] ? 'selected' : '';?>  value="<?php echo ucwords($crow['faq_menu_id']);?>"><?php echo ucwords($crow['faq_menu_name']);?></option>
               <?php
                 }
                  ?>
                 </select>

               </div>
          <h5 class="siteconfi">Description</h5>
              
              <div class="table-responsive">
                  <table style="width:100%" id='myTable' class="table">
                     <thead>
                        <tr>
                           <th>
               <?php if(!isset($_REQUEST['edit']))
                { ?> 
                  <a id="insert-more" onClick="insertRow()" class="add_row123">+</a>
                <?php } ?>
              </th>
                           
                           <th>Title</th>
                           <th>Description</th>
                        </tr>
                     </thead>
                     <tbody>
            <?php if(isset($_REQUEST['edit']))
              { ?> 
            <tr>
              
                           <td>1</td>

                             <td><input type="text" class="form-control" name="faq_title[]" value="<?php echo $row['faq_title'];?>">
                           </td>
                           
                           <td>
                           <textarea id="w3review" name="faq_desc[]" rows="2" cols="50" class="form-control"><?php echo $row['faq_desc'];?></textarea>
                           </td>
                          
                        </tr>
            <?php }else{ ?>
                        <tr>
                           <td>1</td>
                            <td><input type="text" class="form-control" name="faq_title[]" value="<?php echo $row['faq_title'];?>">
                           </td>
                            <td>
                           <textarea id="w3review" name="faq_desc[]" rows="2" cols="50" class="form-control"><?php echo $row['faq_desc'];?></textarea>
                           </td>
                          
                        </tr>
                        <!--<tr>-->
                        <!--   <td>2</td>-->
                        <!--    <td><input type="text" class="form-control" name="faq_title[]" value="<?php echo $row['faq_title'];?>">-->
                        <!--   </td>-->
                        <!--    <td>-->
                        <!--   <textarea id="w3review" name="faq_desc[]" rows="2" cols="50" class="form-control"><?php echo $row['faq_desc'];?></textarea>-->
                        <!--   </td>-->
                          
                        <!--</tr>-->
            <?php } ?>
                     </tbody>
                  </table>
               </div>


           <?php if(isset($_REQUEST['edit']))
                    { ?> 
                        <button type="submit" class="btn btn-primary" name="update">Update</button>
                    <?php } 
                    else
                    { ?>
                        
                        <button type="submit" class="btn btn-primary" name="submit">Submit</button>
                    <?php } ?>
                </form> 
            </div>
        </div>
    </div>
</main>
<script>
   var row = 1;
   var index = row;
   var sr =index;
       function insertRow(){
   
         var table=document.getElementById("myTable");
         var row=table.insertRow(table.rows.length);
   
         var cell0=row.insertCell(0);
         var t0=document.createElement("span");
             t0.innerHTML=++sr;
             cell0.appendChild(t0);
   
   
   
         var cell2=row.insertCell(1);
         var t2=document.createElement("textarea");
             t2.setAttribute("type", "textarea");
             t2.name = "faq_desc[]";
             t2.setAttribute("class","form-control");
             cell2.appendChild(t2);


         
         var cell2=row.insertCell(1);
         var t2=document.createElement("input");
             t2.setAttribute("type", "text");
             t2.name = "faq_title[]";
             t2.setAttribute("class","form-control");
             cell2.appendChild(t2);
   
         
         index++;
   }
   
</script>
<script>
    var url = "faq.php";
</script>
<?php require_once("footer.php");?>

