<?php
use phpformbuilder\Form;
use phpformbuilder\Validator\Validator;
use phpformbuilder\database\Mysql;

/* =============================================
    start session and include form class
============================================= */

session_start();
include_once rtrim($_SERVER['DOCUMENT_ROOT'], DIRECTORY_SEPARATOR) . '/phpformbuilder/Form.php';

/* =============================================
    validation if posted
============================================= */

if ($_SERVER["REQUEST_METHOD"] == "POST" && Form::testToken('dependent-fields') === true) {
    // create validator & auto-validate required fields
    $validator = Form::validate('dependent-fields');

    // additional validation
    $validator->email()->validate('user-email');

    // check for errors
    if ($validator->hasErrors()) {
        $_SESSION['errors']['dependent-fields'] = $validator->getAllErrors();
    } else {
		
		$email_config = array(
            'sender_email'    => 'internalorders@surveyinstrumentsales.com',
            'sender_name'     => 'SIS Internal Order',
            'recipient_email' => 'joe@surveyinstrumentsales.com',
            'subject'         => 'SIS Internal Order Form',
            'filter_values'   => 'dependent-fields'
        );
        $sent_message = Form::sendMail($email_config);
        /* Database insert (disabled for demo) */

        require_once rtrim($_SERVER['DOCUMENT_ROOT'], DIRECTORY_SEPARATOR) . '/phpformbuilder/database/db-connect.php';
        require_once rtrim($_SERVER['DOCUMENT_ROOT'], DIRECTORY_SEPARATOR) . '/phpformbuilder/database/Mysql.php';

        $db = new Mysql();
        $insert['Submitter'] = Mysql::SQLValue($_POST['Orderer']);
        $insert['Manufacturer'] = Mysql::SQLValue($_POST['Manufacturer']);
        $insert['MPN'] = Mysql::SQLValue($_POST['MFR-Part-NO']);
        $insert['Description'] = Mysql::SQLValue($_POST['Description']);
        $insert['Ordered_For'] = Mysql::SQLValue($_POST['Ordering-For']);
        $insert['Count'] = Mysql::SQLValue($_POST['Current-Count']);
        $insert['Needed'] = Mysql::SQLValue($_POST['Amount-needed']);
        $insert['Company_Name'] = Mysql::SQLValue($_POST['Company']);
        $insert['Company_Contact'] = Mysql::SQLValue($_POST['Contact']);
        $insert['Company_Phone'] = Mysql::SQLValue($_POST['Contact-Number']);
        $insert['Company_Needed'] = Mysql::SQLValue($_POST['Quantity-Needed']);
        $insert['Company_Date'] = Mysql::SQLValue($_POST['Date-Needed']);
        if (!$db->insertRow('IPO', $insert)) {
            $user_message = '<p class="alert alert-danger">' . $db->error() . '<br>' . $db->getLastSql() . '</p>' . "\n";
        } else {
            $user_message = '<p class="alert alert-success">Your order has been submitted.</p>' . "\n";
            Form::clear('dependent-fields');
        }
		if (!$db->insertRow('Main_Back_Up', $insert)) {
            $user_message = '<p class="alert alert-danger">' . $db->error() . '<br>' . $db->getLastSql() . '</p>' . "\n";
        } else {
            $user_message = '<p class="alert alert-success">Your order has been submitted.</p>' . "\n";
            Form::clear('dependent-fields');
        }
        Form::clear('dependent-fields'); // just for demo ; delete this line if real database recording.
        $user_message = '<p class="alert alert-success">Your order has been submitted.</p>' . "\n"; // just for demo ; delete this line if real database recording.
		
		
    }
}

/* ==================================================
    The Form
================================================== */

$form = new Form('dependent-fields', 'horizontal', 'novalidate');

$Orderer = Jim;

