<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="author" content="Sahil Kumar">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>ParisBakery - Rewards registration</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css">
    <!-- jQuery 3 -->
    <script src="https://paris.app.mgsoftwaresolutions.com/public/jquery/dist/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/1000hz-bootstrap-validator/0.11.9/validator.min.js"></script>
</head>
 <style>
    body {
      background-image: url('Rewards/public/img/background_registration.jpg');
      background-repeat: no-repeat;
      background-size: 100% 150%;
    }
    .center {
        display: block;
        margin-left: auto;
        margin-right: auto;
        width: 50%;
    }
    
    .text-gold{
        color: gold;
    }
    
    .help-block {
        color: red;
    }

    .form-group.has-error.has-danger .form-control-label {
        color: red;
    }

    .form-group.has-error.has-danger .form-control {
        border: 1px solid red;
        box-shadow: 0 0 0 0.2rem rgba(250, 16, 0, 0.18);
    }
</style> 
<body>
  <div class="container">
    <div class="row justify-content-center">
      <div class="col-md-5 mt-5 rounded" style='background-color: #fff;opacity: 1'>
          <image src='Rewards/public/img/logo.jpg' class="center" />
        <h2 class="text-center font-weight-bold text-danger"><i class='fa fa-star'></i> Rewards registration</h2>
        <hr class="bg-light">                
        <?php
        $msgError = '';
        if(isset($_POST['phone'])){
            $_SESSION['user']['id'] = '-1';
            include 'app/config/dbConfig.php';
            include 'class/DataBase/DataBase.php';
            include 'Entity/EntityRepository.php';
            include 'class/Translator/Translator.php';
            include 'class/Login/LoginRepository.php';
            include 'class/Login/Login.php';
            include 'class/Tools/Tools.php';
            include 'Rewards/Entity/CustomerRewardsRepository.php';    
            
            /*Validar telefono*/
            $phone = $_POST['phone'];
            $phone = str_replace('(','',$phone);
            $phone = str_replace(')','',$phone);
            $phone = str_replace('-','',$phone);
            $phone = str_replace(' ','',$phone);

            if(strlen($phone) == '10'){ 
                $phoneMasked = '('.substr($phone,0,3).') '.substr($phone,3,3).'-'.substr($phone,6,4);
                $customeRepo = new CustomerRewardsRepository();
                $existPhone = $customeRepo->getByPhone($phoneMasked);
                $_POST['phone'] = $phoneMasked;
                
                if(!$existPhone){               
                    $data = $_POST;                
                    if(!empty($data['birthday_year']) && !empty($data['birthday_month']) && !empty($data['birthday_day'])){
                        $birthday_day = str_pad($data['birthday_day'], 2, "0", STR_PAD_LEFT);
                        $date_of_birth = "{$data['birthday_year']}-{$data['birthday_month']}-{$birthday_day}";

                        $data['date_of_birth'] = $date_of_birth; 
                        $data['register_store'] = 1;
                        $data['status'] = 0;
                    }

                    $customeRepo->setOptions($data);
                    $customeRepo->save($customeRepo->getOptions());                

                    echo "<script>document.location = 'rewards_congratulations.php' </script>";
                    exit;
                }else{
                    $msgError = "This phone is already register for rewards.";
                }           
            }else{
                $msgError = "Invalid phone number. $phone";
            }       
        }
        ?>        
        <h5 id='flashmessenger' class="text-center text-danger"><?php echo $msgError ?></h5>
        <h5 class="text-center text-success"></h5>
        <form id="form_registration" action="" method="post" id="form-box" class="p-2">
        <div class="form-group input-group">
            <div class="input-group-prepend">
              <span class="input-group-text"><i class="fas fa-user"></i></span>
            </div>
            <input type="text" name="name" class="form-control" placeholder="Enter your name" required>
            <input type="text" name="last_name" class="form-control" placeholder="Enter your last name" required>
        </div>
        <div class="form-group input-group">
            <div class="input-group-prepend">
              <span class="input-group-text"><i class="fas fa-envelope"></i></span>
            </div>
            <input type="email" id="email" name="email" class="form-control" placeholder="Enter your email"  required>
            <small id="emailvalid" class="form-text text-muted invalid-feedback"> Your email must be a valid email </small> 
        </div>
        <div class="form-group input-group">
            <div class="input-group-prepend">
              <span class="input-group-text"><i class="fas fa-phone"></i></span>
            </div>
            <input type="text" name="phone" autocomplete="off" class="form-control _maskPhone" maxlength="14" placeholder="Enter your phone" required>
        </div>
        <label>Enter your birthday (Optional)</label>
        <div class="form-group input-group">
            <div class="input-group-prepend">
              <span class="input-group-text"><i class="fas fa-birthday-cake"></i></span>
            </div>
            <select id="birthday_year" name="birthday_year" class="form-control">
                <option value=''>Year</option>
                <?php 
                for($year = (float)date('Y') - 18; $year >= 1900; $year--){
                    echo "<option value='{$year}'>{$year}</option>";
                }?>
            </select>
            <select id="birthday_month" name="birthday_month" class="form-control">
                <option value=''>Month</option>
                <option value='01'>January</option>
                <option value='02'>February</option>
                <option value='03'>March</option>
                <option value='04'>April</option>
                <option value='05'>May</option>
                <option value='06'>June</option>
                <option value='07'>July</option>
                <option value='08'>August</option>
                <option value='09'>September</option>
                <option value='10'>October</option>
                <option value='11'>November</option>
                <option value='12'>December</option>
            </select>
            <select id='birthday_day' name="birthday_day" class="form-control">
                <option value=''>Day</option>
            </select>
          </div>
          <div class="form-group">
            <input type="submit" id="send" name="send" class="btn btn-danger btn-block" value="Send">
          </div>
        </form>
      </div>
    </div>
      <p class="text-center" style='color: #FFF'>Paris Bakery <?php echo date('Y') ?></p>
  </div>    
