<?php
// Include config file
require_once "config.php";
//CURRENTLY IGNORING ERROR CHECKING FOR THE INITIAL PROTOTYPE 
// Define variables and initialize with empty values
// $username = $password = $confirm_password = "";
// $username_err = $password_err = $confirm_password_err = "";
$b_structure = $b_years = $b_name = $t_name = $c_acn_name = $c_abn_name = $b_address = $p_address = $t_phone = $m_phone = $email = ""; 
// Processing form data when form is submitted

if($_SERVER["REQUEST_METHOD"] == "POST"){
    
        
        
        //(0,1,'W Innovations','W Innovations','1234567890','1234567890','77 Main Street Mittagong NSW 2575','77 Main Street Mittagong NSW 2575','4444 4444','04 4444 4444','help@winnovations.au')
        // "INSERT INTO users (username, password) VALUES (?, ?)";
        if(empty(trim($_POST["b_years"]))){
            $b_years_err = "Please enter a password.";     
        } elseif(strlen(trim($_POST["b_years"])) < 1){
            $b_years_err = "Password must have atleast 6 characters.";
        } else{
            $b_years = trim($_POST["b_years"]);
        }
        if(empty(trim($_POST["b_name"]))){
            $b_name_err = "Please enter a password.";     
        } elseif(strlen(trim($_POST["b_name"])) < 1){
            $b_name_err = "Password must have atleast 6 characters.";
        } else{
            $b_name = trim($_POST["b_name"]);
        }
        if(empty(trim($_POST["t_name"]))){
            $t_name_err = "Please enter a password.";     
        } elseif(strlen(trim($_POST["t_name"])) < 1){
            $t_name_err = "Password must have atleast 6 characters.";
        } else{
            $t_name = trim($_POST["t_name"]);
        }
        if(empty(trim($_POST["c_acn_name"]))){
            $c_acn_name_err = "Please enter a password.";     
        } elseif(strlen(trim($_POST["c_acn_name"])) < 1){
            $c_acn_name_err = "Password must have atleast 6 characters.";
        } else{
            $c_acn_name = trim($_POST["c_acn_name"]);
        }
        if(empty(trim($_POST["c_abn_name"]))){
            $c_abn_name_err = "Please enter a password.";     
        } elseif(strlen(trim($_POST["c_abn_name"])) < 1){
            $c_abn_name_err = "Password must have atleast 6 characters.";
        } else{
            $c_abn_name = trim($_POST["c_abn_name"]);
        }
        if(empty(trim($_POST["b_address"]))){
            $b_address_err = "Please enter a password.";     
        } elseif(strlen(trim($_POST["b_address"])) < 1){
            $b_address_err = "Password must have atleast 6 characters.";
        } else{
            $b_address = trim($_POST["b_address"]);
        }
        if(empty(trim($_POST["p_address"]))){
            $p_address_err = "Please enter a password.";     
        } elseif(strlen(trim($_POST["p_address"])) < 1){
            $p_address_err = "Password must have atleast 6 characters.";
        } else{
            $p_address = trim($_POST["p_address"]);
        }
        if(empty(trim($_POST["t_phone"]))){
            $t_phone_err = "Please enter a password.";     
        } elseif(strlen(trim($_POST["t_phone"])) < 1){
            $t_phone_err = "Password must have atleast 6 characters.";
        } else{
            $t_phone = trim($_POST["t_phone"]);
        }
        if(empty(trim($_POST["m_phone"]))){
            $m_phone_err = "Please enter a password.";     
        } elseif(strlen(trim($_POST["m_phone"])) < 1){
            $m_phone_err = "Password must have atleast 6 characters.";
        } else{
            $m_phone = trim($_POST["m_phone"]);
        }
        if(empty(trim($_POST["email"]))){
            $email_err = "Please enter a password.";     
        } elseif(strlen(trim($_POST["email"])) < 1){
            $email_err = "Password must have atleast 6 characters.";
        } else{
            $email = trim($_POST["email"]);
        }

        $sql ="INSERT INTO test_forms (b_structure, b_years, b_name, t_name, c_acn_name, c_abn_name, b_address, p_address, t_phone, m_phone, email) VALUES (?,?,?,?,?,?,?,?,?,?,?)"; 

        if($stmt = mysqli_prepare($link, $sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "sssssssssss", $param_b_structure, $param_b_years, $param_b_name, $param_t_name, $param_c_acn_name, $param_c_abn_name, $param_b_address, $param_p_address, $param_t_phone, $param_m_phone, $param_email);
            
            // Set parameters
            //uncomment b structure and set it with an if statement in php when we are finished with it, currently i just want it to run so im just going to hard code a value in
            //$param_b_structure = $b_structure;


            $param_b_structure = 1;
            $param_b_years = (int)$b_years;
            $param_b_name = $b_name;
            $param_t_name = $t_name;
            $param_c_acn_name = $c_acn_name;
            $param_c_abn_name = $c_abn_name;
            $param_b_address = $b_address;
            $param_p_address = $p_address;
            $param_t_phone = $t_phone;
            $param_m_phone = $m_phone;
            $param_email = $email;

            // $param_b_structure = 1;
            // $param_b_years = 5;
            // $param_b_name = "dsdasdasdasda";
            // $param_t_name = "dsdasdasdasda";
            // $param_c_acn_name = "dsdasdasdasda";
            // $param_c_abn_name = "dsdasdasdasda";
            // $param_b_address = "dsdasdasdasda";
            // $param_p_address = "dsdasdasdasda";
            // $param_t_phone = "dsdasdasdasda";
            // $param_m_phone = "dsdasdasdasda";
            // $param_email = "dsdasdasdasda";
            //echo "Form Submitted"
            

            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                // Redirect to login page
                header("location: dashboard_test.php");
            } else{
                echo "Oops! Something went wrong. Please try again later.";
            }

            // Close statement
            mysqli_stmt_close($stmt);
        }
    //}
    
    // Close connection
    mysqli_close($link);
}
?>
 
 <!DOCTYPE html>
