<?php
include('../../includes/checkSession.php');
include('../../includes/con2pma3.php');

$companyID = $id;
$expand_id = $_REQUEST['interface_plan_id'];

$userName = $ipusername;

$project1 = $_POST['input-project1'];
$phase1 = $_POST['input-phase1'];
$zone1 = $_POST['input-zone1'];
$project2 = $_POST['input-project2'];
$phase2 = $_POST['input-phase2'];
$zone2 = $_POST['input-zone2'];
$priority = $_POST['input-priority'];
$discipline = $_POST['input-dicipline'];
$party1 = $_POST['input-party1'];
$party11 = $_POST['input-party11'];
$party2 = $_POST['input-party2'];
$party22 = $_POST['input-party22'];
$party3 = $_POST['input-party3'];
$party33 = $_POST['input-party33'];
$location = $_POST['input-location'];
$resp1 = $_POST['input-resp1'];
$resp11 = $_POST['input-resp11'];
$dwg1 = $_POST['input-dwg1'];
$dwg11 = $_POST['input-dwg2'];
$sizeDia = $_POST['input-dia'];
$constraint = $_POST['input-constraint'];
$coord = $_POST['input-coord'];
$descr = $_POST['input-descr'];
$action = $_POST['input-action'];
$resp2 = $_POST['input-resp2'];
$time = $_POST['input-time'];
$sdate = $_POST['input-sdate'];
$edate = $_POST['input-edate'];
$remarks = $_POST['input-remarks'];


$creationDate = date('Y-m-d');

if (isset($_REQUEST['del'])) {

    $RID = $_REQUEST['del'];
    if (mysql_query("UPDATE interfacedetails SET interfacedet_status ='2' WHERE interfacedet_id = '$RID'")) {
        header("location: 03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=DELETED");
    } else {
        header("location: 03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=NOT DELETED");
    }
}

elseif (isset($_REQUEST['Edit'])) {
    $interfaceID = $_REQUEST['Edit'];

    $q1 = "UPDATE interfacedetails SET 
              
  interfacedet_descr ='$descr',        
  interfacedet_action ='$action',       
  interfacedet_remarks ='$remarks',      
  interfacedet_proj1 ='$project1',        
  interfacedet_stage1 ='$zone1',       
  interfacedet_phase1 ='$phase1',       
  interfacedet_proj2 ='$project2',        
  interfacedet_stage2 ='$zone2',       
  interfacedet_phase2 ='$phase2',            
  interfacedet_discipline  ='$discipline',  
  interfacedet_priority  ='$priority',    
  interfacedet_loc ='$location',      
  interfacedet_size ='$sizeDia',      
  interfacedet_constr ='$constraint',     
  interfacedet_coord ='$coord',     
  interfacedet_dwg1 ='$dwg1',       
  interfacedet_dwg11 ='$dwg11',    
  interfacedet_party1 ='$party1',    
  interfacedet_party11 ='$party11',    
  interfacedet_party2 ='$party2',     
  interfacedet_party22 ='$party22',     
  interfacedet_party3 ='$party3',    
  interfacedet_party33 ='$party33',   
  interfacedet_resp1 ='$resp1',       
  interfacedet_resp11 ='$resp11',         
  interfacedet_resp22 ='$resp2',                      
  interfacedet_start ='$sdate',       
  interfacedet_end ='$edate',     
  interfacedet_timedesc   ='$time'
  WHERE interfacedet_id = '$interfaceID'";

    if (mysql_query($q1)) {
        header("location: 03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=UPDATED");
    } else {

        header("location: 03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=NOT UPDATED");

    }

}

elseif(isset($_REQUEST['InActive'])) {

    $bid = $_REQUEST['InActive'];

    if (mysql_query("UPDATE interfacedetails SET interfacedet_status ='0' WHERE interfacedet_id  = '$bid'")) {
        header("location: 03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=HIDDEN");
    } else {
        header("location: 03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=NOT HIDDEN");
    }
}

elseif(isset($_REQUEST['Active'])) {

    $bid = $_REQUEST['Active'];

    if (mysql_query("UPDATE interfacedetails SET interfacedet_status ='1' WHERE interfacedet_id = '$bid'")) {
        header("location: 03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=MADE ACTIVE");
    } else {
        header("location: 03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=NOT ACTIVE");
    }
}


