<?php

namespace App\Modules\System\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Modules\System\Models\Menu;
use Illuminate\Support\Facades\Validator;
use DB;
class TemplateController extends Controller
{
    protected $table;
    function __construct(){
        $this->middleware('auth');
        $this->table = new Menu;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $count_order = $this->table::where('parent','0')->count();
        return view('System.views.template',['order_count'=>$count_order]);
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        if ($request->has('action')) {
            switch ($request->input('action')) {
                case 'fullsave':
                    $json = json_decode($request->input('menu'));
                    $this->table->truncate();
                    $this->table->save_menu_recursive($json);
                    return response()->json(['success'=>'Menu actualizado con exito','animation'=>'reload']);  
                    /*
                    $message = 'Sin guardar'; $animation = 'up';
                    if ($request->has('idupdate')) {
                        for ($i=0; $i <count($request->input('idupdate')) ; $i++) { 
                            $menu = $this->table::find($request->input('idupdate')[$i]);
                            if (!empty($menu)) {
                                $menu->name = $request->input('name')[$i];
                                $menu->slug = $request->input('url')[$i];
                                $menu->icon_text = $request->input('ico')[$i];
                                $menu->save();
                                $animation = 'reload';
                            }
                        }
                        $message = 'Menu actualizado con extio';
                    }
                    if ($request->has('suboptionupdate')) {
                        $subcount = 0;
 
                        foreach ($request->input('sub') as $k => $val) { //item menu 2do nivel
                            $subcount++;
                            $id_sub ='';
                            if (isset($val['snombre']) AND isset($val['surl'])) {

                                $parent = $this->table::where('parent',$val['id'])->get()->last();
                                $parent_order = $parent->order+0.1;
                                $menu->name = $val['snombre'];
                                $menu->slug = $val['surl'];
                                $menu->parent = $val['id'];
                                $menu->order = $parent_order;
                                $menu->icon_text = $val['sico'];
                                $menu->enabled = '1';
                                $menu->nivel = $val['nivel'];
                                $id_sub = $menu->save_menu();
                            }
                            if (isset($val['sub'])) {
                                $subcount = 1;
                                foreach ($val['sub'] as $key => $value) { //item menu 3 tercer nivel
                                    $idsub = (empty($id_sub)) ? $value['id'] : $id_sub;
                                    $parent_fix = $menu::where('parent', $idsub)->count();
                                    $subcount =  (empty($parent_fix)) ? $subcount++ : $parent_fix+1;
                                    $order_id = $menu::where('id', $idsub)->get()->last();
                                    $menu->name = $value['snombre'];
                                    $menu->slug = $value['surl'];
                                    $menu->parent = $idsub;
                                    $menu->order = " $order_id->order".$subcount;
                                    $menu->icon_text = $value['sico'];
                                    $menu->enabled = '1';
                                    $menu->nivel = $value['nivel'];
                                    $menu->save_menu();
                                }
                            }
                        }
                        $animation = 'reload';
                        $message = 'Menu actualizado y sub menus creados con exito';
                    }
                    if ($request->has('addnewmenu')) {
                        foreach ($request->input('jsmenu') as $key => $value) {
                            $menu = new Menu();
                            $parent_order = $menu::where('parent','=','0')->count()+1;
                            $menu->name = $value['jsname'];
                            $menu->slug = $value['jsurl'];
                            $menu->parent = $value['jsparent'];
                            $menu->order =  $parent_order;
                            $menu->icon_text = $value['jsico'] ;
                            $menu->enabled = '1';
                            if ($menu->save()) {     
                                $last_id = $menu->id;
                                if (isset($value['submenu'])) {      
                                    $subcount = 0.0;
                                    foreach ($value['submenu'] as $sub => $valsub) {
                                        $subcount = $subcount+0.1;
                                        $order_sub = $parent_order+$subcount;
                                        $menu->name = $valsub['snombre'];
                                        $menu->slug = $valsub['surl'];
                                        $menu->parent = $last_id;
                                        $menu->order = $order_sub;
                                        $menu->icon_text = $valsub['sico'];
                                        $menu->enabled = '1';
                                        $id_sub = $menu->save_menu();
                                        if (isset($valsub['sub'])) {
                                            $sub_subc = 0;
                                            foreach ($valsub['sub'] as $s3 => $vals3) {
                                                $sub_subc++;
                                                $menu->name = $vals3['snombre'];
                                                $menu->slug = $vals3['surl'];
                                                $menu->parent = $id_sub;
                                                $menu->order = "$order_sub".$sub_subc;
                                                $menu->icon_text = $vals3['sico'];
                                                $menu->enabled = '1';
                                                $id = $menu->save_menu();
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        $animation = 'reload';
                        $message = 'Menu actualizado y nuevos elementos creados con exito';
                    }
                    return response()->json(['success'=>$message,'animation'=>$animation]);  */
                break;
            }
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
       
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $menu = $this->table::where('id',$id);
        if ($menu->count() > 0) {
            $menu->delete();
            $parents = $this->table::where('parent',$id)->get();
            if ($parents->count() > 0) { 
                foreach ($parents as $k ) {
                    $sub3 = $this->table::where('parent',$k['id']);
                    if ($sub3->count() > 0) {
                        $sub3->delete();
                    }
                    $this->destroy($k['id']);
                }
            }
        }
        return response()->json(['success'=>'Eliminado con exito']);  
    }
}
