/*Funcion: insertar parametros
          Param: ParameterInsertView,AuditDataFromWeb
          Return: status
          */
        public CUDView insertParameter(ParameterInsertView parameter, AuditDataFromWeb audit)
        {
            ParameterDao Dao = new ParameterDao();
            String state = Dao.insertParameter(parameter);
            CUDView logic = CUDVerifyLogic.verifierInsertDeleteUpdate("insert", state);

            if (logic.insert == true)
            {
                AuditService auditservice = new AuditService();
                String Action = "Insert new parameter";
                String NewValues = "ParameterName: " + parameter.ParameterName
                    + ", ParameterMinValue: " + parameter.ParameterMinValue
                    + ", ParameterXPlot: " + parameter.ParameterXPlot
                    + ", ParameterYPlot: " + parameter.ParameterYPlot
                    + ", ParameterYPlotRange: " + parameter.ParameterYPlotRange
                    + ", ProcessId: " + parameter.ProcessId
                    + ", UnitId: " + parameter.UnitId;

                auditservice.formInsert(Action, NewValues, audit);
            }
            return logic;
        }
        private void saveAuxiliar()
        {
            if (ProductText.SelectedItem != null & ProcessText.SelectedItem != null)
            {

                if (ProductText.SelectedItem.Value != "" & ProcessText.SelectedItem.Value != "")
                {

                    int ProcessIdNew = Int32.Parse(HttpContext.Current.Session["ProcessId"].ToString());

                    if (verifyParameterTable())
                    {
                        ParameterService parameterService = new ParameterService();

                        //Insert Parameters
                        foreach (GridViewRow gvrow in GridView1.Rows)
                        {
                            ParameterInsertView parameter = new ParameterInsertView();

                            TextBox lblParameter = (TextBox)gvrow.FindControl("Parameter");
                            TextBox lblMinValue = (TextBox)gvrow.FindControl("MinValue");
                            DropDownList lblUnit = (DropDownList)gvrow.FindControl("Unit");
                            CheckBox lblxPlot = (CheckBox)gvrow.FindControl("xPlot");
                            CheckBox lblyPlot = (CheckBox)gvrow.FindControl("yPlot");
                            TextBox lblIncer = (TextBox)gvrow.FindControl("Incer");

                            if (lblParameter.Text != "")
                            {
                                /*System.Diagnostics.Debug.WriteLine(lblParameter.Text);
                                System.Diagnostics.Debug.WriteLine(lblMinValue.Text);
                                System.Diagnostics.Debug.WriteLine(lblUnit.SelectedItem.Value);
                                System.Diagnostics.Debug.WriteLine(lblxPlot.Checked);
                                System.Diagnostics.Debug.WriteLine(lblyPlot.Checked);
                                System.Diagnostics.Debug.WriteLine(lblIncer.Text);*/

                                parameter.ParameterMinValue = Double.Parse(lblMinValue.Text);
                                parameter.ParameterName = lblParameter.Text;
                                parameter.ParameterXPlot = lblxPlot.Checked;
                                parameter.ParameterYPlot = lblyPlot.Checked;
                                parameter.ParameterYPlotRange = Int32.Parse(lblIncer.Text);
                                parameter.ProcessId = ProcessIdNew;
                                parameter.UnitId = Int32.Parse(lblUnit.SelectedItem.Value);

                                CUDView crud = parameterService.insertParameter(parameter);
                                if (crud.insert == false)
                                {
                                    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Could not insert the Parameters')", true);
                                }
                            }
                        }
                        clearFields();
                        fillTable();
                    }
                    else
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Some parameters in Parameter Table has some incorrrect data format.')", true);
                    }
                }
            }
        }
        /*
        Metodo que se utiliza para realizar inserciones
        */
        private void saveAuxiliar()
        {
            //Se verifica que se haya  el producto y el proceso no sean nulos
            if (ProductText.SelectedItem != null & ProcessText.SelectedItem != null)
            {
                //Se verifica que se haya completado el producto y el proceso, es decir que no esten en blanco
                if (ProductText.SelectedItem.Value != "" & ProcessText.SelectedItem.Value != "")
                {
                    //Se verifica que se haya añadido al menos un parametro
                    if (validateParameter())
                    {
                        int ProcessIdNew = Int32.Parse(HttpContext.Current.Session["ProcessId"].ToString());

                        if (verifyParameterTable())
                        {
                            //Se crea el servicio
                            ParameterService parameterService = new ParameterService();

                            //Insert Parameters
                            foreach (GridViewRow gvrow in GridView1.Rows)
                            {
                                ParameterInsertView parameter = new ParameterInsertView();

                                TextBox lblParameter = (TextBox)gvrow.FindControl("Parameter");
                                TextBox lblMinValue = (TextBox)gvrow.FindControl("MinValue");
                                DropDownList lblUnit = (DropDownList)gvrow.FindControl("Unit");
                                CheckBox lblxPlot = (CheckBox)gvrow.FindControl("xPlot");
                                CheckBox lblyPlot = (CheckBox)gvrow.FindControl("yPlot");
                                TextBox lblIncer = (TextBox)gvrow.FindControl("Incer");

                                if (lblParameter.Text != "")
                                {
                                    parameter.ParameterMinValue = Double.Parse(lblMinValue.Text);
                                    parameter.ParameterName = lblParameter.Text;
                                    parameter.ParameterXPlot = lblxPlot.Checked;
                                    parameter.ParameterYPlot = lblyPlot.Checked;
                                    parameter.ParameterYPlotRange = Int32.Parse(lblIncer.Text);
                                    parameter.ProcessId = ProcessIdNew;
                                    parameter.UnitId = Int32.Parse(lblUnit.SelectedItem.Value);
                                    //Se llenan los datos de la auditoria
                                    String user = Context.User.Identity.Name;
                                    AuditDataFromWeb audit = new AuditDataFromWeb();
                                    audit.Reason = "N/A";
                                    audit.StationIP = General.getIp(this.Page);
                                    audit.UserName = user;

                                    //Se realiza la insercion
                                    CUDView crud = parameterService.insertParameter(parameter, audit);
                                    if (crud.insert == false)
                                    {
                                        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Could not insert the Parameters')", true);
                                    }
                                }
                            }
                            clearFields();
                            fillTable();
                        }
                        else
                        {
                            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Some parameters in Parameter Table has some incorrrect data format.')", true);
                        }
                    }
                    else
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Add at least one parameter.')", true);

                    }
                }
            }
        }
 /*
   Funcion: Inserta parametros
   Param: ParameterId, ProcessId,ParameterName,UnitId,ParameterMinValue,ParameterXPlot,ParameterYPlot,ParameterYPlotRange
   Return: Status
   */
 public String insertParameter(ParameterInsertView parameter)
 {
     return context.InsertUpdateDeleteMSQL("EXEC insertParameter @ProcessId=" + parameter.ProcessId + " , @ParameterName= '" + parameter.ParameterName
         + "', @UnitId= " + parameter.UnitId + " , @ParameterMinValue= " + parameter.ParameterMinValue + " , @ParameterXPlot= " + parameter.ParameterXPlot
         + " , @ParameterYPlot= " + parameter.ParameterYPlot + " , @ParameterYPlotRange= " + parameter.ParameterYPlotRange + " ;");
 }