<?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'    => 'webmaster@surveyinstrumentsales.com',
            'sender_name'     => 'SIS Internal Order',
            'recipient_email' => 'webmaster@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('subject', '', 'Your Name');
$form->addOption('subject', 'Eric', 'Eric', '', '');
$form->addOption('subject', 'Jim', 'Jim', '', '');
$form->addOption('subject', 'Joe', 'Joe', '', '');
$form->addOption('subject', 'Julie', 'Julie', '', '');
$form->addOption('subject', 'Mel', 'Mel', '', '');
$form->addOption('subject', 'Rick', 'Rick', '', '');
$form->addSelect('subject', 'Ordering?', 'class=selectpicker, required');



/* Product */


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


$form->setCols(0, 6);
$form->groupInputs('manufacturer', 'model');
$form->addInput('text', 'manufacturer', '', '', 'required=required, placeholder=Manufacturer?');
$form->addInput('text', 'model', '', '', 'placeholder=Model/Part Number');

$form->setCols(0, 0);
$form->addCheckbox('orderingfor', 'stock', 'Stock');
$form->addCheckbox('orderingfor', 'customer', 'Customer');
$form->printCheckboxGroup('orderingfor', 'Ordering For', false);

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

$form->startDependentFields('orderingfor', 'Customer');
$form->setCols(0, 0);
$form->addInput('text', 'orderingfor-Company', '', 'Company Name', 'placeholder=What\'s the company name?');
$form->addInput('text', 'orderingfor-Contact', '', 'Contact', 'placeholder=Who can we contact when it comes in?');
$form->addInput('text', 'orderingfor-Number', '', 'Number', 'placeholder=How many do they need?');
$form->addPlugin('datepicker', '#pick-a-date');
$form->addInput('text', 'pick-a-date', '06/09/2015', 'Pick a date', '');
$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>