<?php
@session_start();
include('./includes/config.php');
include('./template/ahkweb/authentication-signin.php');

if(!empty($_POST['username'])){
    $u=mysqli_real_escape_string($ahk_conn,$_POST['username']);
    $p=mysqli_real_escape_string($ahk_conn,$_POST['password']);
    $q=mysqli_query($ahk_conn,"SELECT * FROM users WHERE phone='$u' OR email='$u'");
    if(mysqli_num_rows($q)==1){
        $r=mysqli_fetch_assoc($q);
        if(password_verify($p,$r['password'])){
            if($r['status']==1){
                $f = $r['phone']; 
                if($webdata['otp_option']=="yes" && $r['otp_system']=="yes"){
                    $o=rand(100000,999999);
                    $_SESSION['otp']=$o;
                    $_SESSION['otp_phone']=$f;

                    $j=json_decode(file_get_contents("$ENDPOINT?api_key=$tng_wa_apikey&sender=$tng_wa_sender&number=91$f&message=".urlencode("Your OTP for login is: $o")."&footer=$site"),1);

                    if($j['status']){
                        echo '<script>
                        $(function(){
                            Swal.fire({
                                title:"OTP Sent",
                                text:"OTP sent to WhatsApp.",
                                input:"text",
                                inputLabel:"Enter OTP",
                                inputPlaceholder:"6-digit OTP",
                                confirmButtonText:"Verify",
                                showCancelButton:true
                            }).then((x)=>{
                                if(x.isConfirmed){
                                    $.post("verify_otp.php",{otp:x.value},function(d){
                                        if(d=="success"){
                                            Swal.fire("Login Successful!","","success")
                                            .then(()=>{ location="admin"; });
                                        }else{
                                            Swal.fire("Invalid OTP","Try again","error");
                                        }
                                    });
                                }
                            });
                        });
                        </script>';
                        exit; 
                    }else{
                        echo '<script>Swal.fire("Error","OTP send failed","error");</script>';
                    }
                }else{
                    $_SESSION['phone']=$f;

                  
                    echo '<script>
                        $(function(){
                            Swal.fire("Success","Login Successful!","success")
                            .then(()=>{ location="admin"; });
                        });
                    </script>';
                    exit; 
                }
            }else{
                $fee=($r['type']=="retailer")?$webdata['ret_reg_fee']:(($r['type']=="distributor")?$webdata['dist_reg_fee']:999);
                echo '<script>
                Swal.fire({
                    title:"Account Inactive",
                    html:"Pay <b>₹'.$fee.'</b> to activate.",
                    icon:"warning",
                    confirmButtonText:"Pay Now"
                }).then(()=>{ location="payment.php?phone='.$r['phone'].'"; });
                </script>';
            }
        }else{
            echo '<script>Swal.fire("Incorrect Password","","error");</script>';
        }
    }else{
        echo '<script>Swal.fire("Not Registered","","error");</script>';
    }
}
?>
