@using CMSMicroservice.Protobuf.Protos.UserContract
@inject Microsoft.JSInterop.IJSRuntime JsRuntime

<MudDialog>
    <DialogContent>
        <MudSimpleTable Dense="true">
            <tbody>
                <tr>
                    <td><strong>شناسه قرارداد:</strong></td>
                    <td>@Contract.Id</td>
                </tr>
                <tr>
                    <td><strong>شناسه کاربر:</strong></td>
                    <td>@Contract.UserId</td>
                </tr>
                <tr>
                    <td><strong>شناسه قالب قرارداد:</strong></td>
                    <td>@Contract.ContractId</td>
                </tr>
                <tr>
                    <td><strong>کد امضا:</strong></td>
                    <td>@Contract.SignGuid</td>
                </tr>
                <tr>
                    <td><strong>فایل PDF:</strong></td>
                    <td>
                        @if (!string.IsNullOrWhiteSpace(Contract.SignedPdfFile))
                        {
                            <MudButton Variant="Variant.Text" Color="Color.Primary" Size="Size.Small"
                                       StartIcon="@Icons.Material.Filled.PictureAsPdf"
                                       OnClick="DownloadPdf">
                                دانلود PDF
                            </MudButton>
                        }
                        else
                        {
                            <MudText Typo="Typo.body2" Color="Color.Secondary">بدون فایل</MudText>
                        }
                    </td>
                </tr>
            </tbody>
        </MudSimpleTable>
    </DialogContent>
    <DialogActions>
        <MudButton OnClick="Close">بستن</MudButton>
    </DialogActions>
</MudDialog>

@code {
    [CascadingParameter] private IMudDialogInstance MudDialog { get; set; } = null!;
    [Parameter] public GetAllUserContractByFilterResponseModel Contract { get; set; } = null!;

    private void Close() => MudDialog.Cancel();

    private async Task DownloadPdf()
    {
        if (!string.IsNullOrWhiteSpace(Contract.SignedPdfFile))
        {
            var title = $"قرارداد کاربر {Contract.UserId}";
            await JsRuntime.InvokeVoidAsync("jsHtmlToPdf", Contract.SignedPdfFile, title);
        }
    }
}
