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 frmShowRelationshipWay : OfficeManagement.UIL.frmBase
    {
        private string ConditionQuery = string.Empty;

        public frmShowRelationshipWay()
        {
            InitializeComponent();
        }

        
        private void SearchRelationshipWay()
        {
            Aram_CRM_Project.BLL.RelationshipWayCRUD RelationshipWayCrud = new Aram_CRM_Project.BLL.RelationshipWayCRUD();
            Aram_CRM_Project.BLL.Entity.RelationshipWay RelationshipWayVar = new Aram_CRM_Project.BLL.Entity.RelationshipWay();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            DataTable dtTemp = new DataTable();

            string StartDay = cboSearchStartDay.SelectedIndex != 0 ? cboSearchStartDay.SelectedItem.ToString() : "01";
            string StartMonth = cboSearchStartMonth.SelectedIndex != 0 ? cboSearchStartMonth.SelectedItem.ToString() : "01";
            string StartYear = txtSearchStartYear.Text.Trim() != "" ? txtSearchStartYear.Text.Trim() : "0000";
            string EndDay = cboSearchEndDay.SelectedIndex != 0 ? cboSearchEndDay.SelectedItem.ToString() : "31";
            string EndMonth = cboSearchEndMonth.SelectedIndex != 0 ? cboSearchEndMonth.SelectedItem.ToString() : "12";
            string EndYear = txtSearchEndYear.Text.Trim() != "" ? txtSearchEndYear.Text.Trim() : "9999";

            RelationshipWayVar.Name = utility.ChangeChar(txtSearchName.Text.Trim());
            RelationshipWayVar.StartDate = StartYear + "/" + StartMonth + "/" + StartDay;
            RelationshipWayVar.EndDate = EndYear + "/" + EndMonth + "/" + EndDay;

            dtTemp = RelationshipWayCrud.SearchRelationshipWay(RelationshipWayVar);
            dgvRelationshipWay.DataSource = dtTemp;
            dgvRelationshipWay.Columns[1].Visible = false;
            dgvRelationshipWay.Columns[0].Width = 60;
            dgvRelationshipWay.Columns[2].Width = 200;
            dgvRelationshipWay.Columns[3].Width = 90;
            dgvRelationshipWay.Columns[4].Width = 90;

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

        private void frmShowRelationshipWay_Load(object sender, EventArgs e)
        {
            cboSearchStartDay.SelectedIndex = 0;
            cboSearchStartMonth.SelectedIndex = 0;
            cboSearchEndDay.SelectedIndex = 0;
            cboSearchEndMonth.SelectedIndex = 0;
        }

        private void frmShowRelationshipWay_Activated(object sender, EventArgs e)
        {
            SearchRelationshipWay();
        }

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

        private void cboSearchStartDay_SelectionChangeCommitted(object sender, EventArgs e)
        {
            SearchRelationshipWay();
        }

        private void cboSearchStartMonth_SelectionChangeCommitted(object sender, EventArgs e)
        {
            SearchRelationshipWay();
        }

        private void txtSearchStartYear_TextChanged(object sender, EventArgs e)
        {
            SearchRelationshipWay();
        }

        private void cboSearchEndDay_SelectionChangeCommitted(object sender, EventArgs e)
        {
            SearchRelationshipWay();
        }

        private void cboSearchEndMonth_SelectionChangeCommitted(object sender, EventArgs e)
        {
            SearchRelationshipWay();
        }

        private void txtSearchEndYear_TextChanged(object sender, EventArgs e)
        {
            SearchRelationshipWay();
        }

        protected override void btnNew_Click(object sender, EventArgs e)
        {
            frmRelaionshipWay fRelationshipWay = new frmRelaionshipWay();

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

        protected override void btnEdit_Click(object sender, EventArgs e)
        {
            frmRelaionshipWay fRelationshipWay = new frmRelaionshipWay();

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

        protected override void btnShow_Click(object sender, EventArgs e)
        {
            frmRelaionshipWay fRelationshipWay = new frmRelaionshipWay();

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

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

            fMessage.DeleteRecordConfirm();
            if (fMessage.ResultQuestion)
            {
                Result = RelationshipWayCrud.DeleteRelationshipWay(dgvRelationshipWay.SelectedCells[1].Value.ToString());
                if (Result > 0)
                {
                    fMessage.DeleteSuccess();
                    frmShowRelationshipWay_Activated(sender, e);
                }
                else if (Result == -1)
                {
                    fMessage.DeleteConflict();
                }
                else
                {
                    fMessage.DeleteError();
                }
            }
        }

        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();

            string StartDay = cboSearchStartDay.SelectedIndex != 0 ? cboSearchStartDay.SelectedItem.ToString() : "01";
            string StartMonth = cboSearchStartMonth.SelectedIndex != 0 ? cboSearchStartMonth.SelectedItem.ToString() : "01";
            string StartYear = txtSearchStartYear.Text.Trim() != "" ? txtSearchStartYear.Text.Trim() : "0000";
            string EndDay = cboSearchEndDay.SelectedIndex != 0 ? cboSearchEndDay.SelectedItem.ToString() : "31";
            string EndMonth = cboSearchEndMonth.SelectedIndex != 0 ? cboSearchEndMonth.SelectedItem.ToString() : "12";
            string EndYear = txtSearchEndYear.Text.Trim() != "" ? txtSearchEndYear.Text.Trim() : "9999";
            string StartDate, EndDate;
            StartDate = StartYear + "/" + StartMonth + "/" + StartDay;
            EndDate = EndYear + "/" + EndMonth + "/" + EndDay;

            ConditionQuery = " WHERE (dbo.tblRelationshipWay.Name LIKE N'%" + utility.ChangeChar(txtSearchName.Text.Trim()) + "%' AND dbo.tblRelationshipWay.StartDate BETWEEN '" + StartDate + "' AND '" + EndDate + "') ORDER BY dbo.tblRelationshipWay.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 = "RelationshipWay";
            fPrintReport.ShowDialog();
        }

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

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

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

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