<?php

namespace App\Http\Controllers\Administrator;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\AccountChartModel;
use App\AccountTypeModel;
use DataTables;
use DB;
use Illuminate\Support\Facades\Crypt;
class AccountChartController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

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


    public function index()
    {
        return view('administrator.account-chart.index');
    }

    public function datatable_get_account_chart()
    {
        $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");
        return  Datatables::of($account_chart)
            ->addIndexColumn()
            ->make(true);
    }
    public function get_account_chart()
    {
        $this->charts   = [];
        $level          = 1;
        $options        = '';
        $table        = '';
        $btn_edit='';
        $btn_delete='';
        $accounts = AccountChartModel::where('acctype',null)->get();
        foreach($accounts as $key => $account) {
            $item['id'] = $account->id;
            $item['text'] = $account->code . " " . $account->acc_name . " " . $account->acc_namekh;
            $item['level'] = 'l' . $level;
//            dd($item['level']);
            $type = AccountTypeModel::find($account->type_id);

            if ($account->acc_status != 0){
                if (Gate::check('Account-Chart-Edit')) {
                    $btn_edit = '<a href="' . route('account-chart.edit', $account->id) . '" class="btn btn-success btn-sm"><i class="fa fa-edit" title="Edit"> </i> </a>';
                }
                if (Gate::check('Account-Chart-Delete')) {
                    $btn_delete = '<a href="#" data-id = "' . $account->id . '" data-toggle="modal" data-target="#form-delete" class="btn-sm btn btn-danger confirm_delete" data-toggle="tooltip" data-placement="top" title="Delete"><i class="fa fa-trash"></i></a>';
                }
            }else{
                if (Gate::check('Account-Chart-Edit')) {
                    $btn_edit = '<button disabled class="btn btn-success btn-sm"><i class="fa fa-edit" title="Edit"></i></button>';
                }
                if (Gate::check('Account-Chart-Delete')) {
                    $btn_delete = '<button class="btn-sm btn btn-danger" disabled title="Delete"><i class="fa fa-trash"></i></button>';
                }
            }
            $table .=   '<tr>
                                <td class="'.$item['level'].'">' . $account->code . '</td>
                                <td>' . $account->acc_name . '</td>
                                <td>' . $account->acc_namekh . '</td>
                                <td>' . $type->acctype_name . '</td>
                                <td>' . $account->acc_des . '</td>
                                <td>' . $btn_edit.$btn_delete . '</td>
                            </tr>';

            array_push($this->charts,$item);

            if($account->children->count() > 0){

                $table .= $this->sub_option($account,$level);
                $this->sub_show($account,$level);
            }
        }
        return $table;
    }
    public function sub_option($account,$level){
        $accounts   = $account->children;
        $level++;
        $options    = '';
        $table    = '';
        $btn_edit='';
        $btn_delete='';
        foreach($accounts as $key => $account){
            $item['id']                 = $account->id;
            $item['text']               = $account->code." ".$account->acc_name." ".$account->acc_namekh;
            $item['level']              = 'l'.$level;
            $type=AccountTypeModel::find($account->type_id);
            if ($account->acc_status != 0){
                if (Gate::check('Account-Chart-Edit')) {
                    $btn_edit = '<a href="' . route('account-chart.edit', $account->id) . '" class="btn btn-success btn-sm"><i class="fa fa-edit" title="Edit"> </i> </a>';
                }
                if (Gate::check('Account-Chart-Delete')) {
                    $btn_delete = '<a href="#" data-id = "' . $account->id . '" data-toggle="modal" data-target="#form-delete" class="btn-sm btn btn-danger confirm_delete" data-toggle="tooltip" data-placement="top" title="Delete"><i class="fa fa-trash"></i></a>';
                }
            }else{
                if (Gate::check('Account-Chart-Edit')) {
                    $btn_edit = '<button disabled class="btn btn-success btn-sm"><i class="fa fa-edit" title="Edit"></i></button>';
                }
                if (Gate::check('Account-Chart-Delete')) {
                    $btn_delete = '<button class="btn-sm btn btn-danger" disabled title="Delete"><i class="fa fa-trash"></i></button>';
                }
            }
            $table .=   '<tr>
                                <td class="'.$item['level'].'">' . $account->code . '</td>
                                <td>' . $account->acc_name . '</td>
                                <td>' . $account->acc_namekh . '</td>
                                <td>' . $type->acctype_name . '</td>
                                <td>' . $account->acc_des . '</td>
                                <td>' . $btn_edit.$btn_delete . '</td>
                            </tr>';
            if($account->children->count() > 0){
                $table .= $this->sub_option($account,$level);
            }
            if($key == count($accounts)-1){
                $level -=1;
            }
        }
        return $table;
    }
    public function sub_show($account,$level){
        $accounts   = $account->children;
        $level++;
        $table='';
        $btn_edit='';
        $btn_delete='';
        foreach($accounts as $key => $account){
            $item['id']                 = $account->id;
            $item['text']               = $account->code." ".$account->acc_name." ".$account->acc_namekh;
            $item['level']              = 'l'.$level;
            $type=AccountTypeModel::find($account->type_id);
            if ($account->acc_status != 0){
                if (Gate::check('Account-Chart-Edit')) {
                    $btn_edit = '<a href="' . route('account-chart.edit', $account->id) . '" class="btn btn-success btn-sm"><i class="fa fa-edit" title="Edit"> </i> </a>';
                }
                if (Gate::check('Account-Chart-Delete')) {
                    $btn_delete = '<a href="#" data-id = "' . $account->id . '" data-toggle="modal" data-target="#form-delete" class="btn-sm btn btn-danger confirm_delete" data-toggle="tooltip" data-placement="top" title="Delete"><i class="fa fa-trash"></i></a>';
                }
            }else{
                if (Gate::check('Account-Chart-Edit')) {
                    $btn_edit = '<button disabled class="btn btn-success btn-sm"><i class="fa fa-edit" title="Edit"></i></button>';
                }
                if (Gate::check('Account-Chart-Delete')) {
                    $btn_delete = '<button class="btn-sm btn btn-danger" disabled title="Delete"><i class="fa fa-trash"></i></button>';
                }
            }
            $table .=   '<tr>
                                <td class="'.$item['level'].'">' . $account->code . '</td>
                                <td>' . $account->acc_name . '</td>
                                <td>' . $account->acc_namekh . '</td>
                                <td>' . $type->acctype_name . '</td>
                                <td>' . $account->acc_des . '</td>
                                <td>' . $btn_edit.$btn_delete . '</td>
                            </tr>';
            array_push($this->charts,$table);
            if($account->children->count() > 0){
                $this->sub_show($account,$level);
            }
            if($key == count($accounts)-1){
                $level -=1;
            }
        }
    }
    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('administrator.account-chart.create',compact('account_type'));
    }

    public function get_acc_code($id)
    {
        $account_chart = $id;
        $count_chart = AccountChartModel::where('id', $account_chart)->first();
        $count = AccountChartModel::where('acctype', $account_chart)->count();
        $count += 1;
        $number = $count_chart->code . $count;

        return response()->json($number);
    }
    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(
                'code' => $acc_num,
                'acc_name' => $request->acc_name,
                'type_id' =>  $request->acctype,
                'acctype' =>  $acc_type_name,
                'acc_namekh' => $request->acc_namekh,
                'acc_des' => $request->acc_des,
                'acc_delete' => 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();
        }
    }

   
}
