<?php

class crm extends mainController {

    public function __construct() {
        parent::__construct();
        $this->load->model('crm');
    }

    public function landing() {
        $vars['pageName'] = 'crm';
        $vars['title'] = crmModel::getmsg('crm');
        $vars['system_name'] = crmModel::getmsg('crm');
        //        $vars['breadCrumb'] = [[crmModel::getmsg('crm'), 'crm']];
        $vars['menu_blocks'] = array(
            array('msg' => 'customer_settings', 'prmsn' => '', 'icon' => 'fa fa-home'
                  , 'detail' => array(
                      array('msg' => 'customers', 'prmsn' => 'crm_customers', 'lnk' => SITE_LINK . 'crm/customers', 'icon' => 'fa fa-users')
                      , array('msg' => 'contacts', 'prmsn' => 'crm_contacts', 'lnk' => SITE_LINK . 'crm/contacts', 'icon' => 'fa fa-user-plus')
                      , array('msg' => 'Import_Data', 'prmsn' => 'import_data', 'mdl' => '#UploadExcelFile', 'icon' => 'fa fa-upload')
                  )),
            array('msg' => 'deals', 'prmsn' => '', 'icon' => 'fa fa-list-ol'
                  , 'detail' => array(
                      array('msg' => 'deals', 'prmsn' => 'deals', 'lnk' => SITE_LINK . 'crm/deals', 'icon' => 'fa fa-money')
                      , array('msg' => 'pipeline', 'prmsn' => 'pipeline', 'lnk' => SITE_LINK . 'crm/pipeline', 'icon' => 'fa fa-align-justify')
                      , array('msg' => 'contra', 'prmsn' => 'crm_contra', 'lnk' => SITE_LINK . 'crm/contralist', 'icon' => 'fa fa-bars')
                  )),
            array('msg' => 'activities', 'prmsn' => '', 'icon' => 'fa fa-list-ol'
                  , 'detail' => array(
                      array('msg' => 'calendar', 'prmsn' => 'crm_activity', 'lnk' => SITE_LINK . 'crm/calendar', 'icon' => 'fa fa-calendar')
                  )),
            array('msg' => 'general_guide', 'prmsn' => 'general_guide', 'icon' => 'fa fa-list-ol'
                  , 'detail' => array(
                      array('msg' => 'general_contact', 'prmsn' => 'general_contact', 'lnk' => SITE_LINK . 'crm/general_contact', 'icon' => 'fa fa-user')
                      ,array('msg' => 'phone_guide', 'prmsn' => 'phone_guide', 'lnk' => SITE_LINK . 'crm/phone_guide', 'icon' => 'fa fa-user')
                  )));
        $this->load->view('crm', $vars);
    }

    public function customers() {
        $vars['pageName'] = 'crm_customers';
        $vars['title'] = crmModel::getmsg('customers');
        $vars['system_name'] = crmModel::getmsg('customers');
        $vars['countries'] = CommonFunctions::CommonView('tbl_all_countries');
        $vars['breadCrumb'] = [[crmModel::getmsg('crm'), 'crm'], [crmModel::getmsg('customers'), 'customers']];
        $this->load->view('crm/customers', $vars);
    }

    public function contacts() {
        $vars['pageName'] = 'crm_contacts';
        $vars['title'] = crmModel::getmsg('contacts');
        $vars['system_name'] = crmModel::getmsg('contacts');
        $vars['breadCrumb'] = [[crmModel::getmsg('crm'), 'crm'], [crmModel::getmsg('contacts'), 'contacts']];
        $this->load->view('crm/contacts', $vars);
    }

