<?php
session_start();
error_reporting(1);
ini_set('display_errors', 1);
require_once "../config.php";
include_once "../ichat/admin/route.php";
if ($session->is_logged_in() != true ) {
        redirect_to(WEB_LINK.'/login');
} else {
    $current_user = User::get_specific_id($session->admin_id);
    $current_user->set_online();
    $group = $current_user->prvlg_group;
}


require_once("../ichat/admin/assets/includes/Library/SessManager.php");
require_once("../ichat/admin/assets/includes/Library/User.php");
$session = new  SessManager();

//require_once "check_admin.php";
require_once 'database1.php';

class edit_devices extends database
{
   public function index($data,$id)
    {
		$where['cl_id']= $id;
		return $this->update('tb_product',$data, $where);
	}
	public function Getdata($id)
    {
		$where['cl_id']= $id;
		return $this->fetchRow('tb_product', $where);
	}

    public function getAllImages($id)
    {
        $where['cl_id'] = $id;
        return $this->fetchAll('tb_files', $where);
    }
}

$obj = new edit_devices();	
$msg="";
if(!isset($_GET['id']))
{
	  header('Location:product.php');
      exit;
}
$id=$_GET['id'];
if(isset($_POST['data']))
{
	$data=$_POST['data'];
	if(trim($data['cl_name']) == '')
    {
        $msg.= "Name is required<br/>";
    }

    if(trim($data['cl_regular_price']) == '')
    {
        $msg.= "Regular Price is required<br/>";
    }

    if(trim($data['cl_sale_price']) == '')
    {
        $msg.= "Sale Price is required<br/>";
    }
	if($msg=="")
	{
		 $res = $obj->index($data,$id);
            if($res)
            {
                    header('Location:products.php?message=Product Updated Successfully');
            }
            else {
                $msg = 'Something Went Wrong';
            }
	}
}

$data = $obj->Getdata($id);
$images = $obj->getAllImages($id);
include 'header.php';
?>

 <link href="../dropzone/dist/dropzone.css" rel="stylesheet" />
 <script src="../dropzone/dist/dropzone.js"></script>

<script src="../ckeditor/ckeditor.js"></script>
            <!-- MAIN CONTENT-->
            <div class="main-content">
                <div class="section__content section__content--p30">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="overview-wrap">
                                    <h2 class="title-1">Edit product</h2>
                                    
                                </div>
								<?php if($msg !=''){echo '<span class="col-md-6 alert alert-danger pull-left">'.$msg.'</span>';}?>
                            </div>
                        </div>
                        
                        <div class="row m-t-25 m-row">
					
                            <div class="col-lg-10">
                                <div class="card">
                                    
                                    <div class="card-body card-block">
                                        <form action="product_file_upload?id=<?php echo htmlspecialchars($_GET['id']) ?>"
                  class="dropzone"
                  id="flie_upload_dropzone">
                    

                  </form>

                  <table class="table table-bordered table-striped">
                      <thead>
                        <tr>
                            <th>
                                Image
                            </th>
                            <th>
                                Extension
                            </th>

                        </tr>
                      </thead>
                      <tbody>
                        <?php
                            if(!empty($images))
                            {
                                foreach($images AS $image)
                                {
                                    ?>
                                    <tr>
                                        <td>
                                            <img src="../uploads/products/<?php echo htmlspecialchars($image['cl_name']) ?>" style="max-width: 150px" />
                                        </td>
                                        <td>
                                            <?php echo htmlspecialchars($image['cl_extension']) ?>
                                        </td>
                                    </tr>
                                    <?php
                                }
                            }
                        ?>
                      </tbody>
                  </table>
									<form method="post" style="margin-top: 25px;">
                                            <div class="form-group">
                                                <label for="Name" class=" form-control-label">Product  Name</label>
                                                <input type="text" name="data[cl_name]" id="Name" placeholder="Product Name" class="form-control" value="<?php if(isset($data['cl_name'])){echo $data['cl_name'];}?>" />
                                            </div>
                                            <div class="form-group">
                                                <label for="Description" class=" form-control-label">Small Specification</label>
                                                <textarea  id="small_specification" name="data[cl_small_specification]" placeholder="Small Specification" class="form-control" value=""><?php if(isset($data['cl_small_specification'])){echo $data['cl_small_specification'];}?></textarea>
                                            </div>
                                            <div class="form-group">
                                                <label for="Description" class=" form-control-label">Big Specification</label>
                                                <textarea  id="big_specification" name="data[cl_big_specification]" placeholder="Big Specification" class="form-control" value="" ><?php if(isset($data['cl_big_specification'])){echo $data['cl_big_specification'];}?></textarea>
                                            </div>
                                            <div class="form-group">
                                                <label for="Description" class=" form-control-label">Description</label>
                                                <textarea id="Description" name="data[cl_description]" placeholder="Description" class="form-control" value="" style="height: 150px"><?php if(isset($data['cl_description'])){echo $data['cl_description'];}?></textarea>
                                            </div>
                                            <div class="form-group">
                                                <label for="Description" class=" form-control-label">Video</label>
                                                <input type="text" id="Description" name="data[cl_video]" placeholder="Video" class="form-control" value="<?php if(isset($data['cl_video'])){echo $data['cl_video'];}?>" />
                                            </div>
                                            <div class="form-group">
                                                <label for="Description" class=" form-control-label">Regular Price</label>
                                                <input type="text" id="Description" name="data[cl_regular_price]" placeholder="Regular Price" class="form-control" value="<?php if(isset($data['cl_regular_price'])){echo $data['cl_regular_price'];}?>" />
                                            </div>
                                            
                                            <div class="form-group">
                                                <label for="Description" class=" form-control-label">Sale Price</label>
                                                <input type="text" id="Description" name="data[cl_sale_price]" placeholder="Sale Price" class="form-control" value="<?php if(isset($data['cl_sale_price'])){echo $data['cl_sale_price'];}?>" />
                                            </div>
                                            

                                            <div class="form-group">
                                              <button type="submit" class="track-btn pull-right">Submit</button>
                                            
                                            </div>
                                        
                                        </form>
                                    </div>
                                </div>
                            </div>
                          
                        </div>
                        
                    <script>
							$(document).ready(function(){
								$('.active').removeClass('active');
								$('#device-menu').addClass('active');
							});

                            $(document).on('click','.cke a', function(){
                                setTimeout(
                                  function() 
                                  {
                                    $('body').removeClass('fade-out');
                                }, 100);
                            });

                            $(document).on('click', '.cke_reset_all a', function(){
                                setTimeout(
                                  function() 
                                  {
                                    $('body').removeClass('fade-out');
                                },
                                100);
                            });
						</script>      
                        
                           <script>
                                  CKEDITOR.replace( 'small_specification');
                                  CKEDITOR.replace( 'big_specification');
                        </script>   

  <?php
  include 'footer.php';
  ?>