<?php

namespace App\Http\Controllers\Accounting;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\account\AccountChartModel;
use App\Model\account\AccountTypeModel;
use DataTables;
use DB;

class AccountChartController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

//    function __construct()
//    {
//        $this->middleware('permission:Account-Chart-List');
//        $this->middleware('permission:Account-Chart-Create', ['only' => ['create','store']]);
//        $this->middleware('permission:Account-Chart-Edit', ['only' => ['edit','update']]);
//        $this->middleware('permission:Account-Chart-Delete', ['only' => ['confirm_delete']]);
//    }


    public function index()
    {
        $account_chart = DB::select("SELECT nso007_account_chart.*,nso007_account_type.acctype_name FROM `nso007_account_chart` 
        LEFT JOIN nso007_account_type 
        ON nso007_account_type.id = nso007_account_chart.acctype WHERE nso007_account_chart.sub_acc = 0 ORDER BY nso007_account_chart.id");

        $sub_account_chart = DB::select("SELECT nso007_account_chart.*,nso007_account_type.acctype_name FROM `nso007_account_chart` 
        LEFT JOIN nso007_account_type 
        ON nso007_account_type.id = nso007_account_chart.acctype WHERE nso007_account_chart.sub_acc = 1 ORDER BY nso007_account_chart.id");
        return view('Accounting.account-chart.index',compact('account_chart','sub_account_chart'));
    }

    public function create()
    {
        $account_type = AccountTypeModel::select(
            DB::raw('CONCAT(id," ",acctype_name) AS account_mixed'),'id')
            ->pluck('account_mixed','id')
            ->prepend('Please Select', '');
        return view('Accounting.account-chart.create',compact('account_type'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'acctype'      => 'required',
            'acc_type_name' => 'required',
            'acc_name'      => 'required',
            'acc_namekh'     => 'required',
        ]);
        $acc_type_name = $request->acc_type_name ? $request->acc_type_name : null;
        $accounttype = $request->accounttype ? $request->accounttype : null;
        $acc_num = $request->acc_num;
        $sub_acc = 0;
        if(!empty($accounttype))
        {
            $acc_type_name = $accounttype;
            $sub_acc = 1;
        }
        try
        {
            DB::beginTransaction();
            $data = array(
                'id' => $acc_num,
                'acc_name' => $request->acc_name,
                'acctype' =>  $acc_type_name,
                'acc_namekh' => $request->acc_namekh,
                'acc_des' => $request->acc_des,
                'acc_delete' => 1,
                'acc_status' => 1,
                'sub_acc' => $sub_acc,
            );
            AccountChartModel::insert($data);
            DB::commit();
            return redirect()->route('account-chart.index')
                ->with('success','Account Chart created successfully');
        }
        catch (\Exception $e)
        {
            DB::rollback();
            return $e->getMessage();
        }
    }
    public function get_account_code(Request $request)
    {
        $account_code = $request->account_code;
        $acc_type = $request->acc_type;
        $account_chart = AccountChartModel::where('acctype',$account_code)->get();
        $acc_num = 0;
        if($acc_type != null)
        {
            $sub_acc = substr($acc_type,0,2);
            $account_num = AccountChartModel::where(DB::raw('substr(acctype,1,2)'),'=',$sub_acc)->orderBy('id','desc')->first();
            $acc_num = $account_num->id ? $account_num->id + 1 : '';
        }
        $data = array('acc_chart' => $account_chart ,'acc_num' => $acc_num);

        echo json_encode($data);
    }
    public function edit($id)
    {
        $account_type = AccountTypeModel::select(
            DB::raw('CONCAT(id," ",acctype_name) AS account_mixed'),'id')
            ->pluck('account_mixed','id')
            ->prepend('Please Select', '');
        $account_chart = AccountChartModel::findOrFail($id);
        $acc_type_name = null;
        $sub_acc = null;
        $acc_type = null;
        $sub_acc_type = null;
        if($account_chart->sub_acc != 0)
        {
            $sub_acc = AccountChartModel::where('id',$account_chart->acctype)->first();
            $sub_acc_type = AccountChartModel::where('id',$sub_acc->acctype)->first();
            $acc_type_name = $sub_acc_type != null ? $sub_acc_type->id .' '.$sub_acc_type->acc_name : null;
            $acc_type = optional($sub_acc_type->account_type)->acctype_name;
        }
        else
        {
            $sub_acc_type = AccountChartModel::where('id',$account_chart->acctype)->first();
            $acc_type_name = $sub_acc_type != null ? $sub_acc_type->id .' '.$sub_acc_type->acc_name : null;
            $acc_type = optional($sub_acc_type->account_type)->acctype_name;
        }

        $sub_acc_of = $sub_acc != null ? $sub_acc->id.' | '.$sub_acc->acc_name : null;
        return view('Accounting.account-chart.edit',compact('account_chart','account_type','acc_type_name','sub_acc_of','sub_acc','acc_type','sub_acc_type'));
    }
    public function confirm_delete(Request $request)
    {
        $delete_acc = AccountChartModel::findOrFail($request->delete_id);
        $delete_acc->delete();
        return redirect()->route('account-chart.index')->with('success','Account chart deleted');
    }
    public function update(Request $request,$id)
    {
        $account = AccountChartModel::findOrFail($id);
        $account->acc_name = $request->acc_name;
        $account->acc_namekh = $request->acc_namekh;
        $account->acc_des = $request->acc_des;
        $account->save();
        return redirect()->route('account-chart.index')
            ->with('success','Account Chart updated successfully');
    }
}
