<?php

use App\Models\Menu;
use App\Models\Setting;
use App\Models\Ticket;

if (!function_exists('menu')) {
    function menu($type = false)
    {
        return Menu::display($type);
    }
}

if (!function_exists('formatPrice')) {
    function formatPrice($price, $currency = null)
    {
        if (!$currency && auth()->check()) {
            $currency = currentCurrency();
        }
        return money(intVal($price), $currency);
    
    }
}



if (!function_exists('price')) {
    function price($price)
    {
        return number_format($price/100, 2, '.', '');
    }
}

if (!function_exists('formatPrice2')) {
    function formatPrice2($price)
    {
      
        $currency = currentCurrency();
        $price= number_format($price*100, 2, '.', '');
                return money(intVal($price), $currency);

    }
}

if (!function_exists('currentCurrency')) {
    function currentCurrency()
    {
        if(office())return  office()->account->currency_code;
        if (auth()->check()) {
            
            return  auth()->user()->account->currency_code;
        }
        return null;
    }
}


if (!function_exists('totalHT')) {
    function totalHT($x,$tax) {
        return $x*(1-$tax/100);
    }
}

if (!function_exists('setting')) {
    function setting($key) {
        return config($key)??Setting::key($key)->pluck('value')->first();
    }
}

if (!function_exists('office')) {
    function office() {
       if(auth()->check()){
           return auth()->user()->office;
       }
       return null;
    }
}

if (!function_exists('statusBg')) {
    function statusBg($status)
    {
        switch ($status) {
            case Ticket::STATE_‫‪USED‬‬:
                return 'bg-success';
                break;
            case Ticket::STATE_‫‪VOID‬‬:
                return 'bg-danger';
                break;
            case Ticket::STATE_‫‪REFUNDED‬‬:
                return 'bg-warning';
                break;
            case Ticket::STATE_‫‪EXCHANGED‬‬:
                return 'bg-secondary';
                break;
            case Ticket::‫‪STATE_OPEN‬‬:
                    return 'bg-info';
                    break;      
            default:
                return 'bg-white';
                break;
        }
    }
}
