using BLL;
using System;
using System.Linq;

namespace FineUIPro.Web.SES
{
    public partial class SESRelatedDataEdit : PageBase
    {
        /// <summary>
        /// 角色编辑页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string view = Request.Params["view"];
                if (view == "1")
                {
                    this.btnSave.Hidden = true;
                    Toolbar2.Hidden = true;
                }
                else
                {
                    GetButtonPower();//按钮权限
                }                
                btnClose.OnClientClick = ActiveWindow.GetHideReference();

                //是否需要交工资料
                this.drpConstRecords.DataTextField = "ConstText";
                this.drpConstRecords.DataValueField = "ConstValue";
                this.drpConstRecords.DataSource = BLL.ConstValue.drpConstItemList(ConstValue.Group_YesOrNo);
                this.drpConstRecords.DataBind();
                this.drpConstRecords.SelectedValue = "N";
                //Funs.FineUIPleaseSelect(this.drpConstRecords);

                //关联交易
                this.drpConnectedTransaction.DataTextField = "ConstText";
                this.drpConnectedTransaction.DataValueField = "ConstValue";
                this.drpConnectedTransaction.DataSource = BLL.ConstValue.drpConstItemList(ConstValue.Group_YesOrNo);
                this.drpConnectedTransaction.DataBind();
                Funs.FineUIPleaseSelect(this.drpConnectedTransaction);

                //状态
                this.drpFCStatus.DataTextField = "ConstText";
                this.drpFCStatus.DataValueField = "ConstValue";
                this.drpFCStatus.DataSource = BLL.ConstValue.drpConstItemList(ConstValue.Group_FC_Status);
                this.drpFCStatus.DataBind();
                Funs.FineUIPleaseSelect(this.drpFCStatus);

                BLL.DisciplineService.InitDropDownList(this.drpDiscispline, true); //专业
                BLL.DisciplineCategoryService.InitDropDownList(this.drpOriginal_WC, true);//专业类别
                BLL.ContractorService.InitDropDownList(this.drpContractor, true);//承包商
                BLL.PriceSchemeService.InitDropDownList(this.drpPricing_Scheme, true);//价格方案
                BLL.ItemService.InitDropDownList(this.drpItem, true);
                BLL.CurrencyService.InitDropDownList(this.drpCurrency, true);  //币种
                BLL.FOTypeService.InitDropDownList(this.drpFOTypeId, true);
                BLL.TypeService.InitDropDownList(this.drpType, true);
                //合同员
                this.drpContract_Admin.DataTextField = "UserName";
                this.drpContract_Admin.DataValueField = "UserId";
                this.drpContract_Admin.DataSource = BLL.Sys_UserService.getUserListByRoleId(BLL.Const.Role_ContractAdministratorId, true);
                this.drpContract_Admin.DataBind();
                Funs.FineUIPleaseSelect(this.drpContract_Admin);

                var q= (from x in Funs.DB.Sys_User
                        join y in Funs.DB.Sys_Role on x.RoleId equals y.RoleId
                        where (x.RoleId == BLL.Const.Role_ContractAdministratorId || x.RoleId == BLL.Const.Role_CostEngineerId || y.RoleName== "Cost Engineer Leader") 
                               && x.IsPost == true select x).ToList();
                this.drpCost_Checker.DataTextField = "UserName";
                this.drpCost_Checker.DataValueField = "UserId";
                this.drpCost_Checker.DataSource = q;
                this.drpCost_Checker.DataBind();
                Funs.FineUIPleaseSelect(this.drpCost_Checker);

                BLL.Sys_UserService.InitUserDropDownList(this.drpUser_Representative, true);
                //BLL.Sys_UserService.InitUserDropDownList(this.drpContract_Person, true);
                BLL.Sys_UserService.InitUserDropDownList(this.drpMain_Coordinator, true);
                BLL.Sys_UserService.InitUserDropDownList(this.drpBuyer, true);

