<?php
class DataValidation extends Core {

    function cpf() {

        $cpf = $_GET['cpf'];
        $account_type = $_GET['accountType'];
        $sql = "SELECT * FROM leads WHERE cpf = ? AND account_type = ?";
        $stmt = parent::$conn->prepare($sql);
        $stmt->execute([$cpf, $account_type]);
        
        if($stmt->rowCount() > 0)
            return ['type' => 'ALREAD_EXIST'];

        return ['type' => 'OK'];

    }

    function cnpj() {

        $cnpj = $_GET['cnpj'];

        $sql = "SELECT companyname, cnpj FROM companies WHERE cnpj = ?";
        $stmt = parent::$conn->prepare($sql);
        $stmt->execute([$cnpj]);
        
        if($stmt->rowCount() > 0)
            return ['type' => 'ALREAD_EXIST', 'data' => $stmt->fetch(PDO::FETCH_ASSOC)];
        
        return ['type' => 'OK'];

    }

    function phone() {

        $phone = $_GET['phone'];
        $account_type = $_GET['accountType'];
        $sql = "SELECT * FROM leads WHERE phone = ? AND account_type = ?";
        $stmt = parent::$conn->prepare($sql);
        $stmt->execute([$phone, $account_type]);
        
        if($stmt->rowCount() > 0)
            return ['type' => 'ALREAD_EXIST'];

        return ['type' => 'OK'];

    }

    function email() {

        $email = $_GET['email'];
        $account_type = $_GET['accountType'];
        $sql = "SELECT * FROM leads WHERE email = ?";
        $stmt = parent::$conn->prepare($sql);
        $stmt->execute([$email]);
        
        if($stmt->rowCount() > 0)
            return ['type' => 'ALREAD_EXIST'];

        return ['type' => 'OK'];

    }

}