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

/* =============================================
    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


    // 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' => 'internalorders@surveyinstrumentsales.com',
            'subject'         => 'SIS Internal Order Form',
            'filter_values'   => 'dependent-fields'
        );
        $sent_message = Form::sendMail($email_config);
        Form::clear('dependent-fields');
    }
}

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

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

/* 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');



/* 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->setCols(0, 0);
$form->addCheckbox('Ordering-For', 'stock', 'Stock');
$form->addCheckbox('Ordering-For', 'customer', 'Customer');
$form->printCheckboxGroup('Ordering-For', 'Ordering For', false);

$form->startDependentFields('Ordering-For', 'Stock');
$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->startDependentFields('Ordering-For', '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">
    <title>SIS Internal Order Form</title>
    <meta name="description" content="SIS Internal Order Form">
	<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootswatch/3.3.7/paper/bootstrap.min.css" media="screen">
    <?php $form->printIncludes('css'); ?>
    <style type="text/css">
        fieldset {
            margin-bottom: 80px;
        }
    </style>
</head>
<body>
    <h1 class="text-center">SIS Internal Order Form</h1>
    <div class="container">
        <div class="row">
            <div class="col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2">
            <?php
            if (isset($sent_message)) {
                echo $sent_message;
            }
            $form->render();
            ?>
            </div>
        </div>
    </div>
    <!-- jQuery -->
    <script src="//code.jquery.com/jquery.min.js"></script>
    <!-- Bootstrap JavaScript -->
    <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <?php
        $form->printIncludes('js');
        $form->printJsCode();
    ?>
</body>
</html>