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.Globalization;
using FarsiMsgBox;


namespace ajans
{
    public partial class form_account : Form
    {
        PersianCalendar PersianDateTime = new PersianCalendar();

        string time = "";
        string date = "";

        DataView objdv;

        public form_account()
        {
            InitializeComponent();
        }

        private void CorrectTime()
        {
            DateTime tt = DateTime.Now;
            string SX = tt.Hour.ToString("D2");
            string SY = tt.Minute.ToString("D2");
            string SZ = tt.Second.ToString("D2");
            time = SX + ":" + SY + ":" + SZ;
        }


        private void CorrectDate()
        {
            DateTime dd = DateTime.Now;
            date = PersianDateTime.GetYear(dd).ToString() + "/" + PersianDateTime.GetMonth(dd).ToString("D2") + "/" + PersianDateTime.GetDayOfMonth(dd).ToString("D2");
        }


        private void form_account_FormClosed(object sender, FormClosedEventArgs e)
        {
            static_variables.form_account_show = false;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            this.Opacity = 1;
            for (int i = 100; i > 1; i--)
            {
                this.Opacity -= 0.01;
                Application.DoEvents();
            }
            this.Close();
        }

        private void dvramount_Click(object sender, EventArgs e)
        {
            if (txtdvrid.Text == "")
            {
                FarsiMessage.ShowMessageBox("لطفا کد راننده را وارد نمایید", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                txtdvrid.Focus();
            }
            else
            {
                service Service = new service();
                dgListDriverAmount.DataSource = Service.SelectDriverAmount(txtdvrid.Text);
                dgListDriverAmount.DataMember = "service";
            }
        }

        private void dvramount2_Click(object sender, EventArgs e)
        {
            service Service = new service();
            dgListDriverAmount.DataSource = Service.SelectDriverAmount2(txtdvrid.Text);
            dgListDriverAmount.DataMember = "driver";
        }

        private void dvramount4_Click(object sender, EventArgs e)
        {
            if (txtdvrid.Text == "")
            {
                FarsiMessage.ShowMessageBox("لطفا کد راننده را وارد نمایید", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                txtdvrid.Focus();
            }
            else if (txtdate1.Text == "")
            {
                FarsiMessage.ShowMessageBox("لطفا تاریخ اول را طبق نمونه وارد نمایید", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                txtdate1.Focus();
            }
            else if (txtdate2.Text == "")
            {
                FarsiMessage.ShowMessageBox("لطفا تاریخ دوم را طبق نمونه وارد نمایید", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                txtdate2.Focus();
            }
            else
            {
                service Service = new service();
                dgListDriverAmount.DataSource = Service.SelectDriverAmount(txtdvrid.Text, txtdate1.Text, txtdate2.Text);
                dgListDriverAmount.DataMember = "service";
            }
        }

        private void dvramount3_Click(object sender, EventArgs e)
        {
            if (txtdvrid.Text == "")
            {
                FarsiMessage.ShowMessageBox("لطفا کد راننده را وارد نمایید", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                txtdvrid.Focus();
            }
            else if (txtdate.Text == "")
            {
                FarsiMessage.ShowMessageBox("لطفا تاریخ را طبق نمونه وارد نمایید", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                txtdate.Focus();
            }
            else
            {
                service Service = new service();
                dgListDriverAmount.DataSource = Service.SelectDriverAmount(txtdvrid.Text, txtdate.Text);
                dgListDriverAmount.DataMember = "service";
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (txtdvrid.Text == "")
            {
                FarsiMessage.ShowMessageBox("لطفا کد راننده را وارد نمایید", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                txtdvrid.Focus();
            }
            else
            {
                service Service = new service();
                dgListDriverAmount.DataSource = Service.SelectDriverAmount3(txtdvrid.Text);
                dgListDriverAmount.DataMember = "service";

                int sum = 0;
                for (int i = 0; i <= dgListDriverAmount.Rows.Count - 1; i++)
                {
                    sum += Convert.ToInt32(dgListDriverAmount["مبلغ", i].Value);
                }
                txtsumamount.Text = sum.ToString();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (txtsumamount.Text == "")
            {
                FarsiMessage.ShowMessageBox("ابتدا کد راننده را وارد نموده و سپس مجموع مبالغ را بدست آورید.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                txtdvrid.Focus();
            }
            else if (txt100.Text == "")
            {
                FarsiMessage.ShowMessageBox("لطفا درصد را وارد نمایید", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                txt100.Focus();
            }
            else
            {
                int A = Int32.Parse(txtsumamount.Text);
                int B = Int32.Parse(txt100.Text);
                double Result = (A * B) / 100;
                lblAjans.Text = Result.ToString();
                lblDriver.Text = (A - Result).ToString();
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (txtdvrid.Text == "")
            {
                FarsiMessage.ShowMessageBox("لطفا کد راننده را وارد نمایید", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                txtdvrid.Focus();
            }
            else
            {
                service Service = new service();
                dgListDriverAmount.DataSource = Service.SelectDriverAmount4(txtdvrid.Text);
                dgListDriverAmount.DataMember = "service";

                int sum = 0;
                for (int i = 0; i <= dgListDriverAmount.Rows.Count - 1; i++)
                {
                    sum += Convert.ToInt32(dgListDriverAmount["مبلغ", i].Value);
                }
                txtsumamount.Text = sum.ToString();
            }
        }

        private void txtsumamount_TextChanged(object sender, EventArgs e)
        {
            lblAjans.Text = "";
            lblDriver.Text = "";
        }

        private void txt100_TextChanged(object sender, EventArgs e)
        {
            lblDriver.Text = "";
            lblAjans.Text = "";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (lblDriver.Text == "" || lblAjans.Text == "")
            {
                FarsiMessage.ShowMessageBox("سهم آژانس و راننده باید مشخص باشد.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
            }
            else
            {
                service Service = new service();
                for (int i = 0; i <= dgListDriverAmount.Rows.Count - 1; i++)
                {
                    Service.UpdateServiceValue((dgListDriverAmount["کد سرویس", i].Value).ToString());
                }
                Service.InsertAmount(txtdvrid.Text, lbldate.Text, lbltime.Text, lblAjans.Text);
                lblAjans.Text = "";
                lblDriver.Text = "";
                txtsumamount.Text = "";
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            CorrectTime();
            CorrectDate();
            lbltime.Text = time;
            lbldate.Text = date;
        }

        private void txtdvrid_TextChanged(object sender, EventArgs e)
        {
            lblAjans.Text = "";
            lblDriver.Text = "";
            txtsumamount.Text = "";
        }

        public void form_account_Load(object sender, EventArgs e)
        {
            account Account = new account();
            objdv = new DataView(Account.SelectAllStock().Tables["coffer"]);
            lblcoffer.DataBindings.Clear();
            lblcoffer.Text = "";
            lblcoffer.DataBindings.Add("text", objdv, "کل موجودی");
        }

        private void btnAll_Click(object sender, EventArgs e)
        {
            account Account = new account();
            objdv = new DataView(Account.SelectAllStock().Tables["coffer"]);
            dgListAjansAmount.DataSource = Account.SelectAllStock();
            dgListAjansAmount.DataMember = "coffer";
            lblcoffer.DataBindings.Clear();
            lblcoffer.Text = "";
            lblcoffer.DataBindings.Add("text", objdv, "کل موجودی");

        }

        private void btnperson_Click(object sender, EventArgs e)
        {
            if (txtdvridA.Text == "")
            {
                FarsiMessage.ShowMessageBox("لطفا کد راننده را وارد نمایید", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                txtdvridA.Focus();
            }
            else
            {
                account Account = new account();
                dgListAjansAmount.DataSource = Account.SelectpersonAmount(txtdvridA.Text);
                dgListAjansAmount.DataMember = "amount";
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (txtdateA1.Text == "")
            {
                FarsiMessage.ShowMessageBox("لطفا تاریخ اول را طبق نمونه وارد نمایید", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                txtdateA1.Focus();
            }
            else if (txtdateA2.Text == "")
            {
                FarsiMessage.ShowMessageBox("لطفا تاریخ دوم را طبق نمونه وارد نمایید", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                txtdateA2.Focus();
            }
            else
            {
                account Account = new account();
                dgListAjansAmount.DataSource = Account.SelectAmount(txtdateA1.Text, txtdateA2.Text);
                dgListAjansAmount.DataMember = "amount";
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (txtdateA.Text == "")
            {
                FarsiMessage.ShowMessageBox("لطفا تاریخ را طبق نمونه وارد نمایید", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                txtdateA.Focus();
            }
            else
            {
                account Account = new account();
                dgListAjansAmount.DataSource = Account.SelectAmount(txtdateA.Text);
                dgListAjansAmount.DataMember = "amount";
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (lblcoffer.Text == "") lblcoffer.Text = "0";
            if (txtam.Text == "")
            {
                FarsiMessage.ShowMessageBox("لطفا مبلغ برداشتی را وارد کنید.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
            }
            else if (Int32.Parse(txtam.Text) > Int32.Parse(lblcoffer.Text))
            {
                FarsiMessage.ShowMessageBox("موجودی صندوق کمتر از مبلغ درخواستی شما می باشد.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
            }
            else
            {
                FarsiMessage.ShowMessageBox("آیا برای برداشت موجودی مطمئن هستید؟", "پیغام", ShowButton.بله_خیر, MsgIcon.Question_1);
                if (FarsiMessage.ClickButton == ButtonClicked.بله)
                {
                    account Account = new account();
                    Account.UpdateCofferStock(txtam.Text);
                    objdv = new DataView(Account.SelectAllStock().Tables["coffer"]);
                    lblcoffer2.DataBindings.Clear();
                    lblcoffer2.Text = "";
                    lblcoffer2.DataBindings.Add("text", objdv, "کل موجودی");
                    frmShow();
                }
            }
        }

        private void txtdvridA_KeyPress(object sender, KeyPressEventArgs e)
        {
            int i = e.KeyChar;
            if (i < 8 || 8 < i && i < 48 || i > 57)
            {
                e.Handled = true;
                System.Console.Beep();
            }

            else
                e.Handled = false;
        }

        private void txtam_KeyPress(object sender, KeyPressEventArgs e)
        {
            int i = e.KeyChar;
            if (i < 8 || 8 < i && i < 48 || i > 57)
            {
                e.Handled = true;
                System.Console.Beep();
            }

            else
                e.Handled = false;
        }

        private void frmShow()
        {
            form_rpt_coffer frm = new form_rpt_coffer();
            frm.lbldate.Text = this.lbldate.Text;
            frm.lbltime.Text = this.lbltime.Text;
            frm.lblafterMoney.Text = this.lblcoffer.Text;
            frm.lblmsMoney.Text = this.txtam.Text;
            frm.lbllaterMoney.Text = this.lblcoffer2.Text;
            frm.ShowDialog();
        }

        private void form_account_Activated(object sender, EventArgs e)
        {
            form_account_Load(null, null);
        }

        private void txtdvrid_KeyPress(object sender, KeyPressEventArgs e)
        {
            int i = e.KeyChar;
            if (i < 8 || 8 < i && i < 48 || i > 57)
            {
                e.Handled = true;
                System.Console.Beep();
            }

            else
                e.Handled = false;
        }

        private void txt100_KeyPress(object sender, KeyPressEventArgs e)
        {
            int i = e.KeyChar;
            if (i < 8 || 8 < i && i < 48 || i > 57)
            {
                e.Handled = true;
                System.Console.Beep();
            }

            else
                e.Handled = false;
        }
    }
}
