<?php

namespace Wave\Http\Controllers\CMS;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\Role;
use Illuminate\Http\Request;
use Yajra\DataTables\Facades\DataTables;

class CrmController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index(){
        $roles = Role::whereIn('id', [
            Role::OWNER_ROLE,
            Role::ADMINISTRATOR_ROLE,
            Role::BOOKKEEPER_ROLE,
            Role::COMPANY_SECRETARY_ROLE,
        ])->get();
        return view('theme::cms.crm.index')->with([
            'roles' => $roles,
        ]);
    }

    public function table(Request $request){
        $users = User::query()
            ->select(
                'users.id',
                'companies.id as company_id',
                'companies.name_english',
                'companies.name_chinese',
                'users.first_name',
                'users.last_name',
                'users.phone',
                'users.email',
                'roles.display_name',
                'users.status',
            )
            ->leftJoin('roles', 'users.role_id', '=', 'roles.id')
            ->leftJoin('companies', 'users.company_id', '=', 'companies.id')
            ->whereIn('users.role_id', [
                Role::OWNER_ROLE,
                Role::ADMINISTRATOR_ROLE,
                Role::BOOKKEEPER_ROLE,
                Role::COMPANY_SECRETARY_ROLE,
            ])
            ->when($request->company_name, function ($query) use ($request) {
                $query->where(function($query2) use ($request){
                    $query2->orWhere('companies.name_english', 'like', '%' . $request->company_name . '%');
                    $query2->orWhere('companies.name_chinese', 'like', '%' . $request->company_name . '%');
                });
            })
            ->when($request->first_name, function ($query) use ($request) {
                $query->where('users.first_name', 'like', '%' . $request->first_name . '%');
            })
            ->when($request->last_name, function ($query) use ($request) {
                $query->where('users.last_name', 'like', '%' . $request->last_name . '%');
            })
            ->when($request->phone, function ($query) use ($request) {
                $query->where('users.phone', 'like', '%' . $request->phone . '%');
            });

        return Datatables::of($users)
            ->escapeColumns(['id'])
            ->addColumn('status', function ($user) {
                $status = $user->status;
                if ($status == 'active') {
                    $status = '<span class="success item-list-note">' . ucwords($status) . '</span>';
                }
                else if ($status == 'inactive') {
                    $status = '<span class="failed item-list-note">' . ucwords($status) . '</span>';
                }

                return $status;
            })
            ->addColumn('actions', function ($user) {
                $suspendAction = '';
                $activateAction = '';
                $removeAction = '';
                $editUserAction = '';

                if (auth()->user()->userRole->hasAccess('manage-crm')) {
                    $suspendAction = '<a class="dropdown-item suspend-user" href="#" data-action="' . route('cms.users.suspend', $user->id) . '">' . __("Suspend user") . '</a>';
                    $activateAction = '<a class="dropdown-item activate-user" href="#" data-action="' . route('cms.users.activate', $user->id) . '">' . __("Activate user") . '</a>';
                    $removeAction = '<a class="dropdown-item remove-user" href="#" data-action="' . route('cms.users.destroy', $user->id) . '">' . __("Remove user") . '</a>';
                    $editUserAction = '<a class="dropdown-item edit-user" href="#" data-action-show="' . route('cms.users.show', $user->id) . '" data-action-update="' . route('cms.users.update', $user->id) . '">' . __("Edit user") . '</a>';
                }

                $viewUserLogAction = '<a class="dropdown-item view-user-log" data-action="' . route("cms.users.accessLogsTable", $user->id) . '" href="#">' . __("View user log") . '</a>';
                $actions = '
                    <div class="dropdown non-custom">
                        <button type="button" data-bs-toggle="dropdown" aria-expanded="false"><img class="max-25" src="' . asset('themes/tailwind/images/more.svg') . '"></button>
                        <div class="dropdown-menu">
                            ' . ($user->status == 'active' ? $suspendAction : $activateAction) . '
                            ' . $removeAction . '
                            ' . $viewUserLogAction . '
                            ' . $editUserAction . '
                        </div>
                    </div>
                ';
                return $actions;
            })
            ->make(true);
    }
}
