using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using MercatorApi;
using System.Windows.Forms;
using System.ComponentModel;


namespace MercatorUi.MovableControls.ButtonsCodes
{
    public static class Script
    {
        public static void Exec(MercatorUi.MovableControls.MovableButton clickedButton)
        {
            MercatorUi.Forms.Sig.SigForm sigForm = (MercatorUi.Forms.Sig.SigForm)clickedButton.Form;

            string newGamtyp = Dialogs.AskString("Welk nieuw gammatype?", "");
            if (string.IsNullOrEmpty(newGamtyp))
                return;
            newGamtyp = newGamtyp.Trim();
            if (MercatorController.xFunctions.xLookUpString("GAMTYP", "LIB", newGamtyp, "ID").TrimEnd() != "")
            {
                Dialogs.Stop("Dit gammatype bestaat al!");
                return;
            }
            string newGamtypId = Api.Ident();
            using (SqlCommand cmd = new SqlCommand("insert into GAMTYP (id,lib) values (@id,@lib)"))
            {
                cmd.Parameters.AddWithValue("id", newGamtypId).SqlDbType = SqlDbType.Char;
                cmd.Parameters.AddWithValue("lib", newGamtyp).SqlDbType = SqlDbType.Char;
                if (!Api.SqlExec(Globals.RepData, cmd))
                    return;
            }
            List<Control> l = clickedButton.Form.FindMovableControlsBySource("S_GAMTYP1");
            if (l.Count > 0)
            {
                MovableControls.MovableComboBox combo = (MovableControls.MovableComboBox)l[0];
                BindingList<MercatorUi._BaseClasses.MercatorComboItem> itemList = (BindingList<MercatorUi._BaseClasses.MercatorComboItem>)combo.DataSource;
                itemList.Add(new MercatorUi._BaseClasses.MercatorComboItem(newGamtyp, newGamtypId));
                combo.SelectedValue = newGamtypId;
            }
        }
    }
}