using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using DeiNiu.wms.Logical;
using DeiNiu.Utils;
public partial class LessonCategory :PageBase
{
    private LlessonCat _logic;
    protected void Page_Load(object sender, EventArgs e)
    {
  
        if (!IsPostBack)
        {
            _logic = new LlessonCat();
            Databound();
        }
        SessionSeting();
    }
 
    /*****************************************   private methods   ******************************************/
    /// 
    /// bound the query list.
    /// 
    private void Databound()
    {
        
     
        //装载选定部门节点的部门数据
        DataView dv = new DataView(_logic.GetAllActiveData().Tables[0]);
        TreeDept.SelectedNode.ChildNodes.Clear();
        BindTree(TreeDept.SelectedNode.Value, TreeDept.SelectedNode, dv);
        //绑定树列表里选中的部门下的所有部门数据
        dv.RowFilter="lc_uplevel = '"+ TreeDept.SelectedValue +"'";
        GridView1.DataSource = dv;
        GridView1.SelectedIndex = -1;
        GridView1.DataBind();
        UpdateSession();
    //    btnSubmit.Enabled = false;
        ClearDetail();
    }
    private void SessionSeting()
    {
        if (IsPostBack)
        {
            _logic = (LlessonCat)Session[appScope.PagelevelObj];
        }
    }
    private void UpdateSession()
    {
        Session[appScope.PagelevelObj] = _logic;
    }
    /// 
    /// set data from shift object to page
    /// 
    private void DetailDataBind()
    {
        DataDetail.Visible = true;
        txtName.Text = _logic.GetLessonCat.lc_name ;
       
        txtDesc.Text = _logic.GetLessonCat.lc_desc;
       
//    chkStatus.Checked = _logic.GetRole.dr;
    }
    /************************************** protected methods (event listeners) ********************************/
    protected void btnAddnew_Click(object sender, EventArgs e)
    {
        chgOperTxt(true);
        _logic.Initialize();
        DetailDataBind();
    }
    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();
    }
    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
    
        int oper = 0;
        oper = _logic.GetLessonCat.ID > 0 ? _logic.GetLessonCat.Update() : _logic.GetLessonCat.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.GetLessonCat.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_Sorting(object sender, GridViewSortEventArgs e)
    {
    }
    //reset the page
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        _logic.Initialize();
        Page_Load(this, e);
    }
    /***************************************** 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.GetLessonCat.ID > 0)) 
       //{
       //    _logic.GetLessonCat.last_modified = DateTime.Now.ToString(); 
       //}
        _logic.GetLessonCat.lc_uplevel = Convert.ToInt32(TreeDept.SelectedValue);
      _logic.GetLessonCat.lc_name= txtName.Text ;
       _logic.GetLessonCat.lc_desc = txtDesc.Text;
     
    }
    private void chgOperTxt(bool addNew)
    {
        DataDetail.Visible = true;
     //   lbEdit.Text = addNew ? "增加新记录" : "修改现有记录";
        btnSubmit.Enabled = true;
    }
    private void ClearDetail()
    {
        txtName.Text = ""; 
        txtDesc.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();
    }
    public void BindTree(string id, TreeNode pNode, DataView dv)
    {
        dv.RowFilter = "lc_uplevel = '" + id + "'";
        foreach (DataRowView row in dv)
        {
            TreeNode node = new TreeNode();
            node.Text = row["lc_name"].ToString();
            node.Value = row["ID"].ToString();
            if (pNode == null)
                //说明是根节点
            {
            
                TreeDept.Nodes.Add(node);
                node.Expanded = true;
      
            }
            else
            {
                pNode.ChildNodes.Add(node);
                node.Expanded = true;
                
            }
            BindTree(node.Value, node, new DataView(dv.Table));
        }
    }
    protected void TreeDept_SelectedNodeChanged(object sender, EventArgs e)
    {
        GridView1.SelectedIndex = -1;
        DataView dv = new DataView(_logic.GetAllActiveData().Tables[0]);
        dv.RowFilter = "lc_uplevel = '" + TreeDept.SelectedValue + "'";
        GridView1.DataSource = dv;
        GridView1.DataBind(); 
        ClearDetail();
    }
}