<?php
header('Content-Type: application/json');
//echo '<pre>';
//var_dump($modelCompany);die;
//$modelCompany = $modelCompany[0];
function nice_phone($phone = NULL)
{
	if(empty($phone))
	{
		return 'N/A';
	}
	$nice = 'INVALID: ' . $phone;
	if( strlen($phone) == 10)
	{
		$area = substr($phone , 0 , 3);
		$prefix = substr($phone , 3 , 3);
		$serial = substr($phone , 6 , 4);
		$nice = $area  .'-' . $prefix  .'-'. $serial ;
	}
	if( strlen($phone) == 11)
	{
		$country = substr($phone , 0 ,1);

		$area = substr($phone , 1 , 3);
		$prefix = substr($phone , 4 , 3);
		$serial = substr($phone , 7 , 4);
		$nice = '+'. $country. ' (' . $area  .') ' . $prefix  .'-'. $serial ;
	}
	return $nice;
}
foreach ($modelCompany as $value)
{
	$owner = $value->owner;
	$data = new stdClass();
	$data->map_link = "https://maps.google.com/maps?q=". $value->adress . '+' . $value->city . '+' . $value->state . '+' . $value->zip;
	$data->address = new stdClass();
	$data->address->full = $value->adress.' '. $value->city . ', '. $value->state .' '. $value->zip;
	$data->address->address = $value->adress;
	$data->address->city = $value->city;
	$data->address->state = $value->state;
	$data->address->zip = $value->zip;
	
	$data->company = new stdClass();
	$data->company->name = $value->name;
	$data->company->phone = $value->phone;
	$data->company->tagline = $value->tagline;
	$data->company->notes = $value->notes;
	
	$data->owner = new stdClass();
	$data->owner->firstname = $value->owner->firstname;
	$data->owner->lastname = $value->owner->lastname;
	$data->owner->email = $value->owner->email;
	$data->owner->notes = $value->owner->notes;
}

if($data == null)
{
	$ret = array('status' => 0, 'message' => 'Record Not Found for '. $phone);
	echo json_encode($ret);
	die();
}

$ret = array('status' => 1, 'message' => 'Records Found for '. $phone, 'data' => $data);
echo json_encode($ret);
die();
?>
<div class="clear"></div>
<h1 style="float: left;">Phone information</h1>
<?php



if (!empty($modelCompany)) {
	foreach ($modelCompany as $value)
	{
		?>
			<div class="box" style="text-align:right; float:right;  margin-left:auto;margin-right: auto;">
				<h2  class="" style="padding:10px; border-radius: 5px;">
					<a style="color: #FFF;" href="https://maps.google.com/maps?q=<?php echo $value->adress . '+' . $value->city . '+' . $value->state . '+' . $value->zip; ?>" target="_blank" class="btn btn-secondary">Google Maps</a>
				</h2>
			</div>
			<div class="clear"></div>
		<?php 
	}
}
//var_dump($modelCompany);die;


