<?php
ob_start();
session_start();
include("fresh_panel/inc/config.php");
    // Getting all language variables into array as global variable
    $i=1;
    $statement = $pdo->prepare("SELECT * FROM tbl_language");
    $statement->execute();
    $result = $statement->fetchAll(PDO::FETCH_ASSOC);
    foreach ($result as $row) {
        define('LANG_VALUE_'.$i,$row['lang_value']);
        $i++;
    }
    
    $status = 0;
    
    if(!isset($_POST['cust_email']) || empty($_POST['cust_email'])){
        $error_message = 'Email Or Mobile Number Can not be empty' ;
    }
    else
    {   
        if(!isset($_POST['cust_password']) || empty($_POST['cust_password'])){
            $error_message = 'Password Can not be empty' ;
        }
        else
        {
            $cust_email = strip_tags($_POST['cust_email']);
            $cust_password = strip_tags($_POST['cust_password']);
    
            $statement = $pdo->prepare("SELECT * FROM tbl_customer WHERE cust_email=? || cust_phone=?");
            $statement->execute(array($cust_email,$cust_email));
            $total = $statement->rowCount();
            $result = $statement->fetchAll(PDO::FETCH_ASSOC);
            foreach($result as $row) {
                $cust_status = $row['cust_status'];
                $row_password = $row['cust_password'];
            }

            if($total==0) {
                $error_message = 'Invalid User Details.';
            } else {
                if( $row_password != base64_encode($cust_password) ) {
                    $error_message = LANG_VALUE_139;
                } else {
                    if($cust_status == 0) {
                        $error_message = LANG_VALUE_148;
                    } else{
                        $_SESSION['customer'] = $row;
                        $status = 1;
                        $success_message = 'User Login Successfully <script>window.location="index.php";</script>';
                    }
                }
                
            }
        }
    }
    
    // response for user
    $myObj->succ = $success_message;
    $myObj->error = $error_message;
    $myObj->log = $status;
    
    $myJSON = json_encode($myObj);
    
    echo $myJSON;
    
?>