<?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\UserEmailLog;

class EmailController extends Controller
{
    private User $userModel;
    private UserEmailLog $userEmailLogModel;

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

    /**
     * 이메일 변경 이력
     * @method GET
     * @see /admin/user/log/email
     */
    public function index(Request $request)
    {
        $params = SearchData::fromRequest($request);
        $userEmailLog = $this->userEmailLogModel->data($params);

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

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

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

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

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