@if($community->present()->canManage())
	<span class="resize-editor-screen-btn resize-editor-screen-btn-open"><i class="fa fa-arrows-alt" aria-hidden="true"></i></span>
     
	<div class="box">
		<div class="box-content">
			@php
				$communityPresenter = $community->present();
				$totalMembers = 0;
				try {
					$totalMembers = (int) $communityPresenter->totalCommunityMembers();
				} catch (\Throwable $e) {
					$totalMembers = (int) ($community->countMembers() ?? 0);
				}
				$categoryMap = [];
				if (!empty($categories)) {
					foreach ($categories as $cat) {
						try {
							$categoryMap[(string) $cat->id] = (string) ($cat->title ?? '');
						} catch (\Throwable $e2) {
							// ignore
						}
					}
				}
			@endphp

			<div style="margin-top:5px; font-size:18px; margin-bottom:10px;">
				{{ trans('community.community-members') }}: <strong>{{ $totalMembers }}</strong>
				<span style="float:right;">
					<a href="{{ $communityPresenter->url('associationmembers') }}" style="color:#ff0000;">
						Association Members
						<i class="fa fa-external-link" aria-hidden="true"></i>
					</a>
					|
					<a href="{{ $communityPresenter->url('invite') }}" style="margin-right:40px;">Community Details</a>
				</span>
			</div>

			<style type="text/css">
				.idh-association-members-table thead tr.idh-association-title th{background:#000;color:#fff;font-weight:700}
				.idh-association-members-table thead tr.idh-association-head th{background:#f2cdbd;color:#000;font-size:11px;font-weight:700;vertical-align:top}
				.idh-association-members-table td{font-size:12px;vertical-align:top}
				.idh-association-members-table td, .idh-association-members-table th{border:2px solid #fff!important}
				.idh-association-members-table-wrap{width:100%;max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch}
				.idh-association-members-table{background:#ddd;width:100%;min-width:1300px}
				.idh-association-members-table .idh-cell-wrap{white-space:normal;word-break:break-word;min-width:90px}
				.idh-association-members-table .idh-cell-address{min-width:140px}
				.idh-association-members-table .idh-cell-about{min-width:180px;max-width:260px}
			</style>
            
            <div class="row">
            	<div class="col-sm-12" style="margin-top:10px; margin-bottom:10px;">
                	<p>Business Registration Link - <a href="{{$community->present()->url('ch/regbiz')}}" target="_blank">{{$community->present()->url('ch/regbiz')}}</a></p>
                    
                    <p>Business listing - <a href="{{$community->present()->url('ch/bizlisting')}}" target="_blank">{{$community->present()->url('ch/bizlisting')}}</a></p>

					@if(!empty($membershipStoreUrl))
						<p>Membership store - <a href="{{$membershipStoreUrl}}" target="_blank">{{$membershipStoreUrl}}</a></p>
					@endif
                </div>
            </div>

			<div class="table-responsive idh-association-members-table-wrap">
				<table class="table table-bordered idh-association-members-table">
					<thead>
						<tr class="idh-association-title">
							<th colspan="15">Association Member Details</th>
						</tr>
						<tr class="idh-association-head">
							<th>Organization name</th>
							<th>Address</th>
							<th>Website</th>
							<th>Contact Name</th>
							<th>Email Address</th>
							<th>Contact Number</th>
							<th>Membership Status</th>
							<th>Joined date</th>
							<th>Membership type</th>
							<th>Amount</th>
							<th>Renewal date</th>
							<th>KYC</th>
							<th>Public Status</th>
							<th>About the organization</th>
							<th>Category</th>
						</tr>
					</thead>
					<tbody>
						@php
							$rows = $associationmembers;
							if ($rows && method_exists($rows, 'items')) {
								$rows = $rows->items();
							}
						@endphp

						@if(empty($rows) || count($rows) === 0)
							<tr>
								<td colspan="15" style="background:#eee;">No association members found.</td>
							</tr>
						@else
							@foreach($rows as $biz)
								@php
									$normalizeDateText = function ($value) {
										if ($value === null) {
											return '';
										}
										try {
											if ($value instanceof \Carbon\CarbonInterface || $value instanceof \DateTimeInterface) {
												return $value->format('M j, Y');
											}
											if (is_string($value)) {
												$v = trim($value);
												if ($v === '' || preg_match('/^(0000-00-00)(\b|\s)/', $v) || preg_match('/^(1970-01-01)(\b|\s)/', $v)) {
													return '';
												}
												if (preg_match('/^\d{4}-\d{2}-\d{2}/', $v)) {
													try {
														return \Carbon\Carbon::parse($v)->format('M j, Y');
													} catch (\Throwable $e) {
														// fall through
													}
												}
												return $v;
											}
											return (string) $value;
										} catch (\Throwable $e) {
											return is_string($value) ? trim($value) : (string) $value;
										}
									};

									$joinedAt = $biz->joined_date ?? $biz->joined_at ?? $biz->created_at ?? null;
									$joinedText = $normalizeDateText($joinedAt);
									$renewalAt = $biz->renewal_date ?? $biz->renewaldate ?? null;
									$renewalText = $normalizeDateText($renewalAt);

									$rawCat = $biz->category_id ?? $biz->category ?? '';
									$key = is_numeric($rawCat) ? (string) ((int) $rawCat) : (string) $rawCat;
									$catTitle = $categoryMap[$key] ?? '';
								@endphp
								<tr style="background:#eee;">
									<td>
                                        <div class="idh-cell-wrap">
                                            {{ $biz->organization_name }}
                                        </div>
                                    </td>
									<td><div class="idh-cell-wrap idh-cell-address">{{ $biz->organization_address }}</div></td>
									<td>
										<div class="idh-cell-wrap">
											@if(!empty($biz->website))
												<a href="{{ $biz->website }}" target="_blank" rel="noopener">{{ $biz->website }}</a>
											@else
												-
											@endif
										</div>
									</td>
									<td><div class="idh-cell-wrap">{{ $biz->admin_name }}</div></td>
									<td><div class="idh-cell-wrap">{{ $biz->organization_email ?: $biz->admin_email }}</div></td>
									<td><div class="idh-cell-wrap">{{ $biz->contact }}</div></td>
									<td><div class="idh-cell-wrap">{{ $biz->membership_status ?? $biz->member_ship_status }}</div></td>
									<td><div class="idh-cell-wrap">{{ $joinedText !== '' ? $joinedText : '-' }}</div></td>
									<td><div class="idh-cell-wrap">{{ $biz->membership_type }}</div></td>
									<td><div class="idh-cell-wrap">{{ $biz->amount }}</div></td>
									<td><div class="idh-cell-wrap">{{ $renewalText !== '' ? $renewalText : '-' }}</div></td>
									<td><div class="idh-cell-wrap">{{ $biz->kyc_status ?? $biz->kyc }}</div></td>
									<td>
										<div class="idh-cell-wrap">
											@php
												$publicRaw = $biz->public_status ?? $biz->public ?? $biz->is_public ?? $biz->status ?? null;
												$publicNormalized = is_string($publicRaw) ? strtolower(trim($publicRaw)) : $publicRaw;
												$isActive = false;
												if ($publicNormalized === null || $publicNormalized === '') {
													$isActive = false;
												} elseif (is_bool($publicNormalized)) {
													$isActive = $publicNormalized;
												} elseif (is_numeric($publicNormalized)) {
													$isActive = ((int) $publicNormalized) > 0;
												} elseif (is_string($publicNormalized)) {
													$isActive = in_array($publicNormalized, ['1','true','active','yes','y','on'], true);
												}
											@endphp
											{{ $isActive ? 'Active' : 'Inactive' }}
											<div style="margin-top:6px;">
												<a class="btn btn-xs btn-primary" href="{{ $communityPresenter->url('associationmembers/'.(int) $biz->id.'/edit') }}">Edit</a>
											</div>
										</div>
									</td>
									<td><div class="idh-cell-wrap idh-cell-about">
                                        
                                         @if(strlen($biz->about_organization) >70)
                                            <div class="business-product-list-desc-short" id="business-product-list-desc-short-{{$biz->id}}">
                                                {{strip_tags(Str::limit($biz->about_organization, 70))}}
                                                 <a class="business-product-list-desc-more" data-id="{{$biz->id}}"
                                                   data-content="">{{trans('businessecommerce.read-more')}}</a>
                                            </div>
                                            
                                            <div class="business-product-list-desc-full" id="business-product-list-desc-full-{{$biz->id}}">
                                                {!! $biz->about_organization !!}
                                                <a class="business-product-list-desc-less" data-id="{{$biz->id}}"
                                                   data-content="">{{trans('businessecommerce.read-less')}}</a>
                                            </div>
                                        @else
                                            {!! $biz->about_organization !!}  
                                        @endif 
                                        </div>
                                    </td>
									<td><div class="idh-cell-wrap">{{ $catTitle !== '' ? $catTitle : $rawCat }}</div></td>
								</tr>
							@endforeach
						@endif
					</tbody>
				</table>
			</div>

			@if($associationmembers && method_exists($associationmembers, 'render'))
				{!! $associationmembers->render() !!}
			@endif
		</div>
	</div>
@endif