    public function deals() {
        $vars['pageName'] = 'deals';
        $vars['title'] = crmModel::getmsg('deals');
        $vars['system_name'] = crmModel::getmsg('deals');
        $default_category_id = smc_session()['default_setting']['default_crm_category'];
        $vars['default_category_data'] = array();
        if(!empty($default_category_id)){
            $default_category_name = CommonFunctions::SpecificAttribute('tbl_product_cat',['Cat_ID'],[$default_category_id],'Cat_Name');
            $vars['default_category_data'][]=['id'=>$default_category_id,'text'=>$default_category_name]; 
        }
        $vars['deal_stages'] = CommonFunctions::CommonView('tbl_deals_stage');
        $vars['currencies'] = CommonFunctions::CommonView('tbl_common_currency');
        $vars['breadCrumb'] = [[crmModel::getmsg('crm'), 'crm'], [crmModel::getmsg('deals'), 'deals']];
        $this->load->view('crm/deals', $vars);
    }

    public function pipeline() {
        $vars['pageName'] = 'pipeline';
        $vars['title'] = crmModel::getmsg('pipeline');
        $vars['system_name'] = crmModel::getmsg('pipeline');
        $default_category_id = smc_session()['default_setting']['default_crm_category'];
        $vars['default_category_data'] = array();
        if(!empty($default_category_id)){
            $default_category_name = CommonFunctions::SpecificAttribute('tbl_product_cat',['Cat_ID'],[$default_category_id],'Cat_Name');
            $vars['default_category_data'][]=['id'=>$default_category_id,'text'=>$default_category_name]; 
        }
        $vars['deal_stages'] = CommonFunctions::CommonView('tbl_deals_stage');
        $vars['currencies'] = CommonFunctions::CommonView('tbl_common_currency');
        $vars['breadCrumb'] = [[crmModel::getmsg('crm'), 'crm'], [crmModel::getmsg('pipeline'), 'pipeline']];
        $this->load->view('crm/pipeline', $vars);
    }

    public function calendar() {
        $vars['pageName'] = 'crm_activity';
        $vars['title'] = crmModel::getmsg('calendar');
        $vars['system_name'] = crmModel::getmsg('calendar');
        $vars['breadCrumb'] = [[crmModel::getmsg('crm'), 'crm'], [crmModel::getmsg('calendar'), 'calendar']];
        $this->load->view('crm/calendar', $vars);
    }

    public function import() {
        $vars['pageName'] = 'crm_activity';
        $vars['title'] = crmModel::getmsg('import');
        $vars['system_name'] = crmModel::getmsg('import');
        $vars['breadCrumb'] = [[crmModel::getmsg('crm'), 'crm'], [crmModel::getmsg('import'), 'import']];
        $this->load->view('crm/import', $vars);
    }

    public function customerProfile() {
        $vars['pageName'] = 'view_customer_profile';
        $vars['title'] = crmModel::getmsg('customer_profile');
        $vars['system_name'] = crmModel::getmsg('customer_profile');
        $default_category_id = smc_session()['default_setting']['default_crm_category'];
        $vars['default_category_data'] = array();
        if(!empty($default_category_id)){
            $default_category_name = CommonFunctions::SpecificAttribute('tbl_product_cat',['Cat_ID'],[$default_category_id],'Cat_Name');
            $vars['default_category_data'][]=['id'=>$default_category_id,'text'=>$default_category_name]; 
        }
        preg_match('/\/(\d+)$/', $_SERVER['REQUEST_URI'], $match);
        $url = explode("/", $_GET['url']);
        $vars['org_id'] = $url[count($url) - 1];
        $vars['customer'] = CommonFunctions::CommonView('tbl_customer_organization', ['organization_id'], [$vars['org_id']])[0];
        $vars['deal_stages'] = CommonFunctions::CommonView('tbl_deals_stage');
        $vars['currencies'] = CommonFunctions::CommonView('tbl_common_currency');
        $vars['breadCrumb'] = [[crmModel::getmsg('crm'), 'crm'], [crmModel::getmsg('customers'), 'customers'], [crmModel::getmsg('customer_profile'), 'customerProfile/'.$vars['org_id']]];
        $this->load->view('crm/customerProfile', $vars);
    }

