<?php

namespace App\Admin\Controllers;

use App\Models\adminUser;
use Dcat\Admin\Admin;
use Dcat\Admin\Layout\Content;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class LoginController extends BaseController
{
    protected $view = 'login';

    public function getLogin(Content $content)
    {
        $kh['kus'] = $this->db2->table('KEHU')
                    ->selectRaw('KHDM,KHMC')
                    ->whereRaw('ISNULL(KHXZ,0)<>0')
                    ->get()
                    ->toArray();
        $data = [];
        $data = array_merge($data, $kh);
        return $content->full()->body(view($this->view,$data));
    }

    public function login(Request $request, AdminUser $user)
    {
        $validated = $request->validate([
            'khdm' => 'required',
            'password' => 'required',
        ]);
        $param = $request->input();
        $password = $param['password'];
        if(empty($password) || $password != '888888'){
            return response()->json(['status' => false, 'message' => '密码错误！']);
        }
        $user_info = $user->where('username',$param['khdm'])->firstOrFail();
        Admin::guard()->login($user_info);
        return response()->json([
            'status' => true,
            'redirect' => 'index' // 跳转路径
        ]);
    }
}
