<?php
    require_once 'PerfilVicidial.php';
    class PerfilVicidialDatos{        
        public static function getPerfilVicidialByLeadId(string $lead):?PerfilVicidial{
            $ret = null;
            $con = getConnection();
            $query = "SELECT lead_id,IFNULL(status,'') as status,list_id,IFNULL(vendor_lead_code,'') as vendor_lead_code,IFNULL(first_name,'') as first_name, IFNULL(last_name,'') as last_name, IFNULL(email,'') as email, IFNULL(address1,'') as address1, IFNULL(postal_code,'') as postal_code,IFNULL(phone_number,'') as phone_number,IFNULL(alt_phone,'') as alt_phone,IFNULL(address3, '') as address3,IFNULL(source_id, '') as source_id FROM vicidial_list WHERE lead_id = '" . limpia($lead) . "' LIMIT 1";
            if ($result = sv_mysqli_query($con, $query)) {
                if ($row = sv_mysqli_fetch_array($result)) {
                    $ret = new PerfilVicidial($row["lead_id"],$row["status"],ListaDatos::getListaById($row["list_id"]),$row["vendor_lead_code"],$row["first_name"],$row["last_name"],$row["email"],$row["address1"],$row["postal_code"],$row["phone_number"],$row["alt_phone"],$row["address3"], $row["source_id"]);
                }
            }
            sv_mysqli_close($con);
            return $ret;
        }

        public static function getPerfilesVicidialByTelefonoAndListas(string $telefono, array $listas):? array {
            $ret = array();
            $con = getConnection();

            $arrListas = array();
            $strListas = "";

            for ($i = 0;$i<count($listas);$i++) {
                $arrListas[] = $listas[$i]->getId();
            }
            $strListas = implode(',', $arrListas);
    
            $query = "SELECT lead_id,IFNULL(status,'') as status,list_id,IFNULL(vendor_lead_code,'') as vendor_lead_code,IFNULL(first_name,'') as first_name, IFNULL(last_name,'') as last_name, IFNULL(email,'') as email, IFNULL(address1,'') as address1, IFNULL(postal_code,'') as postal_code,IFNULL(phone_number,'') as phone_number,IFNULL(alt_phone,'') as alt_phone,IFNULL(address3, '') as address3,IFNULL(source_id, '') as source_id FROM vicidial_list WHERE list_id IN ($strListas) AND (phone_number = '" . limpia($telefono) . "' OR alt_phone = '".limpia($telefono)."' OR address3 = '".limpia($telefono)."') LIMIT 1000";
            
            if ($result = sv_mysqli_query($con, $query)) {
                while ($row = sv_mysqli_fetch_array($result)) {
                    $ret[] = new PerfilVicidial($row["lead_id"],$row["status"],ListaDatos::getListaById($row["list_id"]),$row["vendor_lead_code"],$row["first_name"],$row["last_name"],$row["email"],$row["address1"],$row["postal_code"],$row["phone_number"],$row["alt_phone"],$row["address3"], $row["source_id"]);
                }
            }
            sv_mysqli_close($con);
            return $ret;
        }

        public static function insertPerfilVicidial(PerfilVicidial $perfilVicidial):int{
            $resp = 0;
            $con = getConnection();
            $query = "INSERT INTO vicidial_list (list_id,vendor_lead_code,first_name,last_name,email,address1,postal_code,phone_number,alt_phone,address3,status,source_id) VALUES ('".limpia($perfilVicidial->getLista()->getId())."','".limpia($perfilVicidial->getClave())."','".limpia($perfilVicidial->getNombre())."','".limpia($perfilVicidial->getApellidos())."','".limpia($perfilVicidial->getCorreo())."','".limpia($perfilVicidial->getDireccion())."','".limpia($perfilVicidial->getCP())."','".limpia($perfilVicidial->getTelefono1())."','".limpia($perfilVicidial->getTelefono2())."','".limpia($perfilVicidial->getTelefono3())."','".limpia($perfilVicidial->getEstado())."','".limpia($perfilVicidial->getSourceId())."')";
            if(sv_mysqli_query($con,$query)){
                $resp = mysqli_insert_id($con);
            }else{
                $resp = abs(mysqli_errno($con))*-1;
            }
            sv_mysqli_close($con);
            return $resp;
        }
    }
?>