@yield('title', lang('staff_model.list'))

@section('breadcrumb')
	<h2>@lang('staff_model.list')</h2>
	@if($this->hasCreateRole)
		<h3><a href="{{ route('create') }}">@lang('master.add_bt')</a></h3>
	@endif
@endsection

@extension('message')

@php
	$actionLinks = [];
	if(!$this->hasUpdateRole) {
		$actionLinks['update'] = null;
	}

	if(!$this->hasDeleteRole) {
		$actionLinks['delete'] = null;
		$actionLinks['deletes'] = null;
	}

	$auth = load_auth();
	$user = $auth->getUser();
	$GLOBALS['auth'] = $auth;
	$GLOBALS['user'] = $user;

	$userModel = \Model::load('UserModel');

	if(area_name() == 'super-admin') {

	}
	else 
	{
		if($auth->hasRole(\RoleGlobal::$role->viewAllBranch)) {
			$userModel->whereIn('branchID', $user->getBranchsId());
		} else {
			$userModel->where('branchID', $user->branchID);
		}
	}

	$staffs = $userModel->select('id', 'name', 'staffId')->toList();
	$GLOBALS['staffNames'] = [];
	$GLOBALS['staffIds'] = [];
	foreach($staffs  as $s) {
		if(isset($s->staffId)) {
			$GLOBALS['staffIds'][] = $s->staffId;
			$GLOBALS['staffNames'][$s->staffId] = $s->name;
		}
	}

	$GLOBALS['emptyAccountText'] = lang('staff_model.empty_account');

	$extensionOption = [
		'name' => 'table-staffs',
		'title' => 'staffs',
		'controller_url' => route('index'),
		'min_width' => '1300px',
		'custom_value_column' => [
			'image' => function($v) {
				if($v['image']) {
					return '<img src="'.base_url($v['image']).'" alt="Smiley face" height="42" width="42">';
				}
			},
			'account' => function($v) {
				$staffIds = $GLOBALS['staffIds'];
				if(in_array($v['id'], $staffIds)) {
					$staffNames = $GLOBALS['staffNames'];
					return $staffNames[$v['id']];
				} 
				return '<a href="'. url('/user/create?uname=' . $v['code'] . '&name=' . $v['name'] . '&staffId='.$v['id']) .'">' . $GLOBALS['emptyAccountText'] . '</a>';
			}
		],
		'before_get_data' => function($model) {
			if(area_name() == 'super-admin') {

			} else {
				if($GLOBALS['auth']->hasRole(\RoleGlobal::$role->viewAllBranch)) {
					$model->whereIn('branchID', $GLOBALS['user']->getBranchsId());
				} else {
					$model->where('branchID', $GLOBALS['user']->branchID);
				}
			}
		},
		'action_link' => $actionLinks,
	];
@endphp

@extension('datatable', 'StaffsModel', $extensionOption)