<?php
if ($_SERVER['HTTP_HOST'] == 'localhost') {
    $Cser = mysqli_connect("localhost", "root", "", "err_forms");
} else {
    $Cser = mysqli_connect("localhost", "guidestarmarketi_gohealth", "Bismillah@786", "guidestarmarketi_multi_forms");
}

if (strlen($_POST['phone_number']) != 10) {
    $num = "10 characters phone number is required.";
} else {
    $formtype = $_POST['type'];
    $zip = '00000';
    if ($_POST['zip'] != '') {
        $zip = $_POST['zip'];
    }

    switch ($formtype) {
        case 'm2';  //////// svg
            $headers = array(
                //'Accept: application/json',
                //'Content-Type: application/json',
            );
            $url = "https://retreaverdata.com/data_writing?key=4e356de1-7b34-4028-979d-4c6e63c4171c&caller_number=" . urlencode(trim($_POST['phone_number'])) . "&first_name=" . urlencode(trim($_POST['fname'])) . "&last_name=" . urlencode(trim($_POST['lname'])) . "&caller_zip=" . urlencode(trim($_POST['zip'])) . "&state=" . urlencode(trim($_POST['state'])) . "&email=" . urlencode(trim($_POST['email'])) . "&birthdate=" . urlencode(trim(date("Y-m-d", strtotime($_POST['dob'])))) . "&og_leads_pii_id=" . urlencode(trim($_POST['og_leads_pii_id'])) . "";
            //echo $url;exit;
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            $result = curl_exec($ch);
            $result = json_decode($result);
            curl_close($ch);
            //echo "<pre>";print_r($result);exit;
            $res = $result->status;
            $sql = "INSERT INTO leads(phone ,  first_name , last_name , zip , state ,  email, dob, lead_id, clients, agent_name) 
                    VALUES('" . urlencode(trim($_POST['phone_number'])) . "','" . urlencode(trim($_POST['fname'])) . "',
                    '" . urlencode(trim($_POST['lname'])) . "','" . urlencode(trim($zip)) . "',
                    '" . urlencode(trim($_POST['state'])) . "','" . urlencode(trim($_POST['email'])) . "',
                    '" . urlencode(trim(date("Y-m-d", strtotime($_POST['dob'])))) . "',
                    '" . urlencode(trim($_POST['og_leads_pii_id'])) . "',
                    '" . urlencode(trim($formtype)) . "', '" . (trim($_POST['agent_name'])) . "');";
            break;
        case 'm3'; //////// go health
            $url = 'https://www.brokeroffice.com/api/v1/lead/partner/consumer-data';
            $head = [
                '__tokenid: W5MQQ4KP-YsWW-kEQc-rPqY-NiaXD4Fdibxh',
                'Content-Type: application/json',
                'Accept: application/json'];
            $data = array(
                "firstName" => $_POST['fname'],
                "lastName" => $_POST['lname'],
                "zipCode" => $_POST['zip'],
                "dnis" => "8772527719",
                //"linkId" => "1990",
                "phone1" => $_POST['phone_number']
            );
            $postdata = json_encode($data);
            //echo "<pre>";echo $postdata;
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
            $result = curl_exec($ch);
            curl_close($ch);
            $result = json_decode($result);
            //echo "<pre>";print_r($result);
            if (isset($result->leadId) && $result->leadId != '') {
                $sql = "INSERT INTO leads(phone ,  first_name , last_name , zip , state ,  email, dob, lead_id, api_lead_id, clients, agent_name) 
                    VALUES('" . urlencode(trim($_POST['phone_number'])) . "','" . urlencode(trim($_POST['fname'])) . "',
                    '" . urlencode(trim($_POST['lname'])) . "','" . urlencode(trim($zip)) . "',
                    '" . urlencode(trim($_POST['state'])) . "','" . urlencode(trim($_POST['email'])) . "',
                    '" . urlencode(trim(date("Y-m-d", strtotime($_POST['dob'])))) . "',
                    '" . urlencode(trim($_POST['og_leads_pii_id'])) . "','" . urlencode(trim($result->leadId)) . "',
                    '" . urlencode(trim($formtype)) . "', '" . (trim($_POST['agent_name'])) . "');";
            }
            break;
        case 'm4'; //////// go health
            $url = 'https://www.brokeroffice.com/api/v1/lead/partner/consumer-data';
            $head = [
                '__tokenid: W5MQQ4KP-YsWW-kEQc-rPqY-NiaXD4Fdibxh',
                'Content-Type: application/json',
                'Accept: application/json'];
            $data = array(
                "firstName" => $_POST['fname'],
                "lastName" => $_POST['lname'],
                "zipCode" => $_POST['zip'],
                "dnis" => "8662317853",
                //"linkId" => "1990",
                "phone1" => $_POST['phone_number']
            );
            $postdata = json_encode($data);
            //echo "<pre>";echo $postdata;
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
            $result = curl_exec($ch);
            curl_close($ch);
            $result = json_decode($result);
            //echo "<pre>";print_r($result);
            if (isset($result->leadId) && $result->leadId != '') {
                $sql = "INSERT INTO leads(phone ,  first_name , last_name , zip , state ,  email, dob, lead_id, api_lead_id, clients, agent_name) 
                    VALUES('" . urlencode(trim($_POST['phone_number'])) . "','" . urlencode(trim($_POST['fname'])) . "',
                    '" . urlencode(trim($_POST['lname'])) . "','" . urlencode(trim($zip)) . "',
                    '" . urlencode(trim($_POST['state'])) . "','" . urlencode(trim($_POST['email'])) . "',
                    '" . urlencode(trim(date("Y-m-d", strtotime($_POST['dob'])))) . "',
                    '" . urlencode(trim($_POST['og_leads_pii_id'])) . "','" . urlencode(trim($result->leadId)) . "',
                    '" . urlencode(trim($formtype)) . "', '" . (trim($_POST['agent_name'])) . "');";
            }
            break;
        case 'm57'; //////// selected-quote

            $url = 'https://tsqe5h7zvl.execute-api.us-west-2.amazonaws.com/prod/getinboundmonetization';

            $head = [
                'x-api-key: ElfZ7A0nbO4cVBbeRxW982qPQXOcYKFX7prdyrq9',
                'Content-Type: application/json',
                'Accept: application/json'];
            $data = array(
                "firstName" => $_POST['fname'],
                "lastName" => $_POST['lname'],
                "zip" => $zip,
                "ani" => $_POST['phone_number'],
                "birthday" => date("Y-m-d", strtotime($_POST['dob'])),
                "dnis" => '8444690071',
                "business_rule" => "Inbound",
                "caller" => 'Guidestar',
                "campaign_id" => '14203',
            );
            $postdata = json_encode($data);
            //echo "<pre>";echo $postdata;
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
            $result = curl_exec($ch);
            curl_close($ch);
            $result = json_decode($result, true);
            //echo "<pre>";print_r($result);
            //echo $res = $result['status'];

            if (isset($result['transfer_line']) && $result['transfer_line'] != '') {
                $sql = "INSERT INTO leads(phone ,  first_name , last_name , zip , state ,  email, dob, lead_id, api_lead_id, clients, agent_name) 
                    VALUES('" . urlencode(trim($_POST['phone_number'])) . "','" . urlencode(trim($_POST['fname'])) . "',
                    '" . urlencode(trim($_POST['lname'])) . "','" . urlencode(trim($zip)) . "','" . urlencode(trim($_POST['state'])) . "',
                    '" . urlencode(trim($_POST['email'])) . "','" . urlencode(trim(date("Y-m-d", strtotime($_POST['dob'])))) . "',
                    '" . urlencode(trim($_POST['og_leads_pii_id'])) . "','" . urlencode(trim($result['transfer_line'])) . "',
                    '" . urlencode(trim($formtype)) . "', '" . (trim($_POST['agent_name'])) . "');";
            }
            break;
        case 'justdata';
            $sql = "INSERT INTO leads(phone ,  first_name , last_name , zip , state ,  email, dob, lead_id, api_lead_id, clients, agent_name) 
                    VALUES('" . urlencode(trim($_POST['phone_number'])) . "','" . urlencode(trim($_POST['fname'])) . "',
                    '" . urlencode(trim($_POST['lname'])) . "','" . urlencode(trim($zip)) . "','" . urlencode(trim($_POST['state'])) . "',
                    '" . urlencode(trim($_POST['email'])) . "','" . urlencode(trim(date("Y-m-d", strtotime($_POST['dob'])))) . "',
                    '" . urlencode(trim($_POST['og_leads_pii_id'])) . "','',
                    '" . urlencode(trim($formtype)) . "', '" . (trim($_POST['agent_name'])) . "');";
            break;
    }
    mysqli_query($Cser, $sql);
    $res = 'Your form Successfully Submitted';
}