@namespace AntSK.Pages.Setting.ChatHistory
@page "/setting/chathistory"
@using AntSK.Services.Auth
@inherits AuthComponentBase
@using Microsoft.AspNetCore.Authorization
@using System.ComponentModel
@using AntDesign.TableModels
@using AntSK.Domain.Repositories
@using AntSK.Domain.Common.Map
@attribute [Authorize(Roles = "AntSKAdmin")]

<div class="chat-history-container">
    <Card>
        <TitleTemplate>
            <div class="page-header">
                <div class="header-left">
                    <Icon Type="message" Theme="outline" />
                    <Title Level="2" style="margin-bottom: 0; margin-left: 8px;">聊天记录</Title>
                </div>
                <div class="header-right">
                    <Search 
                        Placeholder="搜索用户名或消息内容" 
                        @bind-Value="searchString" 
                        OnSearch="Search"
                        EnterButton="true"
                        Size="@InputSize.Large"
                        style="width: 300px;" />
                    <span class="total-text">共 @_total 条</span>
                </div>
            </div>
        </TitleTemplate>
        <Body>
            <Table @ref="table"
                   TItem="ChatsDto"
                   DataSource="@chatDtoList"
                   Total="_total"
                   @bind-PageIndex="_pageIndex"
                   @bind-PageSize="_pageSize"
                   OnChange="OnChange"
                   Size="TableSize.Middle"
                   RowKey="x=>x.Id"
                   Bordered="true"
                   Loading="_loading">
                <ColumnDefinitions>
                    <PropertyColumn Property="c=>c.Id" Width="80px">
                        <TitleTemplate>
                            <strong>ID</strong>
                        </TitleTemplate>
                    </PropertyColumn>
                    
                    <PropertyColumn Title="用户" Property="c=>c.UserName" Width="160px">
                        <TitleTemplate>
                            <strong><Icon Type="user" /> 用户</strong>
                        </TitleTemplate>
                    </PropertyColumn>
                    
                    <PropertyColumn Title="应用" Property="c=>c.AppName" Width="150px">
                        <TitleTemplate>
                            <strong><Icon Type="appstore" /> 应用</strong>
                        </TitleTemplate>
                    </PropertyColumn>
                    
                    <PropertyColumn Title="类型" Property="c=>c.SendReveice" Width="120px">
                        <TitleTemplate>
                            <strong><Icon Type="swap" /> 类型</strong>
                        </TitleTemplate>
                    </PropertyColumn>
                    
                    <PropertyColumn Title="消息内容" Property="c=>c.Context" Width="40%">
                        <TitleTemplate>
                            <strong><Icon Type="message" /> 消息内容</strong>
                        </TitleTemplate>
                    </PropertyColumn>
                    
                    <PropertyColumn Title="时间" Property="c=>c.CreateTime" Format="yyyy-MM-dd HH:mm:ss" Width="200px">
                        <TitleTemplate>
                            <strong><Icon Type="clock-circle" /> 时间</strong>
                        </TitleTemplate>
                    </PropertyColumn>
                </ColumnDefinitions>
            </Table>
        </Body>
    </Card>
</div>

