<?php

namespace App\Http\Controllers\Admin;

use App\Models\Gmenu;
use Illuminate\Http\Request;
use App\Http\Controllers\PublicController;

class GmenuController extends PublicController
{
    /**
    * Display a listing of the resource.
    *
    * @return \Illuminate\Http\Response
    */
    public function index()
    {
        return view('admin.gmenu.index');
    }
    public function data(Request $request)
    {
        $model = Gmenu::query();
        if ($request->get('parent_id')!=null ){
            $model = $model->where('parent_id',$request->get('parent_id'));
        }

        if ($request->get('status')!=null ){
            $model = $model->where('status',$request->get('status'));
        }

        if ($request->get('isleaf')!=null ){
            $model = $model->where('isleaf',$request->get('isleaf'));
        }

        if ($request->get('gname')){
            $model = $model->where('gname','like','%'.$request->get('gname').'%');
        }
        $res = $model->orderBy('id','desc')->paginate($request->get('limit',30))->toArray();
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $res['total'],
            'data'  => $res['data']
        ];
        return response()->json($data);
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.gmenu.create');
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->only(['parent_id','gname','sort','gaction','status','iconUrl','isleaf']);
        $data['status'] = (trim($data['status']==1)) ? 1 : 0 ;
        $data['isleaf'] = (trim($data['isleaf'])==1) ? 1 : 0 ;

        if ( Gmenu::create($data) ){
            return redirect()->to(route('admin.gmenu'))->with(['status'=>'添加成功']);
        }
        return redirect()->to(route('admin.gmenu'))->withErrors('系统错误');
    }
    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $gmenu = Gmenu::findOrFail($id);
        return view('admin.gmenu.edit',compact('gmenu'));
    }
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $gmenu = Gmenu::findOrFail($id);
        $data =  $request->only('gname','parent_id','sort','iconUrl','gaction','status','isleaf');
        $data['status'] = (trim($data['status'])==1) ? 1 : 0 ;
        $data['isleaf'] = (trim($data['isleaf'])==1) ? 1 : 0 ;

        if ($gmenu->update($data)){
            return redirect()->to(route('admin.gmenu'))->with(['status'=>'更新用户成功']);
        }
        return redirect()->to(route('admin.gmenu'))->withErrors('系统错误');
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)){
            return response()->json(['code'=>1,'msg'=>'请选择删除项']);
        }
        if (Gmenu::whereIn('id',$ids)->update(['status'=>0])){
            return response()->json(['code'=>0,'msg'=>'删除成功']);
        }
        return response()->json(['code'=>1,'msg'=>'删除失败']);
    }
}
