<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends Public_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model(array('products/product_model', 'category/category_model', 'members/members_model'));
    }

    public function index() {
        //home category
        $home_cat = array('field' => "*", 'condition' => "AND status='1' AND home_menu = '1'", 'limit' => "8", 'debug' => FALSE);
        $category_result = $this->category_model->getcategory($home_cat);
        $data['home_cat'] = $category_result;

        $home_cat1 = array('field' => "*", 'condition' => "AND status='1' AND home_cat = '1' AND parent_id = '32'", 'limit' => "4", 'debug' => FALSE);
        $category_result = $this->category_model->getcategory($home_cat1);
        $data['home_cat1'] = $category_result;

        $data['middlebanner1'] = $this->db->query("SELECT * FROM wps_banners WHERE banner_position = 'Index Middle 1' AND status = '1'")->row_array();
        $data['middlebanner2'] = $this->db->query("SELECT * FROM wps_banners WHERE banner_position = 'Index Middle 2' AND status = '1'")->row_array();
        $data['bottomBanner1'] = $this->db->query("SELECT * FROM wps_banners WHERE banner_position = 'Index Bottom 1' AND status = '1'")->row_array();
        $data['bottomBanner2'] = $this->db->query("SELECT * FROM wps_banners WHERE banner_position = 'Index Bottom 2' AND status = '1'")->row_array();
        $data['bottomBanner3'] = $this->db->query("SELECT * FROM wps_banners WHERE banner_position = 'Index Bottom 3' AND status = '1'")->row_array();
        $data['bottomBanner4'] = $this->db->query("SELECT * FROM wps_banners WHERE banner_position = 'Index Bottom 4' AND status = '1'")->row_array();
        $data['shopcategory'] = $this->db->query("SELECT * FROM wps_categories WHERE parent_id = '0' AND status = '1'")->result_array();
        $data['gallery'] = $this->db->query("SELECT * FROM wps_gallery WHERE gallery_parent_id = '1' AND status = '1' limit 0,10")->result_array();
        $data['client'] = $this->db->query("SELECT * FROM wps_gallery WHERE gallery_parent_id = '3' AND status = '1' ")->result_array();
        $data['testimonial'] = $this->db->query("SELECT * FROM wps_testimonial WHERE status = '1' ")->result_array();

        //Popular products
        $data['popular_products'] = $this->get_popular_product();

        //New Arrival products
        $data['new_products'] = $this->get_new_product();

        $content = "";
        $homeHeading = '';
        //Check if it is subdomain
        $uri_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
        $uri_segments = explode('/', $uri_path);
        $st = $uri_segments[1];
        if (strstr($st, '.html')) {
            $st = substr($st, 0, -5);
        }

        $stArray = $this->db->query("SELECT meta_id, page_url FROM wps_meta_tags WHERE is_fixed='L' AND page_url='" . $st . "'")->row_array();
        if (is_array($stArray) & !empty($stArray)) {
            $locId = $stArray['meta_id'];
            $resprosub = $this->db->query("SELECT * FROM wps_subloccontent WHERE status = '1' AND FIND_IN_SET($locId,location_id)")->row();
            if (is_object($resprosub) && !empty($resprosub)) {
                $key1 = $resprosub->meta_key1;
                $key2 = $resprosub->meta_key2;
                $key3 = $resprosub->meta_key3;
                $content = str_replace('{location}', ucwords(locationName($st)), str_replace('{key3}', $key3, str_replace('{key2}', $key2, str_replace('{key1}', $key1, $resprosub->description))));
                $homeHeading = 'Welcome to KK International - ' . ucwords(locationName($st));
            }
        } else {
            $content = get_db_field_value('wps_cms_pages', 'page_description', "WHERE page_id = '10'");
            // $homeHeading = get_db_field_value('wps_cms_pages', 'page_name', "WHERE page_id = '10'");
             $homeHeading = 'Oxygen Therapy Equipment';
        }
        $data['subheading1'] = get_db_field_value('wps_cms_pages', 'page_name', "WHERE page_id = '10'");
        $data['subheading2'] = get_db_field_value('wps_cms_pages', 'page_name', "WHERE page_id = '10'");
        $data['subheading3'] = get_db_field_value('wps_cms_pages', 'page_name', "WHERE page_id = '10'");
        $data['subheading4'] = get_db_field_value('wps_cms_pages', 'page_name', "WHERE page_id = '10'");
        $data['homeHeading'] = $homeHeading;
        $data['home'] = $content;

        //banners
        $data['banner'] = $this->db->query("SELECT * FROM wps_banners WHERE status = '1' AND banner_position = 'Index Slider' order by banner_id asc")->result_array();
        $data['blogs'] = $this->db->query("SELECT * FROM wps_blog WHERE status = '1' order by article_id desc limit 3")->result_array();

        $this->load->view('view_home', $data);
    }

    public function get_popular_product() {
        $condtion['status'] = '1';
        $condtion['where'] = "popular_product = '1'";
        $condtion['orderby'] = 'products_id DESC';
        $product_list = $this->product_model->get_products('5', '0', $condtion);
        return $product_list;
    }

    public function get_new_product() {
        $condtion['status'] = '1';
        $condtion['where'] = "newarrival_product = '1'";
        //$condtion['orderby'] = 'products_id ASC';
        $product_list = $this->product_model->get_products('8', '0', $condtion);
        return $product_list;
    }

    public function search_products() {
        $keyword = $this->input->post('keyword');
        $where = "";
        //$qry = "SELECT p.friendly_url, p.product_name, pm.media from wps_products p LEFT JOIN wps_products_media as pm ON p.products_id=pm.products_id where p.status='1' and (p.product_name LIKE '%" . $keyword . "%' OR p.product_code LIKE '%" . $keyword . "%' OR p.products_description LIKE '%" . $keyword . "%' OR FIND_IN_SET(p.products_description,'" . $keyword . "')) AND pm.is_default = 'Y' $where ";
        $qry="SELECT friendly_url, category_name, category_image, category_description, category_id from wps_categories where status='1' and (category_name LIKE '%" . $keyword . "%' OR friendly_url LIKE '%" . $keyword . "%' OR category_description LIKE '%" . $keyword . "%') $where";
        $products = $this->db->query($qry)->result_array();
        if (count($products) > 0) {
            ?>
            <ul class="searching_listing" style="margin-bottom: 0;border: 1px solid gray;padding: 10px; z-index: 999999 !important;">
            <?php foreach ($products as $prod) { ?>
                    <li style=" float: none !important; margin-left: 0px !important; padding: 5px; cursor: pointer;" onclick="window.location.href = '<?php echo base_url($prod['friendly_url']); ?>'">
                        <img src="<?php echo get_image('category', $prod['category_image'], '50', '50', 'R'); ?>" width="32" height="32"> &nbsp; <?php echo $prod['category_name']; ?>
                    </li>
                <?php
            }
            echo "</ul>";
        } else {
            ?>
                <ul class="searching_listing" style="margin-bottom: 0; width: 100%; border: 1px solid gray;padding: 10px; z-index: 999999 !important;">
                    <li style="width: 99%;">Oh Snap! Result Not Found</li>
                </ul>
            <?php
        }
    }

    public function submit_newslater() {
        $this->form_validation->set_rules('txtemail', 'Email', 'trim|required');
        if ($this->form_validation->run() == TRUE) {
            $posted_data = array(
                'subscriber_email' => $this->input->post('txtemail'),
                'message' => 'Signup For Newsletter',
                'subscribe_date' => $this->config->item('config.date.time')
            );
            $result = $this->members_model->safe_insert('wps_newsletters', $posted_data, false);
            $subject = "WebpulseIndia.com - Signup For Newsletter from www.delhi-interior.com";
            $content = get_content('wps_auto_respond_mails', '3');
            $body = $content->email_content;
            $body = str_replace('{mem_name}', 'Admin', $body);
            $body = str_replace('{admin_email}', $this->input->post('txtemail'), $body);
            $body = str_replace('{date}', $this->config->item('config.date.time'), $body);
            $body = str_replace('{site_name}', $this->config->item('site_name'), $body);
            $mail_conf = array(
                'subject' => $subject,
                'to_email' => 'webpulse.php01@gmail.com',
                'reply_to' => 'enquiry@webpulseindia.com',
                'reply_name' => $this->input->post('wname'),
                'from_email' => 'enquiry@webpulseindia.com',
                'from_name' => $this->input->post('wname'),
                'body_part' => $body
            );
            $this->dmailer->mail_notify($mail_conf);
            $res = 'success';
        } else {
            $res = '<div class="alert alert-danger">
      <strong>' . validation_errors() . '
      </div>';
        }
        echo $res;
        exit;
    }

}