/* Orderer

$form->startFieldset('First Thing\'s First.  Who\'s Ordering?');
$form->addOption('Orderer', '', 'Your Name');
$form->addOption('Orderer', 'Jeff', 'Jeff', '', '');
$form->addOption('Orderer', 'Jim', 'Jim', '', '');
$form->addOption('Orderer', 'Joe', 'Joe', '', '');
$form->addOption('Orderer', 'Julie', 'Julie', '', '');
$form->addOption('Orderer', 'Mel', 'Mel', '', '');
$form->addOption('Orderer', 'Rick', 'Rick', '', '');
$form->addSelect('Orderer', 'Ordering?', 'class=selectpicker, required'); */

$form->addInput('hidden', 'Orderer', $Orderer);


/* Product */


$form->startFieldset('What Do You Need?');


$form->setCols(0, 6);
$form->groupInputs('Manufacturer', 'MFR-Part-NO', 'Description');
$form->addInput('text', 'Manufacturer', '', '', 'required=required, placeholder=Manufacturer?');
$form->addInput('text', 'MFR-Part-NO', '', '', 'required=required, placeholder=Manufacturer Part Number');
$form->addInput('text', 'Description', '', '', 'required=required, placeholder=Description');


$form->startFieldset('How Many?');

/*$form->startDependentFields('Ordering-For', 'Stock, Stock_Customer');*/
$form->setCols(3, 6);
$form->addInput('text', 'Current-Count', '', 'Current Count', 'required=required, placeholder=How many do you have in stock?');
$form->addInput('text', 'Amount-needed', '', 'Amount needed?', 'required=required, placeholder=How many do you need?');
/*$form->endDependentFields();*/



$form->setCols(3, 9, 'md');
$form->addRadio('Ordering-For', 'Stock', 'Stock');
$form->addRadio('Ordering-For', 'Customer', 'Customer');
$form->addRadio('Ordering-For', 'Stock & Customer', 'Stock_Customer');
$form->printRadioGroup('Ordering-For', 'Ordering For', false, 'required');


$form->startDependentFields('Ordering-For', 'Customer, Stock_Customer');
$form->setCols(0, 0);
$form->addInput('text', 'Company', '', 'Company Name', 'required=required, placeholder=What\'s the company name?');
$form->addInput('text', 'Contact', '', 'Contact', 'required=required, placeholder=Who can we contact when it comes in?');
$form->addInput('text', 'Contact-Number', '', 'Phone Number', 'required=required, placeholder=What\'s the best number for them?');
// $form->addInput('text', 'Quantity-Needed', '', 'Quantity', 'required=required, placeholder=How many do they need?');
$form->addInput('text', 'Date-Needed', '', 'Date', 'placeholder=When do they need it?');
$form->endDependentFields();




$form->setCols(3, 9, 'sm');
$form->addBtn('reset', 'reset-btn', 1, 'Reset <span class="glyphicon glyphicon-cancel append"></span>', 'class=btn btn-warning', 'my-btn-group');
$form->addBtn('submit', 'submit-btn', 1, 'Send <span class="glyphicon glyphicon-envelope append"></span>', 'class=btn btn-success ladda-button, data-style=zoom-in', 'my-btn-group');
$form->printBtnGroup('my-btn-group');

// Custom radio & checkbox css
$form->addPlugin('nice-check', 'form', 'default', ['%skin%' => 'purple']);

// jQuery validation
$form->addPlugin('formvalidation', '#dependent-fields');
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>SIS Internal Order Form</title>
	<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">

    <!-- Bootstrap 4 CSS -->

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <!-- Font awesome icons -->

    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" integrity="sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay" crossorigin="anonymous">
	    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootswatch/3.3.7/paper/bootstrap.min.css" media="screen">
    <?php $form->printIncludes('css'); ?>
</head>
<body>
    <h1 class="text-center">SIS Internal Order Form</h1>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8 col-lg-6">
            <?php
            if (isset($user_message)) {
                echo $user_message;
            }
            $form->render();
            ?>
            </div>
        </div>
    </div>

    <!-- jQuery -->

    <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>

    <!-- Bootstrap 4 JavaScript -->

    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
    <?php
        $form->printIncludes('js');
        $form->printJsCode();
    ?>
</body>
</html>

