using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MercatorApi;
using MercatorController;
using System.Data.Sql;
using System.Data.SqlClient;
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 Cle1 = sigForm.DataSource.Rows[0][ "s_cle1" ].ToString().Trim();
             string GamTyp = xFunctions.xLookUp( "GAMTYP" , "LIB" , Cle1, "ID" ).ToString();
             if (GamTyp == "" )
            {
                 if (Api.Answer( "Gamma creĆ«ren voor dit artikel ?" ))
                {
                    GamTyp = Api.Ident();
                    
                     // Toevoeging van het nieuwe gamma
                     SqlCommand oCommand = new SqlCommand ( string .Format( "insert into gamtyp(id,lib) values ('{0}','{1}')" , GamTyp, Cle1));
                     if (Api.SqlExec(MercatorUi.Globals.RepData, oCommand))
                    {
                         if (sigForm.DataSource.Rows[0][ "s_gamtyp2" ].ToString().Trim() != GamTyp.Trim())
                        {
                             List < Control > l = sigForm.FindMovableControlsBySource( "S_GAMTYP2" );
                             if (l.Count > 0)
                            {
                                 // Update van de comboboxen met de gamma's
                                 foreach (MercatorUi.MovableControls.MovableComboBox cboGamEnum in l)
                                {
                                     BindingList <MercatorUi._BaseClasses.MercatorComboItem> LCbo = ( BindingList <MercatorUi._BaseClasses.MercatorComboItem>) cboGamEnum.DataSource;
                                    MercatorUi._BaseClasses.MercatorComboItem item = new MercatorUi._BaseClasses.MercatorComboItem(Cle1.Trim(), GamTyp.Trim());
                                    LCbo.Add(item);
                                }
                                                                                                                             
                                 // Update van de waarde in de artikelfiche
                                sigForm.DataSource.Rows[0][ "s_gamtyp2" ] = GamTyp;
                            }
                        }
                         // Gammalijst weergeven
                        Globals.Main.ShowExternalForm( "" , "MercatorUi.Forms.Param.ParamGamEnumsForm" , "ParamGamEnumsForm" );
                         // Zich positioneren op het juiste gamma
                         List < Form > lGam = MercatorUi._Divers.OpenFormsByType( typeof (MercatorUi.Forms.Param.ParamGamEnumsForm));
                         if (lGam.Count > 0)
                        {
                            MercatorUi.Forms.Param.ParamGamEnumsForm GamEnumForm = (MercatorUi.Forms.Param.ParamGamEnumsForm) lGam[0];
                            GamEnumForm.ComboBoxType.SelectedValue = GamTyp.Trim();
                        }
                    }
                }
            }
        }
    }
}