@using System.Reflection
@using System.ComponentModel.DataAnnotations
@using Shared.DTOs
@using Shared.DTOs.Warehouse
@typeparam T

<div class="row">
    <div class="col-md-12">
        <div class="mb-4">
            <div class="row">
                <div class="col-md-12">
                    <div class="card">
                        <div class="table-responsive text-nowrap">
                            <table class="table">
                                <thead class="table-light">
                                    <tr>
                                        @{
                                            PropertyInfo[] properties = typeof(T).GetProperties();
                                            foreach (var item in properties)
                                            {
                                                if (item.GetCustomAttributes(typeof(DisplayAttribute), false).Length > 0)
                                                {
                                                    <th>
                                                        @item.CustomAttributes.Where(w => w.AttributeType.Name == "DisplayAttribute").Select(s => s.NamedArguments.Where(w => w.MemberName == "Name").Select(ss => ss.TypedValue.Value).First()).First().ToString()
                                                    </th>
                                                }

                                            }
                                            <th>عملیات</th>
                                        }
                                    </tr>
                                </thead>
                                <tbody class="table-border-bottom-0">
                                    @{
                                        foreach (var item in ModelinComponent)
                                        {
                                            <tr>
                                                @{
                                                    properties = item.GetType().GetProperties();
                                                    int id = 0;
                                                    foreach (PropertyInfo property in properties)
                                                    {
                                                        if (property.Name.ToLower() == "id")
                                                            id = Convert.ToInt32(property.GetValue(item, null));

                                                        if (property.CustomAttributes.Any(w => w.AttributeType.Name == "DisplayAttribute"))
                                                        {

                                                            if (property.PropertyType == typeof(Nullable<System.Decimal>)
                                                            || property.PropertyType == typeof(System.Decimal) && item.ToString() != "Shared.DTOs.Warehouse.CirculationDto")
                                                            {
                                                                <td>
                                                                    @decimal.Parse(property.GetValue(item, null).ToString()).ToString("N0") ريال
                                                                </td>
                                                            }
                                                            else if (property.PropertyType == typeof(Nullable<System.Boolean>)
                                                            || property.PropertyType == typeof(System.Boolean))
                                                            {
                                                                var res = property.GetValue(item, null);
                                                                if (res == null)
                                                                {
                                                                    <td>
                                                                        ...
                                                                    </td>
                                                                }
                                                                else
                                                                    {
                                                                        if (res.ToString().ToLower()=="true")
                                                                        {
                                                                        <td>
                                                                            دارد
                                                                        </td>
                                                                        }
                                                                    else
                                                                    {
                                                                        <td>
                                                                            ندارد
                                                                        </td>
                                                                    }
                                                                    }
                                                             
                                                            }
                                                            else if (property.Name.ToLower() == "msgtype" && item.ToString() == "Shared.DTOs.Warehouse.CirculationDto")
                                                            {
                                                                var convertmodel = (CirculationDto)Convert.ChangeType(item, typeof(CirculationDto));
                                                                switch (convertmodel.Type)
                                                                {
                                                                    case TypeCirculation.Receipt:
                                                                        <td style="background-color:#ffab00">@property.GetValue(item, null)</td>
                                                                        break;

                                                                    case TypeCirculation.Remittance:
                                                                        <td style="background-color:#66c732">@property.GetValue(item, null)</td>
                                                                        break;

                                                                   

                                                                    default:
                                                                        <td>@property.GetValue(item, null)</td>
                                                                        break;
                                                                }
                                                            }
                                                            else if (property.Name.ToLower() == "id" && item.ToString() != "Shared.DTOs.SentTaxDto")
                                                            {
                                                                if (id > 0)
                                                                {
                                                                    if (OnMultipleOfThree.HasValue)
                                                                    {
                                                                        <td><button @onclick="()=>OnMultipleOfThree.Value.InvokeAsync(Convert.ToInt32(id))" type="button" class="btn btn-link">@property.GetValue(item, null)</button></td>
                                                                    }
                                                                    else if (OnMultipleOfThreeGeneric.HasValue)
                                                                    {
                                                                        <td><button @onclick="()=>OnMultipleOfThreeGeneric.Value.InvokeAsync(item)" type="button" class="btn btn-link">@property.GetValue(item, null)</button></td>
                                                                    }
                                                                }
                                                                else
                                                                {
                                                                    <td><button type="button" class="btn btn-link disabled">@property.GetValue(item, null)</button></td>
                                                                }

                                                            }
                                                            else if (property.Name.ToLower() == "msgsentstatus" && item.ToString() == "Shared.DTOs.SentTaxDto")
                                                            {
                                                                var convertmodel = (SentTaxDto)Convert.ChangeType(item, typeof(SentTaxDto));
                                                                switch (convertmodel.SentStatus)
                                                                {
                                                                    case SentStatus.Send:
                                                                        <td style="background-color:#6495ED">@property.GetValue(item, null)</td>
                                                                        break;

                                                                    case SentStatus.Successful:
                                                                        <td style="background-color:#2AD51B">@property.GetValue(item, null)</td>
                                                                        break;

                                                                    case SentStatus.Unsuccessful:
                                                                        <td style="background-color:#C70000">@property.GetValue(item, null)</td>
                                                                        break;

                                                                    case SentStatus.pending:
                                                                        <td style="background-color:#FCFF2A">@property.GetValue(item, null)</td>
                                                                        break;

                                                                    case SentStatus.IN_PROGRESS:
                                                                        <td style="background-color:#FCFF2A">@property.GetValue(item, null)</td>
                                                                        break;

                                                                    case SentStatus.NOT_FOUND:
                                                                        <td style="background-color:#F19109">@property.GetValue(item, null)</td>
                                                                        break;

                                                                    case SentStatus.Unknown:
                                                                        <td style="background-color:#932D2D">@property.GetValue(item, null)</td>
                                                                        break;

                                                                    default:
                                                                        <td>@property.GetValue(item, null)</td>
                                                                        break;
                                                                }
                                                            }
                                                            else
                                                            {
                                                                <td>@property.GetValue(item, null)</td>
                                                            }

                                                        }

                                                    }
                                                    if (item.ToString() == "Shared.DTOs.SentTaxDto")
                                                    {
                                                        var convertmodel = (SentTaxDto)Convert.ChangeType(item, typeof(SentTaxDto));
                                                        if (convertmodel.SentStatus == SentStatus.Send
                                                        || convertmodel.SentStatus == SentStatus.pending
                                                        || convertmodel.SentStatus == SentStatus.IN_PROGRESS)
                                                        {
                                                            if (OnMultipleOfThree.HasValue)
                                                            {
                                                                <td><button @onclick="()=>OnMultipleOfThree.Value.InvokeAsync(Convert.ToInt32(id))" type="button" class="btn btn-link">تعیین وضعیت</button></td>

                                                            }
                                                            else if (OnMultipleOfThreeGeneric.HasValue)
                                                            {
                                                                <td><button @onclick="()=>OnMultipleOfThreeGeneric.Value.InvokeAsync(item)" type="button" class="btn btn-link">تعیین وضعیت</button></td>

                                                            }
                                                        }
                                                        else if (convertmodel.SentStatus == SentStatus.Unsuccessful)
                                                        {
                                                            if (OnMultipleOfThree.HasValue)
                                                            {
                                                                <td><button @onclick="()=>OnMultipleOfThree.Value.InvokeAsync(Convert.ToInt32(id))" type="button" class="btn btn-link">نمایش خطا</button></td>

                                                            }
                                                            else if (OnMultipleOfThreeGeneric.HasValue)
                                                            {
                                                                <td><button @onclick="()=>OnMultipleOfThreeGeneric.Value.InvokeAsync(item)" type="button" class="btn btn-link">نمایش خطا</button></td>

                                                            }
                                                        }
                                                        else
                                                        {
                                                            <td><button type="button" class="btn btn-link disabled">...</button></td>
                                                        }
                                                    }

                                                    else
                                                    {
                                                        string Title = "ویرایش";
                                                        if (id > 0  || item.ToString() == "Shared.DTOs.Warehouse.CirculationDto" )
                                                        {
                                                            if (item.ToString() == "Shared.DTOs.Warehouse.CirculationDto")
                                                            {
                                                                 Title = "انتخاب";
                                                            }
                                                            if (OnMultipleOfThree.HasValue)
                                                            {
                                                                <td><button @onclick="()=>OnMultipleOfThree.Value.InvokeAsync(Convert.ToInt32(id))" type="button" class="btn btn-link">@Title</button></td>

                                                            }
                                                            else if (OnMultipleOfThreeGeneric.HasValue)
                                                            {
                                                                <td><button @onclick="()=>OnMultipleOfThreeGeneric.Value.InvokeAsync(item)" type="button" class="btn btn-link">@Title</button></td>

                                                            }
                                                        }
                                                        else
                                                        {
                                                            <td><button type="button" class="btn btn-link disabled">@Title</button></td>
                                                        }
                                                    }


                                                }
                                            </tr>
                                        }

                                    }
                                </tbody>
                            </table>
                        </div>
                    </div>


                </div>
            </div>
        </div>
    </div>
</div>

@code {
    [Parameter]
    public List<T> ModelinComponent { get; set; } = new List<T>();
    [Parameter] public EventCallback<int>? OnMultipleOfThree { get; set; }
    [Parameter] public EventCallback<T>? OnMultipleOfThreeGeneric { get; set; }

}
