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 System.Globalization;
using Stimulsoft.Report;

namespace check
{
    public partial class ckeck : Form
    {

        static SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\check.mdf;Integrated Security=True");
        PersianCalendar pc = new PersianCalendar();

        public ckeck()
        {
            InitializeComponent();
        }

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

        private string CheckSelect()
        {
            string _status = string.Empty;
            if (rb_barghashti.IsChecked)
                _status = "برگشتی";
            else if (rb_pass.IsChecked)
                _status = "پاس شده";
            else if (rb_batel.IsChecked)
                _status = "باطل شده";
            else if (rb_pasgherefte.IsChecked)
                _status = "پس گرفته";
            else
                _status = "در جریان";
            return _status;

        }


        private void SelectRbtn(string status)
        {
            if (status == "برگشتی")
                rb_barghashti.IsChecked = true;
            else if (status == "پاس شده")
                rb_pass.IsChecked = true;
            else if (status == "باطل شده")
                rb_batel.IsChecked = true;
            else if (status == "پس گرفته")
                rb_pasgherefte.IsChecked = true;
            else
                rb_jaryan.IsChecked = true;
        }

        public void GetAccountName(string name)
        {
            lbl_name_family.Text = name;
        }

        private void clear()
        {
            txt_code.Clear();
            lbl_namebanks.Text = "........................";
            date_ckeck.Text = "";
            txt_namber_ckeck.Clear();
            txt_babat.Clear();
            txt_vajh.Clear();
            txt_mablag.Clear();
            lbl_name_family.Text = "..............................";
            cmb_type_ckeck.Text = "";
            date_status.Text = "";
            date1.Text = "";
            date2.Text = "";
            date_one.Text = "";
            cmb_type_search.Text = "";
        }

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


        public void search_ckeckstatus()
        {
            DataSet ds = new DataSet();
            SqlDataAdapter adp = new SqlDataAdapter();
            adp.SelectCommand = new SqlCommand();
            adp.SelectCommand.Connection = con;
            adp.SelectCommand.CommandText = "select * from ckeck where ckeckstatus='" + cmb_type_search.Text + "'";
            adp.Fill(ds, "t1");
            dgv_ckeck.DataSource = ds;
            dgv_ckeck.DataMember = "t1";
        }


        public void search_one()
        {
            DataSet ds = new DataSet();
            SqlDataAdapter adp = new SqlDataAdapter();
            adp.SelectCommand = new SqlCommand();
            adp.SelectCommand.Connection = con;
            adp.SelectCommand.CommandText = "select * from ckeck where dateckeck='" + date_one.Text + "'";
            adp.Fill(ds, "t1");
            dgv_ckeck.DataSource = ds;
            dgv_ckeck.DataMember = "t1"; 
        }



