using BLL;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNet = System.Web.UI.WebControls;
namespace FineUIPro.Web.CQMS.Information
{
    public partial class StatementEdit : PageBase
    {
        #region 定义变量
        /// 
        /// 方案审查主键
        /// 
        public string StatementId
        {
            get
            {
                return (string)ViewState["StatementId"];
            }
            set
            {
                ViewState["StatementId"] = value;
            }
        }
        #endregion
        #region 定义集合
        /// 
        /// 定义会签意见集合
        /// 
        public static List approves = new List();
        #endregion
        public int ContactImg
        {
            get
            {
                return Convert.ToInt32(ViewState["ContactImg"]);
            }
            set
            {
                ViewState["ContactImg"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BLL.SecretLevelService.InitSecretLevelDropDownList(drpSecretLevel, true);
                UnitService.InitUnitByProjectIdUnitTypeDropDownList(drpUnit, CurrUser.LoginProjectId, BLL.Const.ProjectUnitType_2, true);
                ContactImg = 0;
                //CommonService.GetAllButtonList(CurrUser.LoginProjectId, CurrUser.UserId, Const.StatementMenuId);
                txtCompileDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                StatementId = Request.Params["statementId"];
                if (!string.IsNullOrWhiteSpace(StatementId))
                {
                    HFStatementId.Text = StatementId;
                    Model.CQMS_Statement statement = StatementService.GetStatementByStatementId(StatementId);
                    txtStatementCode.Text = statement.StatementCode;
                    if (!string.IsNullOrEmpty(statement.SecretLevelId))
                    {
                        drpSecretLevel.SelectedValue = statement.SecretLevelId;
                    }
                    if (!string.IsNullOrEmpty(statement.UnitId))
                    {
                        drpUnit.SelectedValue = statement.UnitId;
                        ConfirmFormService.InitConfirmFormList(drpConfirmFormId, this.CurrUser.LoginProjectId, string.Empty, true);
                    }
                    this.txtContractNo.Text = statement.ContractNo;
                    this.txtTaskTheme.Text = statement.TaskTheme;
                    if (!string.IsNullOrEmpty(statement.ConfirmFormId))
                    {
                        this.drpConfirmFormId.SelectedValue = statement.ConfirmFormId;
                        List list = new List();
                        Model.CQMS_ConfirmForm confirmForm = BLL.ConfirmFormService.GetConfirmFormByConfirmFormId(statement.ConfirmFormId);
                        list.Add(confirmForm);
                        drpConfirmFormId.DataValueField = "ConfirmFormId";
                        drpConfirmFormId.DataTextField = "ConfirmFormCode";
                        drpConfirmFormId.DataSource = list;
                        drpConfirmFormId.DataBind();
                    }
                    if (!string.IsNullOrEmpty(statement.ProductionSource))
                    {
                        this.rblProductionSource.SelectedValue = statement.ProductionSource;
                    }
                    this.txtJobContent.Text = statement.JobContent;
                    if (statement.CompileDate != null)
                    {
                        txtCompileDate.Text = string.Format("{0:yyyy-MM-dd}", statement.CompileDate);
                    }
                    if (statement.Edition != null)
                    {
                        txtEdition.Text = statement.Edition.ToString();
                    }
                    bindApprove();
                    if (statement.State == Const.Statement_Compile)
                    {
                        agree.Hidden = true;
                        options.Hidden = true;
                        optio.Hidden = true;
                        StatementService.Init(drpHandleType, Const.Statement_Compile, false);
                        UserService.InitUserProjectIdUnitTypeDropDownList(drpHandleMan, CurrUser.LoginProjectId, BLL.Const.ProjectUnitType_1, false);
                    }
                    else if (statement.State == Const.Statement_ReCompile)
                    {
                        this.trNext.Hidden = true;
                        agree.Hidden = true;
                        options.Hidden = true;
                        optio.Hidden = true;
                    }
                    else if (statement.State == Const.Statement_Audit1)
                    {
                        NoEdit();
                        this.trNext.Hidden = true;
                        this.trPlanCost.Hidden = false;
                        if (statement.PlanCost != null)
                        {
                            this.txtPlanCost.Text = statement.PlanCost.ToString();
                        }
                    }
                    else if (statement.State == Const.Statement_Audit2 || statement.State == Const.Statement_Audit3)
                    {
                        NoEdit();
                        this.trNext.Hidden = true;
                        this.trPlanCost.Hidden = false;
                        if (statement.PlanCost != null)
                        {
                            this.txtPlanCost.Text = statement.PlanCost.ToString();
                        }
                        this.txtPlanCost.Enabled = false;
                        this.trAuditCost.Hidden = false;
                        if (statement.AuditCost != null)
                        {
                            this.txtAuditCost.Text = statement.AuditCost.ToString();
                        }
                    }
                    else if (statement.State == Const.Statement_Audit4)
                    {
                        NoEdit();
                        this.trNext.Hidden = true;
                        this.trPlanCost.Hidden = false;
                        if (statement.PlanCost != null)
                        {
                            this.txtPlanCost.Text = statement.PlanCost.ToString();
                        }
                        this.txtPlanCost.Enabled = false;
                        this.trAuditCost.Hidden = false;
                        if (statement.AuditCost != null)
                        {
                            this.txtAuditCost.Text = statement.AuditCost.ToString();
                        }
                        this.txtAuditCost.Enabled = false;
                        this.trRealCost.Hidden = false;
                        if (statement.RealCost != null)
                        {
                            this.txtRealCost.Text = statement.RealCost.ToString();
                        }
                    }
                }
                else
                {
                    agree.Hidden = true;
                    txtEdition.Text = "0";
                    options.Hidden = true;
                    optio.Hidden = true;
                    plApprove2.Hidden = true;
                    StatementService.Init(drpHandleType, Const.Statement_Compile, false);
                    ConfirmFormService.InitConfirmFormList(drpConfirmFormId, this.CurrUser.LoginProjectId, string.Empty, true);
                    //UserService.InitUserProjectIdUnitTypeDropDownList(drpHandleMan, CurrUser.LoginProjectId, BLL.Const.ProjectUnitType_1, false);
                    //txtStatementCode.Text = SQLHelper.RunProcNewId2("SpGetNewCode3ByProjectId", "dbo.CQMS_Statement", "Code", CurrUser.LoginProjectId);
                }
            }
        }
        private void NoEdit()
        {
            txtStatementCode.Enabled = false;
            drpSecretLevel.Enabled = false;
            drpUnit.Enabled = false;
            txtContractNo.Enabled = false;
            txtTaskTheme.Enabled = false;
            drpConfirmFormId.Enabled = false;
            txtJobContent.Enabled = false;
            rblProductionSource.Enabled = false;
            txtCompileDate.Enabled = false;
            txtEdition.Enabled = false;
            ContactImg = -1;
        }
        /// 
        /// 审批列表
        /// 
        private void bindApprove()
        {
            var list = StatementApproveService.getListData(StatementId);
            gvApprove.DataSource = list;
            gvApprove.DataBind();
        }
        public string man(Object man)
        {
            string appman = string.Empty;
            if (UserService.GetUserByUserId(man.ToString()) != null)
            {
                appman = UserService.GetUserByUserId(man.ToString()).UserName;
            }
            return appman;
        }
        #region 保存/提交
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //validate(Const.BtnSave, "save");
            if (CommonService.GetAllButtonPowerList(CurrUser.LoginProjectId, CurrUser.UserId, Const.StatementMenuId, Const.BtnSave))
            {
                SaveStatement("save");
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
                Alert.ShowInTop("保存成功!", MessageBoxIcon.Success);
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            validate(Const.BtnSubmit, "submmit");
        }
        /// 
        /// 保存验证
        /// 
        /// 
        /// 
        public void validate(string buttonName, string tip)
        {
            if (CommonService.GetAllButtonPowerList(CurrUser.LoginProjectId, CurrUser.UserId, Const.StatementMenuId, buttonName))
            {
                string err = string.Empty;
                if (this.drpSecretLevel.SelectedValue == BLL.Const._Null)
                {
                    err += "请选择密级,";
                }
                if (this.drpUnit.SelectedValue == BLL.Const._Null)
                {
                    err += "请选择分包单位,";
                }
                if (this.drpConfirmFormId.SelectedValue == BLL.Const._Null)
                {
                    err += "请选择签证工作确认编号,";
                }
                if (this.trPlanCost.Hidden == false && string.IsNullOrEmpty(this.txtPlanCost.Text.Trim()) && this.rblIsAgree.SelectedValue == "true")
                {
                    err += "请输入申报签证费用,";
                }
                if (this.trAuditCost.Hidden == false && string.IsNullOrEmpty(this.txtAuditCost.Text.Trim()) && this.rblIsAgree.SelectedValue == "true")
                {
                    err += "请输入确认签证费用,";
                }
                if (this.trRealCost.Hidden == false && string.IsNullOrEmpty(this.txtRealCost.Text.Trim()) && this.rblIsAgree.SelectedValue == "true")
                {
                    err += "请输入同意签证费用,";
                }
                //if (!AttachFileService.Getfile(HFStatementId.Text, Const.StatementMenuId))
                //{
                //    err += "请上传附件,";
                //}
                //if (trOne.Nodes[0].Nodes.Count == 0 && trTwo.Nodes.Count == 0 && trThree.Nodes.Count == 0 &&
                //    trFour.Nodes.Count == 0 && trFive.Nodes.Count == 0 && trSixe.Nodes.Count == 0)
                //{
                //    err += "请选择总包会签人员,";
                //}
                if (!string.IsNullOrWhiteSpace(err))
                {
                    err = err.Substring(0, err.LastIndexOf(","));
                    err += "!";
                }
                if (!string.IsNullOrWhiteSpace(err))
                {
                    Alert.ShowInTop(err, MessageBoxIcon.Warning);
                    return;
                }
                if (!string.IsNullOrWhiteSpace(StatementId))
                {//更新时操作
                    if (tip == "save")
                    {
                        EditConstructSol("save");
                    }
                    else
                    {
                        EditConstructSol("submit");
                    }
                }
                else
                {
                    if (tip == "save")
                    {
                        SaveStatement("save");
                    }
                    else
                    {
                        SaveStatement("submit");
                    }
                    //添加时操作           
                }
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
                if (tip == "save")
                {
                    tip = "保存成功!";
                }
                else
                {
                    tip = "提交成功!";
                }
                Alert.ShowInTop(tip, MessageBoxIcon.Success);
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
            }
        }
        /// 
        /// 编辑时候保存
        /// 
        private void EditConstructSol(string saveType)
        {
            Model.CQMS_Statement statement = new Model.CQMS_Statement();
            statement.StatementCode = txtStatementCode.Text.Trim();
            statement.SecretLevelId = this.drpSecretLevel.SelectedValue;
            statement.ProjectId = CurrUser.LoginProjectId;
            statement.UnitId = this.drpUnit.SelectedValue;
            statement.ContractNo = this.txtContractNo.Text.Trim();
            statement.TaskTheme = this.txtTaskTheme.Text.Trim();
            statement.JobContent = this.txtJobContent.Text.Trim();
            statement.ProductionSource = this.rblProductionSource.SelectedValue;
            statement.ConfirmFormId = this.drpConfirmFormId.SelectedValue;
            int edtion = Convert.ToInt32(txtEdition.Text);
            statement.Edition = edtion;
            if (!string.IsNullOrEmpty(txtCompileDate.Text.Trim()))
            {
                statement.CompileDate = Convert.ToDateTime(txtCompileDate.Text.Trim());
            }
            if (!string.IsNullOrEmpty(StatementId))
            {
                statement.StatementId = StatementId;
                Model.CQMS_Statement statement1 = StatementService.GetStatementByStatementId(StatementId);
                if (saveType == "submit")
                {
                    Model.CQMS_StatementApprove approve = StatementApproveService.GetConstructSoluAppByApproveMan(StatementId, CurrUser.UserId, Convert.ToInt32(statement1.Edition));
                    if (approve != null)
                    {
                        approve.IsAgree = Convert.ToBoolean(this.rblIsAgree.SelectedValue);
                        approve.ApproveIdea = this.txtOptions.Text.Trim();
                        approve.ApproveDate = DateTime.Now;
                        StatementApproveService.UpdateStatementApprove(approve);
                    }
                    if (statement1.State == BLL.Const.Statement_Compile)
                    {
                        statement.State = this.drpHandleType.SelectedValue;
                        Model.CQMS_StatementApprove reApprove = new Model.CQMS_StatementApprove();
                        reApprove.StatementId = statement.StatementId;
                        reApprove.ApproveMan = this.drpHandleMan.SelectedValue;
                        reApprove.ApproveType = this.drpHandleType.SelectedValue;
                        reApprove.Edition = edtion;
                        StatementApproveService.AddStatementApprove(reApprove);
                    }
                    else if (statement1.State == BLL.Const.Statement_ReCompile)
                    {
                        //获取上个版次不同意的审批记录
                        List lastNotAggreeApproves = BLL.StatementApproveService.GetNotAggreeApprovesByStatementIdAndEdtion(StatementId, statement1.Edition.Value - 1);
                        statement.State = lastNotAggreeApproves[0].ApproveType;
                        statement.PlanCost = statement1.PlanCost;
                        statement.AuditCost = statement1.AuditCost;
                        statement.RealCost = statement1.RealCost;
                        foreach (var item in lastNotAggreeApproves)
                        {
                            Model.CQMS_StatementApprove reApprove = new Model.CQMS_StatementApprove();
                            reApprove.StatementId = statement.StatementId;
                            reApprove.ApproveMan = item.ApproveMan;
                            reApprove.ApproveType = item.ApproveType;
                            reApprove.Edition = edtion;
                            StatementApproveService.AddStatementApprove(reApprove);
                        }
                    }
                    else if (statement1.State == BLL.Const.Statement_Audit1)
                    {
                        if (this.rblIsAgree.SelectedValue == "true")    //同意
                        {
                            statement.PlanCost = Funs.GetNewDecimalOrZero(this.txtPlanCost.Text.Trim());
                            statement.State = BLL.Const.Statement_Audit2;
                            Model.CQMS_StatementApprove reApprove = new Model.CQMS_StatementApprove();
                            reApprove.StatementId = statement.StatementId;
                            var users = (from x in Funs.DB.Sys_User
                                         join y in Funs.DB.Project_ProjectUnit
                                         on x.UnitId equals y.UnitId
                                         join p in Funs.DB.Project_ProjectUser
                                         on x.UserId equals p.UserId
                                         where p.RoleId.Contains(Const.ControlEngineer)
                                         && y.UnitType == Const.ProjectUnitType_1 && p.ProjectId == CurrUser.LoginProjectId && y.ProjectId == CurrUser.LoginProjectId
                                         select x).ToList();
                            if (users.Count() > 0)
                            {
                                reApprove.ApproveMan = users[0].UserId;
                            }
                            reApprove.ApproveType = BLL.Const.Statement_Audit2;
                            reApprove.Edition = edtion;
                            StatementApproveService.AddStatementApprove(reApprove);
                        }
                        else   //不同意
                        {
                            if (!string.IsNullOrEmpty(this.txtPlanCost.Text.Trim()))
                            {
                                statement.PlanCost = Funs.GetNewDecimalOrZero(this.txtPlanCost.Text.Trim());
                            }
                            statement.State = Const.Statement_ReCompile;
                            statement.Edition = edtion + 1;
                            Model.CQMS_StatementApprove reApprove = new Model.CQMS_StatementApprove();
                            reApprove.StatementId = statement.StatementId;
                            reApprove.ApproveMan = statement1.CompileMan;
                            reApprove.ApproveType = Const.WorkOrder_ReCompile;
                            reApprove.Edition = edtion + 1;
                            StatementApproveService.AddStatementApprove(reApprove);
                        }
                    }
                    else if (statement1.State == BLL.Const.Statement_Audit2)
                    {
                        if (this.rblIsAgree.SelectedValue == "true")    //同意
                        {
                            statement.PlanCost = Funs.GetNewDecimalOrZero(this.txtPlanCost.Text.Trim());
                            statement.AuditCost = Funs.GetNewDecimalOrZero(this.txtAuditCost.Text.Trim());
                            statement.State = BLL.Const.Statement_Audit3;
                            Model.CQMS_StatementApprove reApprove = new Model.CQMS_StatementApprove();
                            reApprove.StatementId = statement.StatementId;
                            var users = (from x in Funs.DB.Sys_User
                                         join y in Funs.DB.Project_ProjectUnit
                                         on x.UnitId equals y.UnitId
                                         join p in Funs.DB.Project_ProjectUser
                                         on x.UserId equals p.UserId
                                         where p.RoleId.Contains(Const.ControlManager)
                                         && y.UnitType == Const.ProjectUnitType_1 && p.ProjectId == CurrUser.LoginProjectId && y.ProjectId == CurrUser.LoginProjectId
                                         select x).ToList();
                            if (users.Count() > 0)
                            {
                                reApprove.ApproveMan = users[0].UserId;
                            }
                            reApprove.ApproveType = BLL.Const.Statement_Audit3;
                            reApprove.Edition = edtion;
                            StatementApproveService.AddStatementApprove(reApprove);
                        }
                        else   //不同意
                        {
                            if (!string.IsNullOrEmpty(this.txtPlanCost.Text.Trim()))
                            {
                                statement.PlanCost = Funs.GetNewDecimalOrZero(this.txtPlanCost.Text.Trim());
                            }
                            if (!string.IsNullOrEmpty(this.txtAuditCost.Text.Trim()))
                            {
                                statement.AuditCost = Funs.GetNewDecimalOrZero(this.txtAuditCost.Text.Trim());
                            }
                            statement.State = Const.Statement_ReCompile;
                            statement.Edition = edtion + 1;
                            Model.CQMS_StatementApprove reApprove = new Model.CQMS_StatementApprove();
                            reApprove.StatementId = statement.StatementId;
                            reApprove.ApproveMan = statement1.CompileMan;
                            reApprove.ApproveType = Const.WorkOrder_ReCompile;
                            reApprove.Edition = edtion + 1;
                            StatementApproveService.AddStatementApprove(reApprove);
                        }
                    }
                    else if (statement1.State == BLL.Const.Statement_Audit3)
                    {
                        if (this.rblIsAgree.SelectedValue == "true")    //同意
                        {
                            statement.PlanCost = Funs.GetNewDecimalOrZero(this.txtPlanCost.Text.Trim());
                            statement.AuditCost = Funs.GetNewDecimalOrZero(this.txtAuditCost.Text.Trim());
                            statement.State = BLL.Const.Statement_Audit4;
                            Model.CQMS_StatementApprove reApprove = new Model.CQMS_StatementApprove();
                            reApprove.StatementId = statement.StatementId;
                            var users = (from x in Funs.DB.Sys_User
                                         join y in Funs.DB.Project_ProjectUnit
                                         on x.UnitId equals y.UnitId
                                         join p in Funs.DB.Project_ProjectUser
                                         on x.UserId equals p.UserId
                                         where p.RoleId.Contains(Const.ProjectManager)
                                         && y.UnitType == Const.ProjectUnitType_1 && p.ProjectId == CurrUser.LoginProjectId && y.ProjectId == CurrUser.LoginProjectId
                                         select x).ToList();
                            if (users.Count() > 0)
                            {
                                reApprove.ApproveMan = users[0].UserId;
                            }
                            reApprove.ApproveType = BLL.Const.Statement_Audit4;
                            reApprove.Edition = edtion;
                            StatementApproveService.AddStatementApprove(reApprove);
                        }
                        else   //不同意
                        {
                            if (!string.IsNullOrEmpty(this.txtPlanCost.Text.Trim()))
                            {
                                statement.PlanCost = Funs.GetNewDecimalOrZero(this.txtPlanCost.Text.Trim());
                            }
                            if (!string.IsNullOrEmpty(this.txtAuditCost.Text.Trim()))
                            {
                                statement.AuditCost = Funs.GetNewDecimalOrZero(this.txtAuditCost.Text.Trim());
                            }
                            statement.State = Const.Statement_ReCompile;
                            statement.Edition = edtion + 1;
                            Model.CQMS_StatementApprove reApprove = new Model.CQMS_StatementApprove();
                            reApprove.StatementId = statement.StatementId;
                            reApprove.ApproveMan = statement1.CompileMan;
                            reApprove.ApproveType = Const.WorkOrder_ReCompile;
                            reApprove.Edition = edtion + 1;
                            StatementApproveService.AddStatementApprove(reApprove);
                        }
                    }
                    else if (statement1.State == BLL.Const.Statement_Audit4)
                    {
                        if (this.rblIsAgree.SelectedValue == "true")    //同意
                        {
                            statement.PlanCost = Funs.GetNewDecimalOrZero(this.txtPlanCost.Text.Trim());
                            statement.AuditCost = Funs.GetNewDecimalOrZero(this.txtAuditCost.Text.Trim());
                            statement.RealCost = Funs.GetNewDecimalOrZero(this.txtRealCost.Text.Trim());
                            statement.State = BLL.Const.Statement_Complete;
                            Model.CQMS_StatementApprove reApprove = new Model.CQMS_StatementApprove();
                            reApprove.StatementId = statement.StatementId;
                            reApprove.ApproveType = BLL.Const.Statement_Complete;
                            reApprove.Edition = edtion;
                            StatementApproveService.AddStatementApprove(reApprove);
                        }
                        else   //不同意
                        {
                            if (!string.IsNullOrEmpty(this.txtPlanCost.Text.Trim()))
                            {
                                statement.PlanCost = Funs.GetNewDecimalOrZero(this.txtPlanCost.Text.Trim());
                            }
                            if (!string.IsNullOrEmpty(this.txtAuditCost.Text.Trim()))
                            {
                                statement.AuditCost = Funs.GetNewDecimalOrZero(this.txtAuditCost.Text.Trim());
                            }
                            if (!string.IsNullOrEmpty(this.txtRealCost.Text.Trim()))
                            {
                                statement.RealCost = Funs.GetNewDecimalOrZero(this.txtRealCost.Text.Trim());
                            }
                            statement.State = Const.Statement_ReCompile;
                            statement.Edition = edtion + 1;
                            Model.CQMS_StatementApprove reApprove = new Model.CQMS_StatementApprove();
                            reApprove.StatementId = statement.StatementId;
                            reApprove.ApproveMan = statement1.CompileMan;
                            reApprove.ApproveType = Const.WorkOrder_ReCompile;
                            reApprove.Edition = edtion + 1;
                            StatementApproveService.AddStatementApprove(reApprove);
                        }
                    }
                    StatementService.UpdateStatement(statement);
                }
                LogService.AddSys_Log(CurrUser, statement.StatementCode, StatementId, Const.StatementMenuId, "修改工程签证结算单");
            }
        }
        #endregion
        #region 添加时候的保存
        /// 
        /// 保存方案审查
        /// 
        /// 保存类型
        private void SaveStatement(string saveType)
        {
            //if (tvHSE.CheckedNodes.Count == 0 || (tvHSE.CheckedNodes.Count > 0 && tvHSE.CheckedNodes[0].Value == "0"))
            //{
            //    ScriptManager.RegisterStartupScript(this, typeof(string), "_alert", "alert('请选择HSE会签人员!')", true);
            //    return;
            //}
            Model.CQMS_Statement statement = new Model.CQMS_Statement();
            statement.StatementCode = txtStatementCode.Text.Trim();
            statement.SecretLevelId = this.drpSecretLevel.SelectedValue;
            statement.ProjectId = CurrUser.LoginProjectId;
            statement.UnitId = this.drpUnit.SelectedValue;
            statement.ContractNo = this.txtContractNo.Text.Trim();
            statement.TaskTheme = this.txtTaskTheme.Text.Trim();
            statement.JobContent = this.txtJobContent.Text.Trim();
            statement.ProductionSource = this.rblProductionSource.SelectedValue;
            if (this.drpConfirmFormId.SelectedValue != BLL.Const._Null)
            {
                statement.ConfirmFormId = this.drpConfirmFormId.SelectedValue;
            }
            int edtion = Convert.ToInt32(txtEdition.Text);
            statement.Edition = edtion;
            if (!string.IsNullOrEmpty(txtCompileDate.Text.Trim()))
            {
                statement.CompileDate = Convert.ToDateTime(txtCompileDate.Text.Trim());
            }
            if (saveType == "submit")
            {
                statement.State = Const.Statement_Audit1;
            }
            else
            {
                statement.State = Const.Statement_Compile;
            }
            if (!string.IsNullOrEmpty(HFStatementId.Text))
            {
                statement.StatementId = HFStatementId.Text;
            }
            else
            {
                statement.StatementId = SQLHelper.GetNewID(typeof(Model.CQMS_Statement));
            }
            statement.CompileMan = CurrUser.UserId;
            statement.Edition = Convert.ToInt32(txtEdition.Text);
            //if (!string.IsNullOrEmpty(HFStatementId.Text))
            //{
            //    StatementService.UpdateStatement(statement);
            //}
            //else
            //{
                StatementService.AddStatement(statement);
            //}
            if (saveType == "submit")
            {
                Model.CQMS_StatementApprove approve1 = new Model.CQMS_StatementApprove();
                approve1.StatementId = statement.StatementId;
                approve1.ApproveDate = DateTime.Now;
                approve1.ApproveMan = this.CurrUser.UserId;
                approve1.ApproveType = Const.Statement_Compile;
                approve1.Edition = Convert.ToInt32(txtEdition.Text);
                StatementApproveService.AddStatementApprove(approve1);
                Model.CQMS_StatementApprove approve2 = new Model.CQMS_StatementApprove();
                approve2.StatementId = statement.StatementId;
                approve2.ApproveMan = this.drpHandleMan.SelectedValue;
                approve2.ApproveType = Const.Statement_Audit1;
                approve2.Edition = Convert.ToInt32(txtEdition.Text);
                StatementApproveService.AddStatementApprove(approve2);
            }
            else
            {
                Model.CQMS_StatementApprove approve1 = new Model.CQMS_StatementApprove();
                approve1.StatementId = statement.StatementId;
                approve1.ApproveMan = this.CurrUser.UserId;
                approve1.ApproveType = Const.Statement_Compile;
                approve1.Edition = Convert.ToInt32(txtEdition.Text);
                StatementApproveService.AddStatementApprove(approve1);
            }
            LogService.AddSys_Log(CurrUser, statement.StatementCode, StatementId, Const.StatementMenuId, "添加工程签证结算单");
            //}
            //提交
            //if (saveType == "submit")
            //{
            //    countersign(statement.StatementId);
            //}
            //LogService.AddSys_Log(CurrUser, statement.StatementCode, StatementId, Const.StatementMenuId, "编制工程签证结算单");
        }
        #endregion
        protected void imgBtnFile_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(HFStatementId.Text))   //新增记录
            {
                HFStatementId.Text = SQLHelper.GetNewID(typeof(Model.CQMS_Statement));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(
            String.Format("../../AttachFile/webuploader.aspx?type={0}&toKeyId={1}&path=FileUpload/Statement&menuId={2}",
            ContactImg, HFStatementId.Text, Const.StatementMenuId)));
        }
        protected void btnapprove_Click(object sender, EventArgs e)
        {
            //HFStatementId.Text
            var approve = StatementApproveService.GetStatementApproveByApproveMan(HFStatementId.Text, CurrUser.UserId);
            if (approve != null)
            {
                var approveId = approve.StatementApproveId;
                PageContext.RegisterStartupScript(WindowAtt.GetShowReference(
                String.Format("../../AttachFile/webuploader.aspx?type={0}&toKeyId={1}&path=FileUpload/Statement&menuId={2}",
                0, approveId, Const.StatementMenuId)));
            }
        }
        protected void gvApprove_RowCommand(object sender, GridCommandEventArgs e)
        {
            object[] keys = gvApprove.DataKeys[e.RowIndex];
            string fileId = string.Empty;
            if (keys == null)
            {
                return;
            }
            else
            {
                fileId = keys[0].ToString();
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(
                 String.Format("../../AttachFile/webuploader.aspx?type={0}&toKeyId={1}&path=FileUpload/Statement&menuId={2}",
                 -1, fileId, Const.StatementMenuId)));
        }
        protected void drpHandleType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.drpHandleType.SelectedValue == BLL.Const.Statement_Complete) //审批完成
            {
                this.drpHandleMan.Enabled = false;
                this.drpHandleMan.Required = false;
                this.drpHandleMan.Items.Clear();
            }
            else
            {
                this.drpHandleMan.Enabled = true;
                this.drpHandleMan.Required = true;
                UserService.InitUserProjectIdUnitTypeDropDownList(drpHandleMan, CurrUser.LoginProjectId, BLL.Const.ProjectUnitType_1, false);
            }
        }
        protected void rblIsAgree_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.rblIsAgree.SelectedValue == "false")
            {
                this.drpHandleType.SelectedValue = BLL.Const.Statement_ReCompile;
                this.drpHandleMan.Enabled = true;
                this.drpHandleMan.Required = true;
                Model.CQMS_StatementApprove approve = BLL.StatementApproveService.GetCompile(this.StatementId);
                if (approve != null)
                {
                    List list = new List();
                    Model.Sys_User compileMan = BLL.UserService.GetUserByUserId(approve.ApproveMan);
                    list.Add(compileMan);
                    drpHandleMan.DataValueField = "UserId";
                    drpHandleMan.DataTextField = "UserName";
                    this.drpHandleMan.DataSource = list;
                    this.drpHandleMan.DataBind();
                }
            }
            else
            {
                this.drpHandleType.SelectedValue = BLL.Const.Statement_Complete;
                this.drpHandleMan.Enabled = false;
                this.drpHandleMan.Required = false;
                this.drpHandleMan.Items.Clear();
            }
        }
        protected void drpUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.drpUnit.SelectedValue != BLL.Const._Null)
            {
                UserService.InitUserProjectIdUnitIdDropDownList(drpHandleMan, CurrUser.LoginProjectId, this.drpUnit.SelectedValue, false);
                ConfirmFormService.InitConfirmFormList(drpConfirmFormId, this.CurrUser.LoginProjectId, this.drpUnit.SelectedValue, true);
            }
            else
            {
                drpHandleMan.Items.Clear();
            }
        }
    }
}