<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
include $_SERVER["DOCUMENT_ROOT"] . '/JGWentworth/Model/database.php';
include $_SERVER["DOCUMENT_ROOT"] . '/JGWentworth/Model/validate.php';
if (isset($_POST['createContact'])) {
    $newUserID = $_POST['UserID'];
    $newMember = $_POST['MemberID'];
    $newSubject = valString($_POST['Subject'], true);
    $newDuartion = valString($_POST['Duration'], true);
    $newDateTime = valDate($_POST['DateTime'], true);
    $newResult = valString($_POST['Result'], true);
    $sql = "INSERT INTO sql591897.CONTACT (ContactID,UserID,MemberID, Subject, Duration, DateTime, Result)" . " VALUES (NULL, '" . $newUserID . "', '" . $newMember . "', '" . $newSubject . "', '" . $newDuartion . "','" . $newDateTime . "','" . $newResult . "');";
    $pdo->exec($sql);
    header("Location: /JGWentworth/View/contact.php");
}
// end second if
        $sql = "UPDATE COMPANY_MEMBER " . "SET CompanyID ='" . $companyID . "', FirstName = '" . $editCompFName . "', " . "LastName = '" . $editCompLName . "', Title ='" . $editCompTitle . ", " . "Phone = '" . $editCompPhone . "', Email = '" . $editCompEmail . "', " . "DateFirstContact = '" . $editCompFContacted . "' " . "WHERE MemberID ='" . $compClientId . "';";
        $pdo->query($sql);
    }
    header("Location: /JGWentworth/View/Client.php");
}
/******* Edit No Company Client *********************************************************************************************/
if (isset($_POST['editNoCompClient'])) {
    $target_dir = $_SERVER["DOCUMENT_ROOT"] . '/JGWentworth/ClientImages/';
    $target_file = $target_dir . basename($_FILES['editNoCompPhoto']['name']);
    $compClientId = $_POST['editClientID'];
    $editClientFName = valString($_POST['editClientFName'], true);
    $editClientLName = valString($_POST['editClientLName'], true);
    $editClientTitle = valString($_POST['editClientTitle'], true);
    $editClientEmail = valEmail($_POST['editClientEmail'], true);
    $editClientPhone = valPhone($_POST['editClientPhone'], true);
    $editClientAddress = valString($_POST['editClientAddress'], true);
    $editClientFContacted = valDate($_POST['editClientFContacted'], true);
    if (!empty($target_file)) {
        move_uploaded_file($_FILES['editNoCompPhoto']['tmp_name'], $target_file);
        $sql = "UPDATE NON_MEMBER " . "SET FirstName ='" . $editCompFName . "', LastName ='" . $editCompLName . "', " . "Title ='" . $editCompTitle . "', Email ='" . $editCompEmail . "', Phone ='" . $editCompPhone . "',  " . "Address = '" . $editClientAddress . "', DateFirstContact ='" . $editCompFContacted . "', " . "PhotoURL ='" . $target_file . "' " . "WHERE MemberID ='" . $editClientID . "';";
        $pdo->query($sql);
    }
    if (empty($target_file)) {
        $sql = "UPDATE NON_MEMBER " . "SET FirstName = '" . $editCompFName . "', " . "LastName = '" . $editCompLName . "', Title ='" . $editCompTitle . ", " . "Email = '" . $editCompEmail . "', Phone = '" . $editCompPhone . "',  " . "Address = '" . $editClientAddress . "', DateFirstContact = '" . $editCompFContacted . "' " . "WHERE MemberID ='" . $editClientID . "';";
        $pdo->query($sql);
    }
    header("Location: /JGWentworth/View/Client.php");
}
/******* Delete Company Client **********************************************************************************************/
if (isset($_GET['delete_compClient'])) {
    $memberId = $_GET['delete_compClient'];
    $newRole = valString($_POST['Role'], true);
    $newPhone = valPhone($_POST['phone'], true);
    $newEmail = valEmail($_POST['email'], true);
    $newDepartment = valString($_POST['department'], true);
    $newUserName = valString($_POST['username'], true);
    $newPassword = valString($_POST['pass'], true);
    $newUser = new userClass($newfName, $newlName, $newRole, $newPhone, $newEmail, $newDepartment, $newUserName, $newPassword);
    $newUser->createUser();
    header("Location: /JGWentworth/View/User.php");
}
// end first if
if (isset($_POST['EditUser'])) {
    $fName = valString($_POST['fName'], true);
    $lName = valString($_POST['lName'], true);
    $role = valString($_POST['Role'], true);
    $phone = valPhone($_POST['phone'], true);
    $email = valEmail($_POST['email'], true);
    $department = valString($_POST['department'], true);
    $userName = valString($_POST['username'], true);
    $password = valString($_POST['pass'], true);
    $userID = $_POST['editID'];
    $updateUser = new userClass($fName, $lName, $role, $phone, $email, $department, $userName, $password, $userID);
    $updateUser->updateUser();
    header("Location: /JGWentworth/View/User.php");
}
// end second if
if (isset($_GET['delete'])) {
    $deleteID = urldecode(base64_decode($_GET['delete']));
    userClass::deleteUser($deleteID);
    header("Location: /JGWentworth/View/User.php");
}
 */
require_once $_SERVER["DOCUMENT_ROOT"] . '/JGWentworth/Model/companyClass.php';
include $_SERVER["DOCUMENT_ROOT"] . '/JGWentworth/Model/validate.php';
if (isset($_POST['edit-submit'])) {
    // retrieve posted data
    $id = $_POST['editID'];
    $name = valString($_POST['editName'], true);
    $type = valString($_POST['editType'], true);
    $dateBusiness = valDate($_POST['editDate'], true);
    $compAdd = valString($_POST['editAddress'], true);
    //send update to database
    $com = new companyClass($name, $dateBusiness, $compAdd, $type, $id);
    $com->updateCompany();
    echo '<script type="text/javascript">', 'redirect();', '</script>';
}
// end first if
if (isset($_POST['create-submit'])) {
    $newName = valString($_POST['newCompName'], true);
    $newType = valString($_POST['newBusiness'], true);
    $newDate = valDate($_POST['newDateOfBusiness'], true);
    $newAddress = valString($_POST['newAddress'], true);
    $newCom = new companyClass($newName, $newDate, $newAddress, $newType);
    $newCom->createCompany();
    echo '<script type="text/javascript">', 'redirect();', '</script>';
}
// end second if
if (isset($_GET['delete'])) {
    $deleteID = urldecode(base64_decode($_GET['delete']));
    companyClass::deleteCompany($deleteID);
    echo '<script type="text/javascript">', 'redirect();', '</script>';
}