<?php

namespace App\Http\Controllers\Admin;

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

/**
 * 活动管理类
 */
class ActivityController extends PublicController
{


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

    }//end index()


    /**
     * 数据获取
     *
     * @access public
     * @param  mixed $request 参数.
     * @return array
     */
    public function data(Request $request)
    {
        $activityModel = new Activity;
        $getActivityWhere = [];
        // 查询参数.
        if (empty($request->get('activity_title')) !== true) {
            $getActivityWhere['activity_title'] = $request->get('activity_title');
        }
        // 查询参数.
        if (strlen($request->get('activity_delete'))) {
            $getActivityWhere['activity_delete'] = $request->get('activity_delete');
        }

        // 查询数据.
        $res = $activityModel->getAllActivity($getActivityWhere, $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');
        $status = $request->get('status') == 0 ? 1 : 0;
        if (empty($ids)){
            return response()->json(['code'=>1,'msg'=>'请选择更改状态项']);
        }
        if (ActivityDatabase::whereIn('activity_identity',$ids)->update(['activity_delete'=>$status])){
            return response()->json(['code'=>0,'msg'=>'更改状态成功']);
        }
        return response()->json(['code'=>1,'msg'=>'更改状态失败']);
    }
}
