<?php
use Proxim\Application;
use Proxim\Configuration;
use Proxim\Database\DbQuery;
use Proxim\Hook;
use Proxim\Module\Module;
use Proxim\Util\ArrayUtils;

class Overview extends Module
{
    public function __construct()
    {
        $this->name = 'overview';
        $this->icon = 'fa fa-tachometer-alt';
        $this->version = '1.0.0';
        $this->prox_versions_compliancy = array('min' => '1.0.0', 'max' => Application::VERSION);
        $this->author = 'Dominic Tanui';

        $this->bootstrap = true;
        parent::__construct();

        $this->displayName = 'Overview';
        $this->description = 'See key statistics for all your orders, customers, sales all with just a glance!';
    }

    public function checkAccess() {
        $user = $this->application->container->user;
        return $user->is_sub_admin ? false : true;
    }

    public function install()
    {
        return parent::install() && $this->registerHook(['displayAdminNavBarBeforeStart']);
    }

    /**
     * Echoes a template.
     *
     * @param string $templateName Template name
     */
    public function showTemplate($templateName)
    {
        echo $this->getTemplateContent($templateName);
    }

    /**
     * Return a template.
     *
     * @param string $templateName          Template name
     * @param array  $additionnalParameters Additionnal parameters to inject on the Twig template
     *
     * @return string Parsed template
     */
    private function getTemplateContent($templateName, $additionnalParameters = array())
    {
        $this->smarty->assign($additionnalParameters);
        return $this->fetch(__DIR__ . '/views/' . $templateName.'.tpl');
    }

    public function hookDisplayAdminNavBarBeforeStart() 
    {
        return $this->showTemplate('nav_item');
    }

    public function getContent()
    {
        $smarty = $this->smarty;
        $user = $this->application->container->user;

        $insights = $chart = array();

        if($user->is_admin) {
            // get insights
            /* total customers */
            $insights['customers'] = Db::getInstance()->getValue('SELECT COUNT(customer_id) FROM '. Db::prefix('customer'));

            /* total employees */
            $insights['employees'] = Db::getInstance()->getValue('SELECT COUNT(employee_id) FROM '. Db::prefix('employee'));

            /* total orders */
            $insights['orders'] = Db::getInstance()->getValue('SELECT COUNT(order_id) FROM '. Db::prefix('order') . ' WHERE is_paid = 1');

            /* total customer payments */
            $insights['customer_payments'] = Db::getInstance()->getValue('SELECT COALESCE(SUM(total),0) FROM '. Db::prefix('order') . ' WHERE is_paid = 1');

            /* total writer payments */
            $insights['writer_payments'] = Db::getInstance()->getValue('SELECT COALESCE(SUM(writer_pay),0) FROM '. Db::prefix('order') . ' WHERE is_writer_paid = 1');

            /* total editor payments */
            $insights['editor_payments'] = Db::getInstance()->getValue('SELECT COALESCE(SUM(editor_pay),0) FROM '. Db::prefix('order') . ' WHERE is_editor_paid = 1');

            // get chart data
            for($i=1; $i <= 12; $i++) {
                /* get customers */
                $chart['customers'][$i] = Db::getInstance()->getValue("SELECT COUNT(customer_id) FROM " . Db::prefix('customer') . " WHERE YEAR(reg_date) = YEAR(CURRENT_DATE()) AND MONTH(reg_date) = $i");
                
                /* get orders */
                $chart['orders'][$i] = Db::getInstance()->getValue("SELECT COUNT(order_id) FROM " . Db::prefix('order') . " WHERE is_paid = 1 AND YEAR(date_add) = YEAR(CURRENT_DATE()) AND MONTH(date_add) = $i");

                /* get sales */
                $chart['sales'][$i] = Db::getInstance()->getValue("SELECT COALESCE(SUM(total),0) FROM " . Db::prefix('order') . " WHERE is_paid = 1 AND YEAR(date_add) = YEAR(CURRENT_DATE()) AND MONTH(date_add) = $i");

                /* get payments */
                $chart['payments'][$i] = Db::getInstance()->getValue("SELECT COALESCE(SUM(writer_pay),0) FROM " . Db::prefix('order') . " WHERE is_writer_paid = 1 AND YEAR(date_add) = YEAR(CURRENT_DATE()) AND MONTH(date_add) = $i");
            } 
        } else {
            
            // get chart data
            for($i=1; $i <= 12; $i++) {
                /* get income */
                $sql = new DbQuery();
                $sql->from("order");

                if($user->is_writer) {
                    $sql->select("COALESCE(SUM(writer_pay),0)");
                    $sql->where("writer_id = " . (int) $user->id);
                    $sql->where("is_writer_paid = 1");
                } else {
                    $sql->select("COALESCE(SUM(editor_pay),0)");
                    $sql->where("editor_id = " . (int) $user->id);
                    $sql->where("is_editor_paid = 1");
                }
                
                $sql->where("YEAR(date_add) = YEAR(CURRENT_DATE()) AND MONTH(date_add) = $i");
                $chart['income'][$i] = Db::getInstance()->getValue($sql);

            } 

            $outstanding_payment = $user->getOutstanding(); 
            $smarty->assign([
                'outstanding_payment' => $outstanding_payment,
                'last_pay' => $user->last_pay,
                'total_paid' => $user->total_paid,
            ]);
        }

        $smarty->assign([
            'insights' => $insights,
            'chart' => $chart,
            'view' => 'overview'
        ]);

        return $this->getTemplateContent('overview');
    }

}