</body>
<script>       
    $(function(){
        $('#birthday_month').on('change',function(){    
            var selected_mont = $(this).val();
            if(selected_mont != ''){ 
                var last_day = {'01':31,'02':29,'03':31,'04':30,'05':31,'06':30,'07':31,'08':31,'09':30,'10':31,'11':30,'12':31}

                for (var first_day = 1; first_day <= last_day[selected_mont]; first_day++) { 
                    $('#birthday_day').append($('<option>', {value:first_day, text:first_day}));
                }
            }         
        });
        
        // Validate Email                
        $('#email').on('blur',function(){
            let regex =  
            /^([_\-\.0-9a-zA-Z]+)@([_\-\.0-9a-zA-Z]+)\.([a-zA-Z]){2,7}$/; 
            let s = email.value; 
            if (regex.test(s)) { 
                email.classList.remove("is-invalid"); 
                emailError = true;
            } else { 
                email.classList.add("is-invalid"); 
                emailError = false; 
            } 
        });
    
        $('._maskPhone').keydown(function (e) {
            var key = e.which || e.charCode || e.keyCode || 0;
            $phone = $(this);

            // Don't let them remove the starting '('
            if ($phone.val().length === 1 && (key === 8 || key === 46)) {
                $phone.val('('); 
                return false;
            } 
            // Reset if they highlight and type over first char.
            else if ($phone.val().charAt(0) !== '(') {
                $phone.val('('+$phone.val()); 
            }

            // Auto-format- do not expose the mask as the user begins to type
            if (key !== 8 && key !== 9) {
                if ($phone.val().length === 4) {
                    $phone.val($phone.val() + ')');
                }
                if ($phone.val().length === 5) {
                    $phone.val($phone.val() + ' ');
                }           
                if ($phone.val().length === 9) {
                    $phone.val($phone.val() + '-');
                }
            }

            // Allow numeric (and tab, backspace, delete) keys only
            return (key == 8 || 
                    key == 9 ||
                    key == 46 ||
                    (key >= 48 && key <= 57) ||
                    (key >= 96 && key <= 105)); 
        })
    });       
</script>
</html>