<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Miniprogram\Core;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\FeedbackModel;

class FeedbackController extends Controller
{

    /**
     * 获取举报列表
     * @param  Request  $request
     * @return array
     * @throws \ApiException
     */
    public function index(Request $request)
    {
        $limit = $request->get('limit', 10);
        $offset = ($request->input('page', 1) - 1) * $limit;
        $build = FeedbackModel::when($request->filled('search'), function ($query) use ($request) {
            return $query->where('search', 'like', "%" . $request->search . "%");
        })->when($request->filled('reply'), function ($query) use ($request) {
            if ($request->reply) {
                return $query->where('reply_at', '>', 0);
            } else {
                return $query->where('reply_at', '=', 0);
            }
        });
        $count = $build->count();
        $feedbacks = $build->orderBy(
            $request->input('sort_by', 'id'),
            $request->input('order', 'asc')
        )->offset($offset)
            ->limit($limit)
            ->get();
        foreach ($feedbacks as $key => $feedback) {
            $feedback->userinfo;
        }
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => [
                'list' => $feedbacks,
                'count' => $count,
                'limit' => $limit
            ]
        ]);
    }


    public function show($id)
    {
        $feedback = FeedbackModel::findOrFail($id);
        $feedback->userinfo;
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $feedback
        ]);
    }


    /**
     * 回复
     * @param  Request  $request
     * @return array
     */
    public function reply(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer',
            'content' => 'required|max:200',
        ], [
            'id.required' => '请选择一条反馈',
            'content.required' => '请写回复内容',
            'content.max' => '回复内容字数不得超出200字',
        ]);

        $feedback = FeedbackModel::findOrFail($request->id);
        if (!$feedback) {
            return ['code' => 404, 'message' => '不存在'];
        }
        if ($feedback->reply_at > 0) {
            return ['code' => 403, 'message' => '已回复'];
        }
        $feedback->reply_content = $request->input('content');
        $feedback->reply_at = time();

        $response = $this->notice($feedback);
        if ($response) {
            $feedback->save();
            return response([
                'code' => 200,
                'message' => 'OK'
            ]);
        } else {
            return response([
                'code' => 422,
                'message' => '发送失败',
            ]);
        }
    }


    public function notice($feedback)
    {
        $public_id = config('miniprogram.public_id');
        $template_id = "IFUqurIlP88yg1KJDSGI3zAA0oRSRLFaxgeZerTrbe4";
        $page = 'pages/starter/starter?launch_type=free&url=%2Fpages%2Fme%2Fme';
        $data = array(
            'keyword1' => [
                'value' => $feedback->content,
            ],
            'keyword2' => [
                'value' => $feedback->created_at->format('Y-m-d H:i'),
            ],
            'keyword3' => [
                'value' => "已回复",
            ],
            'keyword4' => [
                'value' => $feedback->reply_content,
            ],
            'keyword5' => [
                'value' => "感谢你为我们提供宝贵的建议",
            ],
        );
        $core = new Core();
        $result = $core->miniTemplate($feedback->uid, $template_id, $public_id, $page, $data);
        return $result;
    }


    /**
     * 忽略
     * @param $id
     * @return array|\Illuminate\Http\JsonResponse
     */
    public function ignore($id)
    {
        $feedback = FeedbackModel::find($id);
        if ($feedback->reply_at > 0) {
            return response([
                'code' => 403,
                'message' => '已处理'
            ]);
        }
        $feedback->reply_at = time();
        $feedback->save();
        return response([
            "code" => 200,
            "message" => "OK"
        ]);
    }
}
