<?php
include('../../includes/con2pma3.php');
session_start();
if (isset($_SESSION['UserName'])) {
    $dwgID = $_POST['dwg_id'];
    $sheet_no= $_POST['dwgsheet'];
    $rev_no= $_POST['dwgrevision'];
    $range_no	= $_POST['range_no'];
    $dwg_title= $_POST['dwg_title'];
	$dwg_title2= $_POST['dwg_title2'];
	$dwg_idate= $_POST['dwg_idate'];
	$discipline= $_POST['discipline'];
    $attachment= $_FILE['attachment'];
//    $status= $_POST['status'];
//    $created_date= $_POST['dwgid'];
//    $created_by	= $_POST['dwgid'];
//    $last_modified_date= $_POST['dwgid'];
//    $last_modified_by= $_POST['dwgid'];
    $date = date('Y-m-d');
    $id = $_SESSION['id'];

    $task =isset($_POST['task'])?$_POST['task']:"";

    switch($task){
        case "add":
            $newfilename ="";
            $uploaddir = 'uploads/';
            if($_FILES["attachment"]["name"] !=""){
                $ext = pathinfo($_FILES["attachment"]["name"], PATHINFO_EXTENSION);

                $originalName = pathinfo($_FILES["attachment"]["name"], PATHINFO_FILENAME);
                $newfilename = $originalName."_".rand(1,1000)."-".$dwgID."-".$rev_no.".".$ext;
                if(move_uploaded_file($_FILES["attachment"]["tmp_name"],$uploaddir . $newfilename)){
                    $newfilename = $newfilename;
                }else{
                    $newfilename= "";
                }

            }
			
            if (mysql_query("INSERT INTO scope_dwg_revisions "
                . "SET "
                . "`dwg_id`='$dwgID',"
                . "`sheet_no`='$sheet_no',"
                . "`rev_no`='$rev_no',"
                . "`range_no`='$range_no',"
                . "`dwg_title`='$dwg_title',"
                . "`attachment`='$newfilename',"
                . "`status`=1,"
                . "`companyID`='$id',"
                . "`created_date`='$date',"
                . "`created_by`='$discipline_id',"
				. "`dwg_title2`='$dwg_title2',"
				. "`dwg_idate`='$dwg_idate',"
				. "`discipline`='$discipline'")) {


                header("location: 03-06Scope-02Drawings01.php?expand_id=".$dwgID."&msg=SAVED");
            } else {
				$E = mysql_error($con);
                header("location: 03-06Scope-02Drawings01.php?expand_id=".$dwgID."&msg=NOT SAVED&error=$E");
            }
            break;
        case "update":
            $oldfile =$_POST['old_filename'];
            $ID = $_POST['recordID'];
            $newfilename ="";
            $uploaddir = 'uploads/';
            if($_FILES["attachment"]["name"] !=""){
                $ext = pathinfo($_FILES["attachment"]["name"], PATHINFO_EXTENSION);
                $originalName = pathinfo($_FILES["attachment"]["name"], PATHINFO_FILENAME);
                $newfilename = $originalName."_".rand(1,1000)."-".$dwgID."-".$rev_no.".".$ext;
                if(move_uploaded_file($_FILES["attachment"]["tmp_name"],$uploaddir . $newfilename)){
                    $newfilename = $newfilename;
                }else{
                    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";exit;
                    $newfilename= $oldfile;
                }

            } else {
                $newfilename =$oldfile;
            }
            if (mysql_query("UPDATE  scope_dwg_revisions "
                . "SET "
                . "`sheet_no`='$sheet_no',"
                . "`rev_no`='$rev_no',"
                . "`dwg_title`='$dwg_title',"
				. "`dwg_title2`='$dwg_title2',"
				. "`discipline`='$discipline',"
								. "`dwg_idate`='$dwg_idate',"
                . "`attachment`='$newfilename' WHERE id = '$ID'" )) {


                header("location: 03-06Scope-02Drawings01.php?expand_id=".$dwgID."&msg=SAVED");
            } else {
				
                header("location: 03-06Scope-02Drawings01.php?expand_id=".$dwgID."&msg=NOT SAVED");
            }
            break;
        default :
            if(isset($_GET['del']) && $_GET['del']>0){
                $ID = $_GET['del'];
                $dwgID = $_GET['dwgID'];
                if (mysql_query("DELETE  FROM scope_dwg_revisions  WHERE id = '$ID'" )) {


                    header("location: 03-06Scope-02Drawings01.php?expand_id=".$dwgID."&msg=DELETED");
                } else {
                    header("location: 03-06Scope-02Drawings01.php?expand_id=".$dwgID."&msg=NOT DELETED");
                }

            }
            break;

    }


} else {
    header("location: ../index.php?msg=YOU ARE NOT LOGGED IN");
}
?>