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

namespace RezerveBeliteHavaPeimaCS
{
    public partial class frmServiceDef : Form
    {
        public frmServiceDef()
        {
            InitializeComponent();
        }

        private void dbIns()
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
            {
                MessageBox.Show("مقادری تمامی فیلدها باید کامل وارد شوند");
            }
            else
            {
                FirstClass db = new FirstClass();
                db.cmd.Parameters.Add("@ServiceDescript", SqlDbType.NVarChar).Value = textBox1.Text.Trim();
                db.cmd.Parameters.Add("@ServiceDate", SqlDbType.NVarChar).Value = textBox2.Text.Trim();
                db.cmd.Parameters.Add("@Quantity", SqlDbType.NVarChar).Value = textBox3.Text.Trim();
                db.cmd.Parameters.Add("@ExpireBit", SqlDbType.NVarChar).Value = 0;
                db.cmd.Parameters.Add("@ServiceTypeID", SqlDbType.Int).Value = comboBox1.SelectedValue;

                db.exeCommand("sp_ServicesDef_Insert");
                grdFill();
                txtClear();
                MessageBox.Show("رکورد جدید درج شد.");
            }
        }

        private void cmbFill()
        {
            FirstClass db = new FirstClass();
            DataTable dt = new DataTable();

            dt = db.dbOut(@"SELECT     TOP 100 PERCENT ServiceTypeID, ServiceDesCript FROM    ServiceTypes ORDER BY ServiceTypeID");
            comboBox1.DataSource = dt;

            comboBox1.ValueMember = "ServiceTypeID";
            comboBox1.DisplayMember = "ServiceDesCript";
        }

        private void grdFill()
        {
            FirstClass db = new FirstClass();
            DataTable dt = new DataTable();

            dt = db.dbOut(@"SELECT     TOP 100 PERCENT ServicesDef.ServiceID AS [کد ], ServicesDef.ServiceDescript AS [شرح], 
                            ServiceTypes.ServiceDesCript AS [نوع سرویس], ServicesDef.ServiceDate AS تاریخ, ServicesDef.Quantity AS [تعداد صندلی], 
                            ServicesDef.ExpireBit AS وضعیت  FROM         ServicesDef INNER JOIN ServiceTypes ON ServicesDef.ServiceTypeID = ServiceTypes.ServiceTypeID ORDER BY ServicesDef.ServiceID DESC");
            dataGridView1.DataSource = dt;
            cmbFill();
        }

        private void dbDel()
        {
            if (textBox4.Text == "")
            {
                MessageBox.Show("کد وارد نشده");
            }
            else
            {
                FirstClass db = new FirstClass();
                db.cmd.Parameters.Add("@ServiceID", SqlDbType.NVarChar).Value = textBox4.Text;
                db.exeCommand("sp_ServicesDef_DeleteRow");
                textBox4.Text = "";
                grdFill();
                MessageBox.Show("رکورد مورد نظر حذف شد");
            }
        }

        private void txtClear()
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
        }


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

        private void button2_Click(object sender, EventArgs e)
        {
            dbDel();
        }

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