using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;

namespace UniversalEditor
{
    public partial class columnBulkEdit : XtraForm
    {
        frmMain frmColBulkEdit;
        public columnBulkEdit(frmMain form)
        {
            frmColBulkEdit = form;
            InitializeComponent();
        }

        private void ColMultiplier_Load(object sender, EventArgs e)
        {
            if (frmColBulkEdit.file == null)
            {
                this.Close();
                return;
            }
            init();
        }

        public void init()
        {
            if (frmColBulkEdit.file.table == null) return;
            comboBox1.Properties.Items.Clear();
            for (int i = 0; i < frmColBulkEdit.file.table.Columns.Count; i++)
            {
                comboBox1.Properties.Items.Add(frmColBulkEdit.file.table.Columns[i].ColumnName);
            }
            comboBox1.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int colIndex = frmColBulkEdit.file.getColIndex(comboBox1.Properties.Items[comboBox1.SelectedIndex].ToString());
            if (colIndex < 0) return;

            if (checkBox1.Checked != true)
            {
                for (int i = 0; i < frmColBulkEdit.file.table.Rows.Count; i++)
                {
                    try
                    {
                        frmColBulkEdit.file.table.Rows[i][colIndex] = txtBulk.Text;
                        string t = frmColBulkEdit.FileTabs.SelectedTabPage.Text;
                        string n = t.Replace("*", String.Empty);
                        frmColBulkEdit.FileTabs.SelectedTabPage.Text = n;
                        frmColBulkEdit.FileTabs.SelectedTabPage.Text = frmColBulkEdit.FileTabs.SelectedTabPage.Text + "*";
                    }
                    catch (Exception ex) { XtraMessageBox.Show(ex.Message); break; }
                }
            }
            else
            {
                if (((DevExpress.XtraGrid.Views.Grid.GridView)frmColBulkEdit.dataGrid.Views[0]).SelectedRowsCount != 0)
                {
                    foreach (int RowHandle in ((DevExpress.XtraGrid.Views.Grid.GridView)frmColBulkEdit.dataGrid.Views[0]).GetSelectedRows())
                    {
                        try
                        {
                            frmColBulkEdit.file.table.Rows[RowHandle][colIndex] = txtBulk.Text;
                            string t = frmColBulkEdit.FileTabs.SelectedTabPage.Text;
                            string n = t.Replace("*", String.Empty);
                            frmColBulkEdit.FileTabs.SelectedTabPage.Text = n;
                            frmColBulkEdit.FileTabs.SelectedTabPage.Text = frmColBulkEdit.FileTabs.SelectedTabPage.Text + "*";
                        }
                        catch (Exception ex) { XtraMessageBox.Show(ex.Message); break; }
                    }
                }
                else 
                {
                    XtraMessageBox.Show("Please select the appropriate rows to modify.");
                    frmColBulkEdit.SQLStatus.Caption = "Please select the appropriate rows to modify."; 
                }
            }
            //this.Close();
            frmColBulkEdit.SQLStatus.Caption = comboBox1.SelectedItem.ToString() + " column values have been set to \"" + txtBulk.Text + "\".";
        }

    }
}
