<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Dashboard extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('User_Model');
        $this->load->model('Counter_Model');
    }

    function test()
    {
//        $this->load->dbutil();
//
//        $prefs = array(
//            'ignore' => array(),                     // List of tables to omit from the backup
//            'format' => 'zip',                       // gzip, zip, txt
//            'filename' => 'mybackup1.sql',              // File name - NEEDED ONLY WITH ZIP FILES
//            'add_drop' => TRUE,                        // Whether to add DROP TABLE statements to backup file
//            'add_insert' => TRUE,                        // Whether to add INSERT data to backup file
//            'newline' => "\n"                         // Newline character used in backup file
//        );
//
//
//// Backup your entire database and assign it to a variable
//        $backup = $this->dbutil->backup($prefs);
//
//// Load the file helper and write the file to your server
//        $this->load->helper('file');
//        write_file('mybackup1.gz', $backup);
//
//// Load the download helper and send the file to your desktop
//        $this->load->helper('download');
//        force_download('mybackup.gz', $backup);

        $sql = file_get_contents(base_url('mybackup1.sql'));
        foreach (explode(";\n", $sql) as $sql) {
            $sql = trim($sql);
            //echo  $sql.'<br>============<br>';
            if ($sql) {
                $this->db->query($sql);
            }
        }

    }

    function index()
    {
        $data['count_Storeroom'] = $this->Counter_Model->count_Storeroom();
        $data['count_StoreUser'] = $this->Counter_Model->count_StoreUser();
        $data['count_Product'] = $this->Counter_Model->count_Product();
        $data['count_Contact'] = $this->Counter_Model->count_Contact();
        $data['count_Buyer'] = $this->Counter_Model->count_Buyer();
        $data['count_Seller'] = $this->Counter_Model->count_Seller();
        // محاسبه ارزش مالی کل انبار
        $product = $this->Counter_Model->get_AllProduct();
        $price = 0;
        $count = 0;
        foreach ($product as $element) {
            $price = $price + ($element->product_Price * $element->product_Amount);
            $count = $count + $element->product_Amount;
        }
        $data['AllPrice'] = $price;
        $data['AllCount'] = $count;
        //پایان محاسبه ارزش مالی کل انبار
        $data['Count_Importation_now'] = $this->Counter_Model->get_AllImportation_Count_Date(date('Y/n/j', strtotime("now")));
        $data['Count_Importation_1'] = $this->Counter_Model->get_AllImportation_Count_Date(date('Y/n/j', strtotime("-1 days")));
        $data['Count_Importation_2'] = $this->Counter_Model->get_AllImportation_Count_Date(date('Y/n/j', strtotime("-2 days")));
        $data['Count_Importation_3'] = $this->Counter_Model->get_AllImportation_Count_Date(date('Y/n/j', strtotime("-3 days")));
        $data['Count_Importation_4'] = $this->Counter_Model->get_AllImportation_Count_Date(date('Y/n/j', strtotime("-4 days")));
        $data['Count_Importation_5'] = $this->Counter_Model->get_AllImportation_Count_Date(date('Y/n/j', strtotime("-5 days")));
        $data['Count_Importation_6'] = $this->Counter_Model->get_AllImportation_Count_Date(date('Y/n/j', strtotime("-6 days")));
        $data['Count_Exportation_now'] = $this->Counter_Model->get_AllExportation_Count_Date(date('Y/n/j', strtotime("now")));
        $data['Count_Exportation_1'] = $this->Counter_Model->get_AllExportation_Count_Date(date('Y/n/j', strtotime("-1 days")));
        $data['Count_Exportation_2'] = $this->Counter_Model->get_AllExportation_Count_Date(date('Y/n/j', strtotime("-2 days")));
        $data['Count_Exportation_3'] = $this->Counter_Model->get_AllExportation_Count_Date(date('Y/n/j', strtotime("-3 days")));
        $data['Count_Exportation_4'] = $this->Counter_Model->get_AllExportation_Count_Date(date('Y/n/j', strtotime("-4 days")));
        $data['Count_Exportation_5'] = $this->Counter_Model->get_AllExportation_Count_Date(date('Y/n/j', strtotime("-5 days")));
        $data['Count_Exportation_6'] = $this->Counter_Model->get_AllExportation_Count_Date(date('Y/n/j', strtotime("-6 days")));
        $this->load->view('Dashboard/index', $data);
    }

    function not_access()
    {
        $this->load->view('Panel/not_access');
    }

    function event()
    {
        $data['event'] = $this->Counter_Model->get_AllEvent();
        $this->load->view('Event/Show', $data);
    }

    function Report_AllProducts()
    {
        $data['allProducts'] = $this->Counter_Model->Get_AllProduct_Report();
        $this->load->view('Report/ShowProducts', $data);

    }

    function Report_AllImportation()
    {
        $data['allImportation'] = $this->Counter_Model->Get_importation_Report();
        $this->load->view('Report/ShowImportations', $data);

    }

    function Report_AllExportation()
    {
        $data['allExportation'] = $this->Counter_Model->Get_exportation_Report();
        $this->load->view('Report/ShowExportation', $data);

    }

    function LogOut()
    {
        $retrieve = $this->User_Model->Set_UserLogout($this->user_id, $this->NowDateTime());
        if ($retrieve) {
            $this->session->sess_destroy();
            redirect(base_url());
        } else {
            redirect(base_url());
        }

    }
}
