<?php

namespace App\System\Controller;

use App\System\Basic\beCache;
use App\System\Basic\beContoller;
use App\System\Model\CurrencieModel;
use App\System\Model\CurrencyModel;
use App\System\Model\InvoiceModel;
use App\System\Model\SettingModel;
use App\System\Model\UserProductCancellationsModel;
use App\System\Vendor\bePlugin;

class DashboardController extends beContoller {
    public function admin(){
        /*$mainfile = BE_APPPATH."resources/cache/version.json";
        $build = 0;
        if(file_exists($mainfile)){
            $system = file_get_contents($mainfile);
            $system = json_decode($system,true);
            $build = $system["build"];
        }
        global $be;
        $settings = $be->settings;
        $response = ConnectionController::beSendRequestControlPanel([
            "method" => "checkuser",
            "data" => [
                "version" => $build,
                "cid" => $settings["system_id"],
                "ctoken" => $settings["system_token"],
                "secret" => $settings["system_secret"]
            ]
        ]);
        if(!$response["status"] && $response["message"]){
            beError("Lisans Hatası",$response["message"]);
        }*/
        global $be;
        $settings = $be->settings;
        $systemcid = $settings["system_currency"];
        $notes = SettingModel::beGetSettings("admin_notes")["admin_notes"];
        $currencie = CurrencyModel::beGetCurrencies("cid,cname,cicon",$systemcid);
        $timeprices = json_decode(beCache::beGetCache("stats_times_prices"),true);
        $todayprices = json_decode(beCache::beGetCache("stats_today_prices"),true)["todayprices"];
        $todaytickets = json_decode(beCache::beGetCache("stats_today_tickets"),true)["todaytickets"];
        $plugindata = ["pluginstemplate" => []];
        $plugindata = bePlugin::beRunHook("admin_dashboard_start",$plugindata);
        $userproductcancellations = UserProductCancellationsModel::beTable("be_user_product_cancellations as upc")->beSelect("upc.upcid,upc.upid,upc.uid,upc.upcdateline,u.name,u.surname,up.upname,up.uptag")->beWhere("upcstatus","0")->beJoin("INNER JOIN be_users as u ON u.uid = upc.uid INNER JOIN be_user_products as up ON up.upid = upc.upid")->beGetAll();
        return beGetView("admin.dashboard",["notes" => $notes,"todayprices" => $todayprices,"todaytickets" => $todaytickets,"timeprices" => $timeprices,"systemcid" => $systemcid,"currencie" => $currencie,"plugindata" => $plugindata,"userproductcancellations" => $userproductcancellations]);
    }
    public function notesave_post(){
        $notes = $_POST["notes"];
        SettingModel::beUpdate(["svalue" => $notes])->beWhere("scode","admin_notes")->beExecute();
        return beAjaxSuccess(["message" => "Başarılı bir şekilde yönetici notları kaydedildi"]);
    }
}

?>