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

namespace BongahCS
{
    public partial class frmContactsRegin : Form
    {
        public frmContactsRegin()
        {
            InitializeComponent();
        }


        private void grdFill()
        {

            try
            {
                FirstClass db = new FirstClass();
                DataTable dt = new DataTable();

                dt = db.dbOut(@"SELECT     tblContracts.ContractID AS [کد قرارداد], tblForuShandegan.Name + N' ' + tblForuShandegan.Sname AS [صاحب ملک], 
                      tblKharidaran.Name + N'  ' + tblKharidaran.Sname AS مشتری, tblMoamelehType.MoamelehType AS [نوع معامله], 
                      tblMelks.Type AS [نوع ملک], tblContracts.datOdMoaMeleh AS [تاریخ معامله], tblContracts.MablagheGarardad AS [مبلغ قرارداد], 
                      tblContracts.HagheBongah AS [حق بنگاه], tblContracts.RahgiriCode AS [کد رهگیری], tblContracts.SerialNumber AS [شماره سریال]
                        FROM         tblContracts INNER JOIN
                      tblKharidaran ON tblContracts.KharidarID = tblKharidaran.KharidarID INNER JOIN
                      tblForuShandegan ON tblContracts.ForooshandehID = tblForuShandegan.ForuShandehID INNER JOIN
                      tblMelks ON tblContracts.MelkID = tblMelks.MelkID INNER JOIN
                      tblMoamelehType ON tblContracts.MoamelehTypeID = tblMoamelehType.MoamelehTypeID");
                dataGridView1.DataSource = dt;
                textBox1.Text = db.dtShamsi();
                cmbFill();
            }
            catch
            {
                MessageBox.Show("رکوردی جهت نمایش وجود ندارد");
            }
        }

        private void cmbFill()
        {
            FirstClass db1 = new FirstClass();
            FirstClass db2 = new FirstClass();
            FirstClass db3 = new FirstClass();
            FirstClass db4 = new FirstClass();

            DataTable dt1 = new DataTable();
            DataTable dt2 = new DataTable();
            DataTable dt3 = new DataTable();
            DataTable dt4 = new DataTable();

            dt1 = db1.dbOut("SELECT TOP 100 PERCENT KharidarID, Name + N'  ' + Sname + N' کد ملی:  ' + MelliCode AS Expr1 FROM  tblKharidaran ORDER BY Sname");
            dt2 = db2.dbOut("SELECT TOP 100 PERCENT ForuShandehID, Name + N' ' + Sname + N' کد ملی  ' + MelliCode AS Expr1 FROM   tblForuShandegan ORDER BY Sname");
            dt3 = db3.dbOut("SELECT TOP 100 PERCENT MelkID, Type + N' شماره پرونده  ' + ParvanehNumber + N' کد ثبت  ' + RegisterNumber AS Expr1 FROM     tblMelks ORDER BY MelkID");
            dt4 = db4.dbOut("SELECT     TOP 100 PERCENT MoamelehTypeID, MoamelehType FROM   tblMoamelehType ORDER BY MoamelehType");

            comboBox1.DataSource = dt1;
            comboBox2.DataSource = dt2;
            comboBox3.DataSource = dt3;
            comboBox4.DataSource = dt4;

            comboBox1.ValueMember = "KharidarID";
            comboBox1.DisplayMember = "Expr1";

            comboBox2.ValueMember = "ForuShandehID";
            comboBox2.DisplayMember = "Expr1";

            comboBox3.ValueMember = "MelkID";
            comboBox3.DisplayMember = "Expr1";

            comboBox4.ValueMember = "MoamelehTypeID";
            comboBox4.DisplayMember = "MoamelehType";
        }

        protected void dbIns()
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "" || comboBox1.Text == "" || comboBox2.Text == "" || comboBox3.Text == "" || comboBox4.Text == "") 
            {
                MessageBox.Show("مقادیر تمامی فیلدها باید وارد شوند");
            }
            else
            {
                try
                {
                    FirstClass db = new FirstClass();
                    db.cmd.Parameters.Add("@KharidarID", SqlDbType.Int).Value = comboBox1.SelectedValue;
                    db.cmd.Parameters.Add("@ForooshandehID", SqlDbType.Int).Value = comboBox2.SelectedValue;
                    db.cmd.Parameters.Add("@MelkID", SqlDbType.Int).Value = comboBox3.SelectedValue;
                    db.cmd.Parameters.Add("@MoamelehTypeID", SqlDbType.Int).Value = comboBox4.SelectedValue;
                    db.cmd.Parameters.Add("@datOdMoaMeleh", SqlDbType.NVarChar).Value = textBox1.Text.Trim();
                    db.cmd.Parameters.Add("@MablagheGarardad", SqlDbType.Float).Value = float.Parse(textBox2.Text.Trim());
                    db.cmd.Parameters.Add("@HagheBongah", SqlDbType.Float).Value = float.Parse(textBox3.Text.Trim());
                    db.cmd.Parameters.Add("@RahgiriCode", SqlDbType.NVarChar).Value = textBox4.Text.Trim();
                    db.cmd.Parameters.Add("@SerialNumber", SqlDbType.NVarChar).Value = textBox5.Text.Trim();

                    db.exeCommand("sp_tblContracts_Insert");
                    grdFill();
                    txtClear();
                    MessageBox.Show("رکورد مورد نظر ثبت شد");
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }
        }

        protected void txtClear()
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
        }

        protected void dbDel()
        {
            FirstClass db = new FirstClass();
            db.cmd.Parameters.Add("@ContractID", SqlDbType.Int).Value = int.Parse(textBox6.Text.Trim());
            db.exeCommand("sp_tblContracts_DeleteRow");
            grdFill();
            textBox6.Text = "";
            MessageBox.Show("رکورد مورد نظر حذف گردید");

        }

        private void frmContactsRegin_Load(object sender, EventArgs e)
        {
            grdFill();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dbIns();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox6.Text == "")
            {
                MessageBox.Show("کد معامله ثبت شده مورد نظر در فیلد زرد رنگ وارد نشده است");
            }
            else
            {
                dbDel();
            }
        }
    }
}