//private void form_RemoveAt(FlowLayoutPanel boi_Filter, int index)
        //{
        //    if (index == countField)
        //        form_Remove(boi_Filter);
        //    else
        //    {
        //        listIndexRemove.Add(index);
        //        foreach (Control c in boi_Filter.Controls)
        //        {
        //            if (c.Name == "uc" + index.ToString())
        //            {
        //                c.Visible = false;
        //                break;
        //            }
        //        }
        //    }
        //}

        //private void form_Remove(FlowLayoutPanel boi_Filter)
        //{
        //    int id = countField;
        //    countField--;
        //    if (countField == -1) return;
        //    foreach (Control c in boi_Filter.Controls)
        //        if (c.Name == "uc" + id.ToString())
        //        {
        //            boi_Filter.Controls.Remove(c);
        //            break;
        //        }
        //}

        private void form_Submit(string dbName, string dbCaption, FlowLayoutPanel boi_Filter)
        {
            //string dbName = txt_Name.Text, dbCaption = txt_Caption.Text;
            if (string.IsNullOrEmpty(dbName))
            {
                MessageBox.Show("Please input Model Name and fields: name, type, caption.");
                return;
            }

            dbName = dbName.ToUpper().Trim();

            if (MessageBox.Show("Are you sure update model [" + dbName + "] ?", "Update Model",
                                MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation,
                                MessageBoxDefaultButton.Button2) != DialogResult.Yes)
            {
                return;
            }

            bool exist = db.ExistModel(dbName);

            if (exist)
            {
                MessageBox.Show("Model Name exist. Please choose other name.");
                return;
            }

            var li    = new List <FieldInfo>();
            int index = 1;

            foreach (Control c in boi_Filter.Controls)
            {
                var o  = new FieldInfo();
                int ki = 0;
                foreach (Control fi in c.Controls)
                {
                    #region
                    if (fi.Name == "name" + index.ToString())
                    {
                        o.NAME = (fi as TextBox).Text;
                    }
                    else if (fi.Name == "type" + index.ToString())
                    {
                        int ix = (fi as ComboBox).SelectedIndex;
                        o.TYPE_NAME = (fi as ComboBox).Items[ix].ToString();
                    }
                    else if (fi.Name == "auto" + index.ToString())
                    {
                        o.IS_KEY_AUTO = (fi as CheckBox).Checked;
                    }
                    else if (fi.Name == "kit" + index.ToString())
                    {
                        #region

                        if (o.IS_KEY_AUTO)
                        {
                            continue;
                        }

                        object _coltrol = (fi as ComboBox).SelectedItem;
                        if (_coltrol != null)
                        {
                            try
                            {
                                o.KIT = (ControlKit)((int)(_coltrol as ComboboxItem).Value);
                            }
                            catch { }
                        }

                        #endregion
                    }
                    else if (fi.Name == "link_type" + index.ToString())
                    {
                        if (o.IS_KEY_AUTO)
                        {
                            continue;
                        }
                        o.JOIN_TYPE = JoinType.NONE;
                        object ct = (fi as ComboBox).SelectedItem;
                        if (ct != null)
                        {
                            try
                            {
                                o.JOIN_TYPE = (JoinType)((int)(ct as ComboboxItem).Value);
                            }
                            catch { }
                        }
                    }
                    else if (fi.Name == "value_default" + index.ToString())
                    {
                        if (o.IS_KEY_AUTO)
                        {
                            continue;
                        }
                        string vd = (fi as TextBox).Text;
                        o.VALUE_DEFAULT = vd == null ? new string[] { } : vd.Split('|');
                    }
                    else if (fi.Name == "link_model" + index.ToString())
                    {
                        if (o.IS_KEY_AUTO)
                        {
                            continue;
                        }
                        object ct = (fi as ComboBox).SelectedItem;
                        if (ct != null)
                        {
                            o.JOIN_MODEL = (ct as ComboboxItem).Value as string;
                        }
                    }
                    else if (fi.Name == "link_field" + index.ToString())
                    {
                        if (o.IS_KEY_AUTO)
                        {
                            continue;
                        }
                        object ct = (fi as ComboBox).SelectedItem;
                        if (ct != null)
                        {
                            o.JOIN_FIELD = (ct as ComboboxItem).Value as string;
                        }
                    }
                    else if (fi.Name == "link_view" + index.ToString())
                    {
                        if (o.IS_KEY_AUTO)
                        {
                            continue;
                        }
                        object ct = (fi as ComboBox).SelectedItem;
                        if (ct != null)
                        {
                            o.JOIN_VIEW = (ct as ComboboxItem).Value as string;
                        }
                    }
                    else if (fi.Name == "index" + index.ToString())
                    {
                        o.IS_INDEX = (fi as CheckBox).Checked;
                    }
                    else if (fi.Name == "null" + index.ToString())
                    {
                        o.IS_ALLOW_NULL = (fi as CheckBox).Checked;
                        if (o.IS_KEY_AUTO || o.IS_INDEX)
                        {
                            o.IS_ALLOW_NULL = false;
                        }
                    }
                    else if (fi.Name == "caption" + index.ToString())
                    {
                        o.CAPTION = (fi as TextBox).Text;
                    }
                    else if (fi.Name == "caption_short" + index.ToString())
                    {
                        o.CAPTION_SHORT = (fi as TextBox).Text;
                    }
                    else if (fi.Name == "des" + index.ToString())
                    {
                        o.DESCRIPTION = (fi as TextBox).Text;
                    }
                    else if (fi.Name == "mobi" + index.ToString())
                    {
                        o.MOBI = (fi as CheckBox).Checked;
                    }
                    else if (fi.Name == "tablet" + index.ToString())
                    {
                        o.TABLET = (fi as CheckBox).Checked;
                    }
                    else if (fi.Name == "duplicate" + index.ToString())
                    {
                        o.IS_NOT_DUPLICATE = (fi as CheckBox).Checked;
                    }
                    else if (fi.Name == "encrypt" + index.ToString())
                    {
                        o.IS_ENCRYPT = (fi as CheckBox).Checked;
                    }
                    else if (fi.Name == "order_edit" + index.ToString())
                    {
                        int _vi = 0;
                        if (int.TryParse((fi as TextBox).Text, out _vi))
                        {
                            o.ORDER_EDIT = _vi;
                        }
                    }
                    else if (fi.Name == "order_view" + index.ToString())
                    {
                        int _vi = 0;
                        if (int.TryParse((fi as TextBox).Text, out _vi))
                        {
                            o.ORDER_VIEW = _vi;
                        }
                    }
                    else if (fi.Name == "func_edit" + index.ToString())
                    {
                        o.FUNC_EDIT = fi.Text == dbFunc.title_FUNC_VALIDATE_ON_FORM ? "": fi.Text;
                    }
                    else if (fi.Name == "func_before_update" + index.ToString())
                    {
                        o.FUNC_BEFORE_UPDATE = fi.Text == dbFunc.title_FUNC_BEFORE_ADD_OR_UPDATE ? "" : fi.Text;
                    }
                    else if (fi.Name == "key_url" + index.ToString())
                    {
                        o.ORDER_KEY_URL = string.IsNullOrEmpty(fi.Text) ? 0 : int.Parse(fi.Text);
                    }
                    else if (fi.Name == "show_in_grid" + index.ToString())
                    {
                        o.ONLY_SHOW_IN_DETAIL = (fi as CheckBox).Checked;
                    }
                    else if (fi.Name == "full_text_search" + index.ToString())
                    {
                        o.IS_FULL_TEXT_SEARCH = (fi as CheckBox).Checked;
                    }
                    else if (fi.Name == "key_for_sync" + index.ToString())
                    {
                        o.IS_KEY_SYNC_EDIT = (fi as CheckBox).Checked;
                    }
                    else if (fi.Name == "field_change" + index.ToString())
                    {
                        try
                        {
                            o.FieldChange = string.IsNullOrEmpty(fi.Text) ? dbFieldChange.NONE : (dbFieldChange)int.Parse(fi.Text);
                        }
                        catch { }
                    }
                    ki++;
                    #endregion
                }//end for fields

                if (!string.IsNullOrEmpty(o.NAME) && o.Type != null)
                {
                    #region
                    switch (o.KIT)
                    {
                    case ControlKit.RADIO:
                        o.JOIN_TYPE = JoinType.DEF_VALUE;
                        if (o.VALUE_DEFAULT == null || o.VALUE_DEFAULT.Length == 0 || (o.VALUE_DEFAULT.Length == 1 && o.VALUE_DEFAULT[0] == ""))
                        {
                            MessageBox.Show("Please input field [ " + o.NAME + " ] attributed [ Value Default ]");
                            return;
                        }
                        break;

                    case ControlKit.SELECT:
                        if (o.JOIN_TYPE == JoinType.DEF_VALUE && (o.VALUE_DEFAULT == null || o.VALUE_DEFAULT.Length == 0 || (o.VALUE_DEFAULT.Length == 1 && o.VALUE_DEFAULT[0] == "")))
                        {
                            MessageBox.Show("Please input field [ " + o.NAME + " ] attributed [ Value Default ]");
                            return;
                        }
                        if (o.JOIN_TYPE == JoinType.JOIN_MODEL && (string.IsNullOrEmpty(o.JOIN_MODEL) || string.IsNullOrEmpty(o.JOIN_FIELD)))
                        {
                            MessageBox.Show("Please input field [ " + o.NAME + " ] attributed [ JOIN MODEL - JOIN FIELD ]");
                            return;
                        }
                        break;

                    case ControlKit.LOOKUP:
                        if (o.JOIN_TYPE == JoinType.JOIN_MODEL && (string.IsNullOrEmpty(o.JOIN_MODEL) || string.IsNullOrEmpty(o.JOIN_FIELD)))
                        {
                            MessageBox.Show("Please input field [ " + o.NAME + " ] attributed [ JOIN MODEL - JOIN FIELD ]");
                            return;
                        }
                        break;
                    }

                    if (o.JOIN_TYPE == JoinType.JOIN_MODEL && !string.IsNullOrEmpty(o.JOIN_MODEL) && !string.IsNullOrEmpty(o.JOIN_FIELD))
                    {
                        string[] types = db.GetFields(o.JOIN_MODEL).Where(x => x.NAME == o.JOIN_FIELD).Select(x => x.TYPE_NAME).ToArray();
                        if (types.Length > 0)
                        {
                            o.TYPE_NAME = types[0];
                        }
                    }

                    if (o.JOIN_TYPE == JoinType.DEF_VALUE && o.VALUE_DEFAULT != null && o.VALUE_DEFAULT.Length >= 1 && o.VALUE_DEFAULT[0] != "")
                    {
                        o.TYPE_NAME = typeof(Int32).Name;
                    }

                    #endregion

                    if (o.FieldChange != dbFieldChange.REMOVE)
                    {
                        li.Add(o);
                    }
                }
                else
                {
                    MessageBox.Show("Please input fields: name, type, caption.");
                    c.Focus();
                    return;
                }
                index++;
            }//end for controls

            if (li.Count > 0)
            {
                DB_MODEL m = new DB_MODEL()
                {
                    NAME   = dbName.Replace(" ", "_").Trim().ToUpper(),
                    FIELDS = li.ToArray(),
                };
                if (OnSubmit != null)
                {
                    OnSubmit(m);
                }
            }
            else
            {
                MessageBox.Show("Please input fields: name, type, caption.");
            }
        }
        private void form_Submit(string dbName, string dbCaption, FlowLayoutPanel boi_Filter)
        {
            //string dbName = txt_Name.Text, dbCaption = txt_Caption.Text;
            if (string.IsNullOrEmpty(dbName))
            {
                MessageBox.Show("Please input Model Name and fields: name, type, caption.");
                return;
            }

            dbName = dbName.ToLower().Trim();
            bool exist = db.ExistModel(dbName);

            if (exist)
            {
                MessageBox.Show("Model Name exist. Please choose other name.");
                return;
            }

            var li    = new List <dbField>();
            int index = 1;

            foreach (Control c in boi_Filter.Controls)
            {
                if (listIndexRemove.IndexOf(index) != -1)
                {
                    continue;
                }

                var o  = new dbField();
                int ki = 0;
                foreach (Control fi in c.Controls)
                {
                    if (fi.Name == "name" + index.ToString())
                    {
                        o.Name = (fi as TextBox).Text;
                    }
                    else if (fi.Name == "type" + index.ToString())
                    {
                        int ix = (fi as ComboBox).SelectedIndex;
                        o.TypeName = (fi as ComboBox).Items[ix].ToString();
                    }
                    else if (fi.Name == "auto" + index.ToString())
                    {
                        o.IsKeyAuto = (fi as CheckBox).Checked;
                    }
                    else if (fi.Name == "kit" + index.ToString())
                    {
                        #region

                        if (o.IsKeyAuto)
                        {
                            continue;
                        }

                        object _coltrol = (fi as ComboBox).SelectedItem;
                        if (_coltrol != null)
                        {
                            try
                            {
                                o.Kit = (ControlKit)((int)(_coltrol as ComboboxItem).Value);
                            }
                            catch { }
                        }

                        #endregion
                    }
                    else if (fi.Name == "link_type" + index.ToString())
                    {
                        if (o.IsKeyAuto)
                        {
                            continue;
                        }
                        o.JoinType = JoinType.NONE;
                        object ct = (fi as ComboBox).SelectedItem;
                        if (ct != null)
                        {
                            try
                            {
                                o.JoinType = (JoinType)((int)(ct as ComboboxItem).Value);
                            }
                            catch { }
                        }
                    }
                    else if (fi.Name == "value_default" + index.ToString())
                    {
                        if (o.IsKeyAuto)
                        {
                            continue;
                        }
                        string vd = (fi as TextBox).Text;
                        o.ValueDefault = vd == null ? new string[] { } : vd.Split('|');
                    }
                    else if (fi.Name == "link_model" + index.ToString())
                    {
                        if (o.IsKeyAuto)
                        {
                            continue;
                        }
                        object ct = (fi as ComboBox).SelectedItem;
                        if (ct != null)
                        {
                            o.JoinModel = (ct as ComboboxItem).Value as string;
                        }
                    }
                    else if (fi.Name == "link_field" + index.ToString())
                    {
                        if (o.IsKeyAuto)
                        {
                            continue;
                        }
                        object ct = (fi as ComboBox).SelectedItem;
                        if (ct != null)
                        {
                            o.JoinField = (ct as ComboboxItem).Value as string;
                        }
                    }
                    else if (fi.Name == "caption" + index.ToString())
                    {
                        o.Caption = (fi as TextBox).Text;
                    }
                    else if (fi.Name == "index" + index.ToString())
                    {
                        o.IsIndex = (fi as CheckBox).Checked;
                    }
                    else if (fi.Name == "null" + index.ToString())
                    {
                        o.IsAllowNull = (fi as CheckBox).Checked;
                        if (o.IsKeyAuto || o.IsIndex)
                        {
                            o.IsAllowNull = false;
                        }
                    }
                    else if (fi.Name == "caption_short" + index.ToString())
                    {
                        o.CaptionShort = (fi as TextBox).Text;
                    }
                    else if (fi.Name == "des" + index.ToString())
                    {
                        o.Description = (fi as TextBox).Text;
                    }
                    else if (fi.Name == "mobi" + index.ToString())
                    {
                        o.Mobi = (fi as CheckBox).Checked;
                    }
                    else if (fi.Name == "tablet" + index.ToString())
                    {
                        o.Tablet = (fi as CheckBox).Checked;
                    }
                    else if (fi.Name == "duplicate" + index.ToString())
                    {
                        o.IsDuplicate = (fi as CheckBox).Checked;
                    }
                    else if (fi.Name == "encrypt" + index.ToString())
                    {
                        o.IsEncrypt = (fi as CheckBox).Checked;
                    }

                    ki++;
                }//end for fields

                if (!string.IsNullOrEmpty(o.Name) && o.Type != null)
                {
                    switch (o.Kit)
                    {
                    case ControlKit.CHECK:
                    case ControlKit.RADIO:
                        o.JoinType = JoinType.DEF_VALUE;
                        if (o.ValueDefault == null || o.ValueDefault.Length == 0 || (o.ValueDefault.Length == 1 && o.ValueDefault[0] == ""))
                        {
                            MessageBox.Show("Please input field [ " + o.Name + " ] attributed [ Value Default ]");
                            return;
                        }
                        break;

                    case ControlKit.SELECT:
                        if (o.JoinType == JoinType.DEF_VALUE && (o.ValueDefault == null || o.ValueDefault.Length == 0 || (o.ValueDefault.Length == 1 && o.ValueDefault[0] == "")))
                        {
                            MessageBox.Show("Please input field [ " + o.Name + " ] attributed [ Value Default ]");
                            return;
                        }
                        if (o.JoinType == JoinType.JOIN_MODEL && (string.IsNullOrEmpty(o.JoinModel) || string.IsNullOrEmpty(o.JoinField)))
                        {
                            MessageBox.Show("Please input field [ " + o.Name + " ] attributed [ JOIN MODEL - JOIN FIELD ]");
                            return;
                        }
                        break;

                    case ControlKit.LOOKUP:
                        if (o.JoinType == JoinType.JOIN_MODEL && (string.IsNullOrEmpty(o.JoinModel) || string.IsNullOrEmpty(o.JoinField)))
                        {
                            MessageBox.Show("Please input field [ " + o.Name + " ] attributed [ JOIN MODEL - JOIN FIELD ]");
                            return;
                        }
                        break;
                    }

                    if (o.JoinType == JoinType.JOIN_MODEL && !string.IsNullOrEmpty(o.JoinModel) && !string.IsNullOrEmpty(o.JoinField))
                    {
                        string[] types = db.GetFields(o.JoinModel).Where(x => x.Name == o.JoinField).Select(x => x.TypeName).ToArray();
                        if (types.Length > 0)
                        {
                            o.TypeName = types[0];
                        }
                    }

                    if (o.JoinType == JoinType.DEF_VALUE && o.ValueDefault != null)
                    {
                        o.TypeName = typeof(Int32).Name;
                    }

                    li.Add(o);
                }
                else
                {
                    MessageBox.Show("Please input fields: name, type, caption.");
                    c.Focus();
                    return;
                }

                index++;
            }//end for controls
            if (li.Count > 0)
            {
                dbModel m = new dbModel()
                {
                    Name   = dbName.Replace(" ", "_").Trim().ToUpper(),
                    Fields = li.ToArray(),
                };
                //if (OnSubmit != null) OnSubmit(m);
                generalApiController(dbName, li);
            }
            else
            {
                MessageBox.Show("Please input fields: name, type, caption.");
            }
        }