using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HySoft.BaseCallCenter.Web.customermanage
{
    public partial class personmodify : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Expires = -1;
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["personId"]))
                {
                    hiddPersonId.Value = Request.QueryString["personId"];
                    Initialize(Request.QueryString["personId"]);
                }
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            SaveInfo();
        }
        private void SaveInfo()
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(Request.QueryString["actionFlag"].Trim()))
                {
                    string actionFlag = Request.QueryString["actionFlag"].Trim();
                    if (actionFlag == "add")
                    {
                        int b1 = 0;
                        if (Request.QueryString["cid"] != null && Request.QueryString["cid"] != "")
                        {
                            Model.T_Cus_ContactPerson tccp = new Model.T_Cus_ContactPerson();
                            string cid = Request.QueryString["cid"].Trim();
                            tccp = T_Cus_ContactPerson;
                            tccp.F_CustomerId = Convert.ToInt32(cid);
                            b1 = new BLL.T_Cus_ContactPerson().Add(tccp);
                        }
                        else
                            b1 = new BLL.T_Cus_ContactPerson().Add(T_Cus_ContactPerson);
                        if (b1 > 0)
                        {
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>SaveInfo('success')</script>");
                        }
                        else
                        {
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>SaveInfo('warn')</script>");
                        }
                    }
                    if (actionFlag == "modify")
                    {
                        bool b2 = new BLL.T_Cus_ContactPerson().Update(T_Cus_ContactPerson);
                        if (b2)
                        {
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>SaveInfo('success')</script>");
                        }
                        else
                        {
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>SaveInfo('warn')</script>");
                        }
                    }
                }
            }
            catch
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>SaveInfo('error')</script>");
            }
        }
        #region 信息初始化

        private void Initialize(string id)
        {
            T_Cus_ContactPerson = new BLL.T_Cus_ContactPerson().GetModel(Convert.ToInt32(hiddPersonId.Value));
        }
        #endregion
        #region 公共属性
        private Model.T_Cus_ContactPerson T_Cus_ContactPerson
        {
            get
            {
                Model.T_Cus_ContactPerson _model = new Model.T_Cus_ContactPerson();
                if (hiddPersonId.Value == "" || hiddPersonId.Value == null) { }
                else
                    _model.F_ManId = Convert.ToInt32(hiddPersonId.Value);
                if (hiddCustomerId.Value == "" || hiddCustomerId.Value == null) { }
                else
                    _model.F_CustomerId = Convert.ToInt32(hiddCustomerId.Value);
                _model.F_Name = txtContactName.Text;
                if (hfSex.Value == "1" || hfSex.Value == "")
                {
                    _model.F_Sex = "男";
                }
                else
                {
                    _model.F_Sex = "女";
                }
                _model.F_Duty = txtDuty.Text;
                _model.F_Email = txtEmail.Text;
                _model.F_Telephone = txtTelePhone.Text;
                _model.F_Mobile = txtMobile.Text;
                _model.F_QQ = txtQQ.Text;
                _model.F_Fax = txtFax.Text;
                _model.F_Remark = txtRemark.Value;
                return _model;
            }
            set
            {
                if (value != null)
                {
                    hiddPersonId.Value = value.F_ManId.ToString();
                    hiddCustomerId.Value = value.F_CustomerId.ToString();
                    txtContactName.Text = value.F_Name;
                    if (value.F_Sex == "男")
                    {
                        hfSex.Value = "1";
                    }
                    else
                    {
                        hfSex.Value = "0";
                    }
                    txtDuty.Text = value.F_Duty;
                    txtEmail.Text = value.F_Email;
                    txtTelePhone.Text = value.F_Telephone;
                    txtMobile.Text = value.F_Mobile;
                    txtQQ.Text = value.F_QQ;
                    txtFax.Text = value.F_Fax;
                    txtRemark.Value = value.F_Remark;
                }
            }
        }
        #endregion
        private string getCustomerName(int id)
        {
            Model.T_Cus_CustomerBase model = new BLL.T_Cus_CustomerBase().GetModel(id);
            if (model != null)
                return model.F_CustomerName;
            else return "";
        }
    }
}