@namespace AntSK.Pages.KmsPage
@using AntSK.Domain.Repositories
@using System.ComponentModel.DataAnnotations
@page "/Kms/Detail/{KmsID}"
@inject NavigationManager NavigationManager
@using AntSK.Services.Auth
@inherits AuthComponentBase


<Card Class="tabsCard">
    <CardTabs>
        <Tabs DefaultActiveKey="1">
            <TabPane Key="1">
                <TabTemplate>知识库文档</TabTemplate>
                <ChildContent>
                    <div class="standardList">
                        <Card Class="listCard"
                              Title="知识库文档">

                            <Extra>
                                <Button Type="@ButtonType.Primary" Style="position: absolute; right:360px; margin-bottom: 8px;" OnClick="Refresh">刷新 </Button>
                                <Dropdown Style="position: absolute; right: 20px; margin-bottom: 8px;" Trigger="@(new Trigger[] { Trigger.Click })">
                                    <Overlay>
                                        <Menu>
                                            @(_fileUpload(() => FileShowModal()))
                                            @(_urlUpload(() => UrlShowModal()))
                                            @(_textUpload(() => TextShowModal()))
                                            @(_excelUpload(() => ExcelShowModal()))
                                        </Menu>
                                    </Overlay>
                                    <ChildContent>
                                        <Button>导入 <Icon Type="down" /></Button>
                                    </ChildContent>
                                </Dropdown>
                                <div class="extraContent" style="margin-right:100px;">
                                    <Search Class="extraContentSearch" Placeholder="搜索文档" @bind-Value="_model.Id" />
                                </div>

                            </Extra>
                            <ChildContent>
                                <AntList TItem="KmsDetails"
                                         DataSource="_data"
                                         ItemLayout="ListItemLayout.Horizontal" Size="small">
                                    <ListItem Actions="@(new[] {
                            detail(()=> FileDetail(context.Id)) ,
                            delete(async ()=>await DeleteFile(context.Id)) ,
                             })">
                                        <ListItemMeta Description="@context.Id">
                                            <TitleTemplate>
                                                <div>文件ID</div>
                                            </TitleTemplate>
                                        </ListItemMeta>
                                        <ListItemMeta Description="@context.Type">
                                            <TitleTemplate>
                                                <div>文件类型</div>
                                            </TitleTemplate>
                                        </ListItemMeta>
                                        @if (@context.Type == "file" || @context.Type == "excel")
                                        {
                                            <ListItemMeta Avatar="" Description="@context.FileName">
                                                <TitleTemplate>
                                                    <a>文件名称</a>
                                                </TitleTemplate>
                                            </ListItemMeta>
                                        }
                                        else if (@context.Type == "url")
                                        {
                                            <ListItemMeta Avatar="" Description="@context.Url">
                                                <TitleTemplate>
                                                    <a href="@context.Url" target="_blank">Url</a>
                                                </TitleTemplate>
                                            </ListItemMeta>
                                        }
                                        else if (@context.Type == "text")
                                        {
                                            <ListItemMeta Avatar="" Description="……">
                                                <TitleTemplate>
                                                    <a>文本内容</a>
                                                </TitleTemplate>
                                            </ListItemMeta>
                                        }
                                        <ListItemMeta Avatar="" Description="@context.DataCount.ToString()">
                                            <TitleTemplate>
                                                <a>文档切片数量</a>
                                            </TitleTemplate>
                                        </ListItemMeta>
                                        <ListItemMeta Avatar="" Description="@context.Status.ToString()">
                                            <TitleTemplate>
                                                <a>状态</a>
                                            </TitleTemplate>
                                        </ListItemMeta>
                                        <div class="listContent">
                                            <div class="listContentItem">
                                                <span>创建时间</span>
                                                <p>@context.CreateTime.ToString("yyyy-MM-dd HH:mm")</p>
                                            </div>
                                        </div>
                                    </ListItem>
                                </AntList>
                            </ChildContent>
                        </Card>
                    </div>
                </ChildContent>
            </TabPane>
            <TabPane Key="2">
                <TabTemplate>搜索测试</TabTemplate>
                <ChildContent>
                    <KmsTest KmsId="@KmsId"></KmsTest>
                </ChildContent>
            </TabPane>
        </Tabs>
    </CardTabs>
</Card>