        private void add()
        {
            con.Open();
            string s1 = "insert into ckeck(code,bankname,dateckeck,namberckeck,babat,vajh,mablag,namefamily,typeckeck,ckeckstatus,datestatus)values('" + txt_code.Text + "','" + lbl_namebanks.Text + "','" + date_ckeck.Text + "','" + txt_namber_ckeck.Text + "','" + txt_babat.Text + "','" + txt_vajh.Text + "','" + txt_mablag.Text + "','" + lbl_name_family.Text + "','" + cmb_type_ckeck.Text + "','" + CheckSelect() + "','" + date_status.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 ckeck set code='" + txt_code.Text + "',bankname='" + lbl_namebanks.Text + "',dateckeck='" + date_ckeck.Text + "',namberckeck='" + txt_namber_ckeck.Text + "',babat='" + txt_babat.Text + "',vajh='" + txt_vajh.Text + "',mablag='" + txt_mablag.Text + "',namefamily='" + lbl_name_family.Text + "',typeckeck='" + cmb_type_ckeck.Text + "',ckeckstatus='" + CheckSelect() + "',datestatus='" + date_status.Text + "' where code='" + txt_code.Text + "'";
            SqlCommand c2 = new SqlCommand(s2, con);
            if (MessageBox.Show("آیا می خواهید این را ویرایش کنید", "ویرایش", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                c2.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_ckeck.RowCount - 1; i++)
                {
                    if (Convert.ToBoolean(dgv_ckeck.Rows[i].Cells["Col1"].Value) == true)
                        Code = Code + dgv_ckeck.Rows[i].Cells[1].Value + ",";
                }
                Code = Code.Remove(Code.Length - 1, 1);
                SqlCommand cmd = new SqlCommand("delete from ckeck where code in (" + Code + ")", con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }



        private void count()
        {
            SqlCommand com = new SqlCommand("select count (*) from ckeck where ckeckstatus='در جریان'", con);
            con.Open();
            lbl_jaryan.Text = Convert.ToString((int)com.ExecuteScalar()) + "";
            con.Close();
            //--------------------------------------------------------------------------------------------
            SqlCommand com2 = new SqlCommand("select count (*) from ckeck where ckeckstatus='پاس شده'", con);
            con.Open();
            lbl_pass.Text = Convert.ToString((int)com2.ExecuteScalar()) + "";
            con.Close();
            //--------------------------------------------------------------------------------------------
            SqlCommand com3 = new SqlCommand("select count (*) from ckeck where ckeckstatus='برگشتی'", con);
            con.Open();
            lbl_barghashti.Text = Convert.ToString((int)com3.ExecuteScalar()) + "";
            con.Close();
            //--------------------------------------------------------------------------------------------
            SqlCommand com4 = new SqlCommand("select count (*) from ckeck where ckeckstatus='باطل شده'", con);
            con.Open();
            lbl_batel.Text = Convert.ToString((int)com4.ExecuteScalar()) + "";
            con.Close();
            //--------------------------------------------------------------------------------------------
            SqlCommand com5 = new SqlCommand("select count (*) from ckeck where ckeckstatus='پس گرفته'", con);
            con.Open();
            lbl_pasgherefte.Text = Convert.ToString((int)com5.ExecuteScalar()) + "";
            con.Close();
            //--------------------------------------------------------------------------------------------
        }


        public void print()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("code", typeof(string)));
            dt.Columns.Add(new DataColumn("bankname", typeof(string)));
            dt.Columns.Add(new DataColumn("dateckeck", typeof(string)));
            dt.Columns.Add(new DataColumn("namberckeck", typeof(string)));
            dt.Columns.Add(new DataColumn("babat", typeof(string)));
            dt.Columns.Add(new DataColumn("vajh", typeof(string)));
            dt.Columns.Add(new DataColumn("mablag", typeof(string)));
            dt.Columns.Add(new DataColumn("namefamily", typeof(string)));
            dt.Columns.Add(new DataColumn("typeckeck", typeof(string)));
            dt.Columns.Add(new DataColumn("ckeckstatus", typeof(string)));
            dt.Columns.Add(new DataColumn("datestatus", typeof(string)));

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

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

        private void ckeck_Load(object sender, EventArgs e)
        {
            count();
            show();
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(System.Globalization.CultureInfo.CreateSpecificCulture("fa-IR"));
    
        }

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

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

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

        private void dgv_ckeck_MouseClick(object sender, MouseEventArgs e)
        {
            txt_code.Text = dgv_ckeck.CurrentRow.Cells[1].Value.ToString();
            lbl_namebanks.Text = dgv_ckeck.CurrentRow.Cells[2].Value.ToString();
            date_ckeck.Text = dgv_ckeck.CurrentRow.Cells[3].Value.ToString();
            txt_namber_ckeck.Text = dgv_ckeck.CurrentRow.Cells[4].Value.ToString();
            txt_babat.Text = dgv_ckeck.CurrentRow.Cells[5].Value.ToString();
            txt_vajh.Text = dgv_ckeck.CurrentRow.Cells[6].Value.ToString();
            txt_mablag.Text = dgv_ckeck.CurrentRow.Cells[7].Value.ToString();
            lbl_name_family.Text = dgv_ckeck.CurrentRow.Cells[8].Value.ToString();
            cmb_type_ckeck.Text = dgv_ckeck.CurrentRow.Cells[9].Value.ToString();
            SelectRbtn(dgv_ckeck.CurrentRow.Cells[10].Value.ToString());
            date_status.Text = dgv_ckeck.CurrentRow.Cells[11].Value.ToString();
            
        }

        private void btn_search_type_Click(object sender, EventArgs e)
        {
            if (cmb_type_search.Text == "")
            {
                MessageBox.Show("لطفاً یکی از آیتم های مورد نظر را انتخاب کنید");
            }
            else
            {
                search_ckeckstatus();
            }
        }

        private void btn_accounts_Click(object sender, EventArgs e)
        {
            accountslist ac = new accountslist();
            ac.ShowDialog();
        }

        private void btn_date_now_ckeck_Click(object sender, EventArgs e)
        {
            string year, month, day;
            year = pc.GetYear(DateTime.Now).ToString("0000");
            month = pc.GetMonth(DateTime.Now).ToString("00");
            day = pc.GetDayOfMonth(DateTime.Now).ToString("00");

            date_ckeck.Text = year + "/" + month + "/" + day.ToString();
        }

        private void btn_now_date1_Click(object sender, EventArgs e)
        {
            string year, month, day;
            year = pc.GetYear(DateTime.Now).ToString("0000");
            month = pc.GetMonth(DateTime.Now).ToString("00");
            day = pc.GetDayOfMonth(DateTime.Now).ToString("00");

            date_status.Text = year + "/" + month + "/" + day.ToString();
        }

        private void btn_now_date2_Click(object sender, EventArgs e)
        {
            string year, month, day;
            year = pc.GetYear(DateTime.Now).ToString("0000");
            month = pc.GetMonth(DateTime.Now).ToString("00");
            day = pc.GetDayOfMonth(DateTime.Now).ToString("00");

            date1.Text = year + "/" + month + "/" + day.ToString();
        }

        private void btn_now_date3_Click(object sender, EventArgs e)
        {
            string year, month, day;
            year = pc.GetYear(DateTime.Now).ToString("0000");
            month = pc.GetMonth(DateTime.Now).ToString("00");
            day = pc.GetDayOfMonth(DateTime.Now).ToString("00");

            date2.Text = year + "/" + month + "/" + day.ToString();

        }

        private void btn_search_one_Click(object sender, EventArgs e)
        {
            string year, month, day;
            year = pc.GetYear(DateTime.Now).ToString("0000");
            month = pc.GetMonth(DateTime.Now).ToString("00");
            day = pc.GetDayOfMonth(DateTime.Now).ToString("00");

            date_one.Text = year + "/" + month + "/" + day.ToString();
        }

        private void btn_show_all_Click(object sender, EventArgs e)
        {
            show();
            clear();
        }

        private void btn_search_two_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            SqlDataAdapter adp = new SqlDataAdapter();
            adp.SelectCommand = new SqlCommand();
            adp.SelectCommand.Connection = con;
            adp.SelectCommand.CommandText = "select * from ckeck where dateckeck between '" + date1.Text + "' and '" + date2.Text + "'";
            adp.Fill(ds, "t1");
            dgv_ckeck.DataSource = ds;
            dgv_ckeck.DataMember = "t1";      
        }

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

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

        private void btn_print_Click(object sender, EventArgs e)
        {
            print();
        }

        private void btn_clear_Click(object sender, EventArgs e)
        {
            clear();
        }

    }
}
