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;

namespace Aram_CRM_Project.UIL
{
    public partial class frmSelectCustomer : Form
    {
        public frmSelectCustomer()
        {
            InitializeComponent();
        }

        public string CustomerId = string.Empty;
        public string CustomerName = string.Empty;
        public string CustomerGroupName = string.Empty;
        public string Tel = string.Empty;
        public string Cell = string.Empty;
        public string BirthDate = string.Empty;
        public string MarriageDate = string.Empty;
        public string Address = string.Empty;
        public string CustomerGroupId = string.Empty;
        public string RelationshipWayId = string.Empty;
        public string CityStateId = string.Empty;


        private void SearchCustomer()
        {
            Aram_CRM_Project.BLL.CustomerCRUD CustomerCrud = new Aram_CRM_Project.BLL.CustomerCRUD();
            Aram_CRM_Project.BLL.Entity.Customer CustomerVar = new Aram_CRM_Project.BLL.Entity.Customer();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            DataTable dtTemp = new DataTable();

            CustomerVar.Name = utility.ChangeChar(txtSearchName.Text.Trim());
            CustomerVar.Address = utility.ChangeChar(txtSearchAddress.Text.Trim());
            CustomerVar.Tel = txtSearchTel.Text.Trim();
            CustomerVar.Cell = txtSearchCell.Text.Trim();
            if (cboSearchCustomerGroup.SelectedValue.ToString() == "0")
                CustomerVar.CustomerGroupName = "";
            else
                CustomerVar.CustomerGroupName = cboSearchCustomerGroup.Text;

            if (cboSearchCityState.SelectedValue.ToString() == "0")
                CustomerVar.CityStateName = "";
            else
                CustomerVar.CityStateName = cboSearchCityState.Text;

            if (cboSearchRelationshipWay.SelectedValue.ToString() == "0")
                CustomerVar.RelationshipWayName = "";
            else
                CustomerVar.RelationshipWayName = cboSearchRelationshipWay.Text;


            string StartBirthDay = cboSearchStartBirthDay.SelectedIndex != 0 ? cboSearchStartBirthDay.SelectedItem.ToString() : "00";
            string StartBirthMonth = cboSearchStartBirthMonth.SelectedIndex != 0 ? cboSearchStartBirthMonth.SelectedItem.ToString() : "00";
            string StartBirthYear = txtSearchStartBirthYear.Text.Trim() != "" ? txtSearchStartBirthYear.Text.Trim() : "0000";
            string EndBirthDay = cboSearchEndBirthDay.SelectedIndex != 0 ? cboSearchEndBirthDay.SelectedItem.ToString() : "99";
            string EndBirthMonth = cboSearchEndBirthMonth.SelectedIndex != 0 ? cboSearchEndBirthMonth.SelectedItem.ToString() : "99";
            string EndBirthYear = txtSearchEndBirthYear.Text.Trim() != "" ? txtSearchEndBirthYear.Text.Trim() : "9999";

            string StartMarriageDay = cboSearchStartMarriageDay.SelectedIndex != 0 ? cboSearchStartMarriageDay.SelectedItem.ToString() : "00";
            string StartMarriageMonth = cboSearchStartMarriageMonth.SelectedIndex != 0 ? cboSearchStartMarriageMonth.SelectedItem.ToString() : "00";
            string StartMarriageYear = txtSearchStartMarriageYear.Text.Trim() != "" ? txtSearchStartMarriageYear.Text.Trim() : "0000";
            string EndMarriageDay = cboSearchEndMarriageDay.SelectedIndex != 0 ? cboSearchEndMarriageDay.SelectedItem.ToString() : "99";
            string EndMarriageMonth = cboSearchEndMarriageMonth.SelectedIndex != 0 ? cboSearchEndMarriageMonth.SelectedItem.ToString() : "99";
            string EndMarriageYear = txtSearchEndMarriageYear.Text.Trim() != "" ? txtSearchEndMarriageYear.Text.Trim() : "9999";

            CustomerVar.StartBirthDate = StartBirthYear + "/" + StartBirthMonth + "/" + StartBirthDay;
            CustomerVar.EndBirthDate = EndBirthYear + "/" + EndBirthMonth + "/" + EndBirthDay;
            CustomerVar.StartMarriageDate = StartMarriageYear + "/" + StartMarriageMonth + "/" + StartMarriageDay;
            CustomerVar.EndMarriageDate = EndMarriageYear + "/" + EndMarriageMonth + "/" + EndMarriageDay;

            //Without Marriage and Birth
            if ((CustomerVar.StartBirthDate == "0000/00/00" && CustomerVar.EndBirthDate == "9999/99/99") && (CustomerVar.StartMarriageDate == "0000/00/00" && CustomerVar.EndMarriageDate == "9999/99/99"))
            {
                dtTemp = CustomerCrud.SearchCustomerWithoutDate(CustomerVar);
            }
            //With Marriage    
            else if ((CustomerVar.StartBirthDate == "0000/00/00" && CustomerVar.EndBirthDate == "9999/99/99") && (CustomerVar.StartMarriageDate != "0000/00/00" || CustomerVar.EndMarriageDate != "9999/99/99"))
            {
                dtTemp = CustomerCrud.SearchCustomerWithMarriage(CustomerVar);
            }
            //With Birth
            else if ((CustomerVar.StartBirthDate != "0000/00/00" || CustomerVar.EndBirthDate != "9999/99/99") && (CustomerVar.StartMarriageDate == "0000/00/00" && CustomerVar.EndMarriageDate == "9999/99/99"))
            {
                dtTemp = CustomerCrud.SearchCustomerWithBirth(CustomerVar);
            }
            else
            {
                dtTemp = CustomerCrud.SearchCustomer(CustomerVar);
            }

            dgvCustomer.DataSource = dtTemp;
            dgvCustomer.Columns[1].Visible = false;
            dgvCustomer.Columns[8].Visible = false;
            dgvCustomer.Columns[9].Visible = false;
            dgvCustomer.Columns[10].Visible = false;
            dgvCustomer.Columns[0].Width = 50;
            dgvCustomer.Columns[2].Width = 120;
            dgvCustomer.Columns[3].Width = 90;
            dgvCustomer.Columns[4].Width = 90;
            dgvCustomer.Columns[5].Width = 80;
            dgvCustomer.Columns[6].Width = 85;

            if (dtTemp.Rows.Count == 0)
            {
                btnEnter.Enabled = false;
                dgvCustomer.Enabled = false;
            }
            else
            {
                btnEnter.Enabled = true;
                dgvCustomer.Enabled = true;
            }
            dtTemp.Dispose();
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            lblEvent.Text = "Enter";
            CustomerId = dgvCustomer.SelectedCells[1].Value.ToString();
            CustomerName = dgvCustomer.SelectedCells[2].Value.ToString();
            Tel = dgvCustomer.SelectedCells[3].Value.ToString();
            Cell = dgvCustomer.SelectedCells[4].Value.ToString();
            BirthDate = dgvCustomer.SelectedCells[5].Value.ToString();
            MarriageDate = dgvCustomer.SelectedCells[6].Value.ToString();
            Address = dgvCustomer.SelectedCells[7].Value.ToString();
            CustomerGroupId = dgvCustomer.SelectedCells[8].Value.ToString();
            RelationshipWayId = dgvCustomer.SelectedCells[9].Value.ToString();
            CityStateId = dgvCustomer.SelectedCells[10].Value.ToString();
            this.Close();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

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

        private void frmSelectCustomer_Load(object sender, EventArgs e)
        {
            Aram_CRM_Project.BLL.CustomerCRUD CustomerCrud = new Aram_CRM_Project.BLL.CustomerCRUD();
            Aram_CRM_Project.BLL.Entity.Customer CustomerVar = new Aram_CRM_Project.BLL.Entity.Customer();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            DataTable dtTemp = new DataTable();

            cboSearchStartBirthDay.SelectedIndex = 0;
            cboSearchEndBirthDay.SelectedIndex = 0;
            cboSearchStartBirthMonth.SelectedIndex = 0;
            cboSearchEndBirthMonth.SelectedIndex = 0;
            cboSearchStartMarriageDay.SelectedIndex = 0;
            cboSearchEndMarriageDay.SelectedIndex = 0;
            cboSearchEndMarriageMonth.SelectedIndex = 0;
            cboSearchStartMarriageMonth.SelectedIndex = 0;

            cboSearchCustomerGroup.DataSource = CustomerCrud.ShowCustomerGroupForSearchComboBox();
            cboSearchCustomerGroup.ValueMember = "Id";
            cboSearchCustomerGroup.DisplayMember = "Name";
            cboSearchCustomerGroup.SelectedValue = 0;

            cboSearchCityState.DataSource = CustomerCrud.ShowCityStateForSearchComboBox();
            cboSearchCityState.ValueMember = "Id";
            cboSearchCityState.DisplayMember = "Name";
            cboSearchCityState.SelectedValue = 0;

            cboSearchRelationshipWay.DataSource = CustomerCrud.ShowRelationshipWayForSearchComboBox();
            cboSearchRelationshipWay.ValueMember = "Id";
            cboSearchRelationshipWay.DisplayMember = "Name";
            cboSearchRelationshipWay.SelectedValue = 0;
        }

        private void frmSelectCustomer_Activated(object sender, EventArgs e)
        {
            SearchCustomer();
        }

        private void cboSearchRelationshipWay_SelectionChangeCommitted(object sender, EventArgs e)
        {
            SearchCustomer();
        }

        private void cboSearchCustomerGroup_SelectionChangeCommitted(object sender, EventArgs e)
        {
            SearchCustomer();
        }

        private void cboSearchCityState_SelectionChangeCommitted(object sender, EventArgs e)
        {
            SearchCustomer();
        }

        private void cboSearchStartMarriageDay_SelectionChangeCommitted(object sender, EventArgs e)
        {
            SearchCustomer();
        }

        private void cboSearchStartMarriageMonth_SelectionChangeCommitted(object sender, EventArgs e)
        {
            SearchCustomer();
        }

        private void txtSearchStartMarriageYear_TextChanged(object sender, EventArgs e)
        {
            SearchCustomer();
        }

        private void cboSearchEndMarriageDay_SelectionChangeCommitted(object sender, EventArgs e)
        {
            SearchCustomer();
        }

        private void cboSearchEndMarriageMonth_SelectionChangeCommitted(object sender, EventArgs e)
        {
            SearchCustomer();
        }

        private void txtSearchEndMarriageYear_TextChanged(object sender, EventArgs e)
        {
            SearchCustomer();
        }

        private void txtSearchEndBirthYear_TextChanged(object sender, EventArgs e)
        {
            SearchCustomer();
        }

        private void txtSearchStartBirthYear_TextChanged(object sender, EventArgs e)
        {
            SearchCustomer();
        }

        private void cboSearchEndBirthMonth_SelectionChangeCommitted(object sender, EventArgs e)
        {
            SearchCustomer();
        }

        private void cboSearchEndBirthDay_SelectionChangeCommitted(object sender, EventArgs e)
        {
            SearchCustomer();
        }

        private void cboSearchStartBirthMonth_SelectionChangeCommitted(object sender, EventArgs e)
        {
            SearchCustomer();
        }

        private void cboSearchStartBirthDay_SelectionChangeCommitted(object sender, EventArgs e)
        {
            SearchCustomer();
        }

        private void dgvCustomer_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                lblEvent.Text = "Enter";
                CustomerId = dgvCustomer.SelectedCells[1].Value.ToString();
                CustomerName = dgvCustomer.SelectedCells[2].Value.ToString();
                Tel = dgvCustomer.SelectedCells[3].Value.ToString();
                Cell = dgvCustomer.SelectedCells[4].Value.ToString();
                BirthDate = dgvCustomer.SelectedCells[5].Value.ToString();
                MarriageDate = dgvCustomer.SelectedCells[6].Value.ToString();
                Address = dgvCustomer.SelectedCells[7].Value.ToString();
                CustomerGroupId = dgvCustomer.SelectedCells[8].Value.ToString();
                RelationshipWayId = dgvCustomer.SelectedCells[9].Value.ToString();
                CityStateId = dgvCustomer.SelectedCells[10].Value.ToString();
                this.Dispose();
            }
        }

        private void dgvCustomer_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            lblEvent.Text = "Enter";
            CustomerId = dgvCustomer.SelectedCells[1].Value.ToString();
            CustomerName = dgvCustomer.SelectedCells[2].Value.ToString();
            Tel = dgvCustomer.SelectedCells[3].Value.ToString();
            Cell = dgvCustomer.SelectedCells[4].Value.ToString();
            BirthDate = dgvCustomer.SelectedCells[5].Value.ToString();
            MarriageDate = dgvCustomer.SelectedCells[6].Value.ToString();
            Address = dgvCustomer.SelectedCells[7].Value.ToString();
            CustomerGroupId = dgvCustomer.SelectedCells[8].Value.ToString();
            RelationshipWayId = dgvCustomer.SelectedCells[9].Value.ToString();
            CityStateId = dgvCustomer.SelectedCells[10].Value.ToString();
            this.Close();
        }

        private void txtSearchAddress_TextChanged(object sender, EventArgs e)
        {
            SearchCustomer();
        }

        private void txtSearchTel_TextChanged(object sender, EventArgs e)
        {
            SearchCustomer();
        }

        private void txtSearchCell_TextChanged(object sender, EventArgs e)
        {
            SearchCustomer();
        }
    }
}
