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

namespace Nano_System.DebitCredit
{
    public partial class DebitCredit : Form
    {
        int intFactorID = 0;
        string membID = "";
        public DebitCredit()
        {
            InitializeComponent();
        }

        private void comboBox1_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                object aa = null;
                aa = MessageBox.Show("آیا شماره سند انتخاب شده صحیح است " + "\n" + " پس از تائید شماره سند قفل خواهد شد", "", MessageBoxButtons.OKCancel);

                if (string.Compare(aa.ToString(), "Ok") == 1)
                {
                    try
                    {
                        intFactorID = int.Parse(comboBox1.SelectedValue.ToString());
                        closSanadID(int.Parse(comboBox1.SelectedValue.ToString()));
                        txtTarikh_add(int.Parse(comboBox1.SelectedValue.ToString()));
                        comboBox1.Enabled = false;
                        groupBox2.Enabled = true;
                    }
                    catch { }
                }
            }
            catch { }
        }

        public void closSanadID(int sanadID)
        {
            FirstClass closSanad = new FirstClass();
            closSanad.cmd.Parameters.Add("@SanadID", SqlDbType.Int).Value = sanadID;
            closSanad.cmd.Parameters.Add("@ClosBit", SqlDbType.Int).Value = 1;
            closSanad.exeCommand("sp_SanadClosing_Update");
        }

        public void txtTarikh_add(int sanadID)
        {
            FirstClass tarilhAdd = new FirstClass();
            DataTable dt = new DataTable();
            dt = tarilhAdd.dbOut(@"SELECT     SanadID, SanadDate FROM         dbo.SanadNumers WHERE     (SanadID = '" + sanadID + "')");
            textBox2.Text = dt.Rows[0][1].ToString();
        }

        private void cmbFill()
        {
            FirstClass db1 = new FirstClass();
            DataTable dt = new DataTable();
            dt = db1.dbOut(@"SELECT TOP 100 PERCENT SanadID, SanadDate, ClosBit FROM SanadNumers WHERE (ClosBit = 0) ORDER BY SanadID");
            comboBox1.DataSource = dt;
            comboBox1.ValueMember = "SanadID";
            comboBox1.DisplayMember = "SanadID";
        }
        public void cmbFill2()
        {

            clsForCombo cmbP = new clsForCombo();
            comboBox2.DataSource = cmbP.dbConnection2(@"SELECT FactorForushID, TasfiehBit, ClosedBit  FROM FactForooshID  WHERE (ClosedBit = 0) AND (TasfiehBit = 0)");
            comboBox2.ValueMember = "FactorForushID";
            comboBox2.DisplayMember = "FactorForushID";

        }

        public void cmbFill3()
        {
            clsForCombo cmbp = new clsForCombo();
            comboBox3.DataSource = cmbp.dbConnection2(@"SELECT     PardakhtTypeID, PardakhtType FROM    PardakhtHaTypes ");
            comboBox3.ValueMember = "PardakhtTypeID";
            comboBox3.DisplayMember = "PardakhtType";
        }

        private void grfFill(int intFtorID)
        {
            FirstClass db11 = new FirstClass();
            DataTable dt11 = new DataTable();
            dt11 = db11.dbOut(@"SELECT     TOP 100 PERCENT dbo.SharheAsnad.SanadID, dbo.SharheAsnad.FactorForushID, dbo.SharheAsnad.MablaghePrdakht, 
                        dbo.PardakhtHaTypes.PardakhtType FROM dbo.SharheAsnad INNER JOIN  dbo.PardakhtHaTypes ON dbo.SharheAsnad.PardakhtTypeID = dbo.PardakhtHaTypes.PardakhtTypeID
                        WHERE     (dbo.SharheAsnad.SanadID = '" + intFactorID + "') ORDER BY dbo.SharheAsnad.SanadID");
            dataGridView1.DataSource = dt11;
        }

        public void namGozar(string FactorID)
        {
            FirstClass dbTxt = new FirstClass();
            DataTable dt1 = new DataTable();

            dt1 = dbTxt.dbOut(@"SELECT     FactForooshID.FactorForushID, FactForooshID.DtOfSodoor, FactForooshID.MemBerID, FactForooshID.ClosedBit, Members.Name, 
                      Members.Name + '  ' + Members.Sname AS ex1 
                      FROM    FactForooshID INNER JOIN
                      Members ON FactForooshID.MemBerID = Members.MemBerID
                      WHERE     (FactForooshID.ClosedBit = 0) AND (FactForooshID.FactorForushID = '" + FactorID + "')");

            membID = dt1.Rows[0][2].ToString();
            textBox1.Text = (dt1.Rows[0][5].ToString());
            textBox3.Text = dt1.Rows[0][1].ToString();
        }

        public void chekMoshtari()
        {
            FirstClass db = new FirstClass();
            DataTable dt = new DataTable();
            dt = db.dbOut(@"SELECT  Members.MemBerID, ChekhaieDariaftani.CheckRowID AS EXPR2, ChekhaieDariaftani.CheeckNumber + N'  |   ' + ChekhaieDariaftani.BankNameShobeh AS Expr1, ChekhaieDariaftani.MabLaghOfChek, usDfr
                            FROM  Members INNER JOIN ChekhaieDariaftani ON Members.MemBerID = ChekhaieDariaftani.MemBerID WHERE (Members.MemBerID = N'" + membID + "') AND (ChekhaieDariaftani.usDfr = 0)");

            comboBox4.DataSource = dt;
            comboBox4.ValueMember = "EXPR2";
            comboBox4.DisplayMember = "Expr1";
            try
            {
                textBox4.Text = dt.Rows[0][3].ToString();
                
            }
            catch 
            {
                textBox4.Text = "";
            }
        }

        private void DebitCredit_Load(object sender, EventArgs e)
        {
            cmbFill();
            cmbFill2();
            cmbFill3();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox4.Text != "" && textBox7.Text != "")
            {
                FirstClass dbo = new FirstClass();
                dbo.cmd.Parameters.Add("@SanadID", SqlDbType.Int).Value = int.Parse(comboBox1.SelectedValue.ToString());
                dbo.cmd.Parameters.Add("@FactorForushID", SqlDbType.Int).Value = int.Parse(comboBox2.SelectedValue.ToString());
                dbo.cmd.Parameters.Add("@PardakhtTypeID", SqlDbType.Int).Value = int.Parse(comboBox3.SelectedValue.ToString());
                dbo.cmd.Parameters.Add("@MablaghePrdakht", SqlDbType.Float).Value = float.Parse(textBox4.Text.Trim());
                dbo.cmd.Parameters.Add("@Notes", SqlDbType.NVarChar).Value = textBox7.Text.Trim();
                dbo.exeCommand("sp_SharheAsnad_Insert");
                grfFill(int.Parse(comboBox1.SelectedValue.ToString()));
            }
            else
            {
                MessageBox.Show("مقادیر تمامی فیلدها باید انتخاب شوند");
            }
        }

        private void comboBox2_DropDownClosed(object sender, EventArgs e)
        {
            namGozar(comboBox2.SelectedValue.ToString());
            comboBox3.Enabled = true;
            chekMoshtari();
            //comboBox2.Enabled = false;
        }

        private void comboBox3_DropDownClosed(object sender, EventArgs e)
        {
            if (int.Parse(comboBox3.SelectedValue.ToString()) == 2)
            {
                comboBox4.Enabled = true;

            }
            else
            {
                comboBox4.Enabled = false;

            }
        }

        private void comboBox2_DropDownStyleChanged(object sender, EventArgs e)
        {

        }

        private void chkIDlok()
        {
            FirstClass lcDB = new FirstClass();
            lcDB.cmd.Parameters.Add("@CheckRowID", SqlDbType.Int).Value = int.Parse(comboBox4.SelectedValue.ToString());
            lcDB.cmd.Parameters.Add("@usDfr", SqlDbType.Bit).Value = 1;
            lcDB.exeCommand("sp_ChekhaieDariaftani_inuseBit_Update");
        }

        private void comboBox4_DropDownClosed(object sender, EventArgs e)
        {
            object aa = null;
            aa = MessageBox.Show("آیا شماره چک انتخاب شده صحیح است " + "\n" + "  پس از تائید شماره چک ، قفل خواهد شد و در لیست نمایش داده نخواهد شد", "", MessageBoxButtons.OKCancel);

            if (string.Compare(aa.ToString(), "Ok") == 1)
            {
                try
                {
                    chkIDlok();
                    comboBox4.Enabled = false;

                }
                catch { }
            }
        }

        public void chekVals()
        {

        }
    }
}