<Modal Title="链接读取"
       Visible="@_urlVisible"
       OnOk="@UrlHandleOk"
       OnCancel="@UrlHandleCancel"
       ConfirmLoading="@_urlConfirmLoading">
    <Form Model="@urlModel"
          LabelColSpan="8"
          WrapperColSpan="16"
          @ref="@_urlForm">
        <FormItem Label="URL地址">
            <Input @bind-Value="@context.Url" />
        </FormItem>
        <FormItem Label="切分方式">
            <RadioGroup @bind-Value="@_isQa">
                <Radio Value="false">直接切分</Radio>
                <Radio Value="true">QA切分</Radio>
            </RadioGroup>
        </FormItem>
    </Form>
</Modal>

<Modal Title="文本导入"
       Visible="@_textVisible"
       OnOk="@TextHandleOk"
       OnCancel="@TextHandleCancel"
       ConfirmLoading="@_textConfirmLoading">
    <Form Model="@textModel"
          LabelColSpan="8"
          WrapperColSpan="16"
          @ref="@_textForm">  
        <FormItem Label="文本内容">
            <TextArea @bind-Value="@context.Text" Rows="5" />
        </FormItem>
        <FormItem Label="切分方式">
            <RadioGroup @bind-Value="@_isQa">
                <Radio Value="false">直接切分</Radio>
                <Radio Value="true">QA切分</Radio>
            </RadioGroup>
        </FormItem>
    </Form>
</Modal>

<Modal Title="文件导入"
       Visible="@_fileVisible"
       OnOk="@FileHandleOk"
       OnCancel="@FileHandleCancel"
       ConfirmLoading="@_fileConfirmLoading">   
    <Upload Action="@("api/File/UploadFile")"
            Name="file"
            Drag
            Multiple
            Accept="*/*"
            BeforeUpload="iKMService.BeforeUpload"
            OnSingleCompleted="iKMService.OnSingleCompleted">
        <p class="ant-upload-drag-icon">
            <Icon Type="inbox" />
        </p>
        <p class="ant-upload-text">单击或拖动文件到此区域进行上传</p>
        @if (km.IsOCR == 1)
        {
            <p class="ant-upload-hint">
                支持 txt、word、pdf、md、excel、ppt、jpeg、png、tiff 等文件。
            </p>
        }
        else
        {
            <p class="ant-upload-hint">
                支持 txt、word、pdf、md、excel、ppt 等文件。
            </p>
        }

    </Upload>
    <RadioGroup @bind-Value="@_isQa" Style="margin-top:5px;">
        <Radio Value="false">直接切分</Radio>
        <Radio Value="true">QA切分</Radio>
    </RadioGroup>
</Modal>

<Modal Title="Excel导入"
       Visible="@_excelVisible"
       OnOk="@ExcelHandleOk"
       OnCancel="@ExcelHandleCancel"
       ConfirmLoading="@_excelConfirmLoading">
    <a href="api/File/DownExcelTemplate" target="_blank">下载模板</a>
    <Upload Action="@("api/File/UploadFile")"
            Name="file"
            Drag
            Multiple
            Accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            BeforeUpload="iKMService.BeforeUpload"
            OnSingleCompleted="iKMService.OnSingleCompleted">
        <p class="ant-upload-drag-icon">
            <Icon Type="inbox" />
        </p>
        <p class="ant-upload-text">单击或拖动文件到此区域进行上传</p>
        <p class="ant-upload-hint">
            支持excel文件。
        </p>
    </Upload>
</Modal>

@code {
    RenderFragment _fileUpload(Action clickAction) =>@<MenuItem>
        <a target="_blank" rel="noopener noreferrer" @onclick="@clickAction">
            文件导入
        </a>
    </MenuItem>;

    RenderFragment _urlUpload(Action clickAction) =>@<MenuItem>
        <a target="_blank" rel="noopener noreferrer" @onclick="@clickAction">
            链接读取
        </a>
    </MenuItem>;

    RenderFragment _textUpload(Action clickAction) =>@<MenuItem>
        <a target="_blank" rel="noopener noreferrer" @onclick="@clickAction">
            文本导入
        </a>
    </MenuItem>;

    RenderFragment _excelUpload(Action clickAction) =>@<MenuItem>
        <a target="_blank" rel="noopener noreferrer" @onclick="@clickAction">
            Excel导入
        </a>
    </MenuItem>;

    RenderFragment detail(Action clickAction) => @<a key="detail" @onclick="@clickAction">详情</a>;
    RenderFragment delete(Action clickAction) => @<a key="edit" @onclick="@clickAction">删除</a>;
}
