namespace FineUIPro.Web.HJGLServer.BaseInfo
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using BLL;
    public partial class Weld : PageBase
    {
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
                this.drpJOTY_Group.DataTextField = "Text";
                this.drpJOTY_Group.DataValueField = "Value";
                this.drpJOTY_Group.DataSource = BLL.DropListService.HJGL_DrpJOTY_GroupItem();
                this.drpJOTY_Group.DataBind();
                Funs.FineUIPleaseSelect(this.drpJOTY_Group);
                // 绑定表格
                BindGrid();
            }
        }
        #endregion
        #region 绑定数据
        /// 
        /// 绑定数据
        /// 
        private void BindGrid()
        {
            string strSql = @"SELECT JOTY_ID,JOTY_Code,JOTY_Name,JOTY_Remark,JOTY_Group,(CASE ISNULL(JOTY_Group,'') WHEN '1' THEN '对接焊缝' WHEN '2' THEN '角焊缝' WHEN '3' THEN '支管连接焊缝' END) AS GroupName"
                        + @" FROM HJGL_BS_JointType WHERE 1=1 ";
            List listStr = new List();
            if (!string.IsNullOrEmpty(this.txtCode.Text.Trim()))
            {
                strSql += " AND JOTY_Code LIKE @JOTY_Code";
                listStr.Add(new SqlParameter("@JOTY_Code", "%" + this.txtCode.Text.Trim() + "%"));
            }
            if (!string.IsNullOrEmpty(this.txtName.Text.Trim()))
            {
                strSql += " AND JOTY_Name LIKE @JOTY_Name";
                listStr.Add(new SqlParameter("@JOTY_Name", "%" + this.txtName.Text.Trim() + "%"));
            }
            if (!string.IsNullOrEmpty(this.CurrUser.LoginSystemId))
            {
                strSql += " AND ProjectSoft=@ProjectSoft";
                listStr.Add(new SqlParameter("@ProjectSoft", this.CurrUser.LoginSystemId));
            }
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            // 2.获取当前分页数据
            //var table = this.GetPagedDataTable(Grid1, tb1);
            Grid1.RecordCount = tb.Rows.Count;
            tb = GetFilteredTable(Grid1.FilteredData, tb);
            var table = this.GetPagedDataTable(Grid1, tb);
            Grid1.DataSource = table;
            Grid1.DataBind();
        }
        #endregion
        #region 分页排序
        /// 
        /// 
        /// 
        /// 
        /// 
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        /// 
        /// 分页下拉选择
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        protected void Grid1_FilterChange(object sender, EventArgs e)
        {
            BindGrid();
        }
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }
        #endregion
        #region 删除事件
        /// 
        /// 删除
        /// 
        /// 
        /// 
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_WeldMenuId, Const.BtnDelete))
            {
                if (judgementDelete(hfFormID.Text, true))
                {
                    BLL.HJGL_WeldService.DeleteJointType(hfFormID.Text);
                    BLL.Sys_LogService.AddLog(BLL.Const.System_2, this.CurrUser.LoginProjectId, this.CurrUser.UserId, "删除焊缝类型信息");
                    // 重新绑定表格,并模拟点击[新增按钮]
                    BindGrid();
                    PageContext.RegisterStartupScript("onNewButtonClick();");
                }
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                return;
            }
        }
        /// 
        /// 右键删除事件
        /// 
        /// 
        /// 
        protected void btnMenuDelete_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_WeldMenuId, Const.BtnDelete))
            {
                this.DeleteData();
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                return;
            }
        }
        /// 
        /// 删除方法
        /// 
        private void DeleteData()
        {
            if (Grid1.SelectedRowIndexArray.Length > 0)
            {
                bool isShow = true;
                if (Grid1.SelectedRowIndexArray.Length > 1)
                {
                    isShow = false;
                }
                foreach (int rowIndex in Grid1.SelectedRowIndexArray)
                {
                    string rowID = Grid1.DataKeys[rowIndex][0].ToString();
                    if (judgementDelete(rowID, isShow))
                    {
                        BLL.HJGL_WeldService.DeleteJointType(rowID);
                        BLL.Sys_LogService.AddLog(BLL.Const.System_2, this.CurrUser.LoginProjectId, this.CurrUser.UserId, "删除焊缝类型信息");
                        Alert.ShowInTop("删除完成!", MessageBoxIcon.Success);
                    }
                }
                BindGrid();
                PageContext.RegisterStartupScript("onNewButtonClick();");
            }
        }
        #endregion
        #region 编辑
        /// 
        /// 右键编辑事件
        /// 
        /// 
        /// 
        protected void btnMenuEdit_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_WeldMenuId, Const.BtnModify))
            {
                this.EditData();
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                return;
            }
        }
        /// 
        /// 编辑数据方法
        /// 
        private void EditData()
        {
            if (Grid1.SelectedRowIndexArray.Length == 0)
            {
                Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning);
                return;
            }
            string Id = Grid1.SelectedRowID;
            var jointType = BLL.HJGL_WeldService.GetJointTypeByID(Id);
            if (jointType != null)
            {
                this.txtJotyCode.Text = jointType.JOTY_Code;
                this.txtJotyName.Text = jointType.JOTY_Name;
                this.txtRemark.Text = jointType.JOTY_Remark;
                if (!string.IsNullOrEmpty(jointType.JOTY_Group))
                {
                    this.drpJOTY_Group.SelectedValue = jointType.JOTY_Group;
                }
                hfFormID.Text = Id;
                this.btnDelete.Enabled = true;
            }
        }
        #endregion
        #region 提交按钮
        /// 
        /// 提交按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_WeldMenuId, Const.BtnSave))
            {
                string strRowID = hfFormID.Text;
                if (BLL.HJGL_WeldService.IsExitJotyCode(this.txtJotyCode.Text.Trim(), strRowID, this.CurrUser.LoginSystemId))
                {
                    Alert.ShowInTop("此代号已存在!", MessageBoxIcon.Warning);
                    return;
                }
                Model.HJGL_BS_JointType jointType = new Model.HJGL_BS_JointType();
                jointType.JOTY_Code = this.txtJotyCode.Text.Trim();
                jointType.JOTY_Name = this.txtJotyName.Text.Trim();
                jointType.JOTY_Remark = this.txtRemark.Text.Trim();
                jointType.ProjectSoft = this.CurrUser.LoginSystemId;
                if (!string.IsNullOrEmpty(drpJOTY_Group.SelectedValue))
                {
                    jointType.JOTY_Group = drpJOTY_Group.SelectedValue;
                }
                if (string.IsNullOrEmpty(strRowID))
                {
                    BLL.HJGL_WeldService.AddJointType(jointType);
                    BLL.Sys_LogService.AddLog(BLL.Const.System_2, this.CurrUser.LoginProjectId, this.CurrUser.UserId, "添加焊缝类型信息");
                }
                else
                {
                    jointType.JOTY_ID = strRowID;
                    BLL.HJGL_WeldService.UpdateJointType(jointType);
                    BLL.Sys_LogService.AddLog(BLL.Const.System_2, this.CurrUser.LoginProjectId, this.CurrUser.UserId, "修改焊缝类型信息");
                }
                // 重新绑定表格,并点击当前编辑或者新增的行
                BindGrid();
                Alert.ShowInTop("提交成功!", MessageBoxIcon.Success);
                PageContext.RegisterStartupScript("onNewButtonClick();");
                //PageContext.RegisterStartupScript(String.Format("F('{0}').selectRow('{1}');", Grid1.ClientID, UnitQualitySort.UnitQualitySortId));
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                return;
            }
        }
        #endregion
        #region 判断是否可删除
        /// 
        /// 判断是否可以删除
        /// 
        /// 
        private bool judgementDelete(string id, bool isShow)
        {
            string content = string.Empty;
            if (string.IsNullOrEmpty(content))
            {
                return true;
            }
            else
            {
                if (isShow)
                {
                    Alert.ShowInTop(content, MessageBoxIcon.Error);
                }
                return false;
            }
        }
        #endregion
        #region 查询
        /// 
        /// 查询
        /// 
        /// 
        /// 
        protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            this.BindGrid();
        }
        #endregion
    }
}