<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Site extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->headerData = array();
        $this->footerData = array();
        $this->load->helper("text");
        $this->load->helper('form');
        $this->load->model('Checkdata', '', TRUE);
        $this->load->model('Func', '', TRUE);
        $this->load->model('Get_Lang', '', TRUE);
        $this->load->library('user_agent');
        $this->load->helper('url');
        $this->load->model('DateTimeFunc', '', TRUE);

        $theLang = $this->session->userdata("lang");
        if ($theLang == "ar") {
            $langId = 1;
        } else if ($theLang == "tr") {
            $langId = 3;
        } else {
            $langId = 2;
            $session = array('lang' => "en");
            $this->session->set_userdata($session);
        }

        $this->headerData['contactUs'] = $this->db->query("SELECT coId, companyName, emailAddress, emailAddress2, phoneNumber, phoneNumber2, theText$langId, latitude FROM contact_us WHERE coId = 1")->result_object();
        $this->footerData['contactUs'] = $this->db->query("SELECT coId, companyName, emailAddress, emailAddress2, phoneNumber, phoneNumber2, theText$langId FROM contact_us WHERE coId = 1")->result_object();

        $this->headerData['social'] = $this->db->query("SELECT coId, phoneNumber, facebook, instagram, twitter, youtube, whatsapp$langId as whatsapp FROM contact_us WHERE coId = 1")->result_object();

        $this->footerData['siteSeting'] = $this->db->query("SELECT theValue FROM site_setting")->result_object();
        $this->headerData['siteSeting'] = $this->db->query("SELECT theValue FROM site_setting")->result_object();
        $this->headerData['theLogo'] = $this->headerData['siteSeting'][2]->theValue;
        $this->headerData['backgroundColor'] = $this->headerData['siteSeting'][1]->theValue;
        $this->headerData['color'] = $this->headerData['siteSeting'][0]->theValue;
        $this->footerData['theLogo'] = $this->headerData['siteSeting'][2]->theValue;

        $this->headerData['theSharePhoto'] = 'http://'.$_SERVER['SERVER_NAME'].'/public/uploads/php/files/logos/thumbnail/'.$this->headerData['siteSeting'][2]->theValue;
        $this->headerData['theShareTitle'] = $this->footerData['contactUs'][0]->companyName;
        $this->headerData['companyName'] = $this->footerData['contactUs'][0]->companyName;

        if (!ini_get('date.timezone')) {
//            date_default_timezone_set('GMT');
            date_default_timezone_set('Europe/Istanbul');
        }

        $this->headerData['theLang'] = $theLang;
        $this->headerData['langId'] = $langId;

        $this->headerData['theAbout'] = $this->db->query("SELECT abId, theText$langId as theText, theTitle$langId as theTitle FROM about_us WHERE abId = 15 ")->result_object();


        $this->headerData['headerCategories'] = $this->db->query("SELECT caId, theTitle$langId as theTitle FROM categories WHERE isDeleted = 0 AND parentId = 0 LIMIT 9")->result_object();
        $this->headerData['mainCategories'] = $this->db->query("SELECT caId, theTitle$langId as theTitle FROM categories WHERE isDeleted = 0 AND parentId = 0")->result_object();
    }

    public function index($url = Null, $url3 = NULL) {
        $pageData = array();
        $theLang = $this->session->userdata("lang");
        if ($url) {
            $url2 = str_replace("-", "_", $url);
            if (method_exists($this, $url2)) {
                if ($url3) {
                    $this->$url2($url3);
                } else {
                    $this->$url2();
                }
            } else {

                $checkURL = $this->db->query("SELECT page, content_id FROM links WHERE url = '$url'")->result_object();
                if (empty($checkURL)) {
                     if($url == 'payment-method'){ 
                         $this->privacy(24);
                    }else if($url == 'money-back-guarantee'){ 
                         $this->privacy(23);
                    }else if($url == 'shipping-conditions'){ 
                         $this->privacy(22);
                    }else if($url == 'returns-conditions'){ 
                         $this->privacy(21);
                    }else if($url == 'privacy-policy'){ 
                         $this->privacy(20);
                    }else if($url == 'terms-conditions'){ 
                         $this->privacy(19);
                    }else{
                    header("Location:/error");
                    exit();
                    }
                }else{
                    $thePage = $checkURL[0]->page;
                    $theID = $checkURL[0]->content_id;
                    if ($thePage == 'categories') {
                        $this->category($theID);
                    } elseif ($thePage == 'blog') {
                        $this->blog_details($theID);
                    } elseif ($thePage == 'subCategories') {
                        $this->product_details($theID);
                    } elseif (method_exists($this, $thePage)) {

                        $this->$thePage($theID);
                    } else {                                       
                        header("Location:/error");
                        exit();                    
                    }
                }
            }
        } else {
            $indexID = 0;
            if ($indexID == 0) {
                $indexID = "";
            } else {
                $indexID = "-" . $indexID;
            }
        if ($theLang == "en") {$langId = 2;} else if ($theLang == "tr") {$langId = 3;} else {$langId = 1;}        
        if($langId == 1){$theTitle = "theTitle1 as theTitle";}elseif($langId == 3){$theTitle = "theTitle3 as theTitle";}else{$theTitle = "theTitle2 as theTitle";}                
        if($langId == 1){$theTitles = "theTitle1 as theTitles";}elseif($langId == 3){$theTitles = "theTitle3 as theTitles";}else{$theTitles = "theTitle2 as theTitles";}                
        if($langId == 1){$theText = "theText1 as theText";}elseif($langId == 3){$theText = "theText3 as theText";}else{$theText = "theText2 as theText";}  
        
        $this->headerData['pageTitle'] = $this->Get_Lang->gl("about", $langId);
        if($langId == 1){$theText = "theText1 as theText";}elseif($langId == 3){$theText = "theText3 as theText";}else{$theText = "theText2 as theText";}                
         
        $pageData['homeSlider'] = $this->db->query("SELECT theLink, $theText, thePhoto FROM home_slider ")->result_object();
            
        $pageData['brands'] = $this->db->query("SELECT brId, $theTitle, thePhoto FROM brands WHERE isDeleted = 0 ORDER BY RAND() ")->result_object();

                    
        $pageData['Blogs'] = $this->db->query("SELECT BL.blId, CA.$theTitles, BL.$theTitle, BL.$theText, BL.dateTime, BL.thePhoto, BL.categoryId FROM blog BL INNER JOIN blog_categories CA "
                . " ON CA.caId = BL.categoryId ORDER BY BL.dateTime DESC LIMIT 10")->result_object();    

            $this->load->view("common/header$indexID", $this->headerData);
            $this->load->view("content/index$indexID", $pageData);
            $this->load->view("common/footer$indexID", $this->footerData);
        }
    }

    public function about_us() {
        $pageData = array();
        $theLang = $this->session->userdata("lang");
        $aboutID = 1;
        if ($theLang == "en") {$langId = 2;} else if ($theLang == "tr") {$langId = 3;} else {$langId = 1;}        
        if($langId == 1){$theTitle = "theTitle1 as theTitle";}elseif($langId == 3){$theTitle = "theTitle3 as theTitle";}else{$theTitle = "theTitle2 as theTitle";}                
        if($langId == 1){$theText = "theText1 as theText";}elseif($langId == 3){$theText = "theText3 as theText";}else{$theText = "theText2 as theText";}   
        
        $pageData['aboutUs'] = $this->db->query("SELECT abId, $theTitle, $theText, thePhoto FROM about_us WHERE abId < 19 ")->result_object();

        $this->headerData['pageTitle'] = $this->Get_Lang->gl("about", $langId);
        
        $this->load->view("common/header", $this->headerData);
        $this->load->view("content/about-$aboutID", $pageData);
        $this->load->view("common/footer", $this->footerData);
    }
    
    
    public function privacy($id = 0) {
        $pageData = array();
        $theLang = $this->session->userdata("lang");
        if ($theLang == "en") {$langId = 2;} else if ($theLang == "tr") {$langId = 3;} else {$langId = 1;}        
        if($langId == 1){$theTitle = "theTitle1 as theTitle";}elseif($langId == 3){$theTitle = "theTitle3 as theTitle";}else{$theTitle = "theTitle2 as theTitle";}                
        if($langId == 1){$theText = "theText1 as theText";}elseif($langId == 3){$theText = "theText3 as theText";}else{$theText = "theText2 as theText";}                
        
        $pageData['privacy'] = $this->db->query("SELECT abId, $theTitle, $theText FROM about_us WHERE abId = '$id'")->result_object();
        if($pageData['privacy']){
            $this->headerData['pageTitle'] = $pageData['privacy'][0]->theTitle;
        }else{
            header("Location:/");
            exit();
        }
        
        $this->load->view('common/header', $this->headerData);
        $this->load->view('content/privacy', $pageData);
        $this->load->view('common/footer', $this->footerData);
    }

    public function blog() {
        $pageData = array();
        $theLang = $this->session->userdata("lang");
        $blogID = 3;
        if ($theLang == "en") {$langId = 2;} else if ($theLang == "tr") {$langId = 3;} else {$langId = 1;}        
        if($langId == 1){$theTitle = "theTitle1 as theTitle";}elseif($langId == 3){$theTitle = "theTitle3 as theTitle";}else{$theTitle = "theTitle2 as theTitle";}                
        if($langId == 1){$theText = "theText1 as theText";}elseif($langId == 3){$theText = "theText3 as theText";}else{$theText = "theText2 as theText";}  
        
        $pageData['blogCategories'] = $this->db->query("SELECT caId, $theTitle FROM blog_categories ORDER BY caId DESC")->result_object();
        $pageData['blog'] = $this->db->query("SELECT blId, $theTitle, $theText, dateTime, thePhoto, categoryId FROM blog ORDER BY dateTime DESC")->result_object();
        
        
        $this->headerData['pageTitle'] = $this->Get_Lang->gl("blog", $langId);

        $this->load->view("common/header", $this->headerData);
        $this->load->view("content/blog-$blogID", $pageData);
        $this->load->view("common/footer", $this->footerData);
    }
    
    public function blog_details($blId = 0) {
        $pageData = array();
        $theLang = $this->session->userdata("lang");
        $blogID = 3;
        if ($theLang == "en") {$langId = 2;} else if ($theLang == "tr") {$langId = 3;} else {$langId = 1;}        
        if($langId == 1){$theTitle = "theTitle1 as theTitle";}elseif($langId == 3){$theTitle = "theTitle3 as theTitle";}else{$theTitle = "theTitle2 as theTitle";}                
        if($langId == 1){$theTitles = "theTitle1 as theTitles";}elseif($langId == 3){$theTitles = "theTitle3 as theTitles";}else{$theTitles = "theTitle2 as theTitles";}                
        if($langId == 1){$theText = "theText1 as theText";}elseif($langId == 3){$theText = "theText3 as theText";}else{$theText = "theText2 as theText";}  
        
        $pageData['blog'] = $this->db->query("SELECT blId, $theTitle, $theText, dateTime, thePhoto, categoryId FROM blog WHERE blId = '$blId' ")->result_object();
        if(empty($pageData['blog'])){
            header("Location:/error");
           exit(); 
        }
        $categoryId = $pageData['blog'][0]->categoryId;
        $this->headerData['pageTitle'] = $pageData['blog'][0]->theTitle;
        $pageData['blogCategories'] = $this->db->query("SELECT caId, $theTitle FROM blog_categories WHERE caId = '$categoryId'")->result_object();
        
        $pageData['otherBlog'] = $this->db->query("SELECT BL.blId, CA.$theTitles, BL.$theTitle, BL.dateTime, BL.thePhoto, BL.categoryId FROM blog BL INNER JOIN blog_categories CA "
                . " ON CA.caId = BL.categoryId WHERE BL.blId != '$blId' ORDER BY BL.dateTime DESC LIMIT 10")->result_object();
        
        $pageData['sameBlog'] = $this->db->query("SELECT BL.blId, CA.$theTitles, BL.$theTitle, BL.dateTime, BL.thePhoto, BL.categoryId FROM blog BL INNER JOIN blog_categories CA "
                . " ON CA.caId = BL.categoryId WHERE BL.blId != '$blId' AND BL.categoryId = '$categoryId' ORDER BY BL.dateTime ASC LIMIT 10")->result_object();
        
        

        $this->load->view("common/header", $this->headerData);
        $this->load->view("content/blog-details-$blogID", $pageData);
        $this->load->view("common/footer", $this->footerData);
    }

    public function theCategoriesSeries($caId = 0, $title = " ") {
        $theLang = $this->session->userdata("lang");
        if ($theLang == 'tr') { $langId = 3;$pageTitle="Kategoriler";} else if ($theLang == 'en'){$langId = 2;$pageTitle="Categories";}else{ $langId = 1;$pageTitle="الأقسام الرئيسية";}         
        if($langId == 1){$theTitle = "theTitle1 as theTitle";}elseif($langId == 3){$theTitle = "theTitle3 as theTitle";}else{$theTitle = "theTitle2 as theTitle";}                
        $parent = $this->db->query("SELECT caId, parentId, $theTitle FROM categories WHERE caId = '$caId'")->result_object();
    if (isset($parent[0]->parentId)) { 
        if($parent[0]->parentId > 0) { 
            $title = $parent[0]->theTitle . " > ".$title;
            return $this->theCategoriesSeries($parent[0]->parentId, $title);
        } else {
            $title = $parent[0]->theTitle." > ".$title;
            return $title;
        }
    } else {
        $title = $parent[0]->theTitle." > ".$title;
        return $title;
    }
}

    public function theParentMain($caId = 0) {
        $parent = $this->db->query("SELECT caId, parentId FROM categories WHERE caId = '$caId'")->result_object();
        if ($parent) {
            if ($parent[0]->parentId > 0) {
                return $this->theParentMain($parent[0]->parentId);
            } else {
                return $parent[0]->caId;
            }
        }
    }    
    
    public function category($categoryId = 0) {
        $pageData = array();
        $pageData['categoryId']= $categoryId;
        
        $theLang = $this->session->userdata("lang");
        $categoryID = 6;
        if ($theLang == 'tr') { $langId = 3;$pageTitle="Kategoriler";} else if ($theLang == 'en'){$langId = 2;$pageTitle="Categories";}else{ $langId = 1;$pageTitle="الأقسام الرئيسية";} 
        if($langId == 1){$theTitle = "theTitle1 as theTitle";}elseif($langId == 3){$theTitle = "theTitle3 as theTitle";}else{$theTitle = "theTitle2 as theTitle";}                
        
        $this->headerData['pageTitle'] = "$pageTitle";        
        
        $pageData['checkCategories'] = $this->db->query("SELECT *, $theTitle FROM categories WHERE parentId = '0' AND isDeleted  = 0 ")->result_object();

        
    if($categoryId > 0){
        $pageData['mainCaId'] = $this->theParentMain($categoryId);
        $pageData['thepageName'] = $this->theCategoriesSeries($categoryId, " ");
        $categories = $this->db->query("SELECT *, $theTitle FROM categories WHERE caId = '$categoryId' AND isDeleted  = 0 ")->result_object();
        $categoriesCount = $this->db->query("SELECT count(1) as cnt FROM categories WHERE caId = '$categoryId' AND isDeleted  = 0 ")->result();
        if ($categories) {
            $this->headerData['pageTitle'] = $categories[0]->theTitle;
            $pageData['count'] = $categoriesCount[0]->cnt;

            $pageData['checkCategories'] = $this->db->query("SELECT *, $theTitle FROM categories WHERE parentId = '$categoryId' AND isDeleted  = 0 ")->result_object();
            if (!$pageData['checkCategories']) {
                $categoriesDetails = $this->db->query("SELECT caId, categoryId FROM category_details WHERE categoryId = '$categoryId' AND isDeleted  = 0 ")->result();
                if ($categoriesDetails) {
                    $this->product_details2($categoriesDetails[0]->categoryId);
                } else {
                //    redirect($_SERVER['HTTP_REFERER']);
                    
                    $this->load->view("common/header", $this->headerData);
                    $this->load->view("content/category-$categoryID", $pageData);
                    $this->load->view("common/footer", $this->footerData);
                }
            }else{
                $this->load->view("common/header", $this->headerData);
                $this->load->view("content/category-$categoryID", $pageData);
                $this->load->view("common/footer", $this->footerData);
            }
        }else{
            $this->load->view("common/header", $this->headerData);
            $this->load->view("content/category-$categoryID", $pageData);
            $this->load->view("common/footer", $this->footerData);
        }
    }else{
        $this->load->view("common/header", $this->headerData);
        $this->load->view("content/category-$categoryID", $pageData);
        $this->load->view("common/footer", $this->footerData);
    }
        
    }

    public function product_details($categoryId = 0) {
        $pageData = array();
        $theLang = $this->session->userdata("lang");
        $productID = 1;
        if ($theLang == 'tr') { $langId = 3;$pageTitle="Ürünler";} else if ($theLang == 'en'){$langId = 2;$pageTitle="Products";}else{ $langId = 1;$pageTitle=" المنتجات";} 
        $this->headerData['pageTitle'] = "$pageTitle"; 

        $pageData['thepageName'] = $this->theCategoriesSeries($categoryId, " ");

        $pageData['mainCaId'] = $this->theParentMain($categoryId);

        $pageData['categoryId'] = $categoryId;
        if($langId == 1){$theTitle = "theTitle1 as theTitle";}elseif($langId == 3){$theTitle = "theTitle3 as theTitle";}else{$theTitle = "theTitle2 as theTitle";}                
        if($langId == 1){$theText = "theText1 as theText";}elseif($langId == 3){$theText = "theText3 as theText";}else{$theText = "theText2 as theText";}                
        if($langId == 1){$text = "text1 as text";}elseif($langId == 3){$text = "text3 as text";}else{$text = "text2 as text";}                
        if($langId == 1){$pageDesc = "pageDesc1 as pageDesc";}elseif($langId == 3){$pageDesc = "pageDesc3 as pageDesc";}else{$pageDesc = "pageDesc2 as pageDesc";}                

        
        $pageData['categoryDetails'] = $this->db->query("SELECT CA.*, CAD.*, CA.$theTitle, CAD.$theText, CAD.$text, CA.$pageDesc, CAD.caId as subId FROM categories CA INNER JOIN category_details CAD
           WHERE CA.caId = '$categoryId' AND CA.isDeleted  = 0 AND CAD.categoryId = '$categoryId' AND CAD.isDeleted = 0 ")->result_object();
        if ($pageData['categoryDetails']) {
            $this->headerData['pageTitle'] = $pageData['categoryDetails'][0]->theTitle;

            $subId = $pageData['categoryDetails'][0]->subId;

            $pageData['categorySlider'] = $this->db->query("SELECT * FROM sliders WHERE detailsId = '$subId' ORDER BY slId ASC ")->result_object();

//            $pageData['categoryOptions'] = $this->db->query("SELECT * FROM options WHERE detailsId = '$subId' ")->result_object();
            $pageData['photoSlider'] =  $this->db->query("SELECT slId, detailsId, thePhoto FROM sliders WHERE detailsId = '$subId' ORDER BY slId ASC ")->result_object();
            
            
            $parentId = $pageData['categoryDetails'][0]->parentId;
            $pageData['otherCategories'] = $this->db->query("SELECT *, $theTitle FROM categories WHERE caId != '$categoryId' AND parentId = '$parentId' AND isDeleted  = 0 AND langId = '$langId' ORDER BY rand() LIMIT 3")->result_object();
        } else {
            header("Location:/");
        }

        if (isset($pageData['categoryDetails'][0]->theText)) {
            $this->headerData['pageDescription'] = $pageData['categoryDetails'][0]->pageDesc;
            $this->headerData['theShareContent'] = strip_tags(word_limiter($pageData['categoryDetails'][0]->theText, 20));
        }
        if (isset($pageData['categoryDetails'][0]->thePhoto)) {
            $this->headerData['theSharePhoto'] = base_url() . "public/uploads/php/files/categories/" . $pageData['categoryDetails'][0]->thePhoto;
        }
        $this->headerData['theShareTitle'] = $this->headerData['companyName'] . $this->headerData['pageTitle'];
        
        
        $this->load->view("common/header", $this->headerData);
        $this->load->view("content/products-$productID", $pageData);
        $this->load->view("common/footer", $this->footerData);
    }


    public function checkout() {
        $pageData = array();
        $theLang = $this->session->userdata("lang");
        if ($theLang == "en") {$langId = 2;} else if ($theLang == "tr") {$langId = 3;} else {$langId = 1;}        
       
        $this->headerData['pageTitle'] = $this->Get_Lang->gl("Checkout", $langId);

        $this->load->view('common/header', $this->headerData);
        $this->load->view('content/checkout', $pageData);
        $this->load->view('common/footer', $this->footerData);
    }

    public function contact_us() {
        $pageData = array();
        $theLang = $this->session->userdata("lang");
        $contactID = 1;
        if ($theLang == "en") {$langId = 2;} else if ($theLang == "tr") {$langId = 3;} else {$langId = 1;}        
        if($langId == 1){$theText = "theText1 as theText";}elseif($langId == 3){$theText = "theText3 as theText";}else{$theText = "theText2 as theText";}                
        $pageData['photo'] = $this->db->query("SELECT thePhoto FROM about_us WHERE abId = 1")->result_object();
        
        $pageData['contact'] = $this->db->query("SELECT companyName, $theText, emailAddress, emailAddress2, phoneNumber, phoneNumber2, latitude, longitude FROM contact_us ")->result_object();

        
        $this->headerData['pageTitle'] = $this->Get_Lang->gl("contact_us", $langId);

        $this->load->view("common/header", $this->headerData);
        $this->load->view("content/contact-$contactID", $pageData);
        $this->load->view("common/footer", $this->footerData);
    }

    public function dashboard() {
        $pageData = array();
        $theLang = $this->session->userdata("lang");
        if ($theLang == "en") {$langId = 2;} else if ($theLang == "tr") {$langId = 3;} else {$langId = 1;}        
       
        $this->headerData['pageTitle'] = $this->Get_Lang->gl("Dashboard", $langId);

        $this->load->view('common/header', $this->headerData);
        $this->load->view('content/dashboard', $pageData);
        $this->load->view('common/footer', $this->footerData);
    }

    public function cart() {
        $pageData = array();
        $theLang = $this->session->userdata("lang");
        if ($theLang == "en") {$langId = 2;} else if ($theLang == "tr") {$langId = 3;} else {$langId = 1;}        
       
        $this->headerData['pageTitle'] = $this->Get_Lang->gl("cart", $langId);

        $this->load->view('common/header', $this->headerData);
        $this->load->view('content/cart', $pageData);
        $this->load->view('common/footer', $this->footerData);
    }

    public function faq() {
        $pageData = array();
        $theLang = $this->session->userdata("lang");
        if ($theLang == "en") {$langId = 2;} else if ($theLang == "tr") {$langId = 3;} else {$langId = 1;}        
        if($langId == 1){$theTitle = "theTitle1 as theTitle";}elseif($langId == 3){$theTitle = "theTitle3 as theTitle";}else{$theTitle = "theTitle2 as theTitle";}                
        if($langId == 1){$theText = "theText1 as theText";}elseif($langId == 3){$theText = "theText3 as theText";}else{$theText = "theText2 as theText";}   
        $pageData['question'] = $theTitle;
        $pageData['answer'] = $theText;
        
        $this->headerData['pageTitle'] = $this->Get_Lang->gl("faq", $langId);
        
       $pageData['faq'] = $this->db->query("SELECT caId, $theTitle FROM faq_categories")->result_object();
        

        $this->load->view('common/header', $this->headerData);
        $this->load->view('content/faq', $pageData);
        $this->load->view('common/footer', $this->footerData);
    }

    public function login() {
        $pageData = array();
        $theLang = $this->session->userdata("lang");
        if ($theLang == "en") {$langId = 2;} else if ($theLang == "tr") {$langId = 3;} else {$langId = 1;}        
       
        $this->headerData['pageTitle'] = $this->Get_Lang->gl("login", $langId);

        $this->load->view('common/header', $this->headerData);
        $this->load->view('content/login', $pageData);
        $this->load->view('common/footer', $this->footerData);
    }

    public function wishlist() {
        $pageData = array();
        $theLang = $this->session->userdata("lang");
        if ($theLang == "en") {$langId = 2;} else if ($theLang == "tr") {$langId = 3;} else {$langId = 1;}        
       
        $this->headerData['pageTitle'] = $this->Get_Lang->gl("wishlist", $langId);

        $this->load->view('common/header', $this->headerData);
        $this->load->view('content/wishlist', $pageData);
        $this->load->view('common/footer', $this->footerData);
    }

    public function quickView() {
        $pageData = array();

        $this->load->view('content/popup/quickView', $pageData);
    }

    public function error() {
        $pageData = array();
        $theLang = $this->session->userdata("lang");
        if ($theLang == "en") {$langId = 2;} else if ($theLang == "tr") {$langId = 3;} else {$langId = 1;}        
       
        $this->headerData['pageTitle'] = $this->Get_Lang->gl("error", $langId);

        $this->load->view('common/header', $this->headerData);
        $this->load->view('content/error', $pageData);
        $this->load->view('common/footer', $this->footerData);
    }

    public function admin() {
        header("Location:/admin/login");
        exit();
    }

    public function logout() {
        $this->session->sess_destroy();
        header("Location: /");
    }

    function ajax($param1, $param2 = NULL, $param3 = NULL) {
        switch ($param1) {

            case "changeLanguage":
                $lang = $this->Checkdata->checkInputData($this->input->post("lang"));
                if ($lang == 'ar') {
                    $session = array(
                        'lang' => 'ar'
                    );
                    $this->session->set_userdata($session);
                } else if ($lang == 'tr') {
                    $session = array(
                        'lang' => 'tr'
                    );
                    $this->session->set_userdata($session);
                } else {
                    $session = array(
                        'lang' => 'en'
                    );
                    $this->session->set_userdata($session);
                }
                echo 1;
                break;


            case "sendApply":
                $this->load->helper('form');
                $this->load->library('form_validation');
                $theLang = $this->session->userdata("lang");
                if ($theLang == "en") {
                    $langId = 2;
                } else if ($theLang == "tr") {
                    $langId = 3;
                } else {
                    $langId = 1;
                }

                $this->form_validation->set_rules('name', $this->Get_Lang->gl("Full_Name", $langId), 'required');
                $this->form_validation->set_rules('email', $this->Get_Lang->gl("email_address", $langId), 'required|valid_email');
                $this->form_validation->set_rules('message', $this->Get_Lang->gl("message", $langId), 'required');

                if ($this->form_validation->run() === FALSE) {
                    echo validation_errors();
                } else {
                    $processArr = array();
                    $toDataBase = array();

                    $processArr[$this->Get_Lang->gl("Full_Name", $langId)] = $this->Checkdata->checkInputData($this->input->post("name"));
//                    $processArr["Cep_Telefon"] = $this->Checkdata->checkInputData($this->input->post("phone"));
                    $processArr[$this->Get_Lang->gl("email_address", $langId)] = $this->Checkdata->checkInputData($this->input->post("email"));
                    $processArr[$this->Get_Lang->gl("Subject", $langId)] = $this->Checkdata->checkInputData($this->input->post("subject"));
                    $processArr[$this->Get_Lang->gl("message", $langId)] = $this->input->post("message");

                    $toDataBase['theName'] = $this->Checkdata->checkInputData($this->input->post("name"));
//                    $toDataBase['phoneNumber'] = $processArr["Cep_Telefon"];
                    $toDataBase['emailAddress'] = $this->Checkdata->checkInputData($this->input->post("email"));
                    $toDataBase['howToContact'] = $this->Checkdata->checkInputData($this->input->post("subject"));
                    $toDataBase['note'] = $this->input->post("message");

                    $this->db->insert("contactus_form", $toDataBase);
                    $emailAddress = $toDataBase['emailAddress'];
                    $name = $toDataBase['theName'];

                    $subject = $this->Checkdata->checkInputData($this->input->post("subject"));
                    $htmlBody = "<table>";
                    foreach ($processArr as $key => $value) {
                        $htmlBody .= '<tr><td> ' . str_replace("_", " ", $key) . ': </td><td>' . $processArr[$key] . '</td></tr>';
                    }
                    $htmlBody .= "</table>";

                    $getEmail = $this->db->query("SELECT emailAddress FROM contact_us WHERE coId = 1")->result_object();
                    if ($getEmail[0]->emailAddress) {
                        $theEmail = $getEmail[0]->emailAddress;
                    } else {
                        $theEmail = "ahmedof592@gmail.com";
                    }

                    $this->load->library('email');
                    $config = Array(
                        'protocol' => 'smtp',
                        'smtp_host' => 'mail.tibtop.com.tr',
                        'smtp_port' => 25,
                        'smtp_timeout' => '5',
                        'smtp_crypto' => 'tls',
                        'smtp_user' => 'info@tibtop.com.tr',
                        'smtp_pass' => 'Info99',
                        'mailtype' => 'html',
                        'charset' => 'iso-8859-1'
                    );
                    $this->load->library('email', $config);
                    $result = $this->email
                            ->from("$emailAddress", "$name")
                            ->to("$theEmail")
                            ->bcc("ahmedof592@gmail.com")
                            ->subject($subject)
                            ->message($htmlBody);
                    if ($this->email->send())
                        echo 1;
                    else
                        echo $this->email->print_debugger();
                }
                break;

            case "subscribers":

                $this->load->helper('form');
                $this->load->library('form_validation');

                $this->form_validation->set_rules('subemail', 'Email', 'required|valid_email');

                if ($this->form_validation->run() === FALSE) {
                    echo validation_errors();
                } else {
                    $theEmail = $this->input->post("subemail");
                    $processArr = array();

                    $checkEmail = $this->db->query("SELECT suId FROM subscribers WHERE emailAddress = '$theEmail'")->result_object();

                    if ($checkEmail) {
                        echo $this->Get_Lang->gl("already_exists", $langId)."...";
                    } else {
                        $processArr['emailAddress'] = $theEmail;
                        $this->db->insert("subscribers", $processArr);
                        echo 1;
                    }
                }
                break;


 case "register":
                $this->load->helper('form');
                $this->load->library('form_validation');

                $this->form_validation->set_rules('registerName2', 'First Name', 'required');
                $this->form_validation->set_rules('registerPhoneNumber2', 'Phone Number', 'required');
                $this->form_validation->set_rules('registerCountry2', 'Country', 'required');
                $this->form_validation->set_rules('registerEmailAddress2', 'E-mail', 'required|valid_email');
                $this->form_validation->set_rules('registerPassword2', 'password', 'required');

                if ($this->form_validation->run() === FALSE) {
                    echo validation_errors();
                } else {
                $theLang = $this->session->userdata("lang");
                if ($theLang == "en") {$langId = 2;} else if ($theLang == "tr") {$langId = 3;} else {$langId = 1;}    
                
                    $firstName = $this->Checkdata->checkInputData($this->input->post("registerName2"));
                    $lastName = $this->Checkdata->checkInputData($this->input->post("registerLastName2"));
                    $phone = $this->Checkdata->checkInputData($this->input->post("registerPhoneNumber2"));
                    $country = $this->Checkdata->checkInputData($this->input->post("registerCountry2"));
                    $emailAddress = $this->Checkdata->checkInputData($this->input->post("registerEmailAddress2"));
                    $thePassword = $this->Checkdata->checkInputData($this->input->post("registerPassword2"));

                    $check = $this->db->query("select count(1) as cnt FROM members where emailAddress = '" . $emailAddress . "' ")->result();
                    if ($check[0]->cnt > 0) {
                            echo $this->Get_Lang->gl("email_already_registered", $langId);
                        
                    } else {
                        $processArr = array();
                        $processArr2 = array();
                        $processArr['firstName'] = $firstName;
                        $processArr['lastName'] = $lastName;
                        $processArr['emailAddress'] = $emailAddress;
                        $processArr['phoneNumber'] = $phone;
                        $processArr['country'] = $country;
                        $processArr['thePassword'] = sha1($thePassword);
                        $processArr['date'] = Date("Y-m-d H:i:s");

                        $this->db->insert("members", $processArr);
                        $insertId = $this->db->insert_id();

                        $session = array(
                            'meId' => $insertId,
                            'firstName' => $firstName
                        );
                        $this->session->set_userdata($session);
//                        $sessionIdValue = $this->session->userdata("_ci_session_orders");
//                        $orderNum = $this->db->query("SELECT count(1) as cnt FROM orders WHERE sessionIdValue = '$sessionIdValue' AND orderNumber = 0")->result_object();
//                        if (isset($orderNum)) {
//                            $processArr2['memberId'] = $insertId;
//                            $processArr2['sessionIdValue'] = 0;
//                            $this->db->update("orders", $processArr2, array('sessionIdValue' => $sessionIdValue));
//                        }
                        echo 1;
                    }
                }

                break;

            case "editProfile":
                $this->load->helper('form');
                $this->load->library('form_validation');

                $this->form_validation->set_rules('firstName', 'First Name', 'required');
                $this->form_validation->set_rules('lastName', 'Last Name', 'required');
                $this->form_validation->set_rules('phone', 'Phone Number', 'required');
                $this->form_validation->set_rules('country', 'Country', 'required');
                $this->form_validation->set_rules('email', 'E-mail', 'required|valid_email');
                $this->form_validation->set_rules('password', 'password', 'required');

                if ($this->form_validation->run() === FALSE) {
                    echo validation_errors();
                } else {
                    $meId = $this->session->userdata("meId");

                    $firstName = $this->Checkdata->checkInputData($this->input->post("firstName"));
                    $lastName = $this->Checkdata->checkInputData($this->input->post("lastName"));
                    $phone = $this->Checkdata->checkInputData($this->input->post("phone"));
                    $country = $this->Checkdata->checkInputData($this->input->post("country"));
                    $emailAddress = $this->Checkdata->checkInputData($this->input->post("email"));
                    $thePassword = $this->Checkdata->checkInputData($this->input->post("password"));

                    $processArr = array();
                    $processArr['firstName'] = $firstName;
                    $processArr['lastName'] = $lastName;
                    $processArr['emailAddress'] = $emailAddress;
                    $processArr['phoneNumber'] = $phone;
                    $processArr['countryId'] = $country;


                    $checkPassword = $this->db->query("SELECT thePassword FROM members WHERE meId = $meId ")->result_object();
                    if ($checkPassword) {
                        if ($checkPassword[0]->thePassword != $this->input->post("password")) {
                            $processArr['thePassword'] = sha1($thePassword);
                        }
                    }

                    $this->db->update("members", $processArr, array('meId' => $meId));

                    $session = array(
                        'firstName' => $firstName
                    );
                    $this->session->set_userdata($session);
                    echo 1;
                }

                break;

            case "login":
                $this->load->helper('form');
                $this->load->library('form_validation');

                $this->form_validation->set_rules('email', 'E-mail', 'required|valid_email');
                $this->form_validation->set_rules('password', 'password', 'required');
                $processArr2 = array();

                if ($this->form_validation->run() === FALSE) {
                    echo validation_errors();
                } else {

                    $emailAddress = $this->Checkdata->checkInputData($this->input->post("email_login"));
                    $thePassword = $this->Checkdata->checkInputData($this->input->post("password_login"));

                    $check = $this->db->query("select count(1) as cnt FROM members where emailAddress = '" . $emailAddress . "' and thePassword = '" . sha1($thePassword) . "'")->result();
                    $check2 = $this->db->query("select count(1) as cnt FROM members where emailAddress = '" . $emailAddress . "' ")->result();
                    if ($check[0]->cnt > 0) {
                        $checkActivity = $this->db->query("select count(1) as cnt FROM members where emailAddress = '" . $emailAddress . "' and thePassword = '" . sha1($thePassword) . "' AND isActive = 1")->result();
                        if ($checkActivity[0]->cnt > 0) {
                            $studentsData = $this->db->query("SELECT meId ,firstName FROM members WHERE emailAddress = '" . $emailAddress . "' and thePassword = '" . sha1($thePassword) . "'")->result_array();
                            $session = array(
                                'meId' => $studentsData[0]["meId"],
                                'firstName' => $studentsData[0]["firstName"]
                            );
                            $this->session->set_userdata($session);
                            $sessionIdValue = $this->session->userdata("_ci_session_orders");
                            $memID = $studentsData[0]["meId"];
                            $orderNum = $this->db->query("SELECT orId,productId,colorId,sizeId FROM orders WHERE sessionIdValue = '$sessionIdValue' AND orderNumber = 0")->result_object();
                            if (isset($orderNum)) {
                                foreach ($orderNum as $key => $value) {
                                    $productId = $value->productId;
                                    $colorId = $value->colorId;
                                    $sizeId = $value->sizeId;
                                    $checkIfThisProductIdAdded = $this->db->query("SELECT COUNT(*) as cartCount FROM orders WHERE orderNumber = 0 AND memberId = $memID AND productId = $productId AND colorId = $colorId AND sizeId = $sizeId")->result_object();
                                    if ($checkIfThisProductIdAdded[0]->cartCount > 0) {
                                        $this->db->delete("orders", array('orId' => $value->orId));
                                    } else {
                                        $processArr2['memberId'] = $memID;
                                        $processArr2['sessionIdValue'] = 0;
                                        $this->db->update("orders", $processArr2, array('orId' => $value->orId));
                                    }
                                }
                            }
                            echo 1;
                        } else {
                            $theLang = $this->session->userdata("lang");
                            if ($theLang == 'ar') {
                                echo "عذرا هذا الإيميل غير مفعل يرجى التواصل مع الإدارة";
                            } else if ($theLang == 'tr') {
                                echo "Üzgünüz, bu e-posta etkin değil Lütfen yönetim ile iletişime geçin";
                            } else {
                                echo "Sorry, this email is not activated Please contact the administration";
                            }
                        }
                    } else if ($check2[0]->cnt > 0) {
                        $theLang = $this->session->userdata("lang");
                        if ($theLang == 'ar') {
                            echo "هنالك خطأ في الايميل أو كلمة السر";
                        } else if ($theLang == 'tr') {
                            echo "E-posta Veya Şifre yanlış";
                        } else {
                            echo "The Email Address Or Password İncorrect...";
                        }
                    } else {

                        $theLang = $this->session->userdata("lang");
                        if ($theLang == 'ar') {
                            echo "عليك التسجيل أولا";
                        } else if ($theLang == 'tr') {
                            echo "daha önce kayıt olmalısınız...";
                        } else {
                            echo "you have to register before";
                        }
                    }
                }

                break;





            case "contactUs" :
                $name = $this->input->post("contact_name");
                $emailAddress = $this->input->post("contact_email");
                $phoneNumber = $this->input->post("contact_phone");
                $subject = $this->input->post("contact_subject");
                $message = $this->input->post("contact_message");

                $insert = array();

                $insert['theName'] = $name;
                $insert['emailAddress'] = $emailAddress;
                $insert['phoneNumber'] = $phoneNumber;
                $insert['howToContact'] = $subject;
                $insert['note'] = $message;
                $insert['date'] = Date("Y-m-d");

                $this->db->insert("contactus_form", $insert);

                $subject = " New Contact Us Message ";
                $htmlBody = " New Message has been sucessfully sent to Contact Us Form List
                        <table style='text-align: center;'> <thead class='cf'>";
                foreach ($insert as $key => $value) {
                    $htmlBody .= "<tr><td>" . $key . "</td><td>" . $value . "</td></tr>";
                }

                $htmlBody .= " <th> You Can See It From Admin Control Panel...</th></tr></thead>
                        <tbody>";

                $getEmail = $this->db->query("SELECT emailAddress FROM contact_us WHERE coId = 1")->result_object();
                if ($getEmail[0]->emailAddress) {
                    $theEmail = $getEmail[0]->emailAddress;
                } else {
                    $theEmail = "ahmedof592@gmail.com";
                }

                $htmlBody .= "</tbody></table>";
                $this->load->library('email');
                $config = Array(
                    'protocol' => 'smtp',
                    'smtp_host' => 'mail.masasouq.com',
                    'smtp_port' => 25,
                    'smtp_timeout' => '5',
                    'smtp_crypto' => 'tls',
                    'smtp_user' => 'info@masasouq.com',
                    'smtp_pass' => 'InfoInfo99',
                    'mailtype' => 'html',
                    'charset' => 'iso-8859-1'
                );
                $this->load->library('email', $config);
                $result = $this->email
                        ->from("$emailAddress")
                        ->to("$theEmail")
                        ->bcc("ahmedof592@gmail.com")
                        ->subject($subject)
                        ->message($htmlBody);
                $this->email->send();
                echo 1;
                break;
        }
    }

}
