<?php

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

class Dashboard extends MY_BackendController
{

    public function __construct()
    {
        parent::__construct();

        // $this->fix_table_id();
    }

    protected function fix_table_id()
    {
        $adhocs = [];

        $segments = [
            "CHANGE `id` `id` INT(11) NOT NULL AUTO_INCREMENT",
            "DROP PRIMARY KEY, ADD PRIMARY KEY(`id`)"
        ];

        foreach ($segments as $segment) {
            $sql = "select CONCAT(\"Alter Table `\", TABLE_SCHEMA,\"`.`\", TABLE_NAME, \"` $segment \") as cmd from TABLES where TABLE_SCHEMA = \"artechsy_praise_crm_app_system\"";

            $this->db = $this->load->database("information_schema", true);

            $res = $this->db->query($sql);

            foreach ($res->result_array() as $row) {
                try {
                    echo '<br>' . $row['cmd'];
                    $this->db->query($row['cmd']);
                } catch (Exception $ex) {
                    echo '<br>'  . 'CANNOT APPLY sql';
                }
            }
        }


        exit();


        // 'ALTER TABLE `mas_event_enrollment_item`
    }

    public function index()
    {
        $this->load->model('Student_model');
        $totalCount = $this->Student_model->totalCount();
        $monthlyCount = $this->Student_model->monthlyCount(date("Y"), date("m"));
        $this->load->model('Report_model');
        $yearlyIncome = $this->Report_model->yearlyIncome(date("Y"));
        $monthlyIncome = $this->Report_model->monthlyIncome(date("Y"), date("m"));
        $today = date("Y-m-d");
        $lastMonth = date("Y-m-d", strtotime($today . ' - 1 month + 1 day'));
        $dailyIncome = $this->Report_model->dailyIncome($lastMonth, $today);
        $currentYearIncome = $this->Report_model->currentYearIncome(date("Y"));
        $this->view('backend/dashboard/index', [
            'totalCount' => $totalCount,
            'monthlyCount' => $monthlyCount,
            'yearlyIncome' => $yearlyIncome,
            'monthlyIncome' => $monthlyIncome,
            'dailyIncome' => $dailyIncome,
            'currentYearIncome' => $currentYearIncome,
        ]);
    }
}