elseif (isset($_GET['postComment'])) {
    $tktsid = $_GET['postComment'];

    $comment = $_POST['comment'.$tktsid];

    $q = "INSERT INTO `interface_comments`(`id`, `tktsid`, `userName`, `comment`, `cdate`) VALUES (NULL, '$tktsid', '$userName', '$comment', '$creationDate')";
    if (mysql_query($q)) {

        // update discussion datetime against comment
        $commentDateTime = date("Y-m-d H:i:s");
       // mysql_query("UPDATE  tktstbl SET topicMsgUpdated='$commentDateTime' WHERE  tks_id=".$tktsid);


        if(isset($_GET['more']))
            echo("<script>window.location.href = '03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=COMMENT IS POSTED';</script>");
        else
            echo("<script>window.location.href = '03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=COMMENT IS POSTED';</script>");

    } else{
        if(isset($_GET['more']))
            echo("<script>window.location.href = '03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=COMMENT IS NOT POSTED';</script>");
        else
            echo("<script>window.location.href = '03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=COMMENT IS NOT POSTED';</script>");

    }


}

else if (isset($_GET['sendEmail'])) {

    $topicID = $_GET['sendEmail'];
    $personnelID = $_POST['emailto'];


    if($personnelID == 0){
        $resultMail = mysql_query("SELECT emailAddressOne FROM login where loginId='$companyID' ");
        $rowMail = mysql_fetch_assoc($resultMail);
        $to = $rowMail['emailAddressOne'];
    }else{
        $queryEmail = "SELECT internal_personnel_email FROM internal_personnel WHERE internal_personnel_id='$personnelID'";
        $resultMail = mysql_query($queryEmail);
        $rowMail = mysql_fetch_assoc($resultMail);
        $to = $rowMail['internal_personnel_email'];
    }
    

    $tide = $topicID;

    $subject = "ONE-PPM Projects Ticket Management System";
    $message = "";
//$emailBody="";
    $result_email = mysql_query("SELECT * from interfacedetails where interfacedet_id=$tide");
    //if(mysql_num_rows($result)){
        $row = mysql_fetch_array($result_email);

    $rowD = mysql_fetch_assoc(mysql_query("SELECT * FROM interfaceinitiate LEFT JOIN projects ON interface_plan_pid=projectSerial where interface_plan_id='".$row['interfacedet_plan_id']."'"));



    /////////////////////////////////////////////////
    $message .= '<strong>ID:</strong> '.$row['interfacedet_id'].'<br>
<strong>PROJECT:</strong> '.$rowD['projectCode'].'<br>
<strong>TITLE:</strong> '.$rowD['interface_title'].'<br>
<strong>STAGE:</strong> '.$rowD['interface_stage'].'<br>
<strong>INTERFACE DESCRIPTION:</strong> '.$row['interfacedet_descr'].' ( '.$row['interfacedet_constr'].' ) <br>
<br><strong>COMMENTS AND FILES:</strong><br><br>';
    /////////////////////////////////////////////////
    // Show Comments
    $commentQ = "SELECT * FROM interface_comments WHERE tktsid ='$topicID' ORDER BY id DESC ";
    $resultComment = mysql_query($commentQ);

    while ($rowComment = mysql_fetch_array($resultComment, MYSQL_ASSOC)) {
        $message.= '<span STYLE="color: black; border-bottom: 1px grey dashed; font-size: 8pt">';
        $message.=  $rowComment['cdate'].':  '.$rowComment['userName'].'</span> '.'<span class="">'.$rowComment['comment'].'</span><br>';
    }
    $message .= '<br>';
    $sql_querya="SELECT * FROM interface_attach WHERE tktsid='$topicID' ORDER BY id DESC";
    $result_seta=mysql_query($sql_querya);
    while($rowa=mysql_fetch_assoc($result_seta))
    {
        $message .= $rowa["adate"].': '.$rowa['userName'].':  <a target="_blank"  href="http://pm4.pmanywhere.co/Interface/03-scope/attachments/'.$rowa["path"].'">'.$rowa["path"].'</a><br>';
    }


    if ($result_email) {
        if (mysql_num_rows($result_email)) {


            include ('../../Email-pm/email.php');

            if (sendEmail($to,"",$subject,$message) == 1) {

                $qSent = "INSERT INTO interface_comments (id,tktsid,userName,comment,cdate) VALUES('','$tide','$ipusername', 'Emailed to $to', '$creationDate' )";
                mysql_query($qSent);


                // update discussion datetime against comment
                $commentDateTime = date("Y-m-d H:i:s");
              //  mysql_query("UPDATE  tktstbl SET topicMsgUpdated='$commentDateTime' WHERE  tks_id=".$tide);


                if(isset($_GET['more']))
                    echo("<script>window.location.href = '03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=Email Sent Successfully.';</script>");

                else
                    echo("<script>window.location.href = '03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=Email Sent Successfully.';</script>");

            } else{
                if(isset($_GET['more']))
                    echo("<script>window.location.href = '03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=Sorry, there was a problem sending the email.';</script>");

                else
                    echo("<script>window.location.href = '03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=Sorry, there was a problem sending the email.';</script>");


            }

        } else{
            if(isset($_GET['more']))
                echo("<script>window.location.href = '03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg==Sorry, no matching data was found, email not sent.';</script>");

            else
                echo("<script>window.location.href = '03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=Sorry, no matching data was found, email not sent.';</script>");



        }

    } else{
        if(isset($_GET['more']))
            echo("<script>window.location.href = '03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=Sorry, there was a problem with the database--email not sent.';</script>");

        else
            echo("<script>window.location.href = '03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=Sorry, there was a problem with the database--email not sent.';</script>");

    }


}