<html>

<head>
    <link rel="stylesheet" type="text/css" href="2.css" media="all">
    <link rel="stylesheet" type="text/css" href="1.css" media="all">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Anton">

</head>

<body>

<div class="main-container col1-layout">
        <div class="main">
            <div class="col-main">
                <div class="account-create">
                    <div class="page-title">
                        <h1>A. Customer Details</h1>
                    </div>
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST" id="form-validate"
        class="scaffold-form">
        <div class="fieldset">
            <input type="hidden" name="success_url" value="">
            <input type="hidden" name="error_url" value="">
            <input type="hidden" name="form_key" value="RXbUVikm8xjgbRT1">
    

            <p class="form-instructions">(if company or trading name) all other applicants go straight to section B.
            </p>
            <hr />

            <p class="required">* Required Fields</p>
            <ul class="form-list">
                <!-- <li class="fields">
                    <div class="customer-name-middlename">
                        <label for="0" class="required"><em>*</em>Business Structure</label>
                        <br />
                        <input type="radio" id="0" name="radio" value="">
                        <label for="0">Public Co.</label>
                        <input type="radio" id="1" name="radio" value="">
                        <label for="1">Sole Trader</label>
                        <input type="radio" id="2" name="radio" value="">
                        <label for="2">Pty Ltd Co.</label>
                        <input type="radio" id="3" name="radio" value="">
                        <label for="3">Partnership</label>
                    </div>
                </li> -->
                <li>
                    <label for="b_years" class="required"><em>*</em>Years in Business</label>
                    <div class="input-box">
                        <input type="number" id="b_years" name="b_years" value="" title="Years in Business" max="2000"
                            class="input-text required-entry form-control <?php echo (!empty($b_years_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $b_years; ?>">
                    </div>
                </li>
                <li>
                    <label for="b_name" class="required"><em>*</em>Registered Business/Company Name</label>
                    <div class="input-box">
                        <input type="text" id="b_name" name="b_name" value="" title="Registered Business/Company Name"
                            maxlength="255" class="input-text required-entry form-control <?php echo (!empty($b_name_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $b_name; ?>">
                    </div>
                </li>
                <li>
                    <label for="t_name" class="required"><em>*</em>Trading Name</label>
                    <div class="input-box">
                        <input type="text" id="t_name" name="t_name" value="" title="Trading Name" maxlength="255"
                            class="input-text required-entry form-control <?php echo (!empty($t_name_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $t_name; ?>">
                    </div>
                </li>
                <li>
                    <label for="c_acn_name" class="required"><em>*</em>Customer A.C.N</label>
                    <div class="input-box">
                        <input type="text" id="c_acn_name" name="c_acn_name" value="" title="Customer A.C.N" maxlength="255"
                            class="input-text required-entry form-control <?php echo (!empty($c_acn_name_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $c_acn_name; ?>">
                    </div>
                </li>
                <li>
                    <label for="c_abn_name" class="required"><em>*</em>Customer A.B.N</label>
                    <div class="input-box">
                        <input type="text" id="c_abn_name" name="c_abn_name" value="" title="Customer A.B.N" maxlength="255"
                            class="input-text required-entry form-control <?php echo (!empty($c_abn_name_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $c_abn_name; ?>">
                    </div>
                </li>
                <li>
                    <label for="b_address" class="required"><em>*</em>Registered Business Address</label>
                    <div class="input-box">
                        <input type="text" id="b_address" name="b_address" value="" title="Registered Business Address"
                            maxlength="255" class="input-text required-entry form-control <?php echo (!empty($b_address_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $b_address; ?>">
                    </div>
                </li>
                <li>
                    <label for="p_address" class="required"><em>*</em>Postal Address</label>
                    <div class="input-box">
                        <input type="text" id="p_address" name="p_address" value="" title="Postal Address" maxlength="255"
                            class="input-text required-entry form-control <?php echo (!empty($p_address_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $p_address; ?>">
                    </div>
                </li>
                <li>
                    <label for="t_phone" class="required"><em>*</em>Telephone</label>
                    <div class="input-box">
                        <input type="text" id="t_phone" name="t_phone" value="" title="Telephone" maxlength="255"
                            class="input-text required-entry form-control <?php echo (!empty($t_phone_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $t_phone; ?>">
                    </div>
                </li>
                <li>
                    <label for="m_phone" class="required"><em>*</em>Mobile</label>
                    <div class="input-box">
                        <input type="text" id="m_phone" name="m_phone" value="" title="Mobile" maxlength="255"
                            class="input-text required-entry form-control <?php echo (!empty($m_phone_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $m_phone; ?>">
                    </div>
                </li>
                <li>
                    <label for="email" class="required"><em>*</em>Email</label>
                    <div class="input-box">
                        <input type="email" autocapitalize="off" autocorrect="off" spellcheck="false" name="email"
                            id="email" value="" title="Email Address" class="input-text validate-email required-entry form-control <?php echo (!empty($email_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $email; ?>">
                    </div>
                </li>

            </ul>
        </div>
        <div class="buttons-set">
            <p class="back-link"><a href="" class="back-link"><small>« </small>Back</a></p>
            <button type="submit" title="Register" class="button"><span><span>Register</span></span></button>
        </div>
    </form>
    </div>
    </div>
    </div>
    </div>

</body>

</html>
