using System;
using BLL;

namespace FineUIPro.Web.common.BaseInfo
{
    public partial class UnitEdit : PageBase
    {
        #region 加载页面
        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                string unitId = Request.Params["unitId"];
                BLL.Base_UnitService.UnitTypeDropDownList(this.drpUnitType, true,Resources.Lan.PleaseSelect);

                if (!string.IsNullOrEmpty(unitId))
                {
                    var unit = BLL.Base_UnitService.GetUnit(unitId);
                    if (unit != null)
                    {
                        this.txtUnitCode.Text = unit.UnitCode;
                        this.txtUnitName.Text = unit.UnitName;
                        this.txtCorporate.Text = unit.Corporate;
                        this.txtTelephone.Text = unit.Telephone;
                        this.txtFax.Text = unit.Fax;
                        this.txtAddress.Text = unit.Address;
                        this.txtProjectRange.Text = unit.ProjectRange;
                        this.drpUnitType.SelectedValue = unit.UnitTypeId;
                        
                    }
                }
            }
        }

        /// <summary>
        /// 加载页面
        /// </summary>
        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
        }
        #endregion

        #region 保存
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.UnitMenuId, BLL.Const.BtnSave))
            {
                if (this.drpUnitType.SelectedValue == Const._Null)
                {
                    Alert.ShowInParent(Resources.Lan.SelectUnitType,MessageBoxIcon.Warning);
                    return;
                }
                string unitId = Request.Params["unitId"];
                Model.Base_Unit newUnit = new Model.Base_Unit();
                newUnit.UnitCode = this.txtUnitCode.Text;
                newUnit.UnitName = this.txtUnitName.Text;
                newUnit.Corporate = this.txtCorporate.Text;
                newUnit.Address = this.txtAddress.Text;
                newUnit.Telephone = this.txtTelephone.Text;
                newUnit.Fax = this.txtFax.Text;
                newUnit.ProjectRange = this.txtProjectRange.Text.Trim();
                if (this.drpUnitType.SelectedValue!=Const._Null)
                {
                    newUnit.UnitTypeId = this.drpUnitType.SelectedValue;
                }
               
                if (string.IsNullOrEmpty(unitId))
                {
                    newUnit.UnitId = SQLHelper.GetNewID(typeof(Model.Base_Unit));
                    if (!BLL.Base_UnitService.IsExistUnitCode(newUnit.UnitId, this.txtUnitCode.Text) && !BLL.Base_UnitService.IsExistUnitName(newUnit.UnitId, this.txtUnitName.Text))
                    {

                        BLL.Base_UnitService.AddUnit(newUnit);
                        BLL.Sys_LogService.AddLog(Const.System_1, this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.UnitMenuId, Const.BtnAdd, newUnit.UnitId);
                    }
                    else
                    {
                        Alert.ShowInParent(Resources.Lan.UnitExists,MessageBoxIcon.Warning);
                        return;
                    }
                }
                else
                {
                    if (!BLL.Base_UnitService.IsExistUnitCode(unitId, this.txtUnitCode.Text) && !BLL.Base_UnitService.IsExistUnitName(newUnit.UnitId, this.txtUnitName.Text))
                    {
                        newUnit.UnitId = unitId;
                        BLL.Base_UnitService.updateUnit(newUnit);
                        BLL.Sys_LogService.AddLog(Const.System_1, this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.WelderManageMenuId, Const.BtnModify, unitId);
                    }
                    else
                    {
                        Alert.ShowInParent(Resources.Lan.UnitExists, MessageBoxIcon.Warning);
                        return;
                    }
                }

            }
            else
            {
                Alert.ShowInParent(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning);
            }
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion

        #region 关闭按钮
        protected void btnClose_Click(object sender, EventArgs e)
        {
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion       
    }
}