<?php
session_start();
if(!isset($_SESSION['aid']))
{
	echo '<script>location.href="../index.php"</script>';
}

include "../connection/config.php";

if(isset($_POST['submit']))
{
    $flag  =  $_POST['flag'];
    $iid   =  $_POST['iid'];

    $sd="select * from item_details where iid='$iid'";
    $sdex=mysqli_query($con,$sd);
    $sdf = mysqli_fetch_assoc($sdex);

     $category    =   $_POST['category'];
     $size        =   $_POST['size'];
     $material    =   $_POST['mtrl'];
     $color       =   strtoupper($_POST['color']);
     $price       =   $_POST['price'];
     //$relvl       =   $_POST['relvl'];
     $unit        =   $_POST['unit'];
     $description =   $_POST['description'];
     $for         =   $_POST['usg'];

     //get category name
    $sdc="select * from item_category where cid='$category'";
    $scdex=mysqli_query($con,$sdc);
    $scdf = mysqli_fetch_assoc($scdex);
    $cname=$scdf['category'];

     // item name as array
     $name_parts = array($cname,$for,$size);
     $iname = implode(',', $name_parts);

    $image=$_FILES['img']['name'];
    move_uploaded_file($_FILES['img']['tmp_name'],"items/".$image);

    if(empty($_FILES['img']['name']))
    {
        $image=$sdf['item_img'];
    }
    else
    {
    $image=$_FILES['img']['name'];
    move_uploaded_file($_FILES['img']['tmp_name'],"items/".$image);
    }


if($flag==0)
{

  $sql="insert into item_details(cid,item_name,size,material,color,item_img,item_price,item_unit,item_description,usefor)
        values('$category','$iname','$size','$material','$color','$image','$price','$unit','$description','$for')";
  $exe=mysqli_query($con,$sql);
  echo '<script>location.href="item_details.php"</script>';
}
if($flag==2)
{
    $sqli="update item_details set cid='$category',item_name='$iname',size='$size',material='$material',color='$color',item_img='$image',item_price='$price',item_unit='$unit',item_description='$description',usefor='$for' where iid='$iid'";  
    $exee=mysqli_query($con,$sqli);
    //echo mysqli_error($exee);
    echo '<script>location.href="item_details.php"</script>';
}
}
?>