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.Data.SqlClient;
using Stimulsoft.Report;

namespace check
{
    public partial class account : Form
    {
        static SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\check.mdf;Integrated Security=True");

        public account()
        {
            InitializeComponent();
        }


        public void GetBankName(string name)
        {
            lbl_namebanks.Text = name;
        }

        private void clear()
        {
            txt_code.Clear();
            txt_name_family.Clear();
            txt_number_account.Clear();
            txt_tel.Clear();
            txt_mob.Clear();
            txt_search.Clear();
        }



        public void show()
        {
            DataSet ds = new DataSet();
            SqlDataAdapter adp = new SqlDataAdapter();
            adp.SelectCommand = new SqlCommand();
            adp.SelectCommand.Connection = con;
            adp.SelectCommand.CommandText = "select * from account";
            adp.Fill(ds, "t1");
            dgv_account.DataSource = ds;
            dgv_account.DataMember = "t1";
        }


        private void search()
        {
            DataSet ds = new DataSet();
            SqlDataAdapter adp = new SqlDataAdapter();
            adp.SelectCommand = new SqlCommand();
            adp.SelectCommand.Connection = con;
            adp.SelectCommand.CommandText = "select * from account where namefamily='" + txt_search.Text + "'";
            adp.Fill(ds, "t1");
            dgv_account.DataSource = ds;
            dgv_account.DataMember = "t1";
        }

        private void add()
        {
            con.Open();
            string s1 = "insert into account(code,namefamily,numberaccount,bankname,tel,mob)values('" + txt_code.Text + "','" + txt_name_family.Text + "','" + txt_number_account.Text + "','" + lbl_namebanks.Text + "','" + txt_tel.Text + "','" + txt_mob.Text + "')";
            SqlCommand c1 = new SqlCommand(s1, con);
            if (MessageBox.Show("آیا می خواهید این را ثبت کنید", "ثبت", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                c1.ExecuteNonQuery();
                MessageBox.Show("ثبت شد");
                show();

            }

            con.Close();
        }


        private void edit()
        {
            con.Open();
            string s2 = "update account set code='" + txt_code.Text + "',namefamily='" + txt_name_family.Text + "',numberaccount='" + txt_number_account.Text + "',bankname='" + lbl_namebanks.Text + "',tel='" + txt_tel.Text + "',mob='" + txt_mob.Text + "' where code='" + txt_code.Text + "'";
            SqlCommand c3 = new SqlCommand(s2, con);
            if (MessageBox.Show("آیا می خواهید این را ویرایش کنید", "ویرایش", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                c3.ExecuteNonQuery();
                MessageBox.Show("ویرایش شد");
                show();

            }

            con.Close();
        }


        private void delete()
        {
            if (MessageBox.Show("آیا می خواهید این را پاک کنید؟", "اخطار حذف", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                string Code = string.Empty;

                for (int i = 0; i <= dgv_account.RowCount - 1; i++)
                {
                    if (Convert.ToBoolean(dgv_account.Rows[i].Cells["Col1"].Value) == true)
                        Code = Code + dgv_account.Rows[i].Cells[1].Value + ",";
                }
                Code = Code.Remove(Code.Length - 1, 1);
                SqlCommand cmd = new SqlCommand("delete from account where code in (" + Code + ")", con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            add();
            show();
            clear();
            txt_code.Focus();
        }

        private void btn_edit_Click(object sender, EventArgs e)
        {
            edit();
            show();
            clear();
            txt_code.Focus();
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            delete();
            show();
            clear();
            txt_code.Focus();
        }

        private void account_Load(object sender, EventArgs e)
        {
            show();

            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(System.Globalization.CultureInfo.CreateSpecificCulture("fa-IR"));
        }

        private void dgv_account_MouseClick(object sender, MouseEventArgs e)
        {

            txt_code.Text = dgv_account.CurrentRow.Cells[1].Value.ToString();
            txt_name_family.Text = dgv_account.CurrentRow.Cells[2].Value.ToString();
            txt_number_account.Text = dgv_account.CurrentRow.Cells[3].Value.ToString();
            lbl_namebanks.Text = dgv_account.CurrentRow.Cells[4].Value.ToString();
            txt_tel.Text = dgv_account.CurrentRow.Cells[5].Value.ToString();
            txt_mob.Text = dgv_account.CurrentRow.Cells[6].Value.ToString();

            
        }

        private void btn_banks_Click(object sender, EventArgs e)
        {
            bankslist bl = new bankslist();
            bl.ShowDialog();
        }

        private void btn_search_Click(object sender, EventArgs e)
        {
            search();
        }

        private void btn_print_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("code", typeof(string)));
            dt.Columns.Add(new DataColumn("namefamily", typeof(string)));
            dt.Columns.Add(new DataColumn("numberaccount", typeof(string)));
            dt.Columns.Add(new DataColumn("bankname", typeof(string)));
            dt.Columns.Add(new DataColumn("tel", typeof(string)));
            dt.Columns.Add(new DataColumn("mob", typeof(string)));

            int dtRow = 0;
            for (int i = 0; i <= dgv_account.RowCount - 1; i++)
            {
                if (Convert.ToBoolean(dgv_account.Rows[i].Cells["Col1"].Value) == true)
                {
                    dt.Rows.Add();
                    dt.Rows[dtRow][0] = dgv_account.Rows[i].Cells[1].Value;
                    dt.Rows[dtRow][1] = dgv_account.Rows[i].Cells[2].Value;
                    dt.Rows[dtRow][2] = dgv_account.Rows[i].Cells[3].Value;
                    dt.Rows[dtRow][3] = dgv_account.Rows[i].Cells[4].Value;
                    dt.Rows[dtRow][4] = dgv_account.Rows[i].Cells[5].Value;
                    dt.Rows[dtRow][5] = dgv_account.Rows[i].Cells[6].Value;
                    dtRow += 1;
                }
            }

            if (dt.Rows.Count > 0)
            {
                StiReport stiReport = new StiReport();
                stiReport.Load(Application.StartupPath + "\\selected-account.mrt");
                stiReport.RegBusinessObject("account", dt);
                stiReport.Show();
            }
        }

    }
}
