using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;

namespace DL580_9130
{
    public partial class frmrecords : Form
    {
        public frmrecords()
        {
            InitializeComponent();
        }

        private void frmrecords_Load(object sender, EventArgs e)
        {
            dataGridView1.ColumnCount = 4;
            dataGridView1.Columns[0].HeaderText = Properties.Settings.Default.Setting;
            dataGridView1.Columns[1].HeaderText = Properties.Settings.Default.Setting2;
            dataGridView1.Columns[2].HeaderText = Properties.Settings.Default.Setting21;
            dataGridView1.Columns[3].HeaderText = "";
            dataGridView1.Columns[1].Width =300;
            dataGridView1.Columns[3].Visible = false;
        }
        private void TButton1_Click(object sender, EventArgs e)
        {
            refreshow("select * from cardno");
        }
        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(Properties.Settings.Default.Setting64 + "    ", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) != DialogResult.Yes)
            {
                return;
            }
            if (database.ExecuteNonQuery("delete * from cardno"))
            {
                dataGridView1.Rows.Clear();
                counttxt.Text = counttxt.Text = Properties.Settings.Default.Setting59 + ":" + dataGridView1.Rows.Count;
            }
        }
        public  bool refreshow(string sqlstr)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                dataGridView1.Rows.Clear();
                OdbcDataReader rs = database.Datareader(sqlstr);
                if (rs == null) { return false ; }
                string[] showstr = new string[4];
                while (rs.Read())
                {
                    showstr[0] = (dataGridView1.Rows.Count + 1).ToString();
                    showstr[1] = rs["cardno"].ToString().Trim();
                    showstr[2] = rs["date"].ToString().Trim();
                    showstr[3] = rs["id"].ToString().Trim();
                    dataGridView1.Rows.Add(showstr);
                }
                rs.Dispose();
                counttxt.Text = Properties.Settings.Default.Setting59 + ":" + dataGridView1.Rows.Count;
                return true;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void delButton_ButtonClick(object sender, EventArgs e)
        {
            int count = dataGridView1.SelectedRows.Count;
            if (count <= 0) { return; }
            if (MessageBox.Show(Properties.Settings.Default.Setting65 + "    ", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) != DialogResult.Yes)
            {
                return;
            }
            this.Cursor = Cursors.WaitCursor; 
            database.Transaction();
            for (int row = 0; row < count; row++)
            {
                if(!database.ExecuteNonQuery("delete from cardno where [id]="+ dataGridView1.SelectedRows[row].Cells[3].Value.ToString ()))
                {
                    database.Rollback();
                    this.Cursor = Cursors.Default; 
                    return;
                }
            }
            for (int row = 0; row < count; row++)
            {
                dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
            }
            database.Commit();
            this.Cursor = Cursors.Default; 
        }

        private void TButton2_Click(object sender, EventArgs e)
        {
            frmfind1 frm = new frmfind1(this);
            frm.Show(this);
        }
    }
}
