using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Xml;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace FineUIPro.Web.Transfer
{
    public partial class PunchlistFromEdit : PageBase
    {
        #region 定义项
        /// 
        /// 主键
        /// 
        private string Id
        {
            get
            {
                return (string)ViewState["Id"];
            }
            set
            {
                ViewState["Id"] = value;
            }
        }
        /// 
        /// 项目主键
        /// 
        public string ProjectId
        {
            get
            {
                return (string)ViewState["ProjectId"];
            }
            set
            {
                ViewState["ProjectId"] = value;
            }
        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.Id = Request.Params["Id"];
                this.ProjectId = this.CurrUser.LoginProjectId;
                ProhibitedFields();
                var TransferPunchlistFrom = Funs.DB.Transfer_PunchlistFrom.FirstOrDefault(p => p.Id == this.Id);
                if (TransferPunchlistFrom != null)
                {
                    this.txtNum_NO.Text = TransferPunchlistFrom.Num_NO;
                    this.txtSystem_No.Text = TransferPunchlistFrom.System_No;
                    this.txtSub_Sys_No.Text = TransferPunchlistFrom.Sub_Sys_No;
                    this.txtDESCRIPTION.Text = TransferPunchlistFrom.DESCRIPTION;
                    this.ddlCat.SelectedValue = TransferPunchlistFrom.Cat;
                    this.txtRaised_By.Text = TransferPunchlistFrom.Raised_By;
                    if (TransferPunchlistFrom.Date_Raised != null)
                        this.txtDate_Raised.Text = Convert.ToDateTime(TransferPunchlistFrom.Date_Raised).ToString("yyyy-MM-dd");
                    this.txtDisc.Text = TransferPunchlistFrom.Disc;
                    if (TransferPunchlistFrom.IsEng == true)
                        this.ddlIsEng.SelectedValue = "Y";
                    else
                        this.ddlIsEng.SelectedValue = "N";
                    this.ddlIsMatI.SelectedValue = TransferPunchlistFrom.IsMatI ?? "";
                    this.txtPunch_Type.SelectedValue = TransferPunchlistFrom.Punch_Type ?? "";
                    if (TransferPunchlistFrom.Required_Date != null)
                        this.txtRequired_Date.Text = Convert.ToDateTime(TransferPunchlistFrom.Required_Date).ToString("yyyy-MM-dd");
                    this.txtAction_By.Text = TransferPunchlistFrom.Action_By;
                    this.txtPIC.Text = TransferPunchlistFrom.PIC;
                    this.txtPIC_WUH.Text = TransferPunchlistFrom.PIC_WUH;
                    this.txtCorrection_Action.Text = TransferPunchlistFrom.Correction_Action;
                    if (TransferPunchlistFrom.Actual_Date != null)
                        this.txtActual_Date.Text = Convert.ToDateTime(TransferPunchlistFrom.Actual_Date).ToString("yyyy-MM-dd");
                    this.txtCleared_By.Text = TransferPunchlistFrom.Cleared_By;
                    if (TransferPunchlistFrom.Cleared_Date != null)
                        this.txtCleared_Date.Text = Convert.ToDateTime(TransferPunchlistFrom.Cleared_Date).ToString("yyyy-MM-dd");
                    this.txtConfirmed_By.Text = TransferPunchlistFrom.Confirmed_By;
                    if (TransferPunchlistFrom.Confirmed_Date != null)
                        this.txtConfirmed_Date.Text = Convert.ToDateTime(TransferPunchlistFrom.Confirmed_Date).ToString("yyyy-MM-dd");
                    this.txtVerified_By.Text = TransferPunchlistFrom.Verified_By;
                    if (TransferPunchlistFrom.Verified_Date != null)
                        this.txtVerified_Date.Text = Convert.ToDateTime(TransferPunchlistFrom.Verified_Date).ToString("yyyy-MM-dd");
                    this.txtRemark.Text = TransferPunchlistFrom.Remark;
                }
                else
                {
                    var maxNumNO = 0;
                    var modelTp = Funs.DB.Transfer_PunchlistFrom.OrderByDescending(x => x.Num_NO).FirstOrDefault(x => x.ProjectId == CurrUser.LoginProjectId);
                    if (modelTp == null)
                        maxNumNO = 1;
                    else
                        maxNumNO = Convert.ToInt32(modelTp.Num_NO) + 1;
                    this.txtNum_NO.Text = maxNumNO.ToString("0000");
                }
            }
        }
        
        
        /// 
        /// 禁止编辑字段事件触发时。
        /// 
        /// 
        /// 
        protected void ProhibitedFields()
        {
            var query = (from projectUser in Funs.DB.Project_ProjectUser
                    join projectUnit in Funs.DB.Project_ProjectUnit on new { projectUser.UnitId, projectUser.ProjectId }
                        equals new { projectUnit.UnitId, projectUnit.ProjectId } into projectUnitJoin
                    from projectUnit in projectUnitJoin.DefaultIfEmpty()
                    join sysConst in Funs.DB.Sys_Const on new
                        { GroupId = "ProjectUnitType", ConstValue = projectUnit.UnitType } equals new
                        { sysConst.GroupId, sysConst.ConstValue } into sysConstJoin
                    from sysConst in sysConstJoin.DefaultIfEmpty()
                    where projectUser.UserId == this.CurrUser.UserId
                    select sysConst.ConstValue)
                .Distinct().FirstOrDefault();
            
            if (query != null && query == "2")
            {
                this.txtConfirmed_By.Readonly = true;
                this.txtConfirmed_Date.Readonly = true;
                this.txtVerified_By.Readonly = true;
                this.txtVerified_Date.Readonly = true;
            }
        }
        
        /// 
        /// Photoes附件上传
        /// 
        /// 
        /// 
        protected void btnAttachA_Click(object sender, EventArgs e)
        {
            AttachFileClick("A");
        }
        /// 
        /// Corrected Photos附件上传
        /// 
        /// 
        /// 
        protected void btnAttachB_Click(object sender, EventArgs e)
        {
            AttachFileClick("B");
        }
        /// 
        /// 通过不同的type来保存附件
        /// 
        /// 
        private void AttachFileClick(string type)
        {
            SaveData();
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../AttachFile/webuploader.aspx?type=0&toKeyId={0}_{1}&path=FileUpload/Transfer/PunchlistFrom&menuId={2}", this.Id, type, BLL.Const.PunchlistFromMenuId)));
        }
        /// 
        /// 保存
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (SaveData() == 0)
            {
                return;
            }
            ShowNotify("保存成功", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        /// 
        /// 保存数据
        /// 
        /// 
        private int SaveData()
        {
            int reutState = 0;
            Model.Transfer_PunchlistFrom newTransferFrom = new Model.Transfer_PunchlistFrom()
            {
                Id = Guid.NewGuid().ToString(),
                Num_NO = this.txtNum_NO.Text.Trim(),
                ProjectId = this.ProjectId,
                System_No = this.txtSystem_No.Text.Trim(),
                Sub_Sys_No = this.txtSub_Sys_No.Text.Trim(),
                DESCRIPTION = this.txtDESCRIPTION.Text.Trim(),
                Cat = this.ddlCat.SelectedValue,
                Raised_By = this.txtRaised_By.Text.Trim(),
                Date_Raised = Funs.GetNewDateTime(this.txtDate_Raised.Text),
                Disc = this.txtDisc.Text.Trim(),
                IsEng = this.ddlIsEng.SelectedValue == "Y" ? true : false,
                IsMatI = this.ddlIsMatI.SelectedValue,
                Punch_Type = this.txtPunch_Type.SelectedValue,
                Required_Date = Funs.GetNewDateTime(this.txtRequired_Date.Text),
                Action_By = this.txtAction_By.Text.Trim(),
                PIC = this.txtPIC.Text.Trim(),
                PIC_WUH = this.txtPIC_WUH.Text.Trim(),
                Correction_Action = this.txtCorrection_Action.Text.Trim(),
                Actual_Date = Funs.GetNewDateTime(this.txtActual_Date.Text),
                Cleared_By = this.txtCleared_By.Text.Trim(),
                Cleared_Date = Funs.GetNewDateTime(this.txtCleared_Date.Text),
                Confirmed_By = this.txtConfirmed_By.Text.Trim(),
                Confirmed_Date = Funs.GetNewDateTime(this.txtConfirmed_Date.Text),
                Verified_By = this.txtVerified_By.Text.Trim(),
                Verified_Date = Funs.GetNewDateTime(this.txtVerified_Date.Text),
                Remark = this.txtRemark.Text.Trim()
            };
            if (newTransferFrom.Cleared_Date == null && newTransferFrom.Confirmed_Date == null && newTransferFrom.Verified_Date == null)
                newTransferFrom.Status = "Not Start";
            else if (newTransferFrom.Cleared_Date != null && newTransferFrom.Confirmed_Date != null && newTransferFrom.Verified_Date != null)
                newTransferFrom.Status = "Completed";
            else
                newTransferFrom.Status = "In Progress";
            var TransferPunchlistFrom = Funs.DB.Transfer_PunchlistFrom.FirstOrDefault(p => p.Id == this.Id);
            if (TransferPunchlistFrom == null)
            {
                if (Funs.DB.Transfer_PunchlistFrom.Where(p => p.ProjectId == this.ProjectId && p.Num_NO == newTransferFrom.Num_NO).Count() > 0)
                {
                    ShowNotify("录入的NO.重复了,不能保存", MessageBoxIcon.Warning);
                    return reutState;
                }
                Funs.DB.Transfer_PunchlistFrom.InsertOnSubmit(newTransferFrom);
                this.Id = newTransferFrom.Id;
            }
            else
            {
                if (Funs.DB.Transfer_PunchlistFrom.Where(p => p.ProjectId == this.ProjectId && p.Num_NO == newTransferFrom.Num_NO && p.Id != TransferPunchlistFrom.Id).Count() > 0)
                {
                    ShowNotify("录入的NO.重复了,不能保存", MessageBoxIcon.Warning);
                    return reutState;
                }
                TransferPunchlistFrom.Num_NO = newTransferFrom.Num_NO;
                TransferPunchlistFrom.System_No = newTransferFrom.System_No;
                TransferPunchlistFrom.Sub_Sys_No = newTransferFrom.Sub_Sys_No;
                TransferPunchlistFrom.DESCRIPTION = newTransferFrom.DESCRIPTION;
                TransferPunchlistFrom.Cat = newTransferFrom.Cat;
                TransferPunchlistFrom.Raised_By = newTransferFrom.Raised_By;
                TransferPunchlistFrom.Date_Raised = newTransferFrom.Date_Raised;
                TransferPunchlistFrom.Disc = newTransferFrom.Disc;
                TransferPunchlistFrom.IsEng = newTransferFrom.IsEng;
                TransferPunchlistFrom.IsMatI = newTransferFrom.IsMatI;
                TransferPunchlistFrom.Punch_Type = newTransferFrom.Punch_Type;
                TransferPunchlistFrom.Required_Date = newTransferFrom.Required_Date;
                TransferPunchlistFrom.Action_By = newTransferFrom.Action_By;
                TransferPunchlistFrom.PIC = newTransferFrom.PIC;
                TransferPunchlistFrom.PIC_WUH = newTransferFrom.PIC_WUH;
                TransferPunchlistFrom.Correction_Action = newTransferFrom.Correction_Action;
                TransferPunchlistFrom.Actual_Date = newTransferFrom.Actual_Date;
                TransferPunchlistFrom.Cleared_By = newTransferFrom.Cleared_By;
                TransferPunchlistFrom.Cleared_Date = newTransferFrom.Cleared_Date;
                TransferPunchlistFrom.Confirmed_By = newTransferFrom.Confirmed_By;
                TransferPunchlistFrom.Confirmed_Date = newTransferFrom.Confirmed_Date;
                TransferPunchlistFrom.Verified_By = newTransferFrom.Verified_By;
                TransferPunchlistFrom.Verified_Date = newTransferFrom.Verified_Date;
                TransferPunchlistFrom.Remark = newTransferFrom.Remark;
                TransferPunchlistFrom.Status = newTransferFrom.Status;
            }
            Funs.DB.SubmitChanges();
            reutState = 1;
            return reutState;
        }
    }
}