using GMW_data;
using SteamWare;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace GMW.WebUserControls
{
  public partial class mod_trascPn2Part : ApplicationUserControl
  {
    /// <summary>
    /// Oggetto locale gestione classe datamatrix...
    /// </summary>
    protected DataMatrix DataMatrix_mgr = new DataMatrix();
    /// <summary>
    /// reset della selezione
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnReset_Click(object sender, EventArgs e)
    {
      resetSelezione();
    }
    /// <summary>
    /// resetta la selezione dei valori in caso di modifiche su altri controlli
    /// </summary>
    public void resetSelezione()
    {
      grView.SelectedIndex = -1;
      grView.DataBind();
      raiseEvent(ucEvType.Reset);
    }
    /// <summary>
    /// inserisce nuovo valore da footer
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lblIns_click(object sender, EventArgs e)
    {
      // click su inserimento, chiamo il metodo insert dell'ObjectDataSource
      ods.Insert();
    }
    /// <summary>
    /// gestione evento richiesta nuovo valore (mostra footer, ...)
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnNew_Click(object sender, EventArgs e)
    {
      // reset selezione...
      resetSelezione();
      // mostro il footer oppure la riga dei dettagli x nuovo...
      if (grView.FooterRow != null)
      {
        grView.FooterRow.Visible = true;
      }
    }
    /// <summary>
    /// determina se sia eliminabile il record (=non usato)
    /// </summary>
    /// <param name="idxMaker"></param>
    /// <returns></returns>
    public bool delEnabled(object idxObj)
    {
      bool answ = isWritable();

      return answ;
    }

    /// <summary>
    /// elenco colonne del datagrid
    /// </summary>
    /// <returns></returns>
    protected DataColumnCollection colonneObj()
    {
      DS_DataMatrix.TrascDatamatrixDataTable tabella = new DS_DataMatrix.TrascDatamatrixDataTable();
      DataColumnCollection colonne = tabella.Columns;
      return colonne;
    }
    /// <summary>
    /// recupera i dati di un nuovo record contenuti nel footer di un gridView;
    /// questi devono essere opportunamente nominati (es: txt{0}, dl{0}, ...)
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void recuperaFooter(object sender, ObjectDataSourceMethodEventArgs e)
    {
      //recupero la riga footer...
      DataColumnCollection colonne = colonneObj();
      string nomeCol;
      string tipoColonna = "";
      foreach (DataColumn colonna in colonne)
      {
        nomeCol = colonna.ColumnName;
        // cerco un textbox o quello che sia...
        if (grView.FooterRow.FindControl(string.Format("txt{0}", nomeCol)) != null)
        {
          tipoColonna = "textBox";
        }
        else if (grView.FooterRow.FindControl(string.Format("dl{0}", nomeCol)) != null)
        {
          tipoColonna = "dropDownList";
        }
        else if (grView.FooterRow.FindControl(string.Format("chk{0}", nomeCol)) != null)
        {
          tipoColonna = "checkBox";
        }
        else if (grView.FooterRow.FindControl(string.Format("selAjax_{0}", nomeCol)) != null)
        {
          tipoColonna = "selAjax";
        }
        // in base al tipo salvo negli inputparameters dell'ODS
        switch (tipoColonna)
        {
          case "textBox":
            e.InputParameters[nomeCol] = ((TextBox)grView.FooterRow.FindControl(string.Format("txt{0}", nomeCol))).Text;
            break;
          case "dropDownList":
            e.InputParameters[nomeCol] = ((DropDownList)grView.FooterRow.FindControl(string.Format("dl{0}", nomeCol))).SelectedValue;
            break;
          case "checkBox":
            e.InputParameters[nomeCol] = ((CheckBox)grView.FooterRow.FindControl(string.Format("chk{0}", nomeCol))).Checked;
            break;
          case "selAjax":
            e.InputParameters[nomeCol] = ((mod_selettore_ajax)grView.FooterRow.FindControl(string.Format("selAjax_{0}", nomeCol))).valore;
            break;
          default:
            break;
        }
        tipoColonna = "";
      }
      if (e.InputParameters["PartNumber"] == null) e.InputParameters["PartNumber"] = "";
      if (e.InputParameters["Particolare"] == null) e.InputParameters["Particolare"] = "";
      if (e.InputParameters["FormatoDtmx"] == null) e.InputParameters["FormatoDtmx"] = "";
    }

    protected void ods_Deleting(object sender, ObjectDataSourceMethodEventArgs e)
    {
      if (e.InputParameters["Original_PartNumber"] == null) e.InputParameters["Original_PartNumber"] = "";
    }

    protected void grView_SelectedIndexChanged(object sender, EventArgs e)
    {
      raiseEvent(ucEvType.Selected);
    }
    public string FormatoDtmx
    {
      get
      {
        return hfFormatoDtmx.Value;
      }
      set
      {
        hfFormatoDtmx.Value = value;
        grView.SelectedIndex = -1;
      }
    }
    public string PartNumber
    {
      get
      {
        return hfPartNumber.Value;
      }
      set
      {
        hfPartNumber.Value = value;
        grView.SelectedIndex = -1;
      }
    }
    public string PartNumberSel
    {
      get
      {
        string answ = "";
        try
        {
          answ = grView.SelectedDataKey["PartNumber"].ToString();
        }
        catch
        { }
        return answ;
      }
    }
    protected void ods_Updating(object sender, ObjectDataSourceMethodEventArgs e)
    {
      if (e.InputParameters["PartNumber"] == null) e.InputParameters["PartNumber"] = "";
      if (e.InputParameters["Particolare"] == null) e.InputParameters["Particolare"] = "";
      if (e.InputParameters["FormatoDtmx"] == null) e.InputParameters["FormatoDtmx"] = "";
      if (e.InputParameters["Original_PartNumber"] == null) e.InputParameters["Original_PartNumber"] = "";
    }

    protected void btnNewFromEmpty_Click(object sender, EventArgs e)
    {
      // creo record lunghezza 1...
      DataMatrix_mgr.taTDtmx.Insert("000", "000", FormatoDtmx);
      grView.DataBind();
    }
  }
}