<!doctype html>
<html>

    <head>
        <meta name="viewport" content="width=device-width">
        <title>Rate Us</title>
        <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
         <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
        <link rel="stylesheet" type="text/css" href="css_self/rating.css">
    </head>
    
    <body background="image/food.jpg">
        <nav class="navbar navbar-fixed-top navbar-inverse">
   
   <div class="container">
     <div class="navbar-header">
       <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse">
         <span class="sr-only">Toggle navigation</span>
         <span class="icon-bar"></span>
         <span class="icon-bar"></span>
         <span class="icon-bar"></span>
       </button>
       <a class="navbar-brand" href="index.html">The Royal Cuisine</a>
     </div>
     <div class="collapse navbar-collapse" id="navbar-collapse">
       <ul class="nav navbar-nav navbar-right">
         <li><a href="index.html">Home</a></li>
         <li><a href="about.html">About</a></li>
         <li class="active"><a href="rating.php">Contact</a></li>
         <li><a href="menu.html">Menu</a></li>
         <li><a href="recipe.html">Recipes</a></li>
         <li><a href="register.php">Sign Up</a></li>
       </ul>
     </div>
   </div>
 </nav>

        
        <div id="container">
            <h1>Give Your Honest Review</h1>
            <p>Your review will help us to improve &hearts;</p>
            <?php
    
        // Check for Header Injections
        function has_header_injection($str) {
            return preg_match( "/[\r\n]/", $str );
        }
        
        
        if (isset($_POST['rating_submit'])) {
            
            // Assign trimmed form data to variables
            // Note that the value within the $_POST array is looking for the HTML "name" attribute, i.e. name="email"
            $name   = trim($_POST['name']);
            $email  = trim($_POST['email']);
            $msg    = $_POST['message']; // no need to trim message
        
            // Check to see if $name or $email have header injections
            if (has_header_injection($name) || has_header_injection($email)) {
                
                die(); // If true, kill the script
                
            }
            
            if (!$name || !$email || !$msg) {
                echo '<h4 class="error">All fields required.</h4><a href="rating.php" class="button block">Go back and try again</a>';
                exit;
            }
            
            // Add the recipient email to a variable
            $to = "zaimsat30@gmail.com";
            
            // Create a subject
            $subject = "$name Sent a Message From Your Restaurant Page";
            
            // Construct the message
            $message="";
            $message .= "Name: $name\r\n";
            $message .= "Email: $email\r\n\r\n";
            $message .= "Message:\r\n$msg";
            
            // If the subscribe checkbox was checked
            if (isset($_POST['subscribe']) && $_POST['subscribe'] == 'Subscribe' ) {
            
                // Add a new line to the $message
                $message .= "\r\n\r\nPlease add $email to the mailing list.\r\n";
                
            }
        
            $message = wordwrap($message, 72); // Keep the message neat n' tidy
        
            // Set the mail headers into a variable
            $headers = "MIME-Version: 1.0\r\n";
            $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
            $headers .= "From: " . $name . " <" . $email . ">\r\n";
            $headers .= "X-Priority: 1\r\n";
            $headers .= "X-MSMail-Priority: High\r\n\r\n";
        
            
            // Send the email!
            mail($to, $subject, $message, $headers);
        ?>
        
        <!-- Show success message after email has sent -->
        <h2>Thanks for contacting The Royal Cuisine!</h2>
        <p><b>Please allow 24 hours for a response.</b></p>
        <p><b><a href="index.html" class="button block">&laquo; Go to Home Page</a></b></p>
        
        <?php
            } else {
        ?>
            <form method="post" action="" id="contact-form">
                <fieldset>
                    <legend>Rate us</legend>
                    
                    <label for="name">Your name</label>
                    <input type="text" id="name" name="name">
                    
                    <label for="email">Your email</label>
                    <input type="email" id="email" name="email">
                    
                    <label for="form-address">Your address</label>
                    <input type="text" id="form-address">
                    
                    <label for="form-phone">Your phone number</label>
                    <input type="tel" id="form-phone">
                    
                    <label for="form-message">Send a message!</label>
                    <textarea id="message" name="message"></textarea>
                    <label for="subscribe">Subscribe to newsletter
                    <input type="checkbox" id="subscribe" value="Subscribe" name="subscribe"> </label>
                    <label>Give An Honest Rating</label>
                    <div id="star" class="stars" data-rating="3">
                        <span class="star">&nbsp;</span>
                        <span class="star">&nbsp;</span>
                        <span class="star">&nbsp;</span>
                        <span class="star">&nbsp;</span>
                        <span class="star">&nbsp;</span>
                     </div>

                     <br>

                    <input type="submit" value="Send message" name="rating_submit">
                    <br>
                    <br>
                    <h3>Other Contact Info: </h3>
                    <h4>Our Location: </h6>
                <address>
                The Royal Cuisine<br />
                Ground Floor, Mukti Baban 665/A,<br />
                Chatteshwari Rd, Chittagong 4200<br />
                <abbr title="Phone">Phone: </abbr>
                +88 01938-223322
            </address>
            <address>
                <strong>Support:</strong>   <a href="mailto:royal_cuisine@gmail.com">TheRoyalCuisine9@gmail.com</a><br />
                <strong>Marketing:</strong> <a href="mailto:Marketing_royal_cuisine@gmail.com">Marketing_royal_cuisine@gmail.com</a>
            </address>
                </fieldset>
            </form>
  <?php
            }
        ?>          
        </div><!-- #container -->

        <script>
        
        //initial setup
        document.addEventListener('DOMContentLoaded', function(){
            let stars = document.querySelectorAll('.star');
            stars.forEach(function(star){
                star.addEventListener('click', setRating); 
            });
            
            let rating = parseInt(document.querySelector('.stars').getAttribute('data-rating'));
            let target = stars[rating - 1];
            target.dispatchEvent(new MouseEvent('click'));
        });
        function setRating(ev){
            let span = ev.currentTarget;
            let stars = document.querySelectorAll('.star');
            let match = false;
            let num = 0;
            stars.forEach(function(star, index){
                if(match){
                    star.classList.remove('rated');
                }else{
                    star.classList.add('rated');
                }
                //are we currently looking at the span that was clicked
                if(star === span){
                    match = true;
                    num = index + 1;
                }
            });
            document.querySelector('.stars').setAttribute('data-rating', num);
        }
        
    </script>  
    </body>
    
</html>