<?php

namespace App\Http\Controllers\Admin;

use App\Http\Models\GameUrlData;
use App\Models\GameUrl;
use Illuminate\Http\Request;
use App\Http\Controllers\PublicController;

/**
 * 游戏url及参数类
 */
class GameUrlController extends PublicController
{


    /**
     * 视图加载
     *
     * @access public
     * @return array
     */
    public function index()
    {
        return view('admin.gameUrl.index');

    }//end index()


    /**
     * 数据获取
     *
     * @access public
     * @param  mixed $request 参数.
     * @return array
     */
    public function data(Request $request)
    {
        $gameUrlDataModel = new GameUrlData;
        $getGameUrlWhere = [];
        // 查询参数.
        if (empty($request->get('gameUrl_name')) !== true) {
            $getGameUrlWhere['gameUrl_name'] = $request->get('gameUrl_name');
        }

        // 查询数据.
        $res = $gameUrlDataModel->getGameUrl($getGameUrlWhere, $request->get('limit', 30));
        // 返回参数.
        $data = [
            'code'  => 0,
            'msg'   => '正在请求中...',
            'count' => $res['total'],
            'data'  => $res['data'],
        ];
        return response()->json($data);

    }//end data()


    /**
     * 视图加载
     *
     * @access public
     * @return array
     */
    public function create()
    {
        return view('admin.gameUrl.create');

    }//end create()


    /**
     * 添加数据
     *
     * @access public
     * @param  mixed $request 参数.
     * @return array
     */
    public function store(Request $request)
    {
        $data = $request->only(['gameUrl_name', 'gameUrl_type', 'gameUrl_url', 'gameUrl_data']);

        if ( GameUrl::insert($data)) {
            return redirect()->to(route('admin.gameUrl'))->with(['status'=>'添加成功']);
        }
        return redirect()->to(route('admin.gameUrl'))->withErrors('系统错误');

    }//end store()


    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $gameUrlModel = new GameUrl;
        $gameUrl = $gameUrlModel->where(['gameUrl_id' => $id])->first();
        return view('admin.gameUrl.edit',compact('gameUrl'));
    }
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $gameUrlModel = new GameUrl;
        $gameUrl = $gameUrlModel->where(['gameUrl_id' => $request->get('gameUrl_id')]);
        $data = $request->only(['gameUrl_name', 'gameUrl_type', 'gameUrl_url', 'gameUrl_data']);

        if ($gameUrl->update($data)){
            return redirect()->to(route('admin.gameUrl'))->with(['status'=>'更新成功']);
        }
        return redirect()->to(route('admin.gameUrl'))->withErrors('系统错误');
    }


    /**
     * 删除
     *
     * @access public
     * @param  mixed $request 参数.
     * @return array
     */
    public function destroy(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)){
            return response()->json(['code'=>1,'msg'=>'请选择删除项']);
        }
        if (GameUrl::whereIn('id',$ids)->update(['status'=>0])){
            return response()->json(['code'=>0,'msg'=>'删除成功']);
        }
        return response()->json(['code'=>1,'msg'=>'删除失败']);
    }
}
