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

<div class="channel">

    @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 =>
@<EditorForm class="p-2" AutoGenerateAllItem="false" RowType=RowType.Inline ItemsPerRow=2 LabelWidth=200 Model="Model">

    <FieldItems>
        <EditorItem TValue="string" TModel="Channel" @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.PluginName">
            <EditTemplate Context="value">
                <div class="col-12  col-md-6">
                    <Select @bind-Value="@value.PluginName"
                            Items="@PluginNames" IsDisabled=BatchEditEnable
                            ShowSearch="true">
                        <ItemTemplate Context="name">
                            @if (PluginDcit.TryGetValue(name.Value, out var pluginOutput))
                                                        {
                                                        if (pluginOutput.EducationPlugin)
                                                        {
                            <div class="d-flex">
                                <span>@name.Text</span>
                                <div style="flex-grow: 1;"></div>
                                <Tag Color="Color.Primary">PRO</Tag>
                            </div>
                                                        }
                                                        else
                            {
                                <span>@name.Text</span>
                            }
                                                        }
                                                        else
                            {
                                <span>@name.Value</span>
                            }
                        </ItemTemplate>
                    </Select>
                </div>
            </EditTemplate>
        </EditorItem>

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

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

        <EditorItem @bind-Field="@context.ChannelType">

            <EditTemplate Context="value">
                <div class="col-12 col-sm-6 col-md-6">
                    <Select SkipValidate="true" @bind-Value="@value.ChannelType" OnSelectedItemChanged=@(async(a)=>
                                                                                                             {
                                                                                                                 await InvokeAsync(StateHasChanged);
                                                                                                                 await Task.Delay(100);
                                                                                                                 await InvokeAsync(StateHasChanged);
                                                                                                             }) />
                </div>
            </EditTemplate>

        </EditorItem>


        <EditorItem @bind-Field="@context.RemoteUrl" Ignore=@(context.ChannelType != ChannelTypeEnum.TcpClient && context.ChannelType != ChannelTypeEnum.UdpSession) />
        <EditorItem @bind-Field="@context.BindUrl" Ignore=@(context.ChannelType != ChannelTypeEnum.TcpClient && context.ChannelType != ChannelTypeEnum.UdpSession && context.ChannelType != ChannelTypeEnum.TcpService) />

        <EditorItem @bind-Field="@context.PortName" Ignore=@(context.ChannelType != ChannelTypeEnum.SerialPort) />
        <EditorItem @bind-Field="@context.BaudRate" Ignore=@(context.ChannelType != ChannelTypeEnum.SerialPort) />
        <EditorItem @bind-Field="@context.DataBits" Ignore=@(context.ChannelType != ChannelTypeEnum.SerialPort) />
        <EditorItem @bind-Field="@context.Parity" Ignore=@(context.ChannelType != ChannelTypeEnum.SerialPort) />
        <EditorItem @bind-Field="@context.StopBits" Ignore=@(context.ChannelType != ChannelTypeEnum.SerialPort) />
        <EditorItem @bind-Field="@context.DtrEnable" Ignore=@(context.ChannelType != ChannelTypeEnum.SerialPort) />
        <EditorItem @bind-Field="@context.RtsEnable" Ignore=@(context.ChannelType != ChannelTypeEnum.SerialPort) />
        <EditorItem @bind-Field="@context.StreamAsync" Ignore=@(context.ChannelType != ChannelTypeEnum.SerialPort) />
        <EditorItem @bind-Field="@context.Handshake" Ignore=@(context.ChannelType != ChannelTypeEnum.SerialPort) />


        <EditorItem @bind-Field="@context.CacheTimeout" Ignore=@(context.ChannelType == ChannelTypeEnum.UdpSession || context.ChannelType == ChannelTypeEnum.Other) />
        <EditorItem @bind-Field="@context.ConnectTimeout" Ignore=@(context.ChannelType == ChannelTypeEnum.UdpSession || context.ChannelType == ChannelTypeEnum.TcpService || context.ChannelType == ChannelTypeEnum.Other) />
        <EditorItem @bind-Field="@context.MaxConcurrentCount" Ignore=@(context.ChannelType == ChannelTypeEnum.Other) />


        <EditorItem @bind-Field="@context.MaxClientCount" Ignore=@(context.ChannelType != ChannelTypeEnum.TcpService) />
        <EditorItem @bind-Field="@context.CheckClearTime" Ignore=@(context.ChannelType != ChannelTypeEnum.TcpService) />

        <EditorItem @bind-Field="@context.Heartbeat" Ignore=@(context.ChannelType != ChannelTypeEnum.TcpService && context.ChannelType != ChannelTypeEnum.TcpClient && context.ChannelType != ChannelTypeEnum.UdpSession)>
            <EditTemplate Context="value">
                <div class="col-12 col-md-6">
                    <BootstrapInput @bind-Value=value.Heartbeat ShowLabel="true" class="me-2"></BootstrapInput>
                    <Checkbox @bind-Value=value.HeartbeatHex ShowAfterLabel DisplayText="hex"></Checkbox>
                </div>
            </EditTemplate>
        </EditorItem>


        <EditorItem @bind-Field="@context.HeartbeatTime" Ignore=@(context.ChannelType != ChannelTypeEnum.TcpClient && context.ChannelType != ChannelTypeEnum.UdpSession) />
        <EditorItem @bind-Field="@context.DtuId" Ignore=@(context.ChannelType != ChannelTypeEnum.TcpClient && context.ChannelType != ChannelTypeEnum.UdpSession)>


            <EditTemplate Context="value">
                <div class="col-12 col-md-6">
                    <BootstrapInput @bind-Value=value.DtuId ShowLabel="true" class="me-2"></BootstrapInput>
                    <Checkbox @bind-Value=value.DtuIdHex ShowAfterLabel DisplayText="hex"></Checkbox>
                </div>
            </EditTemplate>

        </EditorItem>

        <EditorItem @bind-Field="@context.DtuIdHex" Ignore=@(context.ChannelType != ChannelTypeEnum.TcpService) />

        <EditorItem @bind-Field="@context.DtuSeviceType" Ignore=@(context.ChannelType != ChannelTypeEnum.TcpService && context.ChannelType != ChannelTypeEnum.UdpSession) />

    </FieldItems>

</EditorForm>;

}