<?php

namespace App\Http\Controllers\Miniprogram;

use App\Models\Common\TokenModel;
use App\Models\User\UserModel;
use App\Models\Messagewall\MessagewallModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class MessageWallController extends Controller
{


    /**
     * @param  Request  $request
     * @param  int  $master_uid
     * @return \Illuminate\Http\JsonResponse
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function show(Request $request, $master_uid = 0)
    {
        $uid = Auth::auth();
        $master_uid = $master_uid ?: $uid;
        $user = UserModel::select('uid', 'nickname', 'headimgurl', 'sex')->where('uid', $master_uid)->first();
        $user->self = ($uid == $master_uid) ? true : false;

        $build = MessagewallModel::where('deleted_at', 0)->where('master_uid', $master_uid);
        $messages = $build->orderBy('id', 'desc')->paginate($request->get('per_page', $request->get('limit', 20)));
        foreach ($messages as $message) {
            $message->user;
        }
        return response()->json([
            'code' => 200,
            'message' => 'OK',
            'data' => [
                'user' => $user,
                'messages' => $messages
            ]
        ]);
    }

    /**
     * 创建留言
     * @param  Request  $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function store(Request $request)
    {
        $uid = Auth::auth();
        $validator = \Validator::make($request->all(), [
            'master_uid' => 'required|integer',
            'content' => 'required|max:32',
        ], [
            'master_uid.*' => '选择指定人',
        ]);
        if ($validator->fails()) {
            return response()->json([
                'code' => 422,
                'errors' => $validator->errors(),
                'message' => $validator->errors()->first()
            ]);
        }
        $message = MessagewallModel::create($request->merge(['guest_uid' => $uid])->all());
        return response()->json([
            'code' => 200,
            'message' => 'OK',
            'data' => $message
        ]);
    }


    /**
     * 删除
     * @param  Request  $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy(Request $request, $id)
    {
        $uid = Auth::auth();
        $message = MessagewallModel::find($id);
        if (!$message) {
            return response()->json([
                'code' => 404,
                'message' => '留言不存在'
            ]);
        }

        if (!in_array($uid, [$message->master_uid, $message->guest_uid])) {
            return response()->json([
                'code' => 403,
                'message' => '没有权限'
            ]);
        }
        $message->deleted_at = time();
        $message->save();
        return response()->json([
            'code' => 200,
            'message' => '删除成功'
        ]);
    }


    public function qrcode(Request $request)
    {
        $uid = Auth::auth();
        $public_id = config('miniprogram.public_id');
        $access_token = TokenModel::getToken($public_id);
        $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$access_token}";
        $data = [
            'scene' => "uid={$uid}&launch_type=wall",
            "page" => "pages/starter/starter",
            'width' => '430',
            'auto_color' => false,
            'line_color' => [
                'r' => 0,
                'g' => 0,
                'b' => 0,
            ],
            'is_hyaline' => false,
        ];
        $response = \Curl::to($url)->withData($data)->asJsonRequest()->post();
        return response($response, 200, ['Content-Type' => 'image/png']);
    }
}
