using FastReport.Dialog;
using System.Text;
using static FastReport.Web.Constants;

namespace FastReport.Web
{
    public partial class Dialog
    {
        private void ListBoxChange(ListBoxControl cb, int index)
        {
            cb.SelectedIndex = index;
            ControlFilterRefresh(cb);
            cb.OnSelectedIndexChanged(null);
        }

        private string GetListBoxHtml(ListBoxControl control)
        {
            if (control.Items.Count == 0)
            {
                control.FillData();
                ControlFilterRefresh(control);
            }
            string id = GetControlID(control);
            string html = $"<select style=\"{GetListBoxStyle(control)}\"" +
                $" name=\"{control.Name}\" size=\"{control.Items.Count}\"" +
                $" onchange=\"{GetEvent(ONCHANGE, control, DIALOG, $"document.getElementById('{id}').selectedIndex")}\"" +
                $" id=\"{id}\" {(control.Enabled ? "" : "disabled")}>{GetListBoxItems(control)}</select>";
            control.FilterData();
            return html;
        }

        private string GetListBoxItems(ListBoxControl control)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < control.Items.Count; i++)
            {
                sb.AppendFormat("<option {0}>{1}</option>",
                    i == control.SelectedIndex ? "selected" : "",
                    control.Items[i]);
            }
            return sb.ToString();
        }

        private string GetListBoxStyle(ListBoxControl control)
        {
            return GetStandardStyle(control);
        }        

    }
}