<style>
    .chat-history-container {
        padding: 24px;
        min-height: 100vh;
    }

    .ant-card {
        border-radius: 10px;
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.06);
        border: 1px solid rgba(0, 0, 0, 0.05);
    }

    .page-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 8px;
        padding: 12px 0 16px 0;
        border-bottom: 1px solid #f0f0f0;
    }

    .header-left {
        display: flex;
        align-items: center;
    }

    .header-left .anticon {
        font-size: 26px;
        color: #1677ff;
        margin-right: 12px;
    }

    .header-left .ant-typography {
        color: rgba(0, 0, 0, 0.85);
        font-weight: 600;
        margin: 0;
    }

    .header-right {
        display: flex;
        align-items: center;
    }

    .total-text {
        margin-left: 12px;
        color: rgba(0,0,0,.45);
        font-size: 12px;
    }

    .ant-input-search-large {
        border-radius: 8px;
        box-shadow: 0 2px 8px rgba(22, 119, 255, 0.12);
        transition: all 0.3s ease;
    }

    .ant-input-affix-wrapper-focused {
        box-shadow: 0 0 0 3px rgba(22, 119, 255, 0.12);
        border-color: #1677ff !important;
    }

    .ant-table {
        border-radius: 8px;
        overflow: hidden;
        background: #fff;
    }

    .ant-table-thead > tr > th {
        background-color: #fafafa;
        font-weight: 600;
        border-bottom: 1px solid #f0f0f0;
        color: rgba(0,0,0,.85);
        letter-spacing: .2px;
    }

    .ant-table-tbody > tr {
        transition: all 0.3s ease;
    }

    .ant-table-tbody > tr:hover > td {
        background-color: #f5f5f5;
    }

    .ant-table-tbody > tr > td {
        padding: 12px 16px;
        vertical-align: top;
    }

    .message-content {
        max-width: 600px;
        display: -webkit-box;
        -webkit-line-clamp: 3;
        -webkit-box-orient: vertical;
        overflow: hidden;
        white-space: normal;
    }

    .message-content .ant-typography {
        margin: 0;
        line-height: 1.6;
    }

    .bubble {
        background: linear-gradient(180deg, #fafafa 0%, #fff 100%);
        padding: 12px 14px;
        border-radius: 10px;
        border: 1px solid #f0f0f0;
        position: relative;
    }

    .bubble::before {
        content: '';
        position: absolute;
        left: -6px;
        top: 14px;
        width: 0;
        height: 0;
        border-top: 6px solid transparent;
        border-bottom: 6px solid transparent;
        border-right: 6px solid #f0f0f0;
    }

    .bubble::after {
        content: '';
        position: absolute;
        left: -5px;
        top: 14px;
        width: 0;
        height: 0;
        border-top: 6px solid transparent;
        border-bottom: 6px solid transparent;
        border-right: 6px solid #fff;
    }

    .time-cell {
        display: inline-flex;
        align-items: center;
        gap: 8px;
        background: #fafafa;
        padding: 6px 10px;
        border-radius: 8px;
        border: 1px solid #e8e8e8;
    }

    .time-cell .date {
        font-size: 13px;
        color: #595959;
        font-weight: 600;
        margin-bottom: 4px;
    }

    .time-cell .time {
        font-size: 11px;
        color: #8c8c8c;
        font-family: 'Courier New', monospace;
    }

    .time-text {
        font-size: 12px;
        color: #595959;
        letter-spacing: 0.2px;
    }

    .ant-tag {
        margin: 0;
        border-radius: 6px;
        font-weight: 500;
        padding: 4px 10px;
        border: 1px solid rgba(0, 0, 0, 0.06);
        transition: all 0.3s ease;
    }

    .type-tag {
        box-shadow: 0 2px 6px rgba(0,0,0,.06);
    }

    .app-tag {
        background: #f0f5ff;
        color: #2f54eb;
        border-color: #adc6ff;
    }

    .user-cell {
        display: inline-flex;
        align-items: center;
        gap: 8px;
    }

    .user-name {
        max-width: 120px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        display: inline-block;
        vertical-align: middle;
    }

    .avatar-icon {
        width: 28px;
        height: 28px;
        border-radius: 50%;
        background: #e6f4ff;
        color: #1677ff;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        padding: 4px;
        box-sizing: border-box;
    }

    /* 响应式设计 */
    @@media (max-width: 1200px) {
        .message-content {
            max-width: 420px;
        }
    }

    @@media (max-width: 768px) {
        .chat-history-container {
            padding: 12px;
        }
        
        .page-header {
            flex-direction: column;
            gap: 16px;
            padding: 16px 0;
        }
        
        .header-left .ant-typography {
            font-size: 20px;
        }
        
        .header-right {
            width: 100%;
        }
        
        .header-right .ant-input-search {
            width: 100% !important;
        }

        .message-content {
            max-width: 100%;
        }

        .user-name { max-width: 160px; }
    }

    @@media (max-width: 480px) {
        .message-content {
            max-width: 100%;
        }
        
        .time-cell {
            padding: 4px;
        }
        
        .time-cell .date {
            font-size: 11px;
        }
        
        .time-cell .time {
            font-size: 10px;
        }

        .user-name { max-width: 120px; }
    }
</style>

@using System.Text.Json;
@code {
    [Inject] IChats_Repositories _chats_Repositories { get; set; } = default!;

    ChatsDto[] chatDtoList = Array.Empty<ChatsDto>();
    ITable? table;
    bool _loading = false;

    int _pageIndex = 1;
    int _pageSize = 10;
    int _total = 0;
    string searchString = string.Empty;

    protected override async Task OnInitializedAsync()
    {
        await InitData();
    }

    public async Task OnChange(QueryModel<ChatsDto> queryModel)
    {
        _loading = true;
        StateHasChanged();
        await InitData();
        _loading = false;
        StateHasChanged();
    }

    private Task InitData()
    {
        try
        {
            if (string.IsNullOrEmpty(searchString))
            {
                _total = _chats_Repositories.Count(p => true);
                var chatList = _chats_Repositories.GetDB().Queryable<Chats, Apps>((c, a) => new object[] {
                    SqlSugar.JoinType.Left, c.AppId == a.Id
                }).Select((c, a) => new ChatsDto
                {
                    Id = c.Id,
                    UserName = c.UserName,
                    AppId = c.AppId,
                    IsSend = c.IsSend,
                    SendReveice = c.IsSend ? "发送" : "接收",
                    Context = c.Context,
                    CreateTime = c.CreateTime,
                    AppName = a.Name ?? "未知应用"
                }).OrderByDescending(c => c.CreateTime).ToPageList(_pageIndex, _pageSize);
                
                chatDtoList = chatList.ToArray();
            }
            else
            {
                _total = _chats_Repositories.Count(p => p.UserName.Contains(searchString) || p.Context.Contains(searchString));
                var chatList = _chats_Repositories.GetDB().Queryable<Chats, Apps>((c, a) => new object[] {
                    SqlSugar.JoinType.Left, c.AppId == a.Id
                }).Select((c, a) => new ChatsDto
                {
                    Id = c.Id,
                    UserName = c.UserName,
                    AppId = c.AppId,
                    IsSend = c.IsSend,
                    SendReveice = c.IsSend ? "发送" : "接收",
                    Context = c.Context,
                    CreateTime = c.CreateTime,
                    AppName = a.Name ?? "未知应用"
                }).Where(c => c.UserName.Contains(searchString) || c.Context.Contains(searchString))
                .OrderByDescending(c => c.CreateTime).ToPageList(_pageIndex, _pageSize);
                
                chatDtoList = chatList.ToArray();
            }
        }
        catch
        {
            // 处理异常，可以添加日志记录
            chatDtoList = Array.Empty<ChatsDto>();
        }
        return Task.CompletedTask;
    }

    private async Task Search(string searchKey)
    {
        _pageIndex = 1; // 重置到第一页
        _loading = true;
        StateHasChanged();
        await InitData();
        _loading = false;
        StateHasChanged();
    }

    public class ChatsDto : Chats
    {
        public string AppName { get; set; } = string.Empty;
        public string SendReveice { get; set; } = string.Empty;
    }
} 