<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\User;
use Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class IndexController extends Controller
{

    public function index()
    {
        return view('admin.dashboard.index');
    }

    public function profile()
    {
        $user = Auth::user();
        // 從環境變數獲取預設密碼
        $defaultPassword = env('DEFAULT_PASSWORD');

        // 判斷用戶密碼是否為預設密碼
        $isDefaultPassword = Hash::check($defaultPassword, $user->password);

        // 傳送資料到前端
        $data = $user->toArray();

        if ($isDefaultPassword) {
            $data['password'] = $defaultPassword;
        } else {
            $data['password'] = null;
        }

        return view('admin.dashboard.profile', ['data' => (Object) $data]);
    }
    public function profileUpdate(Request $request)
    {

        $user = Auth::user();

        $user->email = $request->email;
        if ($request->password != null) {
            $user->password = Hash::make($request->password);
        }
        $user->phone = $request->phone;
        $user->save();

        return response()->json(['status' => 'success', 'message' => '更新成功', 'data' => $user]);

    }

    public function pageProfile()
    {
        $user = Auth::user();
        // 從環境變數獲取預設密碼
        $defaultPassword = env('DEFAULT_PASSWORD');

        // 判斷用戶密碼是否為預設密碼
        $isDefaultPassword = Hash::check($defaultPassword, $user->password);

        // 傳送資料到前端
        $data = $user->toArray();

        if ($isDefaultPassword) {
            $data['password'] = $defaultPassword;
        } else {
            $data['password'] = null;
        }
        return view('admin.profile.index', ['data' => (Object) $data]);
    }
}
