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

namespace Aram_CRM_Project.UIL
{
    public partial class frmShowUser : OfficeManagement.UIL.frmBase
    {
        private string ConditionQuery = string.Empty;

        public frmShowUser()
        {
            InitializeComponent();
        }
        
        private void SearchUser()
        {
            Aram_CRM_Project.BLL.UserCRUD UserCrud = new Aram_CRM_Project.BLL.UserCRUD();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            DataTable dtTemp = new DataTable();

            dtTemp = UserCrud.SearchUser(utility.ChangeChar(txtSearchName.Text.Trim()));
            dgvUser.DataSource = dtTemp;
            dgvUser.Columns[1].Visible = false;
            dgvUser.Columns[0].Width = 60;
            dgvUser.Columns[2].Width = 150;

            if (dtTemp.Rows.Count == 0)
            {
                base.DisableButtonForEmptyDataGrid();
                dgvUser.Enabled = false;
            }
            else
            {
                base.EnableButtonForFullDataGrid();
                dgvUser.Enabled = true;
            }
            dtTemp.Dispose();
        }

        private void frmShowUser_Activated(object sender, EventArgs e)
        {
            SearchUser();
        }

        private void txtSearchName_TextChanged(object sender, EventArgs e)
        {
            SearchUser();
        }

        protected override void btnNew_Click(object sender, EventArgs e)
        {
            frmUser fUser = new frmUser();

            fUser.Location = new Point(this.Left + 110, this.Top + 100);
            fUser.Mode = "New";
            fUser.IdentifyMode("");
            frmShowUser_Activated(sender, e);
        }

        protected override void btnEdit_Click(object sender, EventArgs e)
        {
            frmUser fUser = new frmUser();

            fUser.Location = new Point(this.Left + 110, this.Top + 100);
            fUser.Mode = "Edit";
            fUser.IdentifyMode(dgvUser.SelectedCells[1].Value.ToString());
            frmShowUser_Activated(sender, e);
        }

        protected override void btnShow_Click(object sender, EventArgs e)
        {
            frmUser fUser = new frmUser();

            fUser.Location = new Point(this.Left + 110, this.Top + 100);
            fUser.Mode = "Show";
            fUser.IdentifyMode(dgvUser.SelectedCells[1].Value.ToString());
        }

        protected override void btnDelete_Click(object sender, EventArgs e)
        {
            Aram_CRM_Project.BLL.UserCRUD UserCrud = new Aram_CRM_Project.BLL.UserCRUD();
            frmMessage fMessage = new frmMessage();
            int Result;

            //Check; Is Admin?
            if (dgvUser.SelectedCells[1].Value.ToString() == "1")
            {
                fMessage.ErrorDeleteAdmin();
                return;
            }

            fMessage.DeleteRecordConfirm();
            if (fMessage.ResultQuestion)
            {
                Result = UserCrud.DeleteUser(dgvUser.SelectedCells[1].Value.ToString());
                if (Result > 0)
                {
                    fMessage.DeleteSuccess();
                    frmShowUser_Activated(sender, e);
                }
                else if (Result == -1)
                {
                    fMessage.DeleteConflict();
                }
                else
                {
                    fMessage.DeleteError();
                }
            }
        }

        private void frmShowUser_FormClosed(object sender, FormClosedEventArgs e)
        {
            Aram_CRM_Project.BLL.Entity.StaticVariable.FormUser_Shown = false;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Aram_CRM_Project.BLL.Entity.StaticVariable.FormUser_Shown = false;
            this.Dispose();
        }

        protected override void btnPrint_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;

            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();

            ConditionQuery = " WHERE (dbo.tblUser.Name LIKE N'%" + utility.ChangeChar(txtSearchName.Text.Trim()) + "%') ORDER BY dbo.tblUser.Id Desc ";

            Thread PrintThread = new Thread(PrintReport);
            PrintThread.Start();

            this.Cursor = Cursors.Default;
        }

        private void PrintReport()
        {
            frmPrintReport fPrintReport = new frmPrintReport();
            fPrintReport.ConditionQuery = ConditionQuery;
            fPrintReport.Mode = "User";
            fPrintReport.ShowDialog();
        }

        private void dgvUser_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnEdit_Click(sender, e);
        }

        private void dgvUser_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnEdit_Click(sender, e);
        }
    } 
}
