<?php

namespace App\Http\Controllers;

use App\CommonFunction;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Log;

class LoginController extends Controller
{
    // ログイン処理
    public function login(Request $request, $management_code)
    {
        // ID・パスワードチェック
        $existingMember = User::where('user_primemail', $request->input('login_id'))->where('user_quit_flag', 0)->first();
        if (!$existingMember || !CommonFunction::verifyPassword($existingMember->user_seq, $request->input('password'), $existingMember->user_pass)) {
            return redirect("{$management_code}/login_input")
                ->withErrors(['login' => 'ID/パスワードが間違っています'])
                ->withInput();
        }

        // セッションにユーザ情報を保存してマイページトップへ遷移
        session(['user_id' => $existingMember->user_id]);
        return redirect()->intended(route('mypage', ['management_code' => $management_code]));
    }
}