<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\UserRequest;
use EF;
use App\Head;
use App\Account;
use Crypt;
use App\Journal;
use App\Branch;
use App\Http\Requests\HeadRequest;

class HeadController extends Controller
{
    //
    public function index()
    {   
        $branch = new BranchController;

        $branch_list = $branch->getBranchList();

        $head = new Head;

        $account = new Account;

        $journal = new Journal;

        $chart_of_account = [];


     
        foreach (EF::getNatureList() as $rows) 
        {

            $data = [

                'code'         => EF::getNatureValue($rows),

                'nature'       => $rows,

                'head_list'    => $head->getAllHeads([$rows]),

                'subhead_list' => array(),
            ];


      


            array_push($chart_of_account,$data);
        }

        
        return view('accounts.manage_head',
            [ 
                'branch'      => $branch_list,
                'head'        => $head->getAllHeads(), 
                'headObj'     => $head,
                'accountObj'  => $account,
                'code'        => '',
                'chart_of_account' => $chart_of_account,
                'journal'   => $journal,
            ]
        );
    }

    public function generateNatureWiseCode(Request $request)
    {
        if(\Request::ajax())
        {
            $nature_list = implode(',', EF::getNatureList());

            $request->validate([
                
                'nature' => 'required|in:'.$nature_list,
            ]);

            $head = new head;

            return $head->generateNatureWiseCode($request->input('nature'))['code'];
        }

        return redirect('head');
    }

    public function storeHead(HeadRequest $request)
    {   
        try
        {
            $form_collect = $request->input();
            // $form_collect[EF::branchWhiteLabel()['module']] = decrypt($form_collect[EF::branchWhiteLabel()['module']]);

            // RED SUN BRANCH (PK_ID === 1)
            $form_collect['branch'] = 1;

            $head = new Head;

            $current_head = $head->storeHead($form_collect);
            
            if(isset($current_head->id))
            {

                if($request->ajax())
                {
                    $data['id']       =  Crypt::encrypt($current_head->id);
                    $data['nature']   =  $current_head->nature;
                    $data['head_tag'] = str_replace(' ','_',strtolower('head_'.$current_head->nature));
            
                    return $data;
                }

                parent::setLogMessage($current_head->code.' head has been created');

                parent::successMessage('Head has been created.',parent::getLogMessage());
            }
        } 
        catch (DecryptException $e) 
        {
            
        }


        return redirect('head');
    }

    public function updateHead(HeadRequest $request)
    {   
        
        try 
        {
            $form_collect = $request->input();
            
            $form_collect['head_id'] = decrypt($form_collect['head_id']);
            // $form_collect[EF::branchWhiteLabel()['module'] ] = decrypt($form_collect[EF::branchWhiteLabel()['module'] ]);

            $head = new Head;

            $current_head = $head->updateHead($form_collect);

            if(isset($current_head->id))
            {

                if($request->ajax())
                {
                    $data['id']       =  Crypt::encrypt($current_head->id);
                    $data['nature']   =  $current_head->nature;
                    $data['head_tag'] = str_replace(' ','_',strtolower('head_'.$current_head->nature));
            
                    return $data;
                }
                
                parent::setLogMessage($current_head->code.' head has been updated.');

                parent::successMessage('Head has been updated.',parent::getLogMessage());
            }
        } 
        catch (DecryptException $e) 
        {
            
        }

        return redirect('head');
    }

    public function removeHead($id)
    {   

        parent::errorMessage('Head not found.');

        try 
        {
            $id = decrypt($id);

            $head = new Head;

            $current_head = $head->removeHead($id);

            if(isset($current_head->id) && $current_head->is_recycle == 1)
            {

                if(\Request::ajax())
                {
                    $data['id']       =  Crypt::encrypt($current_head->id);
                    $data['nature']   =  $current_head->nature;
                    $data['head_tag'] =  str_replace(' ','_',strtolower('head_'.$current_head->nature));

            
                    return $data;
                }

                parent::setLogMessage($current_head->code.' head has been deleted.');

                parent::successMessage('Head has been deleted.',parent::getLogMessage());
            }
            else
            {
                parent::errorMessage('head has not been deleted.');
            } 
        } 
        catch (DecryptException $e) 
        {
            
        }

        return redirect()->back();
    }

    function showHeadNatureWise(Request $request)
    {

        if($request->ajax())
        {
            $nature_list = implode(',', EF::getNatureList());

            $request->validate([
                
                'nature' => 'required|in:'.$nature_list,
            ]);

            $head = new Head;

            $account = new Account;

            $journal = new Journal;

            return  view('components.head.head_tabs',[
                'head_list'  => $head->getAllHeads([$request->nature]),
                'accountObj' => $account,
                'nature'     => $request->nature,
                'journal'    => $journal,
            ]);

           
        }

        return redirect('home');
    }

}
