using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL;
using FineUIPro.Web.HSSE.Solution;
using Model;
using AspNet = System.Web.UI.WebControls;
namespace FineUIPro.Web.TestRun
{
    public partial class DriverSchemeEdit : PageBase
    {
        #region 定义变量
        /// 
        ///     方案审查主键
        /// 
        public string ConstructSolutionId
        {
            get => (string)ViewState["ConstructSolutionId"];
            set => ViewState["ConstructSolutionId"] = value;
        }
        #endregion
        public int ContactImg
        {
            get => Convert.ToInt32(ViewState["ContactImg"]);
            set => ViewState["ContactImg"] = value;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitDropDownList(); //加载下拉框
                ContactImg = 0;
                txtCompileDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                ConstructSolutionId = Request.Params["constructSolutionId"];
                rblIsAgree.Hidden = true;
                txtProjectName.Text = ProjectService.GetProjectByProjectId(CurrUser.LoginProjectId).ProjectName;
                if (!string.IsNullOrWhiteSpace(ConstructSolutionId))
                {
                    bindApprove();
                    #region 初始化表单
                    HFConstructSolutionId.Text = ConstructSolutionId;
                    var constructSolution =
                        TestRunConstructSolutionService.GetConstructSolutionByConstructSolutionId(ConstructSolutionId);
                    txtCode.Text = constructSolution.Code;
                    if (!string.IsNullOrEmpty(constructSolution.UnitId))
                    {
                        drpUnit.SelectedValue = constructSolution.UnitId;
                    }
                    if (!string.IsNullOrEmpty(constructSolution.SolutionType))
                    {
                        this.hdSolutionType.Text = constructSolution.SolutionType;
                        this.txtSolutionType.Text = TestRunConstructSolutionService.ConvertSolutionType(constructSolution.SolutionType);
                    }
                    txtSystem.Text = constructSolution.System;
                    if (constructSolution.CompileDate != null)
                    {
                        txtCompileDate.Text = string.Format("{0:yyyy-MM-dd}", constructSolution.CompileDate);
                    }
                    txtUnitWork.Text = constructSolution.UnitWorkIds;
                    if (constructSolution.Edition != null)
                    {
                        txtEdition.Text = constructSolution.Edition.ToString();
                    }
                    txtSolutionName.Text = constructSolution.SolutionName;
                    txtDocContent.Text = HttpUtility.HtmlDecode(constructSolution.Content);
                    #endregion
                    if (constructSolution.State != BLL.Const.TestRunConstructSolution_Complete)
                    {
                        TestRunConstructSolutionService.Init(drpHandleType, constructSolution.State, false);
                    }
                    if (constructSolution.State == BLL.Const.TestRunConstructSolution_Compile || constructSolution.State == BLL.Const.TestRunConstructSolution_ReCompile)
                    {
                        this.rblIsAgree.Hidden = true;
                        UserService.Init2(drpHandleMan, CurrUser.LoginProjectId, false);
                        this.drpHandleMan.SelectedIndex = 1;
                    }
                    else
                    {
                        UserService.Init2(drpHandleMan, CurrUser.LoginProjectId, false);
                        //Funs.Bind(drpHandleMan, UserService.GetMainUserList(this.CurrUser.LoginProjectId));
                        this.rblIsAgree.Hidden = false;
                    }
                    if (constructSolution.State == Const.TestRunConstructSolution_ReCompile)
                    {
                        options.Hidden = true;
                        optio.Hidden = true;
                    }
                    if (constructSolution.State == Const.TestRunConstructSolution_Audit || constructSolution.State == Const.TestRunConstructSolution_Audit2)
                    {
                        txtProjectName.Enabled = false;
                        txtCode.Enabled = false;
                        drpUnit.Enabled = false;
                        //txtSolutionType.Enabled = false;
                        txtSystem.Enabled = false;
                        txtCompileDate.Enabled = false;
                        txtSolutionName.Enabled = false;
                        txtUnitWork.Enabled = false;
                        ContactImg = -2;
                        btnSave.Hidden = true;
                        txtDocContent.Readonly = true;
                        if (constructSolution.State == Const.TestRunConstructSolution_Audit2)
                        {
                            this.drpHandleMan.Items.Clear();
                            this.drpHandleMan.Enabled = false;
                        }
                    }
                    ////提交版本人多次修改
                    //if (constructSolution.CompileMan.Equals(CurrUser.UserId))
                    //{
                    //    txtProjectName.Enabled = true;
                    //    txtCode.Enabled = true;
                    //    drpUnit.Enabled = true;
                    //    drpModelType.Enabled = true;
                    //    drpSpecialType.Enabled = true;
                    //    txtCompileDate.Enabled = true;
                    //    txtSolutionName.Enabled = true;
                    //    txtCNProfessional.Enabled = true;
                    //    txtUnitWork.Enabled = true;
                    //    ContactImg = 0;
                    //    Panel2.Enabled = true;
                    //    rblIsAgree.Hidden = true;
                    //    rblIsAgree.Required = false;
                    //    options.Hidden = true;
                    //    txtOptions.Required = false;
                    //    optio.Hidden = true;
                    //}
                    //    if (drpModelType.SelectedItem.Text == "施工组织设计") drpSpecialType.Enabled = false;
                }
                else
                {
                    txtEdition.Text = "0";
                    options.Hidden = true;
                    optio.Hidden = true;
                    plApprove2.Hidden = true;
                    //txtCode.Text = SQLHelper.RunProcNewId2("SpGetNewCode3ByProjectId",
                    //    "dbo.Solution_TestRunConstructSolution", "Code", CurrUser.LoginProjectId);
                    this.txtSolutionType.Text = TestRunConstructSolutionService.ConvertSolutionType(Request.Params["SolutionType"]);
                    this.hdSolutionType.Text = Request.Params["SolutionType"];
                    TestRunConstructSolutionService.Init(drpHandleType, Const.TestRunConstructSolution_Compile, false);
                    UserService.Init2(drpHandleMan, CurrUser.LoginProjectId, false);
                }
                if (this.hdSolutionType.Text == "12")
                {
                    this.txtUnitWork.Hidden = true;
                    this.txtSystem.Hidden = true;
                }
                AddAttachTab();
            }
        }
        /// 
        ///     向tab增加
        /// 
        private void AddAttachTab()
        {
            if (string.IsNullOrEmpty(HFConstructSolutionId.Text)) //新增记录
                HFConstructSolutionId.Text = SQLHelper.GetNewID(typeof(Solution_TestRunConstructSolution));
            PageContext.RegisterStartupScript(TabStrip1.GetAddTabReference("dynamic_tab2",
                string.Format(
                    "../AttachFile/webuploader.aspx?type={0}&toKeyId={1}&path=FileUpload/Solution&menuId={2}",
                    ContactImg, HFConstructSolutionId.Text, Const.TestRunConstructSolutionMenuId), "附件",
                IconHelper.GetIconUrl(Icon.Attach), false));
            // PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../AttachFile/webuploader.aspx?toKeyId={0}&path=FileUpload/DocManage&menuId={1}", hdDocId.Text, Const.DocManageMenuId)));
        }
        private void InitDropDownList()
        {
            //SolutionTempleteTypeService.InitSolutionTempleteDropDownList(drpModelType, false);
            UnitService.GetUnit(drpUnit, CurrUser.LoginProjectId,
                 false);
        }
        /// 
        ///     审批列表
        /// 
        private void bindApprove()
        {
            var list = TestRunConstructSolutionApproveService.getListData(ConstructSolutionId);
            gvApprove.DataSource = list;
            gvApprove.DataBind();
        }
        public string man(object man)
        {
            var appman = string.Empty;
            if (UserService.GetUserByUserId(man.ToString()) != null)
                appman = UserService.GetUserByUserId(man.ToString()).UserName;
            return appman;
        }
        private void Save(string saveType)
        {
            var constructSolution = new Solution_TestRunConstructSolution();
            if (!string.IsNullOrEmpty(ConstructSolutionId))
            {
                constructSolution =
                    TestRunConstructSolutionService.GetConstructSolutionByConstructSolutionId(ConstructSolutionId);
            }
            constructSolution.Code = txtCode.Text.Trim();
            constructSolution.ProjectId = CurrUser.LoginProjectId;
            if (drpUnit.SelectedValue != "0")
            {
                constructSolution.UnitId = drpUnit.SelectedValue;
            }
            constructSolution.SolutionType = this.hdSolutionType.Text.Trim();
            constructSolution.System = txtSystem.Text.Trim();
            constructSolution.SolutionName = txtSolutionName.Text.Trim();
            if (!string.IsNullOrEmpty(txtCompileDate.Text.Trim()))
            {
                constructSolution.CompileDate = Convert.ToDateTime(txtCompileDate.Text.Trim());
            }
            constructSolution.UnitWorkIds = txtUnitWork.Text.Trim();
            constructSolution.Edition = Convert.ToInt32(txtEdition.Text);
            if (constructSolution.State == BLL.Const.TestRunConstructSolution_ReCompile)
            {
                constructSolution.Edition++;
            }
            constructSolution.ConstructSolutionId = HFConstructSolutionId.Text;
            constructSolution.Content = txtDocContent.Text.Trim();
            if (saveType == "submit")
            {
                constructSolution.State = drpHandleType.SelectedValue.Trim();
            }
            else
            {
                var constructSolution1 =
                        TestRunConstructSolutionService.GetConstructSolutionByConstructSolutionId(ConstructSolutionId);
                if (constructSolution1 != null)
                {
                    if (string.IsNullOrEmpty(constructSolution1.State))
                    {
                        constructSolution.State = BLL.Const.TestRunConstructSolution_Compile;
                    }
                    else
                    {
                        constructSolution.State = constructSolution1.State;
                    }
                }
                else
                {
                    constructSolution.State = BLL.Const.TestRunConstructSolution_Compile;
                }
            }
            if (!string.IsNullOrEmpty(ConstructSolutionId))
            {
                Model.Solution_TestRunConstructSolutionApprove approve1 = BLL.TestRunConstructSolutionApproveService.GetThisApproveByConstructSolutionId(ConstructSolutionId);
                if (approve1 != null && saveType == "submit")
                {
                    approve1.ApproveDate = DateTime.Now;
                    approve1.ApproveIdea = txtOptions.Text.Trim();
                    approve1.IsAgree = Convert.ToBoolean(this.rblIsAgree.SelectedValue);
                    BLL.TestRunConstructSolutionApproveService.UpdateConstructSolutionApprove(approve1);
                }
                if (saveType == "submit")
                {
                    if (this.drpHandleMan.Enabled == true)
                    {
                        Model.Solution_TestRunConstructSolutionApprove approve = new Model.Solution_TestRunConstructSolutionApprove();
                        approve.ConstructSolutionId = ConstructSolutionId;
                        if (this.drpHandleMan.SelectedValue != "0")
                        {
                            approve.ApproveMan = this.drpHandleMan.SelectedValue;
                        }
                        approve.ApproveType = this.drpHandleType.SelectedValue;
                        if (this.drpHandleType.SelectedValue == BLL.Const.TestRunConstructSolution_Complete)
                        {
                            approve.ApproveDate = DateTime.Now.AddMinutes(1);
                        }
                        approve.Edition = Convert.ToInt32(txtEdition.Text);
                        BLL.TestRunConstructSolutionApproveService.AddConstructSolutionApprove(approve);
                    }
                }
                TestRunConstructSolutionService.UpdateConstructSolution(constructSolution);
                LogService.AddSys_Log(CurrUser, constructSolution.Code, ConstructSolutionId,
                    Const.TestRunConstructSolutionMenuId, "修改施工方案");
            }
            else
            {
                constructSolution.CompileMan = CurrUser.UserId;
                TestRunConstructSolutionService.AddConstructSolution(constructSolution);
                if (saveType == "submit")
                {
                    Model.Solution_TestRunConstructSolutionApprove approve1 = new Model.Solution_TestRunConstructSolutionApprove();
                    approve1.ConstructSolutionId = constructSolution.ConstructSolutionId;
                    approve1.ApproveDate = DateTime.Now;
                    approve1.ApproveMan = this.CurrUser.UserId;
                    approve1.ApproveType = BLL.Const.TestRunConstructSolution_Compile;
                    BLL.TestRunConstructSolutionApproveService.AddConstructSolutionApprove(approve1);
                    Model.Solution_TestRunConstructSolutionApprove approve = new Model.Solution_TestRunConstructSolutionApprove();
                    approve.ConstructSolutionId = constructSolution.ConstructSolutionId;
                    if (this.drpHandleMan.SelectedValue != "0")
                    {
                        approve.ApproveMan = this.drpHandleMan.SelectedValue;
                    }
                    approve.ApproveType = this.drpHandleType.SelectedValue;
                    BLL.TestRunConstructSolutionApproveService.AddConstructSolutionApprove(approve);
                }
                else
                {
                    Model.Solution_TestRunConstructSolutionApprove approve1 = new Model.Solution_TestRunConstructSolutionApprove();
                    approve1.ConstructSolutionId = constructSolution.ConstructSolutionId;
                    approve1.ApproveMan = this.CurrUser.UserId;
                    approve1.ApproveType = BLL.Const.TestRunConstructSolution_Compile;
                    BLL.TestRunConstructSolutionApproveService.AddConstructSolutionApprove(approve1);
                }
                LogService.AddSys_Log(CurrUser, constructSolution.Code, ConstructSolutionId,
                    Const.TestRunConstructSolutionMenuId, "添加施工方案");
            }
        }
        /// 
        /// 判断是否全部同意
        /// 
        private void CheckIsAllAgree()
        {
            var edtion = Convert.ToInt32(txtEdition.Text);
            var constructSolution =
                TestRunConstructSolutionService.GetConstructSolutionByConstructSolutionId(ConstructSolutionId);
            var allApproves = TestRunConstructSolutionApproveService.GetHandleConstructSolutionApprovesByConstructSolutionId(ConstructSolutionId,
                    constructSolution.Edition == null ? 0 : Convert.ToInt32(constructSolution.Edition));
            var needApporveCount = allApproves.Where(x => x.ApproveDate == null).Count();
            if (needApporveCount > 0) return; //判断是否最后一个审批
            var count = allApproves.Where(p =>
                p.ApproveDate != null && p.IsAgree != null && Convert.ToBoolean(p.IsAgree)).Count(); //查询会签同意的
            var fcount = allApproves.Where(p =>
                p.ApproveDate != null && p.IsAgree != null && !Convert.ToBoolean(p.IsAgree)).Count(); //查询会签不同意的
            if (count == allApproves.Count)//全部同意
            {
                var cons =
                    TestRunConstructSolutionService.GetConstructSolutionByConstructSolutionId(
                        ConstructSolutionId);
                cons.State = Const.TestRunConstructSolution_Complete;
                cons.CompileDate = DateTime.Now;
                TestRunConstructSolutionService.UpdateConstructSolution(cons);
            }
            //有不同意意见,打回重新编制
            if (fcount > 0)
            {
                var cons =
                    TestRunConstructSolutionService.GetConstructSolutionByConstructSolutionId(
                        ConstructSolutionId);
                var reApprove = new Solution_TestRunConstructSolutionApprove();
                reApprove.ConstructSolutionId = constructSolution.ConstructSolutionId;
                reApprove.ApproveMan = cons.CompileMan;
                reApprove.ApproveType = Const.TestRunConstructSolution_ReCompile;
                edtion++;
                reApprove.Edition = edtion;
                TestRunConstructSolutionApproveService.AddConstructSolutionApprove(reApprove);
                cons.State = Const.TestRunConstructSolution_ReCompile;
                cons.CompileDate = DateTime.Now;
                cons.Edition = edtion;
                constructSolution.State = Const.TestRunConstructSolution_ReCompile;
                TestRunConstructSolutionService.UpdateConstructSolution(cons);
                LogService.AddSys_Log(CurrUser, constructSolution.Code, ConstructSolutionId,
                    Const.TestRunConstructSolutionMenuId, "修改施工方案");
            }
        }
        /// 
        ///     保存验证
        /// 
        /// 
        /// 
        public void validate(string buttonName, string tip)
        {
            if (CommonService.GetAllButtonPowerList(CurrUser.LoginProjectId, CurrUser.UserId,
                    Const.TestRunConstructSolutionMenuId, buttonName))
            {
                var err = string.Empty;
                if (!AttachFileService.Getfile(HFConstructSolutionId.Text, Const.TestRunConstructSolutionMenuId))
                    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(ConstructSolutionId))
                {
                    //更新时操作
                    if (tip == "save")
                        EditConstructSol("save");
                    else
                        EditConstructSol("submit");
                }
                else
                {
                    if (tip == "save")
                        SaveTestRunConstructSolution("save");
                    else
                        SaveTestRunConstructSolution("submit");
                    //添加时操作           
                }
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
                if (tip == "save")
                {
                    tip = "保存成功!";
                }
                else
                {
                    tip = "提交成功!";
                    TestRunConstructSolutionApproveService.PushMessageToNextMan(ConstructSolutionId);
                }
                Alert.ShowInTop(tip, MessageBoxIcon.Success);
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
            }
        }
        #region 添加时候的保存
        /// 
        ///     保存方案审查
        /// 
        /// 保存类型
        private void SaveTestRunConstructSolution(string saveType)
        {
            var constructSolution = new Solution_TestRunConstructSolution();
            constructSolution.Code = txtCode.Text.Trim();
            constructSolution.ProjectId = CurrUser.LoginProjectId;
            if (drpUnit.SelectedValue != "0")
            {
                constructSolution.UnitId = drpUnit.SelectedValue;
            }
            constructSolution.SolutionType = this.hdSolutionType.Text.Trim();
            constructSolution.System = txtSystem.Text.Trim();
            constructSolution.SolutionName = txtSolutionName.Text.Trim();
            if (!string.IsNullOrEmpty(txtCompileDate.Text.Trim()))
            {
                constructSolution.CompileDate = Convert.ToDateTime(txtCompileDate.Text.Trim());
            }
            constructSolution.UnitWorkIds = txtUnitWork.Text.Trim();
            if (saveType == "submit")
            {
                constructSolution.State = drpHandleType.SelectedValue.Trim();
            }
            else
            {
                var constructSolution1 =
                        TestRunConstructSolutionService.GetConstructSolutionByConstructSolutionId(ConstructSolutionId);
                if (constructSolution1 != null)
                {
                    if (string.IsNullOrEmpty(constructSolution1.State))
                    {
                        constructSolution.State = BLL.Const.TestRunConstructSolution_Compile;
                    }
                    else
                    {
                        constructSolution.State = constructSolution1.State;
                    }
                }
                else
                {
                    constructSolution.State = BLL.Const.TestRunConstructSolution_Compile;
                }
            }
            constructSolution.CompileMan = CurrUser.UserId;
            constructSolution.Edition = Convert.ToInt32(txtEdition.Text);
            constructSolution.ConstructSolutionId = HFConstructSolutionId.Text;
            constructSolution.Content = txtDocContent.Text.Trim();
            if (!string.IsNullOrEmpty(ConstructSolutionId))
            {
                Model.Solution_TestRunConstructSolutionApprove approve1 = BLL.TestRunConstructSolutionApproveService.GetThisApproveByConstructSolutionId(ConstructSolutionId);
                if (approve1 != null && saveType == "submit")
                {
                    approve1.ApproveDate = DateTime.Now;
                    approve1.ApproveIdea = txtOptions.Text.Trim();
                    approve1.IsAgree = Convert.ToBoolean(this.rblIsAgree.SelectedValue);
                    BLL.TestRunConstructSolutionApproveService.UpdateConstructSolutionApprove(approve1);
                }
                if (saveType == "submit")
                {
                    Model.Solution_TestRunConstructSolutionApprove approve = new Model.Solution_TestRunConstructSolutionApprove();
                    approve.ConstructSolutionId = ConstructSolutionId;
                    if (this.drpHandleMan.SelectedValue != "0")
                    {
                        approve.ApproveMan = this.drpHandleMan.SelectedValue;
                    }
                    approve.ApproveType = this.drpHandleType.SelectedValue;
                    if (this.drpHandleType.SelectedValue == BLL.Const.TestRunConstructSolution_Complete)
                    {
                        approve.ApproveDate = DateTime.Now.AddMinutes(1);
                    }
                    approve.Edition = Convert.ToInt32(txtEdition.Text);
                    BLL.TestRunConstructSolutionApproveService.AddConstructSolutionApprove(approve);
                }
                TestRunConstructSolutionService.UpdateConstructSolution(constructSolution);
            }
            else
            {
                TestRunConstructSolutionService.AddConstructSolution(constructSolution);
                if (saveType == "submit")
                {
                    Model.Solution_TestRunConstructSolutionApprove approve1 = new Model.Solution_TestRunConstructSolutionApprove();
                    approve1.ConstructSolutionId = constructSolution.ConstructSolutionId;
                    approve1.ApproveDate = DateTime.Now;
                    approve1.ApproveMan = this.CurrUser.UserId;
                    approve1.ApproveType = BLL.Const.TestRunConstructSolution_Compile;
                    BLL.TestRunConstructSolutionApproveService.AddConstructSolutionApprove(approve1);
                    Model.Solution_TestRunConstructSolutionApprove approve = new Model.Solution_TestRunConstructSolutionApprove();
                    approve.ConstructSolutionId = constructSolution.ConstructSolutionId;
                    if (this.drpHandleMan.SelectedValue != "0")
                    {
                        approve.ApproveMan = this.drpHandleMan.SelectedValue;
                    }
                    approve.ApproveType = this.drpHandleType.SelectedValue;
                    BLL.TestRunConstructSolutionApproveService.AddConstructSolutionApprove(approve);
                }
                else
                {
                    Model.Solution_TestRunConstructSolutionApprove approve1 = new Model.Solution_TestRunConstructSolutionApprove();
                    approve1.ConstructSolutionId = constructSolution.ConstructSolutionId;
                    approve1.ApproveMan = this.CurrUser.UserId;
                    approve1.ApproveType = BLL.Const.TestRunConstructSolution_Compile;
                    BLL.TestRunConstructSolutionApproveService.AddConstructSolutionApprove(approve1);
                }
            }
            //if (saveType == "submit")
            //{
            //    var approve1 = new Solution_TestRunConstructSolutionApprove();
            //    approve1.ConstructSolutionId = constructSolution.ConstructSolutionId;
            //    approve1.ApproveDate = DateTime.Now;
            //    approve1.ApproveMan = CurrUser.UserId;
            //    approve1.ApproveType = Const.TestRunConstructSolution_Compile;
            //    approve1.Edition = Convert.ToInt32(txtEdition.Text);
            //    TestRunConstructSolutionApproveService.AddConstructSolutionApprove(approve1);
            //}
            LogService.AddSys_Log(CurrUser, constructSolution.Code, ConstructSolutionId,
                Const.TestRunConstructSolutionMenuId, "添加施工方案");
            LogService.AddSys_Log(CurrUser, constructSolution.Code, ConstructSolutionId,
                Const.TestRunConstructSolutionMenuId, "编制方案审查");
        }
        #endregion
        /// 
        ///     删除未选择的代办记录
        /// 
        /// 
        private void delSolutionApprove(string constructSolutionId, string man, string signType)
        {
            var count = TestRunConstructSolutionApproveService.getListSolutionApproveCount(constructSolutionId, man,
                signType);
            if (count > 0) TestRunConstructSolutionApproveService.delSolutionApprove(constructSolutionId, man);
        }
        //protected void imgBtnFile_Click(object sender, EventArgs e)
        //{
        //    if (string.IsNullOrEmpty(HFConstructSolutionId.Text)) //新增记录
        //        HFConstructSolutionId.Text = SQLHelper.GetNewID(typeof(Solution_TestRunConstructSolution));
        //    PageContext.RegisterStartupScript(WindowAtt.GetShowReference(
        //        string.Format(
        //            "../AttachFile/webuploader.aspx?type={0}&toKeyId={1}&path=FileUpload/Solution&menuId={2}",
        //            ContactImg, HFConstructSolutionId.Text, Const.TestRunConstructSolutionMenuId)));
        //}
        protected void btnapprove_Click(object sender, EventArgs e)
        {
            //HFConstructSolutionId.Text
            var approve =
                TestRunConstructSolutionApproveService.GetConstructSolutionApproveByApproveMan(HFConstructSolutionId.Text,
                    CurrUser.UserId);
            if (approve != null)
            {
                var approveId = approve.ConstructSolutionApproveId;
                PageContext.RegisterStartupScript(WindowAtt.GetShowReference(
                    string.Format(
                        "../AttachFile/webuploader.aspx?type={0}&toKeyId={1}&path=FileUpload/Solution&menuId={2}",
                        0, approveId, Const.TestRunConstructSolutionMenuId)));
            }
        }
        protected void gvApprove_RowCommand(object sender, GridCommandEventArgs e)
        {
            var keys = gvApprove.DataKeys[e.RowIndex];
            var fileId = string.Empty;
            if (keys == null)
                return;
            fileId = keys[0].ToString();
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(
                string.Format(
                    "../AttachFile/webuploader.aspx?type={0}&toKeyId={1}&path=FileUpload/Solution&menuId={2}",
                    -1, fileId, Const.TestRunConstructSolutionMenuId)));
        }
        #region 保存/提交
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //validate(Const.BtnSave, "save");
            if (CommonService.GetAllButtonPowerList(CurrUser.LoginProjectId, CurrUser.UserId,
                    Const.TestRunConstructSolutionMenuId, Const.BtnSave))
            {
                // SaveTestRunConstructSolution("save");
                Save("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");
            //if (!CommonService.GetAllButtonPowerList(CurrUser.LoginProjectId, CurrUser.UserId,
            //        Const.TestRunConstructSolutionMenuId, Const.BtnSubmit))
            //{
            //    Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
            //    return;
            //}
            var err = string.Empty;
            if (!AttachFileService.Getfile(HFConstructSolutionId.Text, Const.TestRunConstructSolutionMenuId))
                err += "请上传附件,";
            if (!string.IsNullOrWhiteSpace(err))
            {
                err = err.Substring(0, err.LastIndexOf(","));
                err += "!";
            }
            if (!string.IsNullOrWhiteSpace(err))
            {
                Alert.ShowInTop(err, MessageBoxIcon.Warning);
                return;
            }
            Save("submit");
            //TestRunConstructSolutionApproveService.PushMessageToNextMan(ConstructSolutionId);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            Alert.ShowInTop("提交成功!", MessageBoxIcon.Success);
        }
        /// 
        ///     编辑时候保存
        /// 
        private void EditConstructSol(string saveType)
        {
            var constructSolution = new Solution_TestRunConstructSolution();
            constructSolution.Code = txtCode.Text.Trim();
            constructSolution.ProjectId = CurrUser.LoginProjectId;
            if (drpUnit.SelectedValue != "0")
            {
                constructSolution.UnitId = drpUnit.SelectedValue;
            }
            constructSolution.SolutionType = this.hdSolutionType.Text.Trim();
            constructSolution.System = txtSystem.Text.Trim();
            constructSolution.SolutionName = txtSolutionName.Text.Trim();
            var edtion = Convert.ToInt32(txtEdition.Text);
            constructSolution.Edition = edtion;
            if (!string.IsNullOrEmpty(txtCompileDate.Text.Trim()))
            {
                constructSolution.CompileDate = Convert.ToDateTime(txtCompileDate.Text.Trim());
            }
            constructSolution.UnitWorkIds = txtUnitWork.Text.Trim();
            if (saveType == "submit")
            {
                constructSolution.State = drpHandleType.SelectedValue.Trim();
            }
            else
            {
                var constructSolution1 =
                        TestRunConstructSolutionService.GetConstructSolutionByConstructSolutionId(ConstructSolutionId);
                if (constructSolution1 != null)
                {
                    if (string.IsNullOrEmpty(constructSolution1.State))
                    {
                        constructSolution.State = BLL.Const.TestRunConstructSolution_Compile;
                    }
                    else
                    {
                        constructSolution.State = constructSolution1.State;
                    }
                }
                else
                {
                    constructSolution.State = BLL.Const.TestRunConstructSolution_Compile;
                }
            }
            if (!string.IsNullOrEmpty(ConstructSolutionId))
            {
                constructSolution.ConstructSolutionId = ConstructSolutionId;
                Model.Solution_TestRunConstructSolutionApprove approve1 = BLL.TestRunConstructSolutionApproveService.GetThisApproveByConstructSolutionId(ConstructSolutionId);
                if (approve1 != null && saveType == "submit")
                {
                    approve1.ApproveDate = DateTime.Now;
                    approve1.ApproveIdea = txtOptions.Text.Trim();
                    approve1.IsAgree = Convert.ToBoolean(this.rblIsAgree.SelectedValue);
                    BLL.TestRunConstructSolutionApproveService.UpdateConstructSolutionApprove(approve1);
                }
                if (saveType == "submit")
                {
                    Model.Solution_TestRunConstructSolutionApprove approve = new Model.Solution_TestRunConstructSolutionApprove();
                    approve.ConstructSolutionId = ConstructSolutionId;
                    if (this.drpHandleMan.SelectedValue != "0")
                    {
                        approve.ApproveMan = this.drpHandleMan.SelectedValue;
                    }
                    approve.ApproveType = this.drpHandleType.SelectedValue;
                    if (this.drpHandleType.SelectedValue == BLL.Const.TestRunConstructSolution_Complete)
                    {
                        approve.ApproveDate = DateTime.Now.AddMinutes(1);
                    }
                    approve.Edition = Convert.ToInt32(txtEdition.Text);
                    BLL.TestRunConstructSolutionApproveService.AddConstructSolutionApprove(approve);
                }
                TestRunConstructSolutionService.UpdateConstructSolution(constructSolution);
                //var constructSolution1 =
                //    TestRunConstructSolutionService.GetConstructSolutionByConstructSolutionId(ConstructSolutionId);
                //if (saveType == "submit")
                //{
                //    if (CurrUser.UserId != constructSolution1.CompileMan) //办理人不是编制人,提示查看审批信息
                //    {
                //        if (constructSolution1.State == Const.TestRunConstructSolution_Audit)
                //        {
                //            constructSolution.State = constructSolution1.State;
                //        }
                //        else
                //        {
                //            constructSolution.State = Const.TestRunConstructSolution_Audit;
                //        }
                //    }
                //    else
                //    {
                //        constructSolution.State = constructSolution1.State;
                //    }
                //    if (!CurrUser.UserId.Equals(constructSolution1.CompileMan))
                //    {
                //        var approve = TestRunConstructSolutionApproveService.GetConstructSoluAppByApproveMan(
                //            ConstructSolutionId, CurrUser.UserId, Convert.ToInt32(constructSolution1.Edition));
                //        if (saveType == "submit")
                //        {
                //            approve.ApproveDate = DateTime.Now;
                //        }
                //        approve.Edition = Convert.ToInt32(edtion);
                //        approve.IsAgree = Convert.ToBoolean(rblIsAgree.SelectedValue);
                //        approve.ApproveIdea = txtOptions.Text.Trim();
                //        TestRunConstructSolutionApproveService.UpdateConstructSolutionApprove(approve);
                //    }
                //    else
                //    {
                //        if (saveType == "submit")
                //        {
                //            if (constructSolution1.State == Const.TestRunConstructSolution_Audit) //==会签状态升级版本
                //            {
                //                var reApprove = new Solution_TestRunConstructSolutionApprove();
                //                reApprove.ConstructSolutionId = constructSolution.ConstructSolutionId;
                //                reApprove.ApproveDate = DateTime.Now;
                //                reApprove.ApproveMan = constructSolution1.CompileMan;
                //                reApprove.ApproveType = Const.TestRunConstructSolution_ReCompile;
                //                edtion++;
                //                reApprove.Edition = edtion;
                //                TestRunConstructSolutionApproveService.AddConstructSolutionApprove(reApprove);
                //            }
                //            else
                //            {
                //                var approves =
                //                    TestRunConstructSolutionApproveService.GetConstructSolApproveByApproveMan(
                //                        ConstructSolutionId, constructSolution1.CompileMan);
                //                approves.ApproveDate = DateTime.Now;
                //                TestRunConstructSolutionApproveService.UpdateConstructSolutionApprove(approves);
                //            }
                //        }
                //    }
                //}
                //else
                //{
                //    constructSolution.State = constructSolution1.State;
                //}
                ////提交时候,更新提交版本
                //if (CurrUser.UserId.Equals(constructSolution1.CompileMan))
                //{
                //    if (constructSolution1.State != Const.TestRunConstructSolution_Audit)
                //    {
                //        edtion++;
                //    }
                //    constructSolution.Edition = Convert.ToInt32(edtion);
                //    constructSolution.State = Const.TestRunConstructSolution_Audit;
                //}
                //TestRunConstructSolutionService.UpdateConstructSolution(constructSolution);
                ////判断状态,全部会签同意,则审批完成
                //if (saveType == "submit")
                //{
                //    var allApproves =
                //        TestRunConstructSolutionApproveService.GetHandleConstructSolutionApprovesByConstructSolutionId(
                //            ConstructSolutionId,
                //            constructSolution.Edition == null ? 0 : Convert.ToInt32(constructSolution.Edition));
                //    var count = allApproves.Where(p =>
                //        p.ApproveDate != null && p.IsAgree != null && Convert.ToBoolean(p.IsAgree)).Count(); //查询会签同意的
                //    var fcount = allApproves.Where(p =>
                //        p.ApproveDate != null && p.IsAgree != null && !Convert.ToBoolean(p.IsAgree)).Count(); //查询会签不同意的
                //    if (count + fcount == allApproves.Count)
                //    {
                //        if (count == allApproves.Count)
                //        {
                //            var cons =
                //                TestRunConstructSolutionService.GetConstructSolutionByConstructSolutionId(
                //                    ConstructSolutionId);
                //            cons.State = Const.TestRunConstructSolution_Complete;
                //            cons.CompileDate = DateTime.Now;
                //            TestRunConstructSolutionService.UpdateConstructSolution(cons);
                //        }
                //        //有不同意意见,打回重新编制
                //        if (fcount > 0)
                //        {
                //            var cons =
                //                TestRunConstructSolutionService.GetConstructSolutionByConstructSolutionId(
                //                    ConstructSolutionId);
                //            var reApprove = new Solution_TestRunConstructSolutionApprove();
                //            reApprove.ConstructSolutionId = constructSolution.ConstructSolutionId;
                //            reApprove.ApproveMan = cons.CompileMan;
                //            reApprove.ApproveType = Const.TestRunConstructSolution_ReCompile;
                //            edtion++;
                //            reApprove.Edition = edtion;
                //            TestRunConstructSolutionApproveService.AddConstructSolutionApprove(reApprove);
                //            cons.State = Const.TestRunConstructSolution_ReCompile;
                //            cons.CompileDate = DateTime.Now;
                //            constructSolution.State = Const.TestRunConstructSolution_ReCompile;
                //            TestRunConstructSolutionService.UpdateConstructSolution(cons);
                //        }
                //    }
                //}
                LogService.AddSys_Log(CurrUser, constructSolution.Code, ConstructSolutionId,
                    Const.TestRunConstructSolutionMenuId, "修改施工方案");
            }
        }
        #endregion
        protected void rblIsAgree_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.drpHandleMan.Enabled = true;
            var constructSolution =
                     TestRunConstructSolutionService.GetConstructSolutionByConstructSolutionId(ConstructSolutionId);
            if (this.rblIsAgree.SelectedValue == "true")
            {
                this.drpHandleType.SelectedIndex = 0;
                UserService.Init2(drpHandleMan, CurrUser.LoginProjectId, false);
                this.drpHandleMan.SelectedIndex = 0;
                if (constructSolution.State == Const.TestRunConstructSolution_Audit2)
                {
                    this.drpHandleMan.Items.Clear();
                    this.drpHandleMan.Enabled = false;
                }
            }
            else
            {
                this.drpHandleType.SelectedIndex = 1;
                var approve = (from x in Funs.DB.Solution_TestRunConstructSolutionApprove where x.ConstructSolutionId == ConstructSolutionId && x.ApproveType == BLL.Const.TestRunConstructSolution_Compile select x).FirstOrDefault();
                if (approve != null)
                {
                    ListItem[] list = new ListItem[1];
                    list[0] = new ListItem(BLL.UserService.GetUserByUserId(approve.ApproveMan).UserName ?? "", approve.ApproveMan);
                    drpHandleMan.DataValueField = "Value";
                    drpHandleMan.DataTextField = "Text";
                    drpHandleMan.DataSource = list;
                    drpHandleMan.DataBind();
                    this.drpHandleMan.SelectedIndex = 0;
                }
            }
        }
        protected void drpHandleType_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
    }
}