<?php

/**
 * Common Library for Sphinx localpages index bussiness details
 * Munish 14-Sep-2022
 */
require_once 'SphinxConn.php';
class bussinessDetails extends sphinxConn
{
    const SPHINX_INDEX = "business_details";
    const PAGE_OFFSET = 0;
    const PAGE_LIMIT = 20;
    const DISTANCE_LIMIT = 100; //100 km

    public function __construct($config = array())
    {
        parent::__construct($config);
    }
    /**
     * Get result basis of distance, Return only max 100 KM results
     * @param type $searchQuery
     * @param type $lat
     * @param type $lon
     * @param type $offset
     * @param type $limit
     * @param type $distance in km
     */
    public function getDistanceSearch($searchQuery, $lat, $lon, $offset = self::PAGE_OFFSET, $limit = self::PAGE_LIMIT, $distance = self::DISTANCE_LIMIT)
    {
        $offset = ($offset === false) ?  self::PAGE_OFFSET : $offset;
        $limit = empty($limit) ?  self::PAGE_LIMIT : $limit;
        $distance = empty($distance) ?  self::DISTANCE_LIMIT : $distance;
        $match = $this->getQuote($searchQuery);
        $sql = "SELECT *,GEODIST($lat,$lon, lat,lon, {in=deg,out=km}) AS distance FROM business_details WHERE MATCH($match) AND distance <= $distance ORDER BY distance ASC limit $offset,$limit OPTION max_matches=99999";
        $result = $this->getDataWithMetaInfo($sql);
        return $result;
    }

    public function getDetails($id)
    {
        $sql = "select * from business_details where id=$id";
        $result = $this->getRow($sql);
        return $result;
    }

    public function getBusinessDetails($id)
    {
        $sql = "select * from business_details where id=$id";
        $row = $this->getRow($sql);
        $result = [];

        if (!empty($row)) {
            $business_hour = '';

            if ($row['business_hour']) {
                $Hours = unserialize(($row['business_hour']));
                $Days = array("MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY");

                foreach ($Days as $Day) {
                    if ($Hours[$Day]) {
                        $SplitHour = explode(',', $Hours[$Day]);

                        if ($SplitHour[0] == '00:00:00-00:00:00')
                            $business_hour .= ucfirst(strtolower($Day)) . ' : Open All Day <br>';
                        else if ($SplitHour[0] == '-')
                            $business_hour .= ucfirst(strtolower($Day)) . ' : Closed <br>';
                        else if (isset($SplitHour[1])) {
                            $Hour = explode('-', $SplitHour[0]);
                            $business_hour .= ucfirst(strtolower($Day)) . ' : ' . date("g:i A", strtotime($Hour[0])) . ' - ' . date("g:i A", strtotime($Hour[1]));
                            $Hour = explode('-', $SplitHour[1]);
                            $business_hour .= ' , ' . date("g:i A", strtotime($Hour[0])) . ' - ' . date("g:i A", strtotime($Hour[1])) . '<br>';
                        } else {
                            $Hour = explode('-', $SplitHour[0]);
                            $business_hour .= ucfirst(strtolower($Day)) . ' : ' . date("g:i A", strtotime($Hour[0])) . ' - ' . date("g:i A", strtotime($Hour[1])) . '<br>';
                        }
                    }
                }
            }

            $result['business_name'] = $row['business_name'];
            $result['description'] = $row['description'];
            $result['address1'] = $row['full_address'];
            $result['biz_id'] = $row['id'];
            $result['city'] = $row['city'];
            $result['state'] = $row['state'];
            $result['zip'] = $row['zip'];
            $result['area_code'] = $row['phone_code'];
            $result['phone'] = $row['phone'];
            $result['phones'] = !empty($row['phone_numbers']) ? explode("::", $row['phone_numbers']) : [];
            $result['record_id'] = $row['record_id'];
            $result['specialOffer'] = $row['specialoffer'];
            $result['specialOffer_url'] = $row['specialoffer_url'];
            $result['lat'] = $row['lat'];;
            $result['lon'] = $row['lon'];;
            $result['url'] = $row['web_address'];
            $result['business_hour'] = $business_hour;
            $result['email'] = $row['email'];
            $result['category'] = !empty($row['category']) ? explode("::", $row['category']) : [];
            $result['payment_options'] = $row['paymentoptions'];
            $result['business_id'] = 'lze' . $row['id'];
            $result['attribution'] = json_decode($row['attribution'], true);
            $result['change_code'] = $row['change_code'];
            $result['business_flag'] = $row['business_flag'];

            if ($row['phone']) {
                $result['phonefrmt'] = "(" . substr($row['phone'], 0, 3) . ") " . substr($row['phone'], 3, 3) . '-' . substr($row['phone'], 6, 4);
            }

            $result['image'] = empty($row['image']) ? array_filter(explode('::', $row['image'])) : '';
        }
        return $result;
    }
}
