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


if(isset($_REQUEST['edit']))
{
    $id = base64_decode($_REQUEST['edit']);
    $ex = $obj->get_whyus_id($id);
    $row = mysqli_fetch_array($ex);
}
if(isset($_POST['update']))
{   
  $update_why_us['id'] = $_POST['id'];
  $update_why_us['section1_text'] = str_replace("'","\'", $_POST['section1_text']);
  $update_why_us['section2_text'] = str_replace("'","\'", $_POST['section2_text']);
  $update_why_us['section3_text'] = str_replace("'","\'", $_POST['section3_text']);
  $update_why_us['where_we_oprate_title'] = str_replace("'","\'", $_POST['where_we_oprate_title']);
  $update_why_us['where_we_oprate_text'] = str_replace("'","\'", $_POST['where_we_oprate_text']);
    $server_url = '//'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
    $upload_dir = '../uploads/why_us/';
        
        if(!empty($_FILES['section1_img']['name']))
        {            
            $tempname = $_FILES['section1_img']['tmp_name'];
            $upload_name = $upload_dir.$_FILES['section1_img']['name'];
            move_uploaded_file($tempname,$upload_name);
            $update_why_us['section1_img'] = $server_url."/".$upload_name;
        }
        else
        {
            $update_why_us['section1_img'] =$_POST['side_img1'];
        }
		if(!empty($_FILES['section2_img']['name']))
        {            
            $tempname = $_FILES['section2_img']['tmp_name'];
            $upload_name = $upload_dir.$_FILES['section2_img']['name'];
            move_uploaded_file($tempname,$upload_name);
            $update_why_us['section2_img'] = $server_url."/".$upload_name;
        }
        else
        {
            $update_why_us['section2_img'] =$_POST['side_img2'];
        }
		if(!empty($_FILES['section3_img']['name']))
        {            
            $tempname = $_FILES['section3_img']['tmp_name'];
            $upload_name = $upload_dir.$_FILES['section3_img']['name'];
            move_uploaded_file($tempname,$upload_name);
            $update_why_us['section3_img'] = $server_url."/".$upload_name;
        }
        else
        {
            $update_why_us['section3_img'] =$_POST['side_img3'];
        }

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

if(isset($_POST['submit']))
{


  $why_us_info['why_desc'] = str_replace("'","\'", $_POST['why_desc']);
  $why_us_info['bottom_desc'] = str_replace("'","\'", $_POST['bottom_desc']);
  
    $server_url = '//'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
    $upload_dir = '../uploads/why_us/';
        
        if(!empty($_FILES['side_img']['name']))
        {            
            $tempname = $_FILES['side_img']['tmp_name'];
            $upload_name = $upload_dir.$_FILES['side_img']['name'];
            move_uploaded_file($tempname,$upload_name);
            $why_us_info['side_img'] = $server_url."/".$upload_name;
        }

        
   $ex = $obj->insert_why_us_info($why_us_info);
    echo '<script>window.location="why_us.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>Why Us
                <a href="why_us.php" class="btn btn-primary btn-sm float-right">Back</a>
            </div>
            <div class="card-body">
                <form method="post" name="frm" id="frm" enctype="multipart/form-data">
                   <input type="hidden" name="id" value="<?php echo $row['id'];?>">
                    
                   <div class="form-group">
                  <label>Section1 Text</label>
                  <textarea id="w3review" name="section1_text" rows="6" cols="50" class="tinymce"><?php echo $row['section1_text'];?></textarea>
                  </div>
                 <div class="form-group">
                     <label>Section1 Image</label><br>
                     <input type="file" class="" name="section1_img" value="<?php echo $row['section1_img'];?>">
                       <input type="hidden" name="side_img1" value="<?php echo $row['section1_img'];?>">
                       <img src="<?php echo $row['section1_img'];?>"  style="width: 300px;">
                     </div>
                        <div class="form-group">
                  <label>Section2 Text</label>
                  <textarea id="w3review" name="section2_text" rows="6" cols="50" class="tinymce"><?php echo $row['section2_text'];?></textarea>
                  </div>
				   <div class="form-group">
                     <label>Section2 Image</label><br>
                     <input type="file" class="" name="section2_img" value="<?php echo $row['section2_img'];?>">
                       <input type="hidden" name="side_img2" value="<?php echo $row['section2_img'];?>">
                       <img src="<?php echo $row['section2_img'];?>"  style="width: 300px;">
                     </div>
					 <div class="form-group">
                  <label>Section3 Text</label>
                  <textarea id="w3review" name="section3_text" rows="6" cols="50" class="tinymce"><?php echo $row['section3_text'];?></textarea>
                  </div>
				   <div class="form-group">
                     <label>Section3 Image</label><br>
                     <input type="file" class="" name="section3_img" value="<?php echo $row['section3_img'];?>">
                       <input type="hidden" name="side_img3" value="<?php echo $row['section3_img'];?>">
                       <img src="<?php echo $row['section3_img'];?>"  style="width: 300px;">
                     </div>
					 
					  <div class="form-group">
                  <label>Where We Operate Title</label>
                  <textarea id="w3review" name="where_we_oprate_title" rows="6" cols="50" class="tinymce"><?php echo $row['where_we_oprate_title'];?></textarea>
                  </div>
				  
				   <div class="form-group">
                  <label>Where We Operate Text</label>
                  <textarea id="w3review" name="where_we_oprate_text" rows="6" cols="50" class="tinymce"><?php echo $row['where_we_oprate_text'];?></textarea>
                  </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 url = "why_us.php";
</script>

<?php require_once("footer.php");?>

