<?php
/**
 * Created by PhpStorm.
 * User: 思维定制
 * Date: 2018/10/26
 * Time: 16:48
 */

namespace App\Http\Controllers\Api\V1;

use App\Models\MapBannerModel;
use App\Models\MapLikeModel;
use App\Models\MapMessageModel;
use App\Models\UserInfoModel;
use App\Models\WechatAppModel;
use EasyWeChat\Factory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Library\ErrorCode;
use App\Models\MapLandmarkModel;
use Auth;

class LandMarkController extends BaseController
{
    public function getLandMark()
    {
        $userAuth = Auth('api')->user();
        if(!$userAuth) return $this->error(null, ErrorCode::NO_AUTH);
        $data = MapLandmarkModel::all();
        foreach ($data as $key=>$val){
            $like = MapLikeModel::where([['user_id',$userAuth->id],['mark_id',$val->id]])->first();
            if($like){
                $val->isLike=1;
            }else{
                $val->isLike=0;
            }
        }
        return $this->api($data);
    }

    public function getSearch(Request $request)
    {
        $userAuth = Auth('api')->user();
        if(!$userAuth) return $this->error(null, ErrorCode::NO_AUTH);
        $keyword = '%'.$request->input('keywords').'%';
        if(empty($keyword)){
            $data = MapLandmarkModel::all();
        }else{
            $data = MapLandmarkModel::where([['title','like',$keyword]])->orWhere([['subtitle','like',$keyword]])->get();
        }
        foreach ($data as $key=>$val){
            $like = MapLikeModel::where([['user_id',$userAuth->id],['mark_id',$val->id]])->first();
            if($like){
                $val->isLike=1;
            }else{
                $val->isLike=0;
            }
        }
        return $this->api($data);
    }

    public function getDetail(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required'
        ], [
            'id.required' => 'ID必填'
        ]);
        $userAuth = Auth('api')->user();
        if(!$userAuth) return $this->error(null, ErrorCode::NO_AUTH);
        if($validator->fails()) {
            return $this->validateError($validator);
        }

        $id = $request->input('id');

        $data = MapLandmarkModel::find($id);
        $banner = MapBannerModel::where('mark_id',$id)->orderBy('sort','desc')->get();
        $img = Array();
        foreach ($banner as $key=>$val){
            $img[]=$val['image'];
        }
        $data->banner = $img;
        $message = MapMessageModel::where('mark_id',$id)->orderBy('id','desc')->get();
        foreach ($message as $val){
            $user = UserInfoModel::find($val->user_id);
            $val->avatar = $user->avatar;
            $val->nickname = $user->nickname;
            $val->imgs = explode(',',$val->img);
        }
        $data->message = $message;
       // $c = explode(',',$message->img);
        $like = MapLikeModel::where([['user_id',$userAuth->id],['mark_id',$id]])->first();
        if($like){
            $data->isLike=1;
        }else{
            $data->isLike=0;
        }

        return $this->api($data);
    }

    public function doLike(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required'
        ], [
            'id.required' => 'ID必填'
        ]);
        $userAuth = Auth('api')->user();
        if(!$userAuth) return $this->error(null, ErrorCode::NO_AUTH);
        if($validator->fails()) {
            return $this->validateError($validator);
        }

        $id = $request->input('id');
        $like = MapLikeModel::where([['user_id',$userAuth->id],['mark_id',$id]])->first();
        if($like){
            $res = $like->delete();
            $mark = MapLandmarkModel::find($id);
            $update['like'] = $mark->like -1;
            $state = 0;
        }else{
            $data['user_id'] = $userAuth->id;
            $data['mark_id'] = $id;
            $res = MapLikeModel::create($data);
            $mark = MapLandmarkModel::find($id);
            $update['like'] = $mark->like +1;
            $state =1;
        }
        $res_up = MapLandmarkModel::where('id',$id)->update($update);
        if($res && $res_up){
            return $this->success($state);
        }else{
            return $this->error(null, ErrorCode::SAVE_FALL);
        }
    }

    public function doComment(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'content' => 'required',
            'mark_id' => 'required'
        ], [
            'content.required' => '详情必填',
            'mark_id.required' => 'id必填'
        ]);
        $userAuth = Auth('api')->user();
        if(!$userAuth) return $this->error(null, ErrorCode::NO_AUTH);
        if($validator->fails()) {
            return $this->validateError($validator);
        }

        $check_land = MapLandmarkModel::find($request->input('mark_id'));
        if(!$check_land) return $this->error(null, ErrorCode::CLIENT_WRONG_PARAMS);

        $data = $request->all();
        $data['user_id'] = $userAuth->id;
      //  print_r($data);die;
        $res = MapMessageModel::create($data);
        if($res){
            return $this->success();
        }else{
            return $this->error(null, ErrorCode::SAVE_FALL);
        }
    }
}