using BLL;
using System;
using System.Linq;
namespace FineUIPro.Web.HSSE.TowerCrane
{
    public partial class TowerCraneEdit : PageBase
    {
        #region 定义变量
        /// 
        ///  主键
        /// 
        public string TowerCraneId
        {
            get
            {
                return (string)ViewState["TowerCraneId"];
            }
            set
            {
                ViewState["TowerCraneId"] = value;
            }
        }
       
        #endregion
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                 ////权限按钮方法
                this.GetButtonPower();       
                this.TowerCraneId = Request.Params["TowerCraneId"];
                 if (!string.IsNullOrEmpty(this.TowerCraneId))
                {
                    Model.HSSE_TowerCrane rectify = Funs.DB.HSSE_TowerCrane.FirstOrDefault(x=>x.TowerCraneId==this.TowerCraneId);
                    if (rectify != null)
                    {
                        this.txtTowerCraneName.Text = rectify.TowerCraneName;
                        this.txtTowerCraneCode.Text = rectify.TowerCraneCode;
                        this.txtVideoAccount.Text = rectify.VideoAccount;
                        this.txtVideoPassword.Text = rectify.VideoPassword;
                        if (rectify.MaxHoist.HasValue)
                        {
                            this.txtMaxHoist.Text = rectify.MaxHoist.Value.ToString("#.##");
                        }
                        if (rectify.MaxMoment.HasValue)
                        {
                            this.txtMaxMoment.Text = rectify.MaxMoment.Value.ToString("#.##");
                        }
                        if (rectify.FrontArmLength.HasValue)
                        {
                            this.txtFrontArmLength.Text = rectify.FrontArmLength.Value.ToString("#.##");
                        }
                        if (rectify.BackArmLength.HasValue)
                        {
                            this.txtBackArmLength.Text = rectify.BackArmLength.Value.ToString("#.##");
                        }
                        if (rectify.ArmHeigh.HasValue)
                        {
                            this.txtArmHeigh.Text = rectify.ArmHeigh.Value.ToString("#.##");
                        }
                        if (rectify.MaxMoment.HasValue)
                        {
                            this.txtMaxMoment.Text = rectify.MaxMoment.Value.ToString("#.##");
                        }
                        if (rectify.TowerCap=="是")
                        {
                            this.isTowerCap.Checked = true;
                        }
                        else
                        {
                            this.isTowerCap.Checked = false;
                        }
                        this.txtRemark.Text = rectify.Remark;
                        
                    }
                }
            }
        }
        #endregion
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Model.HSSE_TowerCrane towerCrane = null;
            if (!string.IsNullOrEmpty(TowerCraneId))
            {
                towerCrane = Funs.DB.HSSE_TowerCrane.FirstOrDefault(x => x.TowerCraneId == this.TowerCraneId);
            }
            else
            {
                towerCrane = new Model.HSSE_TowerCrane(); 
                towerCrane.TowerCraneId = SQLHelper.GetNewID(typeof(Model.HSSE_TowerCrane));
                towerCrane.ProjectId = CurrUser.LoginProjectId;
                towerCrane.CreateDate = DateTime.Now;
                Funs.DB.HSSE_TowerCrane.InsertOnSubmit(towerCrane);
            } 
            towerCrane.TowerCraneCode = this.txtTowerCraneCode.Text.Trim();
            towerCrane.TowerCraneName = this.txtTowerCraneName.Text.Trim();
            towerCrane.VideoAccount = this.txtVideoAccount.Text.Trim();
            towerCrane.VideoPassword = this.txtVideoPassword.Text.Trim();
            if (!string.IsNullOrEmpty(this.txtMaxHoist.Text))
            {
                towerCrane.MaxHoist = double.Parse(this.txtMaxHoist.Text);
            }
            if (!string.IsNullOrEmpty(this.txtMaxMoment.Text))
            {
                towerCrane.MaxMoment = double.Parse(this.txtMaxMoment.Text);
            }
            if (!string.IsNullOrEmpty(this.txtFrontArmLength.Text))
            {
                towerCrane.FrontArmLength = double.Parse(this.txtFrontArmLength.Text);
            }
            if (!string.IsNullOrEmpty(this.txtBackArmLength.Text))
            {
                towerCrane.BackArmLength = double.Parse(this.txtBackArmLength.Text);
            }
            if (!string.IsNullOrEmpty(this.txtArmHeigh.Text))
            {
                towerCrane.ArmHeigh = double.Parse(this.txtArmHeigh.Text);
            }
            if (isTowerCap.Checked)
            {
                towerCrane.TowerCap = "是";
            }
            else
            {
                towerCrane.TowerCap = "否";
            }
            towerCrane.Remark = this.txtRemark.Text.Trim();
            Funs.DB.SubmitChanges(); 
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
        #region 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private void GetButtonPower()
        {
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.TowerCraneMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
            }
        }
        #endregion
    }
}