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

namespace FineUIPro.Web.SES
{
    public partial class ContractorQualificationEdit : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnClose.OnClientClick = ActiveWindow.GetHideReference();
                //txtLimitEndDate.MinDate = DateTime.Now;
                string queId = Request.Params["queId"];
                if (!string.IsNullOrEmpty(queId))
                {
                   var que= Funs.DB.EMC_Contractor_Qualification.FirstOrDefault(x => x.QueId == queId);
                    txtQueName.Text = que.QueName;
                    txtQueDef.Text = que.QueDef;
                    txtLimitStartDate.Text= que.LimitStartDate.HasValue ? string.Format("{0:yyyy-MM-dd}", que.LimitStartDate) : "";
                    txtLimitEndDate.Text = que.LimitEndDate.HasValue ? string.Format("{0:yyyy-MM-dd}", que.LimitEndDate) : "";
                    txtRemark.Text = que.Remark;
                    hdId.Text = queId;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string foId = Request.Params["foId"];
            string quetype = Request.Params["quetype"];
            string queId = Request.Params["queId"];

            if (!string.IsNullOrEmpty(queId))
            {
                Model.EMC_Contractor_Qualification que = Funs.DB.EMC_Contractor_Qualification.FirstOrDefault(x => x.QueId == queId);
                quetype = que.QueType;
                que.QueName = txtQueName.Text.Trim();
                que.QueDef = txtQueDef.Text.Trim();
                que.LimitStartDate = Funs.GetNewDateTime(txtLimitStartDate.Text.Trim());
                que.LimitEndDate = Funs.GetNewDateTime(txtLimitEndDate.Text.Trim());
                que.Remark = txtRemark.Text.Trim();

                var att = from x in Funs.DB.AttachFile where x.ToKeyId == queId select x;
                if (att.Count() > 0)
                {
                    que.AttachUrl = att.First().AttachUrl;
                }
                Funs.DB.SubmitChanges();
                BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Modify Contractor Qualification!");
                ShowNotify("Save successfully!", MessageBoxIcon.Success);
            }
            else
            {
                Model.EMC_Contractor_Qualification que = new Model.EMC_Contractor_Qualification();
                que.FoId = Convert.ToInt32(foId);
                que.QueType = quetype;
                que.QueName = txtQueName.Text.Trim();
                que.QueDef = txtQueDef.Text.Trim();
                que.LimitStartDate = Funs.GetNewDateTime(txtLimitStartDate.Text.Trim());
                que.LimitEndDate = Funs.GetNewDateTime(txtLimitEndDate.Text.Trim());
                que.Remark = txtRemark.Text.Trim();

                if (!string.IsNullOrEmpty(this.hdId.Text))
                {
                    que.QueId = this.hdId.Text.Trim();
                }
                else
                {
                    que.QueId = SQLHelper.GetNewID(typeof(Model.EMC_Contractor_Qualification));
                    this.hdId.Text = que.QueId;
                }
                var att = from x in Funs.DB.AttachFile where x.ToKeyId == que.QueId select x;
                if (att.Count() > 0)
                {
                    que.AttachUrl = att.First().AttachUrl;
                }

                Funs.DB.EMC_Contractor_Qualification.InsertOnSubmit(que);
                Funs.DB.SubmitChanges();

                BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Add Contractor Qualification!");
                ShowNotify("Save successfully!", MessageBoxIcon.Success);
            }
            //PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(quetype)
                   + ActiveWindow.GetHidePostBackReference());
        }

        #region 附件上传
        /// <summary>
        /// 附件上传
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAttach_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.hdId.Text))   //新增记录
            {
                this.hdId.Text = SQLHelper.GetNewID(typeof(Model.EMC_Contractor_Qualification));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../AttachFile/webuploader2.aspx?type=0&toKeyId={0}&path=FileUpload/SES/ContractorQualification&menuId={1}", this.hdId.Text, BLL.Const.ContractorQualificationMenuId)));
        }
        #endregion
    }
}