using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MercatorApi;
using MercatorUi;
using System.Windows.Forms;


namespace Param
{
    public class Customizer : MercatorUi.ICustomizers.IFormLoadCustomizer, MercatorUi.ICustomizers.ISqlCommandUpdater
    {

        public void FormLoadCustomize(Form WindowsForm)
        {
            if (WindowsForm is MercatorUi.Forms.Param.ParamRayonsForm) // on est dans l'écran de paramétrage des rayons
            {
                MercatorUi.Forms.Param.ParamRayonsForm paramRayonsForm = (MercatorUi.Forms.Param.ParamRayonsForm)WindowsForm;
                MercatorUi.GridPro.DataGridViewDoubleTextBoxColumn colCoeff = new MercatorUi.GridPro.DataGridViewDoubleTextBoxColumn();
                colCoeff.Name = "colCoeff";
                colCoeff.HeaderText = "Coeff.";
                colCoeff.Width = 80;
                colCoeff.DataPropertyName = "coeff";
                colCoeff.DisplayFormat = "##0.00";
                colCoeff.MinValue = 0;
                colCoeff.MaxValue = 999.99;
                colCoeff.SortMode = DataGridViewColumnSortMode.NotSortable;
                colCoeff.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                paramRayonsForm.Grid.Columns.Add(colCoeff);
            }
        }

        public void SqlCommandUpdate(System.Data.SqlClient.SqlCommand SqlCommandToModify, Form WindowsForm) // modification de la requête de sauvegarde des données
        {
            if (WindowsForm is MercatorUi.Forms.Param.ParamRayonsForm) // on est bien dans l'écran de paramétrage des rayons
            {
                if (!SqlCommandToModify.CommandText.Contains("coeff")) // cette modification ne doit être effectuée qu'une seule fois. Ensuite la requête modifiée est utilisée pour toutes les lignes de la grille ajoutées ou modifiées
                {
                    SqlCommandToModify.CommandText = SqlCommandToModify.CommandText.Replace(",taux_tva=@taux_tva", ",coeff=@coeff,taux_tva=@taux_tva");
                    SqlCommandToModify.CommandText = SqlCommandToModify.CommandText.Replace(",taux_tva)", ",coeff,taux_tva)");
                    SqlCommandToModify.CommandText = SqlCommandToModify.CommandText.Replace(",@taux_tva)", ",@coeff,@taux_tva)");
                }
                MercatorUi.Forms.Param.ParamRayonsForm paramRayonsForm = (MercatorUi.Forms.Param.ParamRayonsForm)WindowsForm;
                string id = SqlCommandToModify.Parameters["@id"].Value.ToString(); // il nous faut connaître l'ID de la ligne afin de retrouver le coefficient correspondant à cette ligne
                DataTable dt = (DataTable)paramRayonsForm.Grid.DataSource; // derrière la grille, se trouve en fait une DataTable
                DataRow[] foundRows = dt.Select(string.Format("id='{0}'", Api.UnquoteSql(id))); // recherche du coefficient sur base de l'ID.
                if (foundRows.Length > 0) // en principe, cette condition sera toujours remplie
                    SqlCommandToModify.Parameters.AddWithValue("@coeff", foundRows[0]["coeff"]); // on ajoute le paramètre SQL @coeff avec la bonne valeur
            }
        }

    }
}