<!-- 
使用方法:
{{template "components/data_table.html" (map 
  "Headers" (slice 
    (map "Name" "ID" "Key" "id" "Sortable" true "Type" "number")
    (map "Name" "名称" "Key" "name" "Sortable" true)
    (map "Name" "创建日期" "Key" "created_at" "Sortable" true "Type" "date")
    (map "Name" "状态" "Key" "status")
    (map "Name" "操作" "Actions" true)
  )
  "Data" .Items
  "EmptyMessage" "没有找到数据"
  "ID" "my-table"
)}}
-->

<div class="bg-white overflow-hidden rounded-lg shadow-sm">
    <div class="overflow-x-auto">
        <table id="{{if .ID}}{{.ID}}{{else}}data-table{{end}}" class="data-table w-full">
            <thead>
                <tr class="bg-gray-50 text-left">
                    {{range .Headers}}
                    <th class="px-6 py-3 text-xs font-medium text-gray-500 uppercase tracking-wider{{if .Sortable}} sortable{{if .Type}} sort-{{.Type}}{{end}}{{end}}">
                        {{.Name}}
                    </th>
                    {{end}}
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-200 bg-white">
                {{if .Data}}
                    {{range $item := .Data}}
                    <tr class="hover:bg-gray-50 transition-colors">
                        {{range $.Headers}}
                            {{if .Actions}}
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                <div class="flex items-center space-x-2">
                                    {{if $item.ViewLink}}
                                    <a href="{{$item.ViewLink}}" class="text-indigo-600 hover:text-indigo-900">
                                        <i class="fas fa-eye"></i>
                                    </a>
                                    {{end}}
                                    
                                    {{if $item.EditLink}}
                                    <a href="{{$item.EditLink}}" class="text-blue-600 hover:text-blue-900">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    {{end}}
                                    
                                    {{if $item.DeleteLink}}
                                    <a href="{{$item.DeleteLink}}" class="text-red-600 hover:text-red-900" data-confirm="确定要删除这项吗？" data-confirm-type="danger">
                                        <i class="fas fa-trash-alt"></i>
                                    </a>
                                    {{end}}
                                    
                                    {{if $item.CustomActions}}
                                        {{$item.CustomActions}}
                                    {{end}}
                                </div>
                            </td>
                            {{else}}
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                {{if eq .Key "status"}}
                                    {{if eq (index $item .Key) "online"}}
                                        <span class="px-2 py-1 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800">
                                            在线
                                        </span>
                                    {{else if eq (index $item .Key) "offline"}}
                                        <span class="px-2 py-1 inline-flex text-xs leading-5 font-semibold rounded-full bg-gray-100 text-gray-800">
                                            离线
                                        </span>
                                    {{else if eq (index $item .Key) "error"}}
                                        <span class="px-2 py-1 inline-flex text-xs leading-5 font-semibold rounded-full bg-red-100 text-red-800">
                                            错误
                                        </span>
                                    {{else}}
                                        {{index $item .Key}}
                                    {{end}}
                                {{else}}
                                    {{index $item .Key}}
                                {{end}}
                            </td>
                            {{end}}
                        {{end}}
                    </tr>
                    {{end}}
                {{else}}
                    <tr>
                        <td colspan="{{len .Headers}}" class="px-6 py-10 text-center text-gray-500">
                            <div class="flex flex-col items-center justify-center">
                                <svg class="w-12 h-12 text-gray-300 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.172 16.172a4 4 0 015.656 0M9 10h.01M15 10h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                                </svg>
                                <span>{{if .EmptyMessage}}{{.EmptyMessage}}{{else}}没有找到数据{{end}}</span>
                            </div>
                        </td>
                    </tr>
                {{end}}
            </tbody>
        </table>
    </div>
    
    {{if and .Pagination .Pagination.TotalPages}}
        <div class="bg-white px-4 py-3 border-t border-gray-200 sm:px-6">
            <nav class="flex items-center justify-between">
                <div class="hidden sm:block">
                    <p class="text-sm text-gray-700">
                        显示第 
                        <span class="font-medium">{{.Pagination.FirstItem}}</span>
                        到
                        <span class="font-medium">{{.Pagination.LastItem}}</span>
                        条，共
                        <span class="font-medium">{{.Pagination.Total}}</span>
                        条记录
                    </p>
                </div>
                <div class="flex-1 flex justify-between sm:justify-end space-x-1">
                    {{if .Pagination.HasPrev}}
                        <a href="{{.Pagination.PrevURL}}" class="relative inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50">
                            上一页
                        </a>
                    {{else}}
                        <span class="relative inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-md text-gray-300 bg-gray-50 cursor-not-allowed">
                            上一页
                        </span>
                    {{end}}
                    
                    {{if .Pagination.HasNext}}
                        <a href="{{.Pagination.NextURL}}" class="relative inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50">
                            下一页
                        </a>
                    {{else}}
                        <span class="relative inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-md text-gray-300 bg-gray-50 cursor-not-allowed">
                            下一页
                        </span>
                    {{end}}
                </div>
            </nav>
        </div>
    {{end}}
</div>
