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 EditContact : PageBase
    {
        #region Declare
        ContactManager ContM = new ContactManager();
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Page.Title = "NIKAI - ADD/EDIT CONTACT";
                LoadContactData();
            }
        }

        void LoadContactData()
        {
            ContactWithCustNameDTO Contact = ContM.GetContact(Convert.ToInt32(Request.QueryString["ContId"].ToString()), null);
            hdnCustomerId.Value = Contact.Customer_Id.ToString();
            txtCustomerNameEn.Text = Contact.Customer_Name;
            txtCustomerNameAr.Text = Contact.Customer_Name_BL;
            txtContactPersonEn.Text = Contact.Contact_Name;
            txtContactPersonAr.Text = Contact.Contact_Name_BL;
            txtDepartmentEn.Text = Contact.Department;
            txtDepartmentAr.Text = Contact.Department_BL;
            txtMobile.Text = Contact.Mobile;
            rbtStatus.SelectedValue = (Contact.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);
            else
            {
                ContactWithCustNameDTO Contact = new ContactWithCustNameDTO();
                Contact.Contact_Id = Convert.ToInt32(Request.QueryString["ContId"].ToString());
                Contact.Customer_Id = Convert.ToInt32(hdnCustomerId.Value);

                Contact.Contact_Name = (!string.IsNullOrEmpty(txtContactPersonEn.Text)) ? txtContactPersonEn.Text : txtContactPersonAr.Text;
                Contact.Contact_Name_BL = (string.IsNullOrEmpty(txtContactPersonAr.Text)) ? txtContactPersonEn.Text : txtContactPersonAr.Text;
                Contact.Department = (!string.IsNullOrEmpty(txtDepartmentEn.Text)) ? txtDepartmentEn.Text : txtDepartmentAr.Text;
                Contact.Department_BL = (string.IsNullOrEmpty(txtDepartmentAr.Text)) ? txtDepartmentEn.Text : txtDepartmentAr.Text; 
                Contact.Mobile = txtMobile.Text;
                Contact.Status = (rbtStatus.SelectedValue == "1") ? true : false;

                ContM.InsertUpdateContact(Contact);

                RadAjaxManager2.ResponseScripts.Add(string.Format("RefreshParentPage();", DateTime.Now.ToLongTimeString()));
            }
        }

        bool Validate(ref string Error)
        {
            bool validateFlag = false;

            if (Validation.TextBoxValidate(txtContactPersonEn) == false && Validation.TextBoxValidate(txtContactPersonAr) == false)
            {
                Error = (string)GetGlobalResourceObject("Res", "Fill_all_mandatory_fields");
            }
            else
            {
                validateFlag = true;
            }

            return validateFlag;
        }
    }
}