                string id = Request.Params["id"];
                if (!string.IsNullOrEmpty(id))
                {
                    var data = BLL.SESRelatedDataService.GetSESRelatedDataById(id);
                    if (data != null)
                    {
                        this.txtEMial.Text = data.Email;
                        this.txtMainHead.Text = data.MainHead;
                        this.txtMainHeadContact.Text = data.MainHeadContact;
                        this.txtSafetyOfficer.Text = data.SafetyOfficer;
                        this.txtSafetyOfficerContact.Text = data.SafetyOfficerContact;
                        this.txtProjectManager.Text = data.ProjectManager;
                        this.txtProjectManagerContact.Text = data.ProjectManagerContact;
                        this.txtContactPerson.Text = data.ProjectManager;
                        this.txtContract_Tel.Text = data.ProjectManagerContact;
                        if (!string.IsNullOrEmpty(data.Allocation))
                        {
                            drpAllocation.SelectedValue = data.Allocation;
                        }

                        if (!string.IsNullOrEmpty(data.Contractor))
                        {
                            this.drpContractor.SelectedValue = data.Contractor;
                        }
                        if (!string.IsNullOrEmpty(data.Pricing_Scheme))
                        {
                            this.drpPricing_Scheme.SelectedValue = data.Pricing_Scheme;
                        }
                        this.txtFO_NO.Text = data.FO_NO;
                        if (!string.IsNullOrEmpty(data.Item))
                        {
                            this.drpItem.SelectedValue = data.Item;
                        }
                        this.txtMaterial_Group.Text = data.Material_Group;
                        this.txtPurchase_Group.Text = data.Purchase_Group;
                        this.txtCost_Element.Text = data.Cost_Element;
                        this.txtVendor_NO.Text = data.Vendor_NO;
                        if (!string.IsNullOrEmpty(data.Currency))
                        {
                            this.drpCurrency.SelectedValue = data.Currency;
                        }

                        if (!string.IsNullOrEmpty(data.Original_WC))
                        {
                            var wc = from x in Funs.DB.Base_DisciplineCategory where x.Category == data.Original_WC select x;
                            if (wc.Count() > 0)
                            {
                                this.drpOriginal_WC.SelectedValue = wc.First().CategoryId;
                            }
                        }
                        if (!string.IsNullOrEmpty(data.Type))
                        {
                            this.drpType.SelectedValue = data.Type;
                        }
                        if (!string.IsNullOrEmpty(data.Contract_Admin))
                        {
                            this.drpContract_Admin.SelectedValue = data.Contract_Admin;
                        }
                        if (!string.IsNullOrEmpty(data.Cost_Checker))
                        {
                            this.drpCost_Checker.SelectedValue = data.Cost_Checker;
                        }

                        if (!string.IsNullOrEmpty(data.BuyerId))
                        {
                            this.drpBuyer.SelectedValue = data.BuyerId;
                            //var u = BLL.Sys_UserService.getUserIdByUserName(data.Buyer);
                            //if (u != null)
                            //{
                            //    this.drpBuyer.SelectedValue = u.UserId;
                            //}
                        }
                        if (!string.IsNullOrEmpty(data.Main_Coordinator))
                        {
                            this.drpMain_Coordinator.SelectedValue = data.Main_Coordinator;
                            var user = BLL.Sys_UserService.GetUsersByUserId(data.Main_Coordinator);
                            if (user != null)
                            {
                                var dep = BLL.DepartService.GetDepartById(user.DepartId);
                                {
                                    if (dep != null)
                                    {
                                        this.txtApplicant.Text = dep.DepartCode;
                                    }
                                }
                            }
                        }
                        if (!string.IsNullOrEmpty(data.User_Representative))
                        {
                            this.drpUser_Representative.SelectedValue = data.User_Representative;
                        }
                        //this.txtApplicant.Text = data.Applicant; // 主协调人
                        this.txtValidate_Date.Text = data.Validate_Date == null ? "" : Convert.ToDateTime(data.Validate_Date).ToString("yyyy-MM-dd");
                        this.txtExpire_Date.Text = data.Expire_Date == null ? "" : Convert.ToDateTime(data.Expire_Date).ToString("yyyy-MM-dd");
                       
                        if (!string.IsNullOrEmpty(data.FC_Status))
                        {
                            drpFCStatus.SelectedValue = data.FC_Status;
                        }
                       
                        this.txtContactPerson.Text = data.Contract_Person;
                        this.txtContract_Tel.Text = data.Contract_Tel;
                        if (data.Proportion_of_FC_Definition != null)
                        {
                            this.numProportion_of_FC_Definition.Text = (data.Proportion_of_FC_Definition.Value * Convert.ToDecimal(100.0)).ToString();
                        }
                        else
                        {
                            this.numProportion_of_FC_Definition.Text = "";
                        }
                        this.txtActual_Budget.Text = data.Actual_Budget.HasValue ? data.Actual_Budget.Value.ToString() : "";
                        this.txtRemark.Text = data.Remark;
                        if (!string.IsNullOrEmpty(data.ConstRecords))
                        {
                            this.drpConstRecords.SelectedValue = data.ConstRecords.Trim();
                        }
                        
                        if (!string.IsNullOrEmpty(data.DisciplineId))
                        {
                            this.drpDiscispline.SelectedValue = data.DisciplineId;
                        }
                        if (!string.IsNullOrEmpty(data.FOTypeId))
                        {
                            this.drpFOTypeId.SelectedValue = data.FOTypeId;
                        }
                        if (!string.IsNullOrEmpty(data.ConnectedTransaction))
                        {
                            this.drpConnectedTransaction.SelectedValue = data.ConnectedTransaction;
                        }
                        this.txtCheckedValue.Text = BLL.SESReportService.getSumSSRActualCostByFo(data.FO_NO).ToString();
                        this.txtRemainingBudget.Text = (Funs.GetNewDecimalOrZero(data.Actual_Budget.ToString()) - Funs.GetNewDecimalOrZero(this.txtCheckedValue.Text)).ToString();
                        txtNumExceedLimit.Text = data.ExceedLimit != null ? data.ExceedLimit.Value.ToString() : "";

                        if (data.Actual_Budget > 0)
                        {
                            this.txtRemainingBudgetRate.Text = (Convert.ToDecimal(this.txtRemainingBudget.Text) / data.Actual_Budget * 100).ToString();
                        }
                        if (data.Validate_Date.HasValue && data.Expire_Date.HasValue)
                        {
                            decimal s1 = (data.Expire_Date - DateTime.Now).Value.Days;
                            decimal s2 = (data.Expire_Date - data.Validate_Date).Value.Days;
                            if (s2 > 0)
                            {
                                this.txtRemainingDurationRate.Text = (s1 / s2 * 100).ToString();
                            }
                        }
                    }
                }
            }
        }

        // 拷贝
        protected void btnCopy_Click(object sender, EventArgs e)
        {
            string foNo = txtCopy.Text.Trim();
            var data = Funs.DB.FC_SESRelatedData.FirstOrDefault(x => x.FO_NO == foNo);
            if (data != null)
            {
                this.txtEMial.Text = data.Email;
                this.txtMainHead.Text = data.MainHead;
                this.txtMainHeadContact.Text = data.MainHeadContact;
                this.txtSafetyOfficer.Text = data.SafetyOfficer;
                this.txtSafetyOfficerContact.Text = data.SafetyOfficerContact;
                this.txtProjectManager.Text = data.ProjectManager;
                this.txtProjectManagerContact.Text = data.ProjectManagerContact;
                this.txtContactPerson.Text = data.ProjectManager;
                this.txtContract_Tel.Text = data.ProjectManagerContact;
                if (!string.IsNullOrEmpty(data.Allocation))
                {
                    drpAllocation.SelectedValue = data.Allocation;
                }

                if (!string.IsNullOrEmpty(data.Contractor))
                {
                    this.drpContractor.SelectedValue = data.Contractor;
                }
                if (!string.IsNullOrEmpty(data.Pricing_Scheme))
                {
                    this.drpPricing_Scheme.SelectedValue = data.Pricing_Scheme;
                }
                //this.txtFO_NO.Text = data.FO_NO;
                if (!string.IsNullOrEmpty(data.Item))
                {
                    this.drpItem.SelectedValue = data.Item;
                }
                this.txtMaterial_Group.Text = data.Material_Group;
                this.txtPurchase_Group.Text = data.Purchase_Group;
                this.txtCost_Element.Text = data.Cost_Element;
                this.txtVendor_NO.Text = data.Vendor_NO;
                if (!string.IsNullOrEmpty(data.Currency))
                {
                    this.drpCurrency.SelectedValue = data.Currency;
                }

                if (!string.IsNullOrEmpty(data.Original_WC))
                {
                    var wc = from x in Funs.DB.Base_DisciplineCategory where x.Category == data.Original_WC select x;
                    if (wc.Count() > 0)
                    {
                        this.drpOriginal_WC.SelectedValue = wc.First().CategoryId;
                    }
                }
                if (!string.IsNullOrEmpty(data.Type))
                {
                    this.drpType.SelectedValue = data.Type;
                }
                if (!string.IsNullOrEmpty(data.Contract_Admin))
                {
                    this.drpContract_Admin.SelectedValue = data.Contract_Admin;
                }
                if (!string.IsNullOrEmpty(data.Cost_Checker))
                {
                    this.drpCost_Checker.SelectedValue = data.Cost_Checker;
                }

                if (!string.IsNullOrEmpty(data.BuyerId))
                {
                    this.drpBuyer.SelectedValue = data.BuyerId;
                    //var u = BLL.Sys_UserService.getUserIdByUserName(data.Buyer);
                    //if (u != null)
                    //{
                    //    this.drpBuyer.SelectedValue = u.UserId;
                    //}
                }
                if (!string.IsNullOrEmpty(data.Main_Coordinator))
                {
                    this.drpMain_Coordinator.SelectedValue = data.Main_Coordinator;
                    var user = BLL.Sys_UserService.GetUsersByUserId(data.Main_Coordinator);
                    if (user != null)
                    {
                        var dep = BLL.DepartService.GetDepartById(user.DepartId);
                        {
                            if (dep != null)
                            {
                                this.txtApplicant.Text = dep.DepartCode;
                            }
                        }
                    }
                }
                if (!string.IsNullOrEmpty(data.User_Representative))
                {
                    this.drpUser_Representative.SelectedValue = data.User_Representative;
                }
                //this.txtApplicant.Text = data.Applicant;
                this.txtValidate_Date.Text = data.Validate_Date == null ? "" : Convert.ToDateTime(data.Validate_Date).ToString("yyyy-MM-dd");
                this.txtExpire_Date.Text = data.Expire_Date == null ? "" : Convert.ToDateTime(data.Expire_Date).ToString("yyyy-MM-dd");

                if (!string.IsNullOrEmpty(data.FC_Status))
                {
                    drpFCStatus.SelectedValue = data.FC_Status;
                }

                this.txtContactPerson.Text = data.Contract_Person;
                this.txtContract_Tel.Text = data.Contract_Tel;
                if (data.Proportion_of_FC_Definition != null)
                {
                    this.numProportion_of_FC_Definition.Text = (data.Proportion_of_FC_Definition.Value * Convert.ToDecimal(100.0)).ToString();
                }
                else
                {
                    this.numProportion_of_FC_Definition.Text = "";
                }
                this.txtActual_Budget.Text = data.Actual_Budget.HasValue ? data.Actual_Budget.Value.ToString() : "";
                this.txtRemark.Text = data.Remark;
                if (!string.IsNullOrEmpty(data.ConstRecords))
                {
                    this.drpConstRecords.SelectedValue = data.ConstRecords.Trim();
                }

                if (!string.IsNullOrEmpty(data.DisciplineId))
                {
                    this.drpDiscispline.SelectedValue = data.DisciplineId;
                }
                if (!string.IsNullOrEmpty(data.FOTypeId))
                {
                    this.drpFOTypeId.SelectedValue = data.FOTypeId;
                }
                if (!string.IsNullOrEmpty(data.ConnectedTransaction))
                {
                    this.drpConnectedTransaction.SelectedValue = data.ConnectedTransaction;
                }
                this.txtCheckedValue.Text = BLL.SESReportService.getSumSSRActualCostByFo(data.FO_NO).ToString();
                this.txtRemainingBudget.Text = (Funs.GetNewDecimalOrZero(data.Actual_Budget.ToString()) - Funs.GetNewDecimalOrZero(this.txtCheckedValue.Text)).ToString();
                txtNumExceedLimit.Text = data.ExceedLimit != null ? data.ExceedLimit.Value.ToString() : "";

                if (data.Actual_Budget > 0)
                {
                    this.txtRemainingBudgetRate.Text = (Convert.ToDecimal(this.txtRemainingBudget.Text) / data.Actual_Budget * 100).ToString();
                }
                if (data.Validate_Date.HasValue && data.Expire_Date.HasValue)
                {
                    decimal s1 = (data.Expire_Date - DateTime.Now).Value.Days;
                    decimal s2 = (data.Expire_Date - data.Validate_Date).Value.Days;
                    if (s2 > 0)
                    {
                        this.txtRemainingDurationRate.Text = (s1 / s2 * 100).ToString();
                    }
                }
            }
            else
            {
                Alert.ShowInTop("没有该合同号信息!", MessageBoxIcon.Warning);
                return;
            }
        }

        #region 保存
        /// <summary>
        /// 提交按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string id = Request.Params["id"];

            if (this.drpDiscispline.SelectedValue==BLL.Const._Null)
            {
                Alert.ShowInTop("Please select Discispline!", MessageBoxIcon.Warning);
                return;
            }
            if (BLL.SESRelatedDataService.IsExistFO_NO(id, this.txtFO_NO.Text.Trim()))
            {
                Alert.ShowInTop("The FO.NO. already exists!");
                return;
            }            
            if (this.drpContractor.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("Please select Contractor!", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpConstRecords.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("Please select Const.Records!", MessageBoxIcon.Warning);
                return;
            }

            if (this.drpAllocation.SelectedValue == "N")
            {
                if (Funs.GetNewDecimal(numProportion_of_FC_Definition.Text) != null)
                {
                    if (Funs.GetNewDecimal(numProportion_of_FC_Definition.Text) != 100)
                    {
                        Alert.ShowInTop("当Allocation选择N时,Volume Allocation应该是100%,请检查数据后再保存!", MessageBoxIcon.Warning);
                        return;
                    }
                }
                
            }

            Model.FC_SESRelatedData newData = new Model.FC_SESRelatedData();

            if (this.drpContractor.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpContractor.SelectedValue))
            {
                newData.Contractor = this.drpContractor.SelectedValue;
            }
            if (this.drpPricing_Scheme.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpPricing_Scheme.SelectedValue))
            {
                newData.Pricing_Scheme = this.drpPricing_Scheme.SelectedValue;
            }
            newData.FO_NO = this.txtFO_NO.Text.Trim();
            if (this.drpItem.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpItem.SelectedValue))
            {
                newData.Item = this.drpItem.SelectedValue;
            }
            newData.Material_Group = this.txtMaterial_Group.Text.Trim();
            newData.Purchase_Group = this.txtPurchase_Group.Text.Trim();
            newData.Cost_Element = this.txtCost_Element.Text.Trim();
            newData.Vendor_NO = this.txtVendor_NO.Text.Trim();
            if (this.drpCurrency.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpCurrency.SelectedValue))
            {
                newData.Currency = this.drpCurrency.SelectedValue;
            }

            if (this.drpOriginal_WC.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpOriginal_WC.SelectedValue))
            {
                newData.Original_WC = this.drpOriginal_WC.SelectedText;
            }
            if (this.drpType.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpType.SelectedValue))
            {
                newData.Type = this.drpType.SelectedValue;
            }
            if (this.drpContract_Admin.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpContract_Admin.SelectedValue))
            {
                newData.Contract_Admin = this.drpContract_Admin.SelectedValue;
            }
            if (this.drpCost_Checker.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpCost_Checker.SelectedValue))
            {
                newData.Cost_Checker = this.drpCost_Checker.SelectedValue;
            }
            if (this.drpBuyer.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpBuyer.SelectedValue))
            {
                newData.Buyer = this.drpBuyer.SelectedItem.Text.Trim();
                newData.BuyerId = drpBuyer.SelectedValue.Trim();
            }
            if (this.drpMain_Coordinator.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpMain_Coordinator.SelectedValue))
            {
                newData.Main_Coordinator = this.drpMain_Coordinator.SelectedValue;
                var user = BLL.Sys_UserService.GetUsersByUserId(newData.Main_Coordinator);
                if (user != null)
                {
                    var dep = BLL.DepartService.GetDepartById(user.DepartId);
                    {
                        if (dep != null)
                        {
                            this.txtApplicant.Text = dep.DepartCode;
                        }
                    }
                }
            }
            if (this.drpUser_Representative.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpUser_Representative.SelectedValue))
            {
                newData.User_Representative = this.drpUser_Representative.SelectedValue;
            }
            //newData.Applicant = this.txtApplicant.Text.Trim();
            newData.Validate_Date = Funs.GetNewDateTime(this.txtValidate_Date.Text.Trim());
            newData.Expire_Date = Funs.GetNewDateTime(this.txtExpire_Date.Text.Trim());
            if (this.drpFCStatus.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpFCStatus.SelectedValue))
            {
                newData.FC_Status = this.drpFCStatus.SelectedValue;
            }
            if (drpAllocation.SelectedValue != "0")
            {
                newData.Allocation = drpAllocation.SelectedValue;
            }
            
            newData.Contract_Person = this.txtContactPerson.Text.Trim();
            newData.Contract_Tel = this.txtContract_Tel.Text.Trim();
            newData.Email = txtEMial.Text.Trim();
            newData.MainHead = txtMainHead.Text.Trim();
            newData.MainHeadContact = txtMainHeadContact.Text.Trim();
            newData.SafetyOfficer = txtSafetyOfficer.Text.Trim();
            newData.SafetyOfficerContact = txtSafetyOfficerContact.Text.Trim();
            newData.ProjectManager = txtProjectManager.Text.Trim();
            newData.ProjectManagerContact = txtProjectManagerContact.Text.Trim();

            decimal? value = Funs.GetNewDecimal(this.numProportion_of_FC_Definition.Text.Trim());
            if (value != null)
            {
                newData.Proportion_of_FC_Definition = value / Convert.ToDecimal(100.0);
            }
            else
            {
                newData.Proportion_of_FC_Definition = null;
            }
         
            newData.Actual_Budget = Funs.GetNewDecimal(this.txtActual_Budget.Text.Trim());
            newData.Remark = this.txtRemark.Text.Trim();
            if (this.drpDiscispline.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpDiscispline.SelectedValue))
            {
                newData.DisciplineId = this.drpDiscispline.SelectedValue;
            }
            if (this.drpFOTypeId.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpFOTypeId.SelectedValue))
            {
                newData.FOTypeId = this.drpFOTypeId.SelectedValue;
            }

            newData.DateIn = DateTime.Now;

            newData.ConstRecords = this.drpConstRecords.SelectedValue;
            if (this.drpConnectedTransaction.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpConnectedTransaction.SelectedValue))
            {
                newData.ConnectedTransaction = this.drpConnectedTransaction.SelectedValue;
            }

            newData.ExceedLimit = Funs.GetNewInt(this.txtNumExceedLimit.Text.Trim());

            if (!string.IsNullOrEmpty(id))
            {
                newData.ID = Convert.ToInt32(id);
                newData.ModifyDate = DateTime.Now;
                newData.ModifyPerson = this.CurrUser.UserId;

                BLL.SESRelatedDataService.UpdateSESRelatedData(newData);
                BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Modify FC List!");
                ShowNotify("Save successfully!", MessageBoxIcon.Success);
            }
            else
            {
                newData.CreateDate = DateTime.Now;
                newData.CreatePerson = this.CurrUser.UserId;

                BLL.SESRelatedDataService.AddSESRelatedData(newData);
                BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Add FC List!");
                ShowNotify("Save successfully!", MessageBoxIcon.Success);
            }
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion

        #region 权限设置
        /// <summary>
        /// 菜单按钮权限
        /// </summary>
        private void GetButtonPower()
        {
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.UserId, BLL.Const.SESRelatedDateMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
            }
        }
        #endregion

        #region DropDownList下拉选择事件
        /// <summary>
        /// 分包商下拉选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void drpContractor_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txtVendor_NO.Text = string.Empty;
            
            if (this.drpContractor.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpContractor.SelectedValue))
            {
                var contractor = BLL.ContractorService.GetContractorById(this.drpContractor.SelectedValue);
                if (contractor != null)
                {
                    this.txtVendor_NO.Text = contractor.VendorNumber;
                }
            }
        }

        /// <summary>
        /// 根据主协调人加载主协调人部门
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void drpMain_Coordinator_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.drpMain_Coordinator.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpMain_Coordinator.SelectedValue))
            {
                var user = BLL.Sys_UserService.GetUsersByUserId(this.drpMain_Coordinator.SelectedValue);
                if (user != null)
                {
                    if (!string.IsNullOrEmpty(user.DepartId))
                    {
                        this.txtApplicant.Text = BLL.DepartService.GetDepartNameById(user.DepartId);
                    }
                    else
                    {
                        this.txtApplicant.Text = string.Empty;
                    }
                }
                else
                {
                    this.txtApplicant.Text = string.Empty;
                }
            }
            else
            {
                this.txtApplicant.Text = string.Empty;
            }
        }

        /// <summary>
        /// 选择日期更新状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtExpire_Date_TextChanged(object sender, EventArgs e)
        {
            string lastPayment = string.Empty;
            if (txtFO_NO.Text.Trim() != string.Empty)
            {
                var cpt = from x in Funs.DB.CPTList where x.Contract_No == txtFO_NO.Text.Trim() select x;
                if (cpt.Count() > 0)
                {
                    lastPayment = cpt.FirstOrDefault().Last_Payment;
                }
            }

            if (!string.IsNullOrEmpty(this.txtExpire_Date.Text.Trim()))
            {
                DateTime expireDate = Convert.ToDateTime(this.txtExpire_Date.Text.Trim());
                if (expireDate >= Convert.ToDateTime(DateTime.Now.ToShortDateString()) && expireDate.AddMonths(-6) <= Convert.ToDateTime(DateTime.Now.ToShortDateString()))
                {
                    drpFCStatus.SelectedValue= "Expired Soon";
                    //this.txtFC_Status.Text = "Expired Soon";
                }
                else if (expireDate < Convert.ToDateTime(DateTime.Now))
                {
                    drpFCStatus.SelectedValue = "Expired";
                    if (lastPayment=="Y")
                    {
                        drpFCStatus.SelectedValue = "Closed";
                    }
                }
                else if (expireDate >= Convert.ToDateTime(DateTime.Now))
                {
                    drpFCStatus.SelectedValue = "Valid";
                }
            }
           
        }
        #endregion
    }
}