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

namespace FastReport.Web
{
    public partial class Dialog
    {
        private void TextBoxChange(TextBoxControl tb, string data)
        {
            tb.Text = data;
            tb.FilterData();
            tb.OnTextChanged(null);
        }

        private string GetValHook()
        {
            return
                "<script>$.valHooks.textarea = {" +
                "get: function(elem) {" +
                "return elem.value.replace(/\\r?\\n/g, \'\\r\\n\');" +
                "}};</script>";
        }

        private string GetTextBoxHtml(TextBoxControl control)
        {
            string id = GetControlID(control);
            string html;
            if (control.Multiline)
            {
                html = $"<textarea style=\"{GetTextBoxStyle(control)}\" type=\"text\" name=\"{control.Name}\"" +
                    $" onchange=\"{GetEvent(ONCHANGE, control, DIALOG, $"document.getElementById('{id}').value.replace(/\\r?\\n/g, \'\\r\\n\')")}\"" +
                    $" id=\"{id}\" maxlength=\"{control.MaxLength}\" {(control.Enabled ? "" : "disabled")}>{control.Text}</textarea>";
            }
            else
            {
                html = $"<input style=\"{GetTextBoxStyle(control)}\" type=\"text\" name=\"{control.Name}\" value=\"{control.Text}\"" +
                    $" onchange=\"{GetEvent(ONCHANGE, control, DIALOG, $"document.getElementById('{id}').value")}\"" +
                    $" id=\"{id}\" maxlength=\"{control.MaxLength}\" {(control.Enabled ? "" : "disabled")}/>";
            }
            return html;
        }

        private string GetTextBoxStyle(TextBoxControl control)
        {
            return $"{GetStandardStyle(control)} {GetControlAlign(control)}";
        }
    }
}