else if (isset($_GET['attachFile'])) {
    // creating simple variables for input names from the form
    $tid = $_GET['attachFile'];
    // variables for input data


    if ($_FILES["attachment112"]["error"] > 0){
        if(isset($_GET['more']))
            echo("<script>window.location.href = '03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=File Error.';</script>");
        else
            echo("<script>window.location.href = '03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=File Error.';</script>");

    }
    else {
//WARNING: DOCX can not be uploaded. Please try DOC, JPG, PNG, or PDF.

        //    $allowedExts = array("jpg", "jpeg", "png", 'doc', 'pdf');
        //  $extension = end(explode(".", $_FILES["attachment112"]["name"]));
        //if (in_array(strtolower($extension), $allowedExts)) {
//        if($extension == 'docx'){


        if (file_exists("attachments/" . $_FILES["attachment112"]["name"])) {
            if (isset($_GET['more']))
                echo("<script>window.location.href = '03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=File Already Exist. Retry With different file name.';</script>");
            else
                echo("<script>window.location.href = '03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=File Already Exist. Retry With different file name.';</script>");
        } else {
            $imgpath = $_FILES["attachment112"]["name"];
            move_uploaded_file($_FILES["attachment112"]["tmp_name"], "attachments/" . $imgpath);
            // sql query for inserting data into database
            $sql_query = "INSERT INTO interface_attach (id,tktsid,userName,path,adate) VALUES('','$tid', '$userName','$imgpath','$creationDate')";
            // sql query for inserting data into database

            // sql query execution function
            if (mysql_query($sql_query)) {

                // update discussion datetime against attachment
                $commentDateTime = date("Y-m-d H:i:s");
            //    mysql_query("UPDATE  tktstbl SET topicMsgUpdated='$commentDateTime' WHERE  tks_id=" . $tid);

                if (isset($_GET['more'])) {
                    echo("<script>window.location.href = '03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=Data Attached Successfully';</script>");

                } else {
                    echo("<script>window.location.href = '03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=Data Attached Successfully';</script>");

                }

            } else {
                if (isset($_GET['more'])) {
                    echo("<script>window.location.href = '03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=Error occured while Attaching data';</script>");

                } else {
                    echo("<script>window.location.href = '03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=Error occured while Attaching data';</script>");

                }
            }
            // sql query execution function

        }

        /*  }else{
              header("location: 20-01Discussions-posts.php?msg=WARNING: DOCX can not be uploaded. Please try DOC, JPG, PNG, or PDF.");

          }*/
    }


}



else {

    if (mysql_query("INSERT INTO interfacedetails (interfacedet_plan_id, companyID, interfacedet_descr, interfacedet_action, interfacedet_remarks, interfacedet_proj1, interfacedet_stage1, interfacedet_phase1, interfacedet_proj2, interfacedet_stage2, interfacedet_phase2, interfacedet_discipline, interfacedet_priority, interfacedet_loc, interfacedet_size, interfacedet_constr, interfacedet_coord, interfacedet_dwg1, interfacedet_dwg11, interfacedet_party1, interfacedet_party11, interfacedet_party2, interfacedet_party22, interfacedet_party3, interfacedet_party33, interfacedet_resp1, interfacedet_resp11,  interfacedet_resp22, interfacedet_creationdate, createdBy, interfacedet_start, interfacedet_end, interfacedet_timedesc, interfacedet_status) 
          VALUES('$expand_id','$companyID', '$descr', '$action', '$remarks','$project1', '$zone1', '$phase1', '$project2', '$zone2', '$phase2', '$discipline', '$priority', '$location', '$sizeDia', '$constraint', '$coord', '$dwg1', '$dwg11', '$party1', '$party11', '$party2', '$party22', '$party3', '$party33', '$resp1', '$resp11', '$resp2', '$creationDate', '$ipusername', '$sdate', '$edate', '$time' , '1'  )")) {

        header("location: 03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=INFORMATION IS SAVED");

    } else {
        header("location: 03-InterfacePlan01-details01.php?interface_plan_id=$expand_id&msg=INFORMATION IS NOT SAVED: ERROR");
    }
}

?>