<?php

namespace App\Http\Controllers\admin;

use App\Constants\HttpStatus;
use App\Http\Controllers\Controller;
use Exception;

use App\Models\UserFeedback as UserFeedbackModel;

class UserFeedback extends Controller
{

    /**
     * 用户反馈列表
     */
    public function list()
    {
        try {
            $params = request()->validate([
                'page' => ['nullable', 'integer', 'min:1'],
                'limit' => ['nullable', 'integer', 'min:1'],
                'user_id' => ['nullable'],
                'username' => ['nullable'],
                'email' => ['nullable'],
                'start_time' => ['nullable', 'date', 'date_format:Y-m-d', 'required_with:end_time'],
                'end_time' => ['nullable', 'date', 'date_format:Y-m-d', 'required_with:start_time'],
            ]);
            $page = request()->input('page', 1);
            $limit = request()->input('limit', 15);

            $query = new UserFeedbackModel;
            if (!empty($params['user_id'])) {
                $query = $query->where('user_id', $params['user_id']);
            }
            if (!empty($params['email'])) {
                $query = $query->where('email', $params['email']);
            }
            if (!empty($params['username'])) {
                $query = $query->where('username', 'like', "%{$params['username']}%");
            }
            if (!empty($params['start_time'])) {
                $startTime = $params['start_time'] . " 00:00:00";
                $query = $query->where('created_at', '>=', $startTime);
            }
            if (!empty($params['end_time'])) {
                $endTime = $params['end_time'] . " 23:59:59";
                $query = $query->where('updated_at', '<=', $endTime);
            }
            $count = $query->count();
            $list = $query
                ->forPage($page, $limit)
                ->orderByDesc('created_at')
                ->get();
        } catch (Exception $e) {
            return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
        }
        return $this->success(['total' => $count, 'data' => $list]);

    }

}
