using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLayer;
using DTO;
using Helper;

namespace NikaiDSR.Controls
{
    public partial class EditCustomer : PageBase
    {
        #region declarartion
        CommonManager CM = new CommonManager();
        CustomerManager CustM = new CustomerManager();
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Page.Title = "NIKAI - ADD/EDIT CUSTOMER";
                FillBusinessTypeComboBox();
                FillCustomerTypeComboBox();
                FillCustomerGroupComboBox();
                FillLocationComboBox();
                FillSalesChannelComboBox();

                LoadCustomerData();
            }
        }


        void FillBusinessTypeComboBox()
        {
            IList<ParameterDTO> BusinessType = CustM.GetBusinessType();
            Helper.Validation.FillTelerikDropdown(cmbBusinessType, BusinessType, (LanguageId != 27) ? "Description" : "DescriptionBL", "ParameterDetId", "Select", "0");
        }

        void FillCustomerTypeComboBox()
        {
            IList<ParameterDTO> CustomerType = CustM.GetCustomerType();
            Helper.Validation.FillTelerikDropdown(cmbCustomerType, CustomerType, (LanguageId != 27) ? "Description" : "DescriptionBL", "ParameterDetId", "Select", "0");
        }

        void FillCustomerGroupComboBox()
        {
            IList<ParameterDTO> CustomerGroup = CustM.GetCustomerGroup();
            Helper.Validation.FillTelerikDropdown(cmbCustomerGroup, CustomerGroup, (LanguageId != 27) ? "Description" : "DescriptionBL", "ParameterDetId", "Select", "0");
        }

        void FillLocationComboBox()
        {
            IList<LocationDTO> Location = CM.GetLocation(null, null, null);
            Helper.Validation.FillTelerikDropdown(cmbLocation, Location, (LanguageId != 27) ? "Location_Name" : "Location_Name_BL", "Location_Id", "Select", "0");
        }

        void FillSalesChannelComboBox()
        {
            IList<ParameterDTO> SalesChannel = CM.GetSalesChannel();
            Helper.Validation.FillTelerikDropdown(cmbSalesChannel, SalesChannel, (LanguageId != 27) ? "Description" : "DescriptionBL", "ParameterDetId", "Select", "0");
        }

        void LoadCustomerData()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["CustId"].ToString()))
            {
                CustomerDTO Customer = CustM.GetCustomer(Convert.ToInt32(Request.QueryString["CustId"].ToString()), string.Empty);
                txtCustomerCode.Text = Customer.Customer_Code;
                txtCustomerNameEn.Text = Customer.Customer_Name;
                txtCustomerNameAr.Text = Customer.Customer_Name_BL;
                cmbBusinessType.SelectedValue = Customer.Business_Type.ToString();
                cmbCustomerType.SelectedValue = Customer.Customer_Type.ToString();
                cmbCustomerGroup.SelectedValue = Customer.Customer_Group.ToString();
                cmbLocation.SelectedValue = Customer.Location_Id.ToString();
                cmbSalesChannel.SelectedValue = Customer.Sales_Channel.ToString();
                rbtKeyAccount.SelectedValue = (Customer.Key_Account == true) ? "1" : "2";
                //rbtStatus.SelectedValue = (Customer.Status == true) ? "1" : "2";
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string Error = "";
            if (!Validate(ref Error))
            {
                ShowErrorMessage(Error, errMsg, Common.ERROR_MESSAGE_TYPE.ERROR);
                return;
            }

            CustomerDTO Customer = null;

            if (string.IsNullOrEmpty(Request.QueryString["CustId"].ToString()))
            {
                Customer = CustM.GetCustomer(null, txtCustomerNameEn.Text);
                if (Customer != null)
                {
                    Error = (string)GetGlobalResourceObject("Res", "Customer_Name_duplicate");
                    ShowErrorMessage(Error, errMsg, Common.ERROR_MESSAGE_TYPE.ERROR);
                    return;
                }
            }

            Customer = new CustomerDTO();

            if (!string.IsNullOrEmpty(Request.QueryString["CustId"].ToString()))
                Customer.Customer_Id = Convert.ToInt32(Request.QueryString["CustId"].ToString());

            Customer.Customer_Code = txtCustomerCode.Text;
            Customer.Customer_Name = (!string.IsNullOrEmpty(txtCustomerNameEn.Text)) ? txtCustomerNameEn.Text : txtCustomerNameAr.Text;
            Customer.Customer_Name_BL = (string.IsNullOrEmpty(txtCustomerNameAr.Text)) ? txtCustomerNameEn.Text : txtCustomerNameAr.Text;

            if (cmbBusinessType.SelectedValue != "0")
                Customer.Business_Type = Convert.ToInt32(cmbBusinessType.SelectedValue);
            if (cmbCustomerType.SelectedValue != "0")
                Customer.Customer_Type = Convert.ToInt32(cmbCustomerType.SelectedValue);
            if (cmbCustomerGroup.SelectedValue != "0")
                Customer.Customer_Group = Convert.ToInt32(cmbCustomerGroup.SelectedValue);

            Customer.Location_Id = Convert.ToInt32(cmbLocation.SelectedValue);
            Customer.Key_Account = (rbtKeyAccount.SelectedValue == "1") ? true : false;
            Customer.Status = true;
            Customer.Sales_Channel = Convert.ToInt32(cmbSalesChannel.SelectedValue);

            try
            {
                CustM.InsertUpdateCustomer(Customer, UserID);
            }
            catch (Exception ex)
            { }
            RadAjaxManager2.ResponseScripts.Add(string.Format("RefreshParentPage();", DateTime.Now.ToLongTimeString()));
        }

        bool Validate(ref string Error)
        {
            bool validateFlag = false;
            if (((Validation.TextBoxValidate(txtCustomerNameEn) == false) && (Validation.TextBoxValidate(txtCustomerNameAr) == false)) 
                || Validation.ComboBoxValidate(cmbLocation) == false)
            {
                Error = (string)GetGlobalResourceObject("Res", "Fill_all_mandatory_fields");
            }
            else
            {
                validateFlag = true;
            }

            return validateFlag;
        }
    }
}