using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
namespace FineUIPro.Web.CQMS.Information
{
    public partial class ContactSheet : PageBase
    {
        /// 
        /// 项目id
        /// 
        public string ProjectId
        {
            get
            {
                return (string)ViewState["ProjectId"];
            }
            set
            {
                ViewState["ProjectId"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UnitService.InitUnitByProjectIdUnitTypeDropDownList1(drpUnit, this.CurrUser.LoginProjectId, true);//施工单位
                DesignProfessionalService.InitDesignProfessionalDownList(drpCNProfessional, true);//专业
                btnNew.OnClientClick = Window1.GetShowReference("ContactSheetEdit.aspx") + "return false;";
                BindGrid();
                ProjectId = CurrUser.LoginProjectId;
                GetButtonPower();
            }
        }
        private void BindGrid()
        {
            string strSql = @"SELECT a.ContactSheetId ,a.ProjectCode ,a.ProjectId ,a.DocCode ,a.CustomerCode  ,a.InstallationCode ,a.CompanyCode ,a.DeptmentCode ,a.CreatMan
                          , a.CreatDate, a.SignDate, a.DraftReasonCode, a.PlanCompletDate, a.RealCompletDate, a.ContactSheetCode, a.ProfessionCode,a. Contents
                          , a.PriceType, a.ArtificialInput, a.MaterialInput, a.ToolInput,a. States,a. SgDocIds, a.PicUrls,a. OKPicUrls, a.DocUrl
                          , a.CompanySeq, a.DName, a.CIType, a.IsPo,a. UnitPrice,a. Counts,a. ShipIds, a.WorkStates,a. Wgs84,a. AppDocContent
                          , a.AppDocUser, a.AppDocDate, a.AuditUser, a.AuditArtificialInput, a.AuditMaterialInput, a.AuditToolInput
                          , a.AuditUnitPrice,a. AuditCounts,a. LikeRate, a.LikeContactSheetId,b.ContactSheetCode as LikeContactSheetCode,a. UntiCode, a.NeedDesign,a. CdId, a.InstId
                          , a.AppState, a.OwnerCode, a.CreatManName,a. Receiver, a.ConstructionID,a. TEMPLATE_SEQ_ID, a.PdfUrl
                      FROM dbo.CQMS_Information_ContactSheet  a left join dbo.CQMS_Information_ContactSheet b on a.LikeContactSheetId=b.ContactSheetId";
            List listStr = new List();
            strSql += " where a.ProjectId = @ProjectId and a.CIType = 'C' ";
            listStr.Add(new SqlParameter("@ProjectId", this.CurrUser.LoginProjectId));
            if (drpUnit.SelectedValue != BLL.Const._Null)
            {
                strSql += " AND  CHARINDEX(@UnitCode,a.CompanyCode)>0";
                listStr.Add(new SqlParameter("@UnitCode", this.drpUnit.SelectedValue));
            }
            if (drpCNProfessional.SelectedValue != BLL.Const._Null)
            {
                strSql += " AND a.ProfessionCode = @ProfessionCode";
                listStr.Add(new SqlParameter("@ProfessionCode", this.drpCNProfessional.SelectedValue));
            }
            if (!string.IsNullOrEmpty(txtStartTime.Text.Trim()))
            {
                strSql += " AND a.CreatDate >= @DesignDate";
                listStr.Add(new SqlParameter("@DesignDate", txtStartTime.Text.Trim() + " 00:00:00"));
            }
            if (!string.IsNullOrEmpty(txtEndTime.Text.Trim()))
            {
                strSql += " AND a.CreatDate <= @DesignDateE";
                listStr.Add(new SqlParameter("@DesignDateE", txtEndTime.Text.Trim() + " 23:59:59"));
            }
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            Grid1.RecordCount = tb.Rows.Count;
            tb = GetFilteredTable(Grid1.FilteredData, tb);
            var table = this.GetPagedDataTable(Grid1, tb);
            Grid1.DataSource = table;
            Grid1.DataBind();
        }
        //
        //获取办理人姓名
        //
        //
        //
        protected string ConvertCreateMan(object userId)
        {
            var user = BLL.UserService.GetUserByUserId(userId.ToString());
            if (user != null)
            {
                return user.UserName;
            }
            else
            {
                return "";
            }
        }
        //
        //获取办理人姓名
        //
        //
        //
        protected string ConvertMan(object designId)
        {
            if (designId != null)
            {
                Model.CQMS_Information_ContactSheetApprove a = BLL.ContactSheetApproveService.GetDesignApproveByContactSheetId(designId.ToString());
                if (a != null)
                {
                    if (a.AuditMan != null)
                    {
                        return BLL.UserService.GetUserByUserId(a.AuditMan).UserName;
                    }
                }
                else
                {
                    return "";
                }
            }
            return "";
        }
        /// 
        /// 把状态转换代号为文字形式
        /// 
        /// 
        /// 
        protected string ConvertState(object state)
        {
            if (state != null)
            {
                if (state.ToString() == BLL.Const.ContactSheet_ReCompile)
                {
                    return "重新编制";
                }
                else if (state.ToString() == BLL.Const.ContactSheet_Compile)
                {
                    return "变更录入";
                }
                else if (state.ToString() == BLL.Const.ContactSheet_Audit1)
                {
                    return "分包技术负责人审核";
                }
                else if (state.ToString() == BLL.Const.ContactSheet_Audit2)
                {
                    return "专业工程师审核";
                }
                else if (state.ToString() == BLL.Const.ContactSheet_Audit3)
                {
                    return "施工经理审核";
                }
                else if (state.ToString() == BLL.Const.ContactSheet_Audit4)
                {
                    return "QA/QC经理确认";
                }
                else if (state.ToString() == BLL.Const.ContactSheet_Audit5)
                {
                    return "文件上传签字盖章";
                }
                else if (state.ToString() == BLL.Const.ContactSheet_Audit6)
                {
                    return "费控经理";
                }
                else if (state.ToString() == BLL.Const.ContactSheet_Audit7)
                {
                    return "设计审批";
                }
                else if (state.ToString() == BLL.Const.ContactSheet_AuditCompleted)
                {
                    return "审批完成";
                }
                else
                {
                    return "";
                }
            }
            else
            {
                return "";
            }
        }
        /// 
        /// 获取单位名称
        /// 
        /// 
        /// 
        protected string ConvertCarryUnit(object CarryUnitIds)
        {
            string CarryUnitName = string.Empty;
            if (CarryUnitIds != null)
            {
                string[] Ids = CarryUnitIds.ToString().Split(',');
                foreach (string t in Ids)
                {
                    var type = BLL.UnitService.GetUnitByUnitId(t);
                    if (type != null)
                    {
                        CarryUnitName += type.UnitName + ",";
                    }
                }
            }
            if (CarryUnitName != string.Empty)
            {
                return CarryUnitName.Substring(0, CarryUnitName.Length - 1);
            }
            else
            {
                return "";
            }
        }
        #region 数据操作
        protected void btnMenuModify_Click(object sender, EventArgs e)
        {
            if (BLL.CommonService.GetAllButtonPowerList(CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.DesignMenuId, BLL.Const.BtnModify))
            {
                if (Grid1.SelectedRowIndexArray.Length == 0)
                {
                    Alert.ShowInTop("请至少选择一条记录", MessageBoxIcon.Warning);
                    return;
                }
                Model.CQMS_Information_ContactSheet design = BLL.ContactSheetService.GetConfirmFormById(Grid1.SelectedRowID);
                Model.CQMS_Information_ContactSheetApprove approve = BLL.ContactSheetApproveService.GetDesignApproveByContactSheetId(Grid1.SelectedRowID);
                if (design.States == BLL.Const.ContactSheet_ReCompile || design.States == BLL.Const.ContactSheet_Compile)
                {
                    if (approve != null)
                    {
                        if (!string.IsNullOrEmpty(approve.AuditMan))
                        {
                            if (this.CurrUser.UserId == approve.AuditMan || this.CurrUser.UserId == BLL.Const.sysglyId)
                            {
                                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("ContactSheetEdit.aspx?ContactSheetId=" + Grid1.SelectedRowID, "编辑 - ")));
                            }
                            else
                            {
                                Alert.ShowInTop("您不是当前办理人,无法操作!", MessageBoxIcon.Warning);
                            }
                        }
                    }
                    else
                    {
                        Alert.ShowInTop("您不是当前办理人,无法操作!", MessageBoxIcon.Warning);
                    }
                }
                else if (design.States == BLL.Const.ContactSheet_AuditCompleted)
                {
                    PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("ContactSheetEdit.aspx?detail=true&ContactSheetId=" + Grid1.SelectedRowID, "查看 - ")));
                }
                else
                {
                    PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("ContactSheetEdit.aspx?detail=next&ContactSheetId=" + Grid1.SelectedRowID, "查看 - ")));
                }
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
            }
        }
        protected void btnMenuView_Click(object sender, EventArgs e)
        {
            PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("ContactSheetEdit.aspx?detail=true&ContactSheetId=" + Grid1.SelectedRowID, "查看 - ")));
        }
        protected void btnMenuDel_Click(object sender, EventArgs e)
        {
            if (BLL.CommonService.GetAllButtonPowerList(CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.DesignMenuId, BLL.Const.BtnDelete))
            {
                BLL.DesignApproveService.DeleteDesignApprovesByDesignId(Grid1.SelectedRowID);
                BLL.DesignService.DeleteDesign(Grid1.SelectedRowID);
                //BLL.LogService.AddLog(this.CurrUser.UserId, "删除设计变更", this.CurrUser.LoginProjectId);
                BindGrid();
                Alert.ShowInTop("删除数据成功!", MessageBoxIcon.Success);
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
            }
        }
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGrid();
        }
        protected void btnRset_Click(object sender, EventArgs e)
        {
            drpUnit.SelectedIndex = 0;
            drpCNProfessional.SelectedIndex = 0;
            txtStartTime.Text = "";
            txtEndTime.Text = "";
            BindGrid();
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }
        #endregion
        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            BindGrid();
        }
        #region 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private void GetButtonPower()
        {
            if (Request.Params["value"] == "0")
            {
                return;
            }
            var buttonList = BLL.CommonService.GetAllButtonList(this.ProjectId, this.CurrUser.UserId, BLL.Const.DesignMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnModify))
                {
                    this.btnMenuModify.Hidden = false;
                }
                if (buttonList.Contains(BLL.Const.BtnDelete))
                {
                    this.btnMenuDel.Hidden = false;
                }
            }
        }
        #endregion
        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            if (BLL.CommonService.GetAllButtonPowerList(CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.DesignMenuId, BLL.Const.BtnModify))
            {
                if (Grid1.SelectedRowIndexArray.Length == 0)
                {
                    Alert.ShowInTop("请至少选择一条记录", MessageBoxIcon.Warning);
                    return;
                }
                Model.CQMS_Information_ContactSheet design = BLL.ContactSheetService.GetConfirmFormById(Grid1.SelectedRowID);
                Model.CQMS_Information_ContactSheetApprove approve = BLL.ContactSheetApproveService.GetDesignApproveByContactSheetId(Grid1.SelectedRowID);
                if (design.States == BLL.Const.ContactSheet_ReCompile || design.States == BLL.Const.ContactSheet_Compile)
                {
                    if (approve != null)
                    {
                        if (!string.IsNullOrEmpty(approve.AuditMan))
                        {
                            if (this.CurrUser.UserId == approve.AuditMan || this.CurrUser.UserId == BLL.Const.sysglyId)
                            {
                                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("ContactSheetEdit.aspx?ContactSheetId=" + Grid1.SelectedRowID, "编辑 - ")));
                            }
                            else
                            {
                                Alert.ShowInTop("您不是当前办理人,无法操作!", MessageBoxIcon.Warning);
                            }
                        }
                    }
                    else
                    {
                        Alert.ShowInTop("您不是当前办理人,无法操作!", MessageBoxIcon.Warning);
                    }
                }
                else if (design.States == BLL.Const.ContactSheet_AuditCompleted)
                {
                    PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("ContactSheetEdit.aspx?detail=true&ContactSheetId=" + Grid1.SelectedRowID, "查看 - ")));
                }
                else  //下一步操作状态
                {
                    PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("ContactSheetEdit.aspx?detail=next&ContactSheetId=" + Grid1.SelectedRowID, "查看 - ")));
                }
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
            }
        }
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            BindGrid();
        }
    }
}