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.Data.Linq.SqlClient;

namespace RadioLogy
{
    public partial class frmUsersRadioLogy : Form
    {
        public frmUsersRadioLogy()
        {
            InitializeComponent();
        }
        private void UDF_ClearTextBox()
        {
            Txt_Family.Clear();
            Txt_Mobail.Clear();
            Txt_Name.Clear();
            Txt_Password.Clear();
            Txt_Tell.Clear();
            Txt_UserName.Clear();
        }

        private void UDF_GrdFill()
        {
            LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
            var Query = from Row in db.tblUsers
                        orderby Row.UsersID descending
                        where Row.UserTypeID != 1
                        select new
                        {
                            Row.UsersID,
                            Row.UsersName,
                            Row.UsersPass,
                            Row.UserFName,
                            Row.UsersSName,
                            Row.UserTypeID,
                            Row.UserTell,
                            Row.UserMobail,
                            Row.UserBanBit
                        };
            Grd_UsersInformation.DataSource = Query;
        }

        private void UDF_CmbFill()
        {
            LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
            var Query = from Row in db.tblUserTypes
                        orderby Row.UserTypeID descending
                        select new
                        {
                            Row.UserTypeID,
                            Row.UsersTypeDescription
                        };
            Cmb_UserType.DisplayMember = "UsersTypeDescription";
            Cmb_UserType.ValueMember = "UserTypeID";
            Cmb_UserType.DataSource = Query;
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            Txt_Family.Text = String.Empty;
            Txt_Mobail.Text = String.Empty;
            Txt_Name.Text = String.Empty;
            Txt_Password.Text = String.Empty;
            Txt_Tell.Text = String.Empty;
            Txt_UserName.Text = String.Empty;
            btnAdd.SendToBack();
            Btn_Save.BringToFront();
            MessageBox.Show("لطفا موارد را وارد کنید");
        }

        private void frmUsersRadioLogy_Load(object sender, EventArgs e)
        {
            UDF_CmbFill();
            UDF_GrdFill();
        }

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            if (Txt_Family.Text == "" || Txt_Mobail.Text == "" || Txt_Name.Text == "" || Txt_Password.Text == "" || Txt_Tell.Text == "" || Txt_UserName.Text == "")
            {
                MessageBox.Show("ورود موارد خواسته شده الزامی است");
                return;
            }
            LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
            tblUser userRad = new tblUser();
            userRad.UsersName = Txt_UserName.Text.Trim();
            userRad.UsersPass = Txt_Password.Text.Trim();
            userRad.UserFName = Txt_Name.Text.Trim();
            userRad.UsersSName = Txt_Family.Text.Trim();
            userRad.UserTypeID = int.Parse(Cmb_UserType.SelectedValue.ToString());
            userRad.UserTell = Txt_Tell.Text.Trim();
            userRad.UserMobail = Txt_Mobail.Text.Trim();
            userRad.UserBanBit = Chb_BanBit.Checked;
            db.tblUsers.InsertOnSubmit(userRad);
            db.SubmitChanges();
            btnAdd.BringToFront();
            Btn_Save.SendToBack();
            UDF_GrdFill();
            UDF_ClearTextBox();
            MessageBox.Show("رکورد جدید درج شد");
        }

        private void Grd_UsersInformation_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            Int32 selectedCellCount = Grd_UsersInformation. GetCellCount(DataGridViewElementStates.Selected);
            if (selectedCellCount > 0)
            {
                if (Grd_UsersInformation.AreAllCellsSelected(true))
                {
                    MessageBox.Show("All cells are selected", "Selected Cells");
                }
                else
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    for (int i = 0; i < selectedCellCount; i++)
                    {
                        Boolean Bln_Check;
                        Txt_UserID.Text = Grd_UsersInformation.Rows[int.Parse(Grd_UsersInformation.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                        Txt_UserName.Text = Grd_UsersInformation.Rows[int.Parse(Grd_UsersInformation.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                        Txt_Password.Text = Grd_UsersInformation.Rows[int.Parse(Grd_UsersInformation.SelectedCells[i].RowIndex.ToString())].Cells[2].Value.ToString();
                        Txt_Name.Text = Grd_UsersInformation.Rows[int.Parse(Grd_UsersInformation.SelectedCells[i].RowIndex.ToString())].Cells[3].Value.ToString();
                        Txt_Family.Text = Grd_UsersInformation.Rows[int.Parse(Grd_UsersInformation.SelectedCells[i].RowIndex.ToString())].Cells[4].Value.ToString();
                        Txt_Tell.Text = Grd_UsersInformation.Rows[int.Parse(Grd_UsersInformation.SelectedCells[i].RowIndex.ToString())].Cells[6].Value.ToString();
                        Txt_Mobail.Text = Grd_UsersInformation.Rows[int.Parse(Grd_UsersInformation.SelectedCells[i].RowIndex.ToString())].Cells[7].Value.ToString();
                        Bln_Check = Boolean.Parse(Grd_UsersInformation.Rows[int.Parse(Grd_UsersInformation.SelectedCells[i].RowIndex.ToString())].Cells[8].Value.ToString());
                        if (Bln_Check == true)
                        {
                            Chb_BanBit.Checked = true;
                        }
                        else if (Bln_Check == false)
                        {
                            Chb_BanBit.Checked = false;
                        }
                    }
                    UDF_SelectCmbValue(int.Parse(Txt_UserID.Text.Trim()));
                }
            }
        }
        private void UDF_SelectCmbValue(int int_UserID)
        {
            LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
            tblUser Users = db.tblUsers.Single(n => n.UsersID == int_UserID );
            Cmb_UserType.SelectedValue = Users.UserTypeID;
        }

        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            if (Txt_UserName.Text == "")
            {
                MessageBox.Show("انتخاب کاربر مورد نظر برای حذف الزامی است");
                return;
            }
            DialogResult dr = MessageBox.Show("آیا نسبت به عمل حذف اطمینان دارید؟" ,"اخطاریه حذف" ,MessageBoxButtons.OKCancel ,MessageBoxIcon.Warning );
            if (dr == DialogResult.OK)
            {
                LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
                tblUser userss = db.tblUsers.Single(n => n.UsersID == int.Parse(Txt_UserID.Text.Trim()));
                db.tblUsers.DeleteOnSubmit(userss);
                db.SubmitChanges();
                UDF_ClearTextBox();
                UDF_CmbFill();
                UDF_GrdFill();


            }
        }

        private void Btn_Edit_Click(object sender, EventArgs e)
        {
            if (Txt_UserID.Text == "")
            {
                MessageBox.Show("انتخاب کاربر مورد نظر بای ویرایش الزامی است");
                return;
            }
            DialogResult dr = MessageBox.Show("آیا نسبت به عمل ویرایش اطمینان دارید" ,"اخطاریه ویرایش" ,MessageBoxButtons.OKCancel , MessageBoxIcon.Warning );
            if (dr == DialogResult.OK)
            {
                LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
                tblUser userss = db.tblUsers.Single(n => n.UsersID == int.Parse(Txt_UserID.Text.Trim()));
               // userss.UsersName = Txt_UserName.Text.Trim();
                userss.UsersPass = Txt_Password.Text.Trim();
                userss.UserFName = Txt_Name.Text.Trim();
                userss.UsersSName = Txt_Family.Text.Trim();
                userss.UserTypeID = int.Parse(Cmb_UserType.SelectedValue.ToString());
                userss.UserTell = Txt_Tell.Text.Trim();
                userss.UserMobail = Txt_Mobail.Text.Trim();
                userss.UserBanBit = Chb_BanBit.Checked;
                db.SubmitChanges();
               
                UDF_CmbFill();
                UDF_GrdFill();
                UDF_ClearTextBox();
            }

        }

        private void Btn_Search_Click(object sender, EventArgs e)
        {
            if (Txt_Search.Text == "")
            {
                MessageBox.Show("اطفا نام کاربری و یا بخشی از آن را وارد کنید");
                return;
            }
            LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
            var Query = from Row in db.tblUsers
                        where SqlMethods.Like(Row.UsersName, "%" + Txt_Search.Text.Trim() + "%")
                        select new
                        {
                            Row.UsersID,
                            Row.UsersName,
                            Row.UsersPass,
                            Row.UserFName,
                            Row.UsersSName,
                            Row.UserTypeID,
                            Row.UserTell,
                            Row.UserMobail,
                            Row.UserBanBit
                        };
            Grd_UsersInformation.DataSource = Query;
            if (Query.Count() <= 0)
            {
                Txt_Search.Text = "";
                Btn_Refresh.BringToFront();
                Btn_Search.SendToBack();
                MessageBox.Show("رکوردی یافته نشد");
            }

        }

        private void Btn_Refresh_Click(object sender, EventArgs e)
        {
            UDF_CmbFill();
            UDF_GrdFill();
            Btn_Refresh.SendToBack();
            Btn_Search.BringToFront();
        }
    }
}