using System;
using System.Collections.Generic;
using BLL;
using System.Data.SqlClient;
using System.Data;

namespace FineUIPro.Web.HJGL.MaterialManage
{
    public partial class UnitStore : PageBase
    {
        #region 加载页面
        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
                BindUnit(drpUnit);
                BindUnit(drpUnitId);
                // 绑定表格
                BindGrid();

            }
        }
        #endregion

        #region 单位下拉框绑定
        private void BindUnit(DropDownList dropName)
        {
            dropName.DataTextField = "UnitName";
            dropName.DataValueField = "UnitId";
            dropName.DataSource = UnitService.GetUnitByProjectIdUnitTypeList(this.CurrUser.LoginProjectId, string.Empty);
            dropName.DataBind();
            Funs.FineUIPleaseSelect(dropName);
        }
        #endregion

        #region 绑定数据
        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindGrid()
        {
            string strSql = @"SELECT s.UnitStoreId,s.UnitId,s.UnitStoreCode,s.UnitStoreName,s.StorePosition ,u.UnitName 
                              FROM dbo.Weld_UnitStore s
                              LEFT JOIN dbo.Base_Unit u ON u.UnitId = s.UnitId
                              WHERE ProjectId=@ProjectId";
            List<SqlParameter> listStr = new List<SqlParameter>();
            listStr.Add(new SqlParameter("@ProjectId", this.CurrUser.LoginProjectId));
            if (this.drpUnit.SelectedValue != BLL.Const._Null)
            {
                strSql += " AND s.UnitId = @UnitId";
                listStr.Add(new SqlParameter("@UnitId", this.drpUnit.SelectedValue));
            }

            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 分页排序
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        /// <summary>
        /// 分页下拉选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grid1_FilterChange(object sender, EventArgs e)
        {
            BindGrid();
        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }
        #endregion

        #region 删除事件
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_UnitStoreMenuId, Const.BtnDelete))
            {
                if (judgementDelete(hfFormID.Text, true))
                {
                    var store = BLL.UnitStoreService.GetUnitStoreById(hfFormID.Text);
                    BLL.UnitStoreService.DeleteUnitStoreById(hfFormID.Text);
                    BLL.LogService.AddSys_Log(this.CurrUser, store.UnitStoreName, store.UnitStoreId, BLL.Const.HJGL_UnitStoreMenuId, "删除单位仓库");
                    // 重新绑定表格,并模拟点击[新增按钮]
                    BindGrid();
                    PageContext.RegisterStartupScript("onNewButtonClick();");
                }
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                return;
            }
        }

        /// <summary>
        /// 右键删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMenuDelete_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_UnitStoreMenuId, Const.BtnDelete))
            {
                this.DeleteData();
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                return;
            }
        }

        /// <summary>
        /// 删除方法
        /// </summary>
        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))
                    {
                        var store = BLL.UnitStoreService.GetUnitStoreById(rowID);
                        BLL.UnitStoreService.DeleteUnitStoreById(rowID);
                        BLL.LogService.AddSys_Log(this.CurrUser, store.UnitStoreName, store.UnitStoreId, BLL.Const.HJGL_UnitStoreMenuId, "删除单位仓库");
                        ShowNotify("删除完成!", MessageBoxIcon.Success);
                    }
                }

                BindGrid();
                PageContext.RegisterStartupScript("onNewButtonClick();");
            }
        }
        #endregion

        #region 编辑
        /// <summary>
        /// 右键编辑事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMenuEdit_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_UnitStoreMenuId, Const.BtnModify))
            {
                this.EditData();
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                return;
            }
        }

        /// <summary>
        /// 编辑数据方法
        /// </summary>
        private void EditData()
        {
            if (Grid1.SelectedRowIndexArray.Length == 0)
            {
                Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning);
                return;
            }
            string Id = Grid1.SelectedRowID;
            var unitStore = BLL.UnitStoreService.GetUnitStoreById(Id);
            if (unitStore != null)
            {
                if (!string.IsNullOrEmpty(unitStore.UnitId))
                {
                    this.drpUnitId.SelectedValue = unitStore.UnitId;
                }
                this.txtUnitStoreCode.Text = unitStore.UnitStoreCode;
                this.txtUnitStoreName.Text = unitStore.UnitStoreName;
                txtStorePosition.Text = unitStore.StorePosition;

                hfFormID.Text = Id;
                this.btnDelete.Enabled = true;
            }
        }
        #endregion

        #region 提交按钮
        /// <summary>
        /// 提交按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_UnitStoreMenuId, Const.BtnSave))
            {
                string strRowID = hfFormID.Text;
                if (this.drpUnitId.SelectedValue == BLL.Const._Null)
                {
                    Alert.ShowInTop("所属单不能为空!", MessageBoxIcon.Warning);
                    return;
                }
                if (BLL.UnitStoreService.IsExitUnitStore(this.drpUnitId.SelectedValue, this.txtUnitStoreCode.Text.Trim(), strRowID))
                {
                    Alert.ShowInTop("此单位的仓库编码已存在!", MessageBoxIcon.Warning);
                    return;
                }

                Model.Weld_UnitStore store = new Model.Weld_UnitStore();
                store.ProjectId = this.CurrUser.LoginProjectId;
                store.UnitId = this.drpUnitId.SelectedValue;
                store.UnitStoreCode = this.txtUnitStoreCode.Text.Trim();
                store.UnitStoreName = this.txtUnitStoreName.Text.Trim();
                store.StorePosition = txtStorePosition.Text.Trim();
                if (string.IsNullOrEmpty(strRowID))
                {
                    var stores = BLL.UnitStoreService.GetUnitStoreByUnitId(drpUnitId.SelectedValue);
                    if (stores.Count < 5)
                    {
                        strRowID = SQLHelper.GetNewID(typeof(Model.Weld_UnitStore));
                        store.UnitStoreId = strRowID;
                        BLL.UnitStoreService.AddUnitStore(store);
                        BLL.LogService.AddSys_Log(this.CurrUser, store.UnitStoreName, store.UnitStoreId, BLL.Const.HJGL_UnitStoreMenuId, "添加单位仓库");
                    }
                    else
                    {
                        Alert.ShowInTop("单位仓库最多只能创建5个!", MessageBoxIcon.Warning);
                        return;
                    }
                }
                else
                {
                    store.UnitStoreId = strRowID;
                    BLL.UnitStoreService.UpdateUnitStore(store);
                    BLL.LogService.AddSys_Log(this.CurrUser, store.UnitStoreName, store.UnitStoreId, BLL.Const.HJGL_UnitStoreMenuId, "修改单位仓库");
                }

                // 重新绑定表格,并点击当前编辑或者新增的行
                BindGrid();

                ShowNotify("保存成功!", 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 判断是否可删除
        /// <summary>
        /// 判断是否可以删除
        /// </summary>
        /// <returns></returns>
        private bool judgementDelete(string id, bool isShow)
        {
            string content = string.Empty;
            //if (BLL.WeldInfoService.GetWeldInfoByWeldTypeId(id) > 0)
            //{
            //    content = "焊材信息设置中已经使用了该焊材类型,不能删除!";
            //}
            if (string.IsNullOrEmpty(content))
            {
                return true;
            }
            else
            {
                if (isShow)
                {
                    Alert.ShowInTop(content, MessageBoxIcon.Error);
                }
                return false;
            }
        }
        #endregion

        #region 查询
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            this.BindGrid();
        }
        #endregion

        #region Grid行点击事件
        /// <summary>
        /// Grid行点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grid1_RowClick(object sender, GridRowClickEventArgs e)
        {
            this.EditData();
        }
        #endregion
    }
}