<?php require_once('header.php'); ?>
<?php
$error_message='';
$msg1='';
$valid = 0;
if(isset($_POST['form1'])) {

    $valid = 1;
    $mob= $_POST['cust_email'];   
    if(empty($_POST['cust_email'])) {
        $valid = 0;
        $error_message = LANG_VALUE_131."\\n";
        $this->session->set_flashdata('error', $error_message);
    } else {
    
    
        $statement = $this->db->query("SELECT * FROM tbl_customer WHERE cust_email=? || cust_phone=?",array($_POST['cust_email'],$_POST['cust_email']));
        $total = $statement->num_rows();
        if(!$total) {
            $valid = 0;
            $error_message = "Your Email ID OR Mobile Number  is Not Registered. \\n";
            $this->session->set_flashdata('error', $error_message); 
        }
        
    }
    
    if($valid === 1) {

        $otp = mt_rand(100000,999999);
        $otp = base64_encode($otp);
        
         $statement = $this->db->query("UPDATE tbl_customer SET cust_otp=? WHERE cust_email=? || cust_phone=?",array($otp,$_POST['cust_email'],$_POST['cust_email']));
        if ($total != 0) {
           $statement = $this->db->query("SELECT * FROM tbl_customer WHERE cust_email=? || cust_phone=?",array($_POST['cust_email'],$_POST['cust_email']));
           $result =  $statement->result_array();
        foreach($result as $row) {

        $otp = base64_decode ($otp);
        $_SESSION['otp'] = $otp;
        $_SESSION['cust_verify'] = $_POST['cust_email'];
        
            $to = $row['cust_email'];
            $subject = "Forgot Password";
            $message = $data['base_name']." Forgot Password  OTP is <b> $otp </b>";
            $this->email->from($data['contact_email'], $data['base_name']);
            $this->email->to($to);
            $this->email->cc($data['base_name']);
            $this->email->bcc($data['web_url']);
            $this->email->subject($subject);
            $this->email->message($message);
            $this->email->send();
        
        
        $msg1='Forgot Password OTP sent on your Registered Mobile & Email Address';
        $this->session->set_flashdata('success', $msg1);
             
             $sender = $data['sms_sender'];
             $username = $data['sms_user'];
             $password2 = $data['sms_pass']; 
             $number=$row['cust_phone'];
             $message = $data['base_name']."Forgot Password  OTP is $otp";
             $url="http://buzz.azmarq.com/http-api.php?username=".urlencode($username)."&password=".urlencode($password2)."&senderid=".urlencode($sender)."&route=1&number=".urlencode($number)."&message=".urlencode($message);
             
                   $ch = curl_init();
                   $timeout = 90; // set to zero for no timeout
                   $ch = curl_init($url);
                   curl_setopt ($ch, CURLOPT_URL, $url);
                   curl_setopt ($ch, CURLOPT_HEADER, 0);
                   curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
                   curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
                   $res_data = curl_exec($ch);
                   $curl_error = curl_errno($ch);
                   curl_close($ch);
                   $rj = (json_decode($res_data, true));
           
}
            
            
        }
            
    }
}
if(isset($_POST['form2'])){
    if($_SESSION['otp']==$_POST['cust_otp']){
        $valid1=1;
        $msg1="OTP Verified Successfully";
        $this->session->set_flashdata('success', $msg1);
    }
    else{
        $valid1=0;
        $error_message="Invalid OTP Please Enter Valid OTP.";
        $this->session->set_flashdata('error', $error_message);
    }
}
if(isset($_POST['cust_password'])){
    if($_POST['cust_password2']==$_POST['cust_password']){
         $pass = md5($_POST['cust_password']);
        
         $statement = $this->db->query("UPDATE tbl_customer SET cust_password=? WHERE cust_email=? || cust_phone=?",array($pass,$_SESSION['cust_verify'],$_SESSION['cust_verify']));
         
        $valid11=1;
        $msg1="Password Updated Successfully";
        $this->session->set_flashdata('success', $msg1);
    }
    else{
        $valid1=0;
        $error_message="Password & Confirm Password is not Matched.";
        
        $this->session->set_flashdata('error', $error_message);
    }
}
?>

  <section class="checkout-page section-padding">
         <div class="container">
            <div class="row">
               <div class="col-md-6 colsm-8 col-10  mx-auto">
                  <div class="checkout-step">
                     <div class="accordion" id="accordionExample">
                        <div class="card checkout-step-one">
                           <div class="card-header" id="headingOne">
                              <h5 class="mb-0">
                                     <?php echo LANG_VALUE_97; ?>
                              </h5>
                           </div>
                            <div class="card-body">
                    
                    <?php
                              if(!isset($_POST['form2']) && !isset($_POST['form1']) || $valid==0 &&  isset($_POST['form1'])){
                                ?>
                                
                    <form action="" method="post">
                        <div class="row">
                            <div class="col-12">
                                
                                <div class="form-group">
                                    <label for="">Enter <?php echo LANG_VALUE_94; ?> OR Mobile Number *</label>
                                    <input type="text" class="form-control" required name="cust_email">
                                </div>
                                <div class="form-group">
                                   
                                    <input type="submit" class="btn btn-primary form-control" value="<?php echo LANG_VALUE_4; ?>" name="form1">
                                </div>
                                <center><a href="javascript:;" data-target="#bd-example-modal" data-toggle="modal" class="btn btn-xs btn-link"><?php echo LANG_VALUE_12; ?></a></center>
                                
                            </div>
                        </div>                        
                    </form>
                    <?php
                    } 
                    
                    elseif(isset($_POST['form1']) && $error_message=='' || $valid1==0 &&  isset($_POST['form2'])){
                    ?>
                    <form action="" method="post">
                        <div class="row">
                            <div class="col-md-12">
                                
                                <div class="form-group">
                                    <label for="">Enter OTP *</label>
                                    <input type="tel" pattern="[0-9]{6}" required class="form-control" name="cust_otp">
                                </div>
                                <div class="form-group">
                                    <input type="submit" class="btn btn-primary form-control" value="<?php echo LANG_VALUE_4; ?>" name="form2">
                                </div>
                                <center><a href="<?=base_url('forget_password');?>" class="btn btn-xs btn-link">Back</a></center>
                            </div>
                        </div>                        
                    </form>
                    
                    <?php }
                    if(isset($_POST['form2']) && $valid1==1){
                    ?>
                    <form action="" method="post">
                        <div class="row">
                            <div class="col-md-12">
                                
                                <div class="form-group">
                                    <label for="">Enter Password *</label>
                                    <input type="password"  required class="form-control" name="cust_password">
                                </div>
                                <div class="form-group">
                                    <label for="">Enter Confirm Password *</label>
                                    <input type="password"  required class="form-control" name="cust_password2">
                                </div>
                                <div class="form-group">
                                    <input type="submit" class="btn btn-primary form-control" value="<?php echo LANG_VALUE_4; ?>" name="form3">
                                </div>
                                <center><a href="<?=base_url('forget_password');?>" class="btn btn-xs btn-link">Back</a></center>
                            </div>
                        </div>                        
                    </form>
                    
                    <?php } ?>
                              </div>
                           </div>
                        </div>
                     </div>
                  </div>
               </div>
            </div>
         </div>
      </section>
<?php require_once('footer.php'); ?>