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

namespace FineUIPro.Web.SES
{
    public partial class SESRelatedDataContractManage12 : PageBase
    {
        #region 加载
        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnClose.OnClientClick = ActiveWindow.GetHideReference();

                //上传人
                BLL.Sys_UserService.InitUserDropDownList(this.drpUploadMan, true);
                if (CurrUser.Account == Const.Gly)
                {
                    drpUploadMan.Enabled = true;
                }
                else
                {
                    drpUploadMan.SelectedValue = CurrUser.UserId;
                    drpUploadMan.Enabled = false;
                }
                //BLL.FileTypeService.InitFileTypeDropDownList(this.drpFileTypeId, true);
                BLL.DepartService.InitDropDownList(this.drpBycDept, true);

                string fileId = Request.Params["fileId"];
                if (!string.IsNullOrEmpty(fileId))
                {
                    Model.FC_ContractManagement file = BLL.ContractManagementService.GetContractManagementById(fileId);
                    if (file != null)
                    {
                        this.hdId.Text = file.FileId;
                        //if (!string.IsNullOrEmpty(file.FileTypeId))
                        //{
                        //    this.drpFileTypeId.SelectedValue = file.FileTypeId;
                        //}
                        this.txtOccurDate.Text = file.OccurDate.HasValue ? string.Format("{0:yyyy-MM-dd}", file.OccurDate) : "";
                        this.txtFileName.Text = file.FileName;
                        if (!string.IsNullOrEmpty(file.BycDept))
                        {
                            this.drpBycDept.SelectedValue = file.BycDept;
                        }
                        if (!string.IsNullOrEmpty(file.UploadMan))
                        {
                            this.drpUploadMan.SelectedValue = file.UploadMan;
                        }
                        this.txtUploadDate.Text = file.UploadDate.HasValue ? string.Format("{0:yyyy-MM-dd}", file.UploadDate) : "";
                        this.txtRemark.Text = file.Remark;

                        if (!string.IsNullOrEmpty(file.FC_ID.ToString()))
                        {
                            var data = BLL.SESRelatedDataService.GetSESRelatedDataById(file.FC_ID.ToString());
                            if (data != null && !string.IsNullOrEmpty(data.Contractor))
                            {
                                var con = BLL.ContractorService.GetContractorById(data.Contractor);
                                if (con != null)
                                {
                                    string contractor = string.Empty;
                                    if (string.IsNullOrEmpty(con.Contractor) && !string.IsNullOrEmpty(con.ContractorCN))
                                    {
                                        contractor = con.ContractorCN;
                                    }
                                    else if (string.IsNullOrEmpty(con.ContractorCN) && !string.IsNullOrEmpty(con.Contractor))
                                    {
                                        contractor = con.Contractor;
                                    }
                                    else
                                    {
                                        contractor = con.Contractor + " " + con.ContractorCN;
                                    }
                                    txtContractor.Text = contractor;
                                }
                                this.txtFO.Text = data.FO_NO;
                            }
                        }
                    }
                }
                else
                {
                    string id = Request.Params["id"];
                    if (!string.IsNullOrEmpty(id))
                    {
                        var data = BLL.SESRelatedDataService.GetSESRelatedDataById(id);
                        if (data != null)
                        {
                            var con = BLL.ContractorService.GetContractorById(data.Contractor);
                            if (con != null)
                            {
                                string contractor = string.Empty;
                                if (string.IsNullOrEmpty(con.Contractor) && !string.IsNullOrEmpty(con.ContractorCN))
                                {
                                    contractor = con.ContractorCN;
                                }
                                else if (string.IsNullOrEmpty(con.ContractorCN) && !string.IsNullOrEmpty(con.Contractor))
                                {
                                    contractor = con.Contractor;
                                }
                                else
                                {
                                    contractor = con.Contractor + " " + con.ContractorCN;
                                }
                                txtContractor.Text = contractor;
                            }
                            this.txtFO.Text = data.FO_NO;
                        }
                    }
                    this.drpUploadMan.SelectedValue = this.CurrUser.UserId;
                    this.txtUploadDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                    string code = SQLHelper.RunProcNewId("SpGetNewCode3", "dbo.FC_ContractManagement", "FileName", "组织架构-" + txtFO.Text.Trim() + "-");
                    txtFileName.Text = code;
                }
            }
        }
        #endregion

        #region 保存
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.drpBycDept.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择BYC负责部门!", MessageBoxIcon.Warning);
                return;
            }
            string id = Request.Params["id"];
            string fileId = Request.Params["fileId"];
            Model.FC_ContractManagement newContract = new Model.FC_ContractManagement();
            newContract.FC_ID = Convert.ToInt32(id);
            newContract.FileTypeId = "12";//组织架构// this.drpFileTypeId.SelectedValue;
            newContract.FileName = this.txtFileName.Text.Trim();
            if (this.drpUploadMan.SelectedValue != BLL.Const._Null)
            {
                newContract.UploadMan = this.drpUploadMan.SelectedValue;
            }
            newContract.UploadDate = DateTime.Now;
            if (this.drpBycDept.SelectedValue != BLL.Const._Null)
            {
                newContract.BycDept = this.drpBycDept.SelectedValue;
            }
            newContract.OccurDate = Funs.GetNewDateTime(this.txtOccurDate.Text);
            newContract.Remark = this.txtRemark.Text.Trim();
            if (!string.IsNullOrEmpty(fileId))
            {
                newContract.FileId = fileId;
                var att = from x in Funs.DB.AttachFile where x.ToKeyId == fileId select x;
                if (att.Count() > 0)
                {
                    newContract.AttachUrl = att.First().AttachUrl;
                }
                BLL.ContractManagementService.UpdateContractManagement(newContract);
                BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "修改组织架构!");
                ShowNotify("Save successfully!", MessageBoxIcon.Success);
            }
            else
            {
                if (!string.IsNullOrEmpty(this.hdId.Text))
                {
                    newContract.FileId = this.hdId.Text.Trim();
                }
                else
                {
                    newContract.FileId = SQLHelper.GetNewID(typeof(Model.FC_ContractManagement));
                    this.hdId.Text = newContract.FileId;
                }

                var att = from x in Funs.DB.AttachFile where x.ToKeyId == newContract.FileId select x;
                if (att.Count() > 0)
                {
                    newContract.AttachUrl = att.First().AttachUrl;
                }
                BLL.ContractManagementService.AddContractManagement(newContract);
                BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "增加组织架构!");
                ShowNotify("Save successfully!", MessageBoxIcon.Success);
            }
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion

        #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.FC_ContractManagement));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../AttachFile/webuploader2.aspx?type=0&toKeyId={0}&path=FileUpload/SES/SESRelatedData/ContractManagement&menuId={1}", this.hdId.Text, BLL.Const.SESRelatedDateMenuId)));
        }
        #endregion
    }
}