<?php

namespace App\Http\Controllers;

use App\Models\CompanyFinancialMainAccount;
use Illuminate\Http\Request;

class CompanyFinancialMainAccountController extends Controller
{
    /**Show a List of all Main Ledgers belonging to the Company**/
    public function index(){
        $ledgers = [];
        $plAccounts = $bsAccounts = 'empty';
        if(CompanyFinancialMainAccount::first()){
            $ledgers = CompanyFinancialMainAccount::orderBy('type', 'asc')->orderBy('name','asc')->get();

            if(CompanyFinancialMainAccount::where('type', 'BS')->first()){
                $bsAccounts = 'available';
            }
            if(CompanyFinancialMainAccount::where('type', 'PL')->first()){
                $plAccounts = 'available';
            }

        }

        return view('finance-main-ledgers', compact('ledgers', 'bsAccounts', 'plAccounts'));
    }

    /**Get the Account Type Selected**/
    public function getAccountType(Request $request):string
    {
        $res = 'none';
        if($request->ajax()){
            $account = CompanyFinancialMainAccount::find($request->id);
            $res = $account->category_type;
        }
        return $res;
    }

    /**Create a New Account**/
    public function create(){
        return view('adds.add-company-main-account');
    }

    public function store(Request  $request){

        $type = $request['type'];
        $category = $request['category'];
        $categoryType = 'LA';
        if($category == 'c_asset'){
            $categoryType = $request['current-asset'] ?? 'empty';
        }

        $res = 'empty type';
        if(!empty($type)){
            $name = null;
            if($type == 'BS'){
                $name = $request['bs-name'] ?? 'empty';
            }elseif ($type == 'PL'){
                $name = $request['pl-name'] ?? 'empty';
            }
            $res = 'empty name';
            if($name != 'empty'){
                $res = 'empty category';
                if(!empty($category)){
                    $res = 'empty category type';
                    if($categoryType != 'empty'){
                        /**Check if a Similar Name Exists**/
                        $res = 'name exists';
                        if(!CompanyFinancialMainAccount::where('name', $name)->first()){
                            /**Create a New Instance for the Main Account Model**/
                            $init = new CompanyFinancialMainAccount;

                            $res = 'error';
                            if($init->create([
                                'type' => $type,
                                'name' => $name,
                                'category' => $category,
                                'category_type' => $categoryType,
                                'created_by' => auth()->user()->id,
                            ])){
                                $res = 'success';
                            }
                        }
                    }
                }
            }
        }

        return $res;
    }
}
