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

namespace projectcsharp
{
    public partial class frm_fac : Form
    {
        public frm_fac()
        {
            InitializeComponent();
        }
        public SqlConnection con= new SqlConnection("server = . ; database =resturan ; integrated security = true");
        DateTime d = new DateTime();
        int case_save=0;
        private void clr_txt()
        {
            txt_count.Text = "";
            txt_fullprice.Text = "";
            txt_numberfac.Text = "";
            txt_takhfif.Text = "";
        }
        public void Enabled_true()
        {
            btn_new.Enabled=false;
            btn_save.Enabled=true;
            btn_cancel.Enabled = true;
            btn_edit.Enabled = false;
        }
        public void Enabled_false()
        {
            btn_new.Enabled=true;
            btn_save.Enabled=false;
            btn_edit.Enabled = true;
            btn_cancel.Enabled = false;
        }
        public void show_info()
        {

            SqlDataAdapter da1 = new SqlDataAdapter("select * from tbl_customer", con);
            DataTable dt1 = new DataTable();
            da1.Fill(dt1);
            cmb_customer.DataSource = dt1;
            cmb_customer.DisplayMember ="family";

            SqlDataAdapter da2 = new SqlDataAdapter("select * from tbl_food", con);
            DataTable dt2 = new DataTable();
            da2.Fill(dt2);
            cmb_food.DataSource = dt2;
            cmb_food.DisplayMember ="name";
            Enabled_false();
        }

        private void frm_fac_Load(object sender, EventArgs e)
        {
            System.Globalization.PersianCalendar pc = new System.Globalization.PersianCalendar();
            d = DateTime.Now;
            txt_tarikh.Text = pc.GetYear(d).ToString() + "/" + pc.GetMonth(d).ToString() + "/" + pc.GetDayOfMonth(d).ToString();
            show_info();
            dataGridView1.DataSource = "";

        }

        private void btn_fullprice_Click(object sender, EventArgs e)
        {
            double  x=0;
            x = Convert.ToInt64(txt_price.Text) * Convert.ToInt64(txt_count.Text) - Convert.ToInt64(txt_takhfif.Text);
            txt_fullprice.Text = x.ToString();
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            if ((txt_price.Text == "") || (txt_count.Text == "") || (txt_fullprice.Text == "") || (txt_count.Text=="0"))
            {
                MessageBox.Show("اطلاعات مشتری ثبت نشد", "!خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if(case_save == 1)
            {
                
                SqlCommand cmd = new SqlCommand("update tbl_fac set customer=N'" + cmb_customer.Text + "',food=N'" + cmb_food.Text + "',count=N'" + txt_count.Text + "',fullprice=N'" + txt_fullprice.Text + "',tarikh=N'" + txt_tarikh.Text + "' where numberfac=N'" + txt_numberfac.Text + "' ", con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                SqlDataAdapter da = new SqlDataAdapter("select numberfac as [شماره فاکتور], customer as [نام خانوادگی],food as [نام غذا],count as 'تعداد',fullprice as [قیمت کل],tarikh as 'تاریخ' from tbl_fac where customer =N'" + cmb_customer.Text + " '", con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
                Enabled_false();
                MessageBox.Show("فاکتور مشتری با موفقیت صادر شد", ".صدور شد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                clr_txt();
            }
            else if (case_save == 2)
            {
                SqlCommand cmd = new SqlCommand("update tbl_fac set customer=N'" + cmb_customer.Text + "',food=N'" + cmb_food.Text + "',count=N'" + txt_count.Text + "',fullprice=N'" + txt_fullprice.Text + "',tarikh=N'" + txt_tarikh.Text + "' where numberfac=N'" + txt_numberfac.Text + "' ", con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                SqlDataAdapter da = new SqlDataAdapter("select numberfac as [شماره فاکتور], customer as [نام خانوادگی],food as [نام غذا],count as 'تعداد',fullprice as [قیمت کل],tarikh as 'تاریخ' from tbl_fac where customer =N'" + cmb_customer.Text + " '", con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
                Enabled_false();
                MessageBox.Show("فاکتور مشتری با موفقیت ویرایش شد", "ویرایش شد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                clr_txt();
            }
                
        }
         private void btn_new_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("insert into tbl_fac(customer,food,count,fullprice,tarikh) values('','','','','')", con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            SqlDataAdapter da = new SqlDataAdapter("select numberfac as [شماره فاکتور], customer as [نام خانوادگی],food as [نام غذا],count as 'تعداد',fullprice as [قیمت کل],tarikh as 'تاریخ' from tbl_fac where customer='' and food=''", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            txt_numberfac.Text = (dt.Rows[0][0]).ToString();

            cmb_customer.SelectedIndex = 0;
            cmb_food.SelectedIndex = 0;
            txt_count.Text = "";
            txt_fullprice.Text = "";
            txt_takhfif.Text = "";
            case_save=1;
            Enabled_true();
        }

         private void cmb_food_SelectedIndexChanged(object sender, EventArgs e)
         {
             SqlDataAdapter da3 = new SqlDataAdapter("select * from tbl_food", con);
             DataTable dt3 = new DataTable();
             da3.Fill(dt3);
             txt_price.Text = (dt3.Rows[cmb_food.SelectedIndex][2]).ToString();
         }

         private void btn_back_Click(object sender, EventArgs e)
         {
             SqlCommand cmd = new SqlCommand("delete  from  tbl_fac  where customer='' and food=''", con);
             con.Open();
             cmd.ExecuteNonQuery();
             con.Close();
             this.Close();
         }

         private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
         {
             txt_numberfac.Text = dataGridView1[0, dataGridView1.CurrentRow.Index].Value.ToString();
             cmb_customer.Text= dataGridView1[1, dataGridView1.CurrentRow.Index].Value.ToString();
             cmb_food.Text = dataGridView1[2, dataGridView1.CurrentRow.Index].Value.ToString();
             txt_count.Text = dataGridView1[3, dataGridView1.CurrentRow.Index].Value.ToString();
             txt_fullprice.Text = dataGridView1[4, dataGridView1.CurrentRow.Index].Value.ToString();
             txt_tarikh.Text = dataGridView1[5, dataGridView1.CurrentRow.Index].Value.ToString();
             txt_takhfif.Text = ( (Convert.ToInt32(txt_price.Text) * Convert.ToInt32(txt_count.Text))-Convert.ToInt32(txt_fullprice.Text)).ToString();
         }

         private void btn_edit_Click(object sender, EventArgs e)
         {
             case_save = 2;
             Enabled_true();
         }

         private void cmb_customer_SelectedIndexChanged(object sender, EventArgs e)
         {
             SqlDataAdapter da = new SqlDataAdapter("select numberfac as [شماره فاکتور], customer as [نام خانوادگی],food as [نام غذا],count as 'تعداد',fullprice as [قیمت کل],tarikh as 'تاریخ' from tbl_fac where customer=N'" + cmb_customer.Text + "'", con);
             DataTable dt = new DataTable();
             da.Fill(dt);
             dataGridView1.DataSource = dt;
         }

         private void btn_cancel_Click(object sender, EventArgs e)
         {
             SqlCommand cmd = new SqlCommand("delete  from  tbl_fac  where customer='' and food=''", con);
             con.Open();
             cmd.ExecuteNonQuery();
             con.Close();
             txt_numberfac.Text = "";
             Enabled_false();
         }

    }
}