?>
<table width="auto" cellspacing="5" cellpadding="5">
    <?php
    $countHead = 1;
    if (!empty($modelCompany)) {
        foreach ($modelCompany as $value) {
        	$value->zip = trim($value->zip);
        	if(!empty($value->zip) && strlen($value->zip) < 5)
        	{
			while(strlen($value->zip) != 5)
			{
				$value->zip = '0'.$value->zip;
			}
		}
		if(!empty($value->phone))
		{
			
		}
            ?>

            <tr class="row">
                <td class="span6 left" colspan="2"><h4>Company information for (Phone Number #<?php echo $value->phone; ?>)</h4></td>
            </tr>

            <!---------COMPANY INFORMATION-------->    

            <tr class="">
                <td style="text-align: right; display: table-cell;padding:5px;" class="span2 left setfone">Company: </td>
                <td style="font-weight:bold; margin-left:7px;padding:5px;" class="span2 left setfone"> <?php echo $value->name; ?></td>
            </tr>
            <!--company address-->
            <tr class="">
                <td style="text-align: right;display: table-cell;padding:5px;" class="span2 left setfone">Tagline: </td>
                <td style="font-weight:bold;display: table-cell;padding:5px; margin-left:7px;" class="span2 left setfone"> <?php echo $value->tagline; ?></td>
            </tr>
            <!--company phone & Google Map Button-->
            <tr class="">
                <td style="text-align: right;display: table-cell;padding:5px;" class="span2 left setfone">Phone: </td>
                <td style="font-weight:bold; margin-left:7px;padding:5px;" class="span2 left setfone" ><?php echo nice_phone($value->phone); ?></td>
                
            </tr>
            <!--company city-->
            <tr class="">
                <td style="text-align: right;display: table-cell;padding:5px;" class="span2 left setfone">City: </td>
                <td style="font-weight:bold;padding:5px;" class="span2 left setfone"> <?php echo $value->city; ?></td>
            </tr>
            <!--company state-->
            <tr class="">
                <td style="padding:5px; text-align: right;display: table-cell;" class="span2 left setfone">State: </td>
                <td style="font-weight:bold;padding:5px;" class="span2 left setfone"> <?php echo $value->state; ?></td>
            </tr>
            <!--company Zip-->
            <tr class="row">
                <td style="text-align: right;padding:5px;display: table-cell;" class="span2 left setfone">Zip: </td>
                <td style="font-weight:bold;padding:5px;" class="span2 left setfone"> <?php echo $value->zip; ?></td>
            </tr>
            <!--company Notes-->
            <tr class="row">
                <td style="text-align: right;padding:5px;display: table-cell;" class="span2 left setfone">Notes: </td>
                <td style="font-weight:bold;padding:5px;" class="span2 left setfone"> <?php echo $value->notes; ?></td>
            </tr>
            <!---------OWNER INFORMATION------------->
<?/*
            
            <!--OWNER Date of Birth-->
            <tr class="row">
                <td style="padding:5px;text-align: right;" class="span2 left setfone">Date of Birth: </td>
                <td style="padding:5px;font-weight:bold;" class="span2 left setfone"> <?php echo $value->owner->dob; ?></td>
            </tr>
            <!--OWNER Position-->
            <tr class="row">
                <td style="padding:5px;text-align: right;" class="span2 left setfone">Position: </td>
                <td style="padding:5px;font-weight:bold;" class="span2 left setfone"> <?php echo $value->owner->position; ?></td>
            </tr>
            <!--OWNER Phone-->
            <tr class="row">
                <td style="padding:5px;text-align: right;" class="span2 left setfone">Phone: </td>
                <td style="padding:5px;font-weight:bold;" class="span2 left setfone"> <?php echo nice_phone($value->owner->phone); ?></td>
            </tr>
*/?>
<tr class="row">
                <td colspan="2" class="span10 left">
                    <div class="container-radius"><div id="mainmenu">
                            <ul id="yw0" class="nav nav-tabs">
                                <li class="active" id="secondmainmenu">
                                    <a href="#">Owner</a>
                                </li>
                            </ul>
                        </div>
                    </div>
                </td>
            </tr>
            <tr style="margin-top:100px;"></tr>
            <!--OWNER FIRST NAME-->
            <tr class="row" >
                <td style="text-align: right;padding:5px;" class="span2 left setfone">FirstName: </td>
                <td style="padding:5px;font-weight:bold;" class="span2 left setfone"> <?php echo $value->owner->firstname; ?></td>
            </tr>
            <!--OWNER LAST NAME-->
            <tr class="row">
                <td style="padding:5px;text-align: right;" class="span2 left setfone">LastName: </td>
                <td style="padding:5px;font-weight:bold;" class="span2 left setfone"> <?php echo $value->owner->lastname; ?></td>
            </tr>
            <!--OWNER E-Mail-->
            <tr class="row">
                <td style="padding:5px;text-align: right;" class="span2 left setfone">E-Mail: </td>
                <td style="padding:5px;font-weight:bold;" class="span2 left setfone"> <?php echo $value->owner->email; ?></td>
            </tr>
            <!--OWNER E-Mail-->
            <tr class="row">
                <td style="padding:5px;text-align: right;" class="span2 left setfone">Extra Notes: </td>
                <td style="padding:5px;font-weight:bold;" class="span2 left setfone"> <?php echo $value->owner->notes; ?></td>
            </tr>
            <tr>
                <td class="span10 left setborderbottom" colspan="2"></td>
            </tr>

            <?php
            $countHead++;
        }
    } else {
        ?>
        <div class=" erros alert-error" style="color: red;"><h5>Record Not Found</h5></div>
        <?php
    }
    ?>
</table>
<script type="text/javascript">
    function ref() {

        var var1 = document.getElementById('mapsearch').value;
        location.href = location.href + '?var1=' + var1;
//        location.href = location.href + '?var1=' + var1 + '&var2=' + var2;
    }
</script>
<style>
    .setfone{
        font-family: Lucida Grande,Lucida Sans,Arial,sans-serif;
        font-size: 1.1em;
        color: #222222;
    }

    .setborderbottom{
        border-bottom: 1px solid #86B5D9;
    }


</style>