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

namespace HySoft.BaseCallCenter.Web.customermanage
{
    public partial class customeredit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Expires = -1;
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["customerId"]))
                {
                    hiddCustomerId.Value = Request.QueryString["customerId"];
                    Initialize(Request.QueryString["customerId"]);
                }
            }
        }

        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 = new BLL.T_Cus_CustomerBase().Add(T_Cus_CustomerBase);
                        hiddCustomerId.Value = b1.ToString();
                        List<Model.T_Cus_ContactPerson> tcclist = new BLL.T_Cus_ContactPerson().GetModelList("F_CustomerId IS NULL or F_CustomerId<=0");
                        foreach (Model.T_Cus_ContactPerson tcc in tcclist)
                        {
                            tcc.F_CustomerId = Convert.ToInt32(hiddCustomerId.Value);
                            new BLL.T_Cus_ContactPerson().Update(tcc);
                        }
                        if (b1 > 0)
                        {
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>SaveInfo('success')</script>");
                        }
                        else
                        {
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>SaveInfo('warn')</script>");
                        }
                    }
                    if (actionFlag == "edit")
                    {
                        bool b2 = new BLL.T_Cus_CustomerBase().Update(T_Cus_CustomerBase);
                        List<Model.T_Cus_ContactPerson> tcclist = new BLL.T_Cus_ContactPerson().GetModelList("F_CustomerId IS NULL or F_CustomerId<=0");
                        foreach (Model.T_Cus_ContactPerson tcc in tcclist)
                        {
                            tcc.F_CustomerId = T_Cus_CustomerBase.F_CustomerId;
                            new BLL.T_Cus_ContactPerson().Update(tcc);
                        }
                        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_CustomerBase = new BLL.T_Cus_CustomerBase().GetModel(Convert.ToInt32(id));
        }
        #endregion
        #region 公共属性
        private Model.T_Cus_CustomerBase T_Cus_CustomerBase
        {
            get
            {
                Model.T_Cus_CustomerBase _model = new Model.T_Cus_CustomerBase();
                if (hiddCustomerId.Value != "" || hiddCustomerId.Value == "0")
                    _model.F_CustomerId = Convert.ToInt32(hiddCustomerId.Value);
                _model.F_CustomerName = txtCustomerName.Text.Trim();
                _model.F_CustomerCode = txtCustomerCode.Text.Trim();
                if (hiddSeriveDeptId.Value != "")
                    _model.F_ServiceDeptID = Convert.ToInt32(hiddSeriveDeptId.Value);
                _model.F_ServiceDept = drpServiceDept.Text;
                if (hiddProvinceId.Value != "")
                    _model.F_RegionId = Convert.ToInt32(hiddProvinceId.Value);
                _model.F_Province = drpProvince.Text;
                if (hiddCityId.Value != "")
                    _model.F_CityID = Convert.ToInt32(hiddCityId.Value);
                _model.F_City = drpCity.Text;
                //if (hiddProductLineId.Value != "")
                //    _model.F_ProductLineID = Convert.ToInt32(hiddProductLineId.Value);
                //_model.F_ProductLine = drpProductLine.Text;
                //if (hiddCustomerNatureId.Value != "")
                //    _model.F_CustomerNatureID = Convert.ToInt32(hiddCustomerNatureId.Value);
                //_model.F_CustomerNature = drpCustomerNature.Text;
                //if (hiddCustomerClassId.Value != "")
                //    _model.F_CustomerClassID = Convert.ToInt32(hiddCustomerClassId.Value);
                //_model.F_CustomerClass = drpCustomerClass.Text;
                //if (hiddCustomerIndustryId.Value != "")
                //    _model.F_IndustryId = Convert.ToInt32(hiddCustomerIndustryId.Value);
                //_model.F_CustomerIndustry = drpCustomerIndustry.Text;
                //if (hiddRelationShipClassId.Value != "")
                //    _model.F_RelationShipClassID = Convert.ToInt32(hiddRelationShipClassId.Value);
                //_model.F_RelationShipClass = drpRelationShipClass.Text;
                //if (hiddAfterSaleNameID.Value != "")
                //    _model.F_AfterSaleNameID = Convert.ToInt32(hiddAfterSaleNameID.Value);
                //_model.F_AfterSaleName = drpAfterSaleName.Text;
                //基本信息部分
                _model.F_Fax = txtFax.Text.Trim();
                _model.F_Email = txtEmail.Text.Trim();
                _model.F_Telephone = txtTelephone.Text.Trim();
                _model.F_Address = txtAddress.Text.Trim();
                _model.F_PostCode = txtPostCode.Text.Trim();
                _model.F_CurrentVersion = txtCurrentVersion.Text.Trim();
                if (txtSystemStartTime.Text.Trim() != "")
                {
                    DateTime dt = new DateTime();
                    if (DateTime.TryParse(txtSystemStartTime.Text.Trim(), out dt))
                        _model.F_SystemStartTime = Convert.ToDateTime(txtSystemStartTime.Text.Trim());
                }
                if (txtQualityGuaranteeEndTime.Text.Trim() != "")
                {
                    DateTime dt = new DateTime();
                    if (DateTime.TryParse(txtSystemStartTime.Text.Trim(), out dt))
                        _model.F_QualityGuaranteeEndTime = Convert.ToDateTime(txtQualityGuaranteeEndTime.Text.Trim());
                }
                _model.F_SystemType = txtSystemType.Text.Trim();
                _model.F_CustomerStatus = txtCustomerStatus.Text.Trim();
                if (txtDeviceCount.Text.Trim() != "")
                {
                    int count = 0;
                    if (int.TryParse(txtDeviceCount.Text.Trim(), out count))
                        _model.F_DeviceCount = Convert.ToInt32(txtDeviceCount.Text.Trim());
                }
                //备注信息部分
                _model.F_TrackInfo = txtTrackInfo.Value;
                _model.F_Notes = txtNotes.Value;
                //扩展信息部分
                //todo...
                return _model;
            }
            set
            {
                if (value != null)
                {
                    hiddCustomerId.Value = value.F_CustomerId.ToString();
                    txtCustomerName.Text = value.F_CustomerName;
                    txtCustomerCode.Text = value.F_CustomerCode;
                    hiddSeriveDeptId.Value = value.F_ServiceDeptID.ToString();
                    hiddProvinceId.Value = value.F_RegionId.ToString();
                    hiddCityId.Value = value.F_CityID.ToString();
                    //hiddProductLineId.Value = value.F_ProductLineID.ToString();
                    //hiddCustomerNatureId.Value = value.F_CustomerNatureID.ToString();
                    //hiddCustomerClassId.Value = value.F_CustomerClassID.ToString();
                    //hiddCustomerIndustryId.Value = value.F_IndustryId.ToString();
                    //hiddRelationShipClassId.Value = value.F_RelationShipClassID.ToString();
                    //hiddAfterSaleNameID.Value = value.F_AfterSaleNameID.ToString();
                    drpServiceDept.Text = value.F_ServiceDept;
                    drpProvince.Text = value.F_Province;
                    drpCity.Text = value.F_City;
                    //drpProductLine.Text = value.F_ProductLine;
                    //drpCustomerNature.Text = value.F_CustomerNature;
                    //drpCustomerClass.Text = value.F_CustomerClass;
                    //drpCustomerIndustry.Text = value.F_CustomerIndustry;
                    //drpRelationShipClass.Text = value.F_RelationShipClass;
                    //drpAfterSaleName.Text = value.F_AfterSaleName;
                    //基本信息部分
                    txtFax.Text = value.F_Fax;
                    txtEmail.Text = value.F_Email;
                    txtTelephone.Text = value.F_Telephone;
                    txtAddress.Text = value.F_Address;
                    txtPostCode.Text = value.F_PostCode;
                    txtCurrentVersion.Text = value.F_CurrentVersion;
                    txtSystemStartTime.Text = value.F_SystemStartTime.ToString();
                    txtQualityGuaranteeEndTime.Text = value.F_QualityGuaranteeEndTime.ToString();
                    txtSystemType.Text = value.F_SystemType;
                    txtCustomerStatus.Text = value.F_CustomerStatus;
                    txtDeviceCount.Text = value.F_DeviceCount.ToString();
                    //备注信息部分
                    txtTrackInfo.Value = value.F_TrackInfo;
                    txtNotes.Value = value.F_Notes;
                    //扩展信息部分
                    //todo...
                }
            }
        }
        #endregion
    }
}