# Hướng dẫn Migration từ bảng Users sang Nguoi_Dung

## Tổng quan các thay đổi

Dự án đã được cập nhật để sử dụng bảng `nguoi_dung` với các cột tiếng Việt thay vì bảng `users` mặc định.

## Các file đã được cập nhật

### 1. app/Models/User.php
**Thay đổi chính:**
- Sử dụng bảng `nguoi_dung` thay vì `users`
- Cột timestamp tùy chỉnh: `ngay_tao` và `ngay_cap_nhat`
- Các cột mới:
  - `ten_dang_nhap` (username)
  - `mat_khau` (password)
  - `ho_ten` (full name)
  - `email`
  - `don_vi_id` (unit/department ID)
  - `remember_token`

**Các phương thức đặc biệt:**
- `getAuthPassword()`: Trả về `mat_khau` cho xác thực
- `username()`: Sử dụng `ten_dang_nhap` để đăng nhập
- Accessors/Mutators cho `name` và `password` để tương thích với code cũ

### 2. database/migrations/2026_02_07_131108_create_nguoi_dung_table.php
**Cấu trúc bảng:**
```sql
- id (bigint, primary key)
- ten_dang_nhap (string, unique)
- mat_khau (string)
- ho_ten (string)
- email (string, unique)
- don_vi_id (bigint, nullable)
- remember_token (string)
- ngay_tao (timestamp)
- ngay_cap_nhat (timestamp)
```

### 3. database/factories/UserFactory.php
Đã cập nhật để tạo dữ liệu test với các cột mới.

## Các bước tiếp theo

### 1. Chạy Migration
```bash
php artisan migrate
```

### 2. Nếu đã có dữ liệu trong bảng users cũ
Bạn cần tạo migration để chuyển dữ liệu:

```bash
php artisan make:migration migrate_users_to_nguoi_dung
```

Sau đó thêm code migration:
```php
public function up(): void
{
    // Copy data from users to nguoi_dung
    DB::table('nguoi_dung')->insert(
        DB::table('users')->get()->map(function ($user) {
            return [
                'id' => $user->id,
                'ten_dang_nhap' => $user->email, // hoặc tạo username từ email
                'mat_khau' => $user->password,
                'ho_ten' => $user->name,
                'email' => $user->email,
                'don_vi_id' => null,
                'remember_token' => $user->remember_token,
                'ngay_tao' => $user->created_at,
                'ngay_cap_nhat' => $user->updated_at,
            ];
        })->toArray()
    );
}
```

### 3. Cập nhật Laravel Passport
Nếu bạn đang sử dụng Passport, chạy:
```bash
php artisan passport:install
```

### 4. Cập nhật code xác thực
Trong controller hoặc request validation, sử dụng:
- `ten_dang_nhap` thay vì `username` hoặc `email` để đăng nhập
- `mat_khau` thay vì `password`

**Ví dụ Login Request:**
```php
$credentials = [
    'ten_dang_nhap' => $request->ten_dang_nhap,
    'password' => $request->mat_khau, // Vẫn dùng 'password' vì có mutator
];

if (Auth::attempt($credentials)) {
    // Login thành công
}
```

### 5. Tạo bảng don_vi (nếu cần)
Nếu bạn muốn sử dụng quan hệ với bảng đơn vị:

```bash
php artisan make:migration create_don_vi_table
```

Sau đó uncomment dòng foreign key trong migration `nguoi_dung`.

## Lưu ý quan trọng

1. **Tương thích ngược**: Model đã có accessors/mutators cho `name` và `password` để code cũ vẫn hoạt động
2. **Authentication**: Vẫn sử dụng `password` trong Auth::attempt() nhờ mutator
3. **API Responses**: Khi trả về JSON, sử dụng `ho_ten` thay vì `name`
4. **Validation**: Cập nhật rules validation để sử dụng tên cột mới

## Test

Tạo user mới để test:
```php
$user = User::create([
    'ten_dang_nhap' => 'admin',
    'mat_khau' => Hash::make('password'),
    'ho_ten' => 'Quản trị viên',
    'email' => 'admin@example.com',
    'don_vi_id' => 1,
]);
```

Hoặc sử dụng Factory:
```php
User::factory()->create([
    'ten_dang_nhap' => 'testuser',
    'email' => 'test@example.com',
]);