    public function contactProfile() {
        $vars['pageName'] = 'view_contact_profile';
        $vars['title'] = crmModel::getmsg('contact_profile');
        $vars['system_name'] = crmModel::getmsg('contact_profile');
        $default_category_id = smc_session()['default_setting']['default_crm_category'];
        $vars['default_category_data'] = array();
        if(!empty($default_category_id)){
            $default_category_name = CommonFunctions::SpecificAttribute('tbl_product_cat',['Cat_ID'],[$default_category_id],'Cat_Name');
            $vars['default_category_data'][]=['id'=>$default_category_id,'text'=>$default_category_name]; 
        }
        preg_match('/\/(\d+)$/', $_SERVER['REQUEST_URI'], $match);
        $url = explode("/", $_GET['url']);
        $vars['contact_id'] = $url[count($url) - 1];
        $vars['contact'] = CommonFunctions::CommonView('all_contacts', ['contact_id'], [$vars['contact_id']])[0];
        $vars['deal_stages'] = CommonFunctions::CommonView('tbl_deals_stage');
        $vars['currencies'] = CommonFunctions::CommonView('tbl_common_currency');
        $vars['breadCrumb'] = [[crmModel::getmsg('crm'), 'crm'], [crmModel::getmsg('contacts'), 'contacts'], [crmModel::getmsg('contact_profile'), 'customerProfile/'.$vars['contact_id']]];
        $this->load->view('crm/contactProfile', $vars);
    }

    public function dealDetails() {
        $vars['pageName'] = 'view_deal_details';
        $vars['title'] = crmModel::getmsg('deal_details');
        $vars['system_name'] = crmModel::getmsg('deal_details');
        preg_match('/\/(\d+)$/', $_SERVER['REQUEST_URI'], $match);
        $url = explode("/", $_GET['url']);
        $vars['deal_id'] = $url[count($url) - 1];
        $vars['dealDetails'] = CommonFunctions::CommonView('all_deals', ['deal_id'], [$vars['deal_id']])[0];
        $vars['product_category'] = CommonFunctions::CommonView('tbl_product_cat');
        $vars['breadCrumb'] = [[crmModel::getmsg('crm'), 'crm'], [crmModel::getmsg('deals'), 'deals'], [crmModel::getmsg('pipeline'), 'pipeline'], [crmModel::getmsg('deal_details'), 'dealDetails/'.$vars['deal_id']]];
        $this->load->view('crm/dealDetails', $vars);
    }

    public function contralist() {
        $vars['pageName'] = 'crm_contra';
        $vars['title'] = crmModel::getmsg('contra');
        $vars['system_name'] = crmModel::getmsg('contra');
        $vars['currency_list'] = $this->_registry->currency->getAllCurrencies();
        $vars['DefaultCurrencyID'] = $this->_registry->currency->getDefaultCurrency();

        $vars['breadCrumb'] = [[crmModel::getmsg('crm'), 'crm'], [crmModel::getmsg('contra'), 'contralist']];
        $this->load->view('crm/contra-list', $vars);
    }
    public function general_contact() {
        $vars['pageName'] = 'general_contact';
        $vars['title'] = crmModel::getmsg('general_contact');
        $vars['system_name'] = crmModel::getmsg('general_contact');

        $vars['breadCrumb'] = [[crmModel::getmsg('crm'), 'crm'], [crmModel::getmsg('general_contact'), 'general_contact']];
        $this->load->view('crm/general_contact', $vars);
    }
    public function phone_guide() {
        $vars['pageName'] = 'phone_guide';
        $vars['title'] = crmModel::getmsg('phone_guide');
        $vars['system_name'] = crmModel::getmsg('phone_guide');

        $vars['breadCrumb'] = [[crmModel::getmsg('crm'), 'crm'], [crmModel::getmsg('phone_guide'), 'phone_guide']];
        $this->load->view('crm/phone_guide', $vars);
    }

}
