using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using DeiNiu.wms.Logical;
using DeiNiu.Utils;
public partial class RoleMain : PageBase
{
    private LRole _logic;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            _logic = new LRole();
            Databound();
        }
        SessionSeting();
    }
    /// 
    /// bound the query list.
    /// 
    private void Databound()
    { 
        GridView1.DataSource = _logic.GetAllActiveData().Tables[0];
        GridView1.DataBind();
        UpdateSession();
        
        ClearDetail();
    }
    private void SessionSeting()
    {
        if (IsPostBack)
        {
            _logic = (LRole)Session[appScope.PagelevelObj];
        }
    }
    private void UpdateSession()
    {
        Session[appScope.PagelevelObj] = _logic;
    }
    /// 
    /// set data from shift object to page
    /// 
    private void DetailDataBind()
    {
        DataDetail.Visible = true;
        chkApprove.Checked=_logic.GetRole.role_4Approve ;
        txtJobName.Text = _logic.GetRole.role_name;
        txtJobDesc.Text = _logic.GetRole.role_desc;
//    chkStatus.Checked = _logic.GetRole.dr;
    }
    /************************************** protected methods (event listeners) ********************************/
    protected void btnAddnew_Click(object sender, EventArgs e)
    { 
        Databound();
        chgOperTxt(true);
        _logic.Initialize();
        DetailDataBind();
        txtJobName.Focus();
        GridView1.SelectedIndex = -1; 
       
    }
    protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
    {
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        chgOperTxt(false);
        if (GridView1.DataKeys != null)
        {
            string id = GridView1.DataKeys[e.NewEditIndex].Value.ToString();
            _logic.Initialize(int.Parse(id));
        }
        Databound();
        DetailDataBind(); 
        txtJobName.Focus();
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        SetDataBack();
        //shifts id eq 0 means this is new one,to add; else an old one, to update
        if (txtJobDesc.Text.Trim().Length + txtJobName.Text.Trim().Length == 0)
        {
            txtJobName.Focus();
            Databound();
            DataDetail.Visible = true;
             ScriptManager.RegisterStartupScript(btnSubmit, typeof(UpdatePanel), "alert",
                                                  "alert('请输入角色名称。');", true);
            return;
        }
        int oper = 0;
        oper = _logic.GetRole.ID > 0 ? _logic.GetRole.Update() : _logic.GetRole.Add();
        ClearDetail();
        Databound();
      
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        
        if (GridView1 != null)
        {
            if (GridView1.DataKeys != null)
            {
                string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
                _logic.Initialize(int.Parse(id));
            }
        }
        /*
        _logic.GetRole.dr = false;
        _logic.GetRole.Update();
        
       */
        _logic.GetRole.Delete();
        ClearDetail();
        Databound();
    }
    protected void GridView1_PageIndexChanged(object sender, EventArgs e)
    {
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        Databound();
        //GridView1.DataSource = shiftLgc.getAllShifts().Tables[0];
        //     GridView1.DataBind();    
    }
    protected void GridView1_DataBinding(object sender, EventArgs e)
    {
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        CheckBox chk;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            chk = new CheckBox();
            chk.Text = "";
            chk.Enabled = false;
            chk.Checked = e.Row.Cells[3].Text.Trim().Equals("True");
            e.Row.Cells[3].Controls.Add(chk);
        }
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
      
        string sortExpression = e.SortExpression;
        if (GridView1.SortDirection == SortDirection.Ascending)                      //设置排序方向
        {
             
            SortGridView(sortExpression, " DESC");
        }
        else
         {
             
            SortGridView(sortExpression, " ASC");
        }
    }
 private void SortGridView(string sortExpression, string direction)
   {
       DataView dv = new DataView(_logic.GetAllActiveData().Tables[0]);
       dv.Sort = sortExpression + direction;
       GridView1.DataSource = dv;                       //将DataView绑定到GridView上
       GridView1.DataBind();
    } 
    //reset the page
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        _logic.Initialize();
        Page_Load(this, e);
        ClearDetail();
    }
    /***************************************** internal methods ********************************************/
    /// 
    /// set data from page to shift object
    /// 
    private void SetDataBack()
    {
        //    _logic.GetRole.operater = Session["CurrentUserID"].ToString();  todo: write a public method to handle operater,createtime,lastmodfied ... log info.
       // if (_logic.GetRole.ID > 0) 
     //    _logic.GetRole.lastmodified = DateTime.Now.ToString();
     //   _logic.GetRole.dr = true;
        _logic.GetRole.role_name = txtJobName.Text;
        _logic.GetRole.role_desc = txtJobDesc.Text;
        _logic.GetRole.role_4Approve = chkApprove.Checked;
    }
    private void chgOperTxt(bool addNew)
    { 
        btnSubmit.Enabled = true;
         DataDetail.Visible = true;
    }
    private void ClearDetail()
    {
        chkApprove.Checked = false; 
        txtJobName.Text = "";
        txtJobDesc.Text = "";
        DataDetail.Visible = false;
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        chgOperTxt(false);
        if (GridView1.DataKeys != null)
        {
            string id = GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString();
            _logic.Initialize(int.Parse(id));
        }
        Databound();
        DetailDataBind();
    }
   
}