@namespace ThingsGateway.Gateway.Razor
@using ThingsGateway.Admin.Application
@using ThingsGateway.Admin.Razor
@using ThingsGateway.Foundation
@using ThingsGateway.Gateway.Application
@inherits ComponentDefault

<div class="device">

    @if (ValidateEnable)
    {
        <ValidateForm Model="Model" OnValidSubmit="ValidSubmit">
            @renderFragment

            <div class="form-footer">

                <Button ButtonType="ButtonType.Submit" Icon="fa-solid fa-floppy-disk" IsAsync Text=@RazorLocalizer["Save"] />
            </div>
        </ValidateForm>

    }
    else
    {
        @renderFragment
    }
</div>

@code {
    RenderFragment renderFragment =>
    @<Tab>

        <TabItem Text=@GatewayLocalizer["DeviceInformation"]>

            <EditorForm class="p-2" AutoGenerateAllItem="false" RowType=RowType.Inline ItemsPerRow=2 LabelWidth=200 Model="Model">

                <FieldItems>
                    <EditorItem TValue="string" TModel="Device" @bind-Field="@context.Name">
                        <EditTemplate Context="value">
                            <div class="col-12">
                                <h6>@GatewayLocalizer["BasicInformation"]</h6>
                            </div>
                        </EditTemplate>
                    </EditorItem>

                    <EditorItem @bind-Field="@context.Name" Readonly=BatchEditEnable />

                    <EditorItem @bind-Field="@context.Description" />

                    <EditorItem @bind-Field="@context.Enable" />
                    <EditorItem @bind-Field="@context.LogLevel" />

                    <EditorItem TValue="string" TModel="Device" @bind-Field="@context.Name">
                        <EditTemplate Context="value">
                            <div class="col-12">
                                <h6>@GatewayLocalizer["Connection"]</h6>
                            </div>
                        </EditTemplate>
                    </EditorItem>

                    <EditorItem @bind-Field="@context.ChannelId">
                        <EditTemplate Context="value">
                            <div class="col-12  col-md-6 ">
                                <BootstrapInputGroup>
                                    <Select IsVirtualize DefaultVirtualizeItemText=@ChannelName @bind-Value="@value.ChannelId" IsDisabled=BatchEditEnable OnSelectedItemChanged=OnChannelChanged ShowSearch="true" ShowLabel="true" OnQueryAsync=OnChannelSelectedItemQueryAsync />
                                    <Button IsDisabled=BatchEditEnable class="text-end" Icon="fa-solid fa-plus" OnClick="AddChannel"></Button>
                                </BootstrapInputGroup>
                            </div>
                        </EditTemplate>
                    </EditorItem>

                    <EditorItem @bind-Field="@context.IntervalTime" />

                    <EditorItem TValue="string" TModel="Device" @bind-Field="@context.Description">
                        <EditTemplate Context="value">
                            <div class="col-12">
                                <h6>@GatewayLocalizer["Redundant"]</h6>
                            </div>
                        </EditTemplate>
                    </EditorItem>
                    <EditorItem @bind-Field="@context.RedundantEnable" Readonly=BatchEditEnable />
                    <EditorItem @bind-Field="@context.RedundantDeviceId">
                        <EditTemplate Context="value">
                            <div class="col-12  col-md-6">
                                <Select IsVirtualize DefaultVirtualizeItemText=@(DeviceName) @bind-Value="@value.RedundantDeviceId" class="w-100"
                                        OnQueryAsync="(a)=>OnRedundantDevicesQuery(a,value)" IsDisabled="BatchEditEnable"
                                        ShowSearch="true" IsClearable>
                                </Select>
                            </div>
                        </EditTemplate>

                    </EditorItem>

                    <EditorItem @bind-Field="@context.RedundantSwitchType" />
                    <EditorItem @bind-Field="@context.RedundantScanIntervalTime" />
                    <EditorItem @bind-Field="@context.RedundantScript" Rows="1" />

                    <EditorItem TValue="string" TModel="Device" @bind-Field="@context.Description">
                        <EditTemplate Context="value">
                            <div class="col-12">
                                <h6>@GatewayLocalizer["Remark"]</h6>
                            </div>
                        </EditTemplate>
                    </EditorItem>
                    <EditorItem @bind-Field="@context.Remark1" />
                    <EditorItem @bind-Field="@context.Remark2" />
                    <EditorItem @bind-Field="@context.Remark3" />
                    <EditorItem @bind-Field="@context.Remark4" />
                    <EditorItem @bind-Field="@context.Remark5" />

                </FieldItems>

            </EditorForm>
        </TabItem>
        @if (!BatchEditEnable)
    {
        <TabItem Text=@GatewayLocalizer["PluginInformation"]>
            @if (Model.ModelValueValidateForm != null && PluginPropertyEditorItems != null)
        {
        if (PluginPropertyRenderFragment == null)
        {
            <PropertyComponent Model="Model.ModelValueValidateForm" PluginPropertyEditorItems="PluginPropertyEditorItems" Id=@(Model.Id.ToString()) CanWrite="true" />
        }
        else
        {
            @PluginPropertyRenderFragment
        }
        }
        </TabItem>
    }

    </Tab>;

}