<?php
/**
 * @package    Proxim
 * @author     Mike Kivu <mikepaul620@gmail.com | https://webexpert.co.ke>
 * @copyright  2019 Proxim
 * @version    1.5.0
 * @since      File available since Release 1.0.0
 */

namespace Proxim\Dashboard;

use Db;
use Proxim\Database\DbQuery;
use Proxim\Controller;
use Proxim\Pager;
use Proxim\Site\Site;
use Proxim\Configuration;
use Proxim\Order\Order;
use Proxim\User\Employee;
use Proxim\User\Customer;
use Proxim\Validate;
use Proxim\Tools;
use Proxim\Util\DateUtils;
use Proxim\Util\ArrayUtils;

class Mailing extends Controller {

    public function mailing( $sub_view = "send", $id = null ) {
        $app = $this->app;
        $user = $app->container->user;
        $params = $app->request->get();
        $smarty = $app->container->smarty;

        switch ($sub_view) {
            case 'mails':
                $selected_page = ArrayUtils::has($params, 'page') ? (int) ArrayUtils::get($params, 'page') : 1;

                $this->setVars([
                    'page' => [
                        'title' => "Mailing › Mails"
                    ],
                ]);

                $mails = array();

                $sql = new DbQuery();
                $sql->select('m.*');
                $sql->from('mailing_list', 'm');
                $result = Db::getInstance(PROX_USE_SQL_SLAVE)->executeS($sql);
                $total_mails = count($result);

                if( $total_mails > 0) {
                    $params['total_items'] = $total_mails;
                    $params['selected_page'] = $selected_page;
                    $params['items_per_page'] = Configuration::get('MAX_RESULTS', null, 10)*2;
                    $params['url'] = $app->base_uri ."/mailing/mails?page=%s";
        
                    $pager = new Pager( $params );
                    $limit_query = $pager->getLimitSql();

                    $result = Db::getInstance(PROX_USE_SQL_SLAVE)->executeS($sql . $limit_query);
                    foreach( $result as $mail ) {
                        $mail['site_name'] = Configuration::get('SITE_NAME', (int) $mail['site_id']);
                        $mail['date'] = DateUtils::convertToISOFormat( $mail['date_add'] );
                        $mails[] = $mail;
                    }


                    $smarty->assign([
                        'pager' => $pager->getPager()
                    ]);
                }
        
                $smarty->assign([
                    'total_mails' => $total_mails,
                    'mails' => $mails
                ]);
                break;

            case 'send':
                $this->setVars([
                    'page' => [
                        'title' => "Mailing › Send"
                    ],
                ]);
        
                $sql = new DbQuery();
                $sql->select('s.*');
                $sql->from('site', 's');
                $sites = Db::getInstance(PROX_USE_SQL_SLAVE)->executeS( $sql );
        
                $sql = new DbQuery();
                $sql->select('e.*');
                $sql->from('employee', 'e');
                $writers = Db::getInstance(PROX_USE_SQL_SLAVE)->executeS( $sql );
        
                $customers = array();
                $sql = new DbQuery();
                $sql->select('c.customer_id');
                $sql->from('customer', 'c');
                $result = Db::getInstance(PROX_USE_SQL_SLAVE)->executeS( $sql );
        
                foreach ( $result as $customer ) {
                    $customer = new Customer( (int) $customer['customer_id'] );
                    if(Validate::isLoadedObject($customer)) {
                        $customers[] = (array) $customer;
                    }
                }
        
                $customers = ArrayUtils::groupByKey( $customers, 'site_name' );
        
                $sql = new DbQuery();
                $sql->select('m.*');
                $sql->from('mailing_list', 'm');
                $mailing_list = Db::getInstance(PROX_USE_SQL_SLAVE)->executeS( $sql );
        
                $smarty->assign([
                    'customers' => $customers,
                    'writers' => $writers,
                    'mailing_list' => $mailing_list,
                    'sites' => $sites
                ]);
                break;

            case 'add':
                $this->setVars([
                    'page' => [
                        'title' => "Mailing › Add"
                    ],
                ]);

                $sql = new DbQuery();
                $sql->select('s.*');
                $sql->from('site', 's');
                $sites = Db::getInstance(PROX_USE_SQL_SLAVE)->executeS( $sql );

                $smarty->assign([
                    'sites' => $sites
                ]);
                break;

            case 'edit':
                $sql = new DbQuery();
                $sql->select('m.*');
                $sql->from('mailing_list', 'm');
                $sql->where('mail_id = ' . (int) $id );
                $mail = Db::getInstance(PROX_USE_SQL_SLAVE)->getRow( $sql );

                if(!$mail) {
                    $this->setTemplate('404');
                    $this->display();
                }

                $this->setVars([
                    'page' => [
                        'title' => "Mailing › Edit › " . $mail['mail_id']
                    ],
                ]);

                $sql = new DbQuery();
                $sql->select('s.*');
                $sql->from('site', 's');
                $sites = Db::getInstance(PROX_USE_SQL_SLAVE)->executeS( $sql );

                $smarty->assign([
                    'mail' => $mail,
                    'sites' => $sites
                ]);
                break;

            default:
                $this->setTemplate('404');
                $this->display();
                break;
        }

        $smarty->assign([
            'view' => 'mailing',
            'sub_view' => $sub_view,
        ]);
        
        $this->setTemplate('dashboard/mailing');
        $this->display(); 
    }
}