<?php

namespace App\Http\Controllers\Admin\User\Log;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\DTO\SearchData;
use App\Models\User;
use App\Models\UserNameLog;

class NameController extends Controller
{
    private User $userModel;
    private UserNameLog $userNameLogModel;

    public function __construct(User $user, UserNameLog $userNameLogModel)
    {
        $this->userModel = $user;
        $this->userNameLogModel = $userNameLogModel;
    }

    /**
     * 이름 변경 이력
     * @method GET
     * @see /admin/user/log/name
     */
    public function index(Request $request)
    {
        $params = SearchData::fromRequest($request);
        $userNameLog = $this->userNameLogModel->data($params);

        if ($userNameLog->rows > 0) {
            $num = listNum($userNameLog->total, $params->page, $params->perPage);
            foreach ($userNameLog->list as $i => $row) {
                $row->num = $num--;
                $userNameLog->list[$i] = $row;
            }
        }

        return view('admin.user.log.name', [
            'userNameLog' => $userNameLog,
            'params' => $params
        ]);
    }

    /**
     * 이름 변경 이력 삭제
     * @method DELETE
     * @see /admin/user/log/name/destroy
     */
    public function destroy(Request $request)
    {
        $chk = $request->post('chk');

        if ($chk) {
            foreach ($chk as $logID) {
                $this->userNameLogModel->find($logID)->delete();
            }
        }

        $message = '이름 변경 이력 정보가 삭제되었습니다.';
        return redirect()->route('admin.user.log.name.index')->with('message', $message);
    }
}
