<?php 
require_once '../config/db.php';
require_once '../config/database.class.php';
require_once '../config/functions.php';
$d = new database($db_config);


if(isset($_POST)){
	$post  =$_POST;
	$catid = $post['catid'];
	$subcategory = $post['subcategory'];
    $product = $post['product'];
    $code = $post['code'];
    $stock = $post['stock'];
    $price = $post['price'];
    $finish = $post['finish'];
    $unit = $post['unit'];
    $title = $post['title'];
     $detail = $post['detail'];
    $description = $post['description'];
     $choose = $post['choose'];
      $delievery = $post['delievery'];
     $name = '';
    $tmp_name = '';
    $file_url = '';
    $name2 = '';
    $tmp_name2 = '';
    $file_url2 = '';
 
    if(empty($post['id']) && empty($_FILES['image1']['name']))
    {
        //If file not selected displaying a message to choose a file while insert new products-------
        echo getResponse('error','Please choose a file');
          ?>  <script >
 alert('Please choose a file');

   
    </script> <?php
        die;
    }
    else if(!empty($_FILES['image1']['name']))
    {
        $name = $_FILES['image1']['name'];
        $name2 = $_FILES['image2']['name'];
        $tmp_name = $_FILES['image1']['tmp_name'];
        $tmp_name2 = $_FILES['image2']['tmp_name'];
        //Getting temporary file name stored in php tmp folder 
 
        //Path to store files on server
        $path = '../uploads/';
        if(!empty($name))
        {
            //Moving file to temporary location to upload path 
            move_uploaded_file($tmp_name,$path.$name);
            move_uploaded_file($tmp_name2,$path.$name2);
            //Displaying success message 
            //echo "Upload successfully";
        }
        $file_url = $path.$name;
        $file_url2 = $path.$name2;
    }
    
  

 
  
	if($post['id']==''){
    $query = "INSERT INTO product (`catid`,`subcategory`,`product`,`code`,`stock`,`price`,`finish`,`unit`,`title`,`detail`,`description`,`choose`,`delievery`,`image1`,`image2`) VALUES('$catid','$subcategory','$product','$code','$stock','$price','$finish','$unit','$title','$detail','$description','$choose','$delievery','$file_url','$file_url2')";
        $executeQuery = $d->insertQuery($query);
        if($executeQuery){
        	echo getResponse('success','Data saved successfully');
        	
        }else{
        	echo getResponse('error','Something went wrong, please try later!');
        }
	}else{
$id = $post['id'];
		$query = "UPDATE product SET catid='".$catid."',subcategory='".$subcategory."',product='".$product."',code='".$code."',stock='".$stock."',price='".$price."',finish='".$finish."',unit='".$unit."',title='".$title."',detail='".$detail."',description='".$description."',choose='".$choose."',delievery='".$delievery."'";
         if(!empty($name))
        {
            $query .= " , image1='".$file_url."', image2='".$file_url2."' ";
        }
        
         $query .= " WHERE id=$id";
        $executeQuery = $d->updateQuery($query);
        if($executeQuery){
      
        	echo getResponse('success','Data updated successfully');
        	  ?>  <script >
 alert('data updated successfully');
	 document.location.href = "../view/product.php"
   
    </script> <?php
        }else{
        	echo getResponse('error','Something went wrong, please try later!');
        }

	}
}
?>