using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using DeiNiu.wms.Logical;
using DeiNiu.Utils;
public partial class WorkplanMain : PageBase
{
    private LWorkplan _logic;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        lem = (LEmployee)Session["CurrentUser"]; 
        if (!IsPostBack)
        {
            _logic = new LWorkplan();
            Calendar1.SelectedDate = DateTime.Today ;
             Databound();
            
        }
        SessionSeting();
    }
    /// 
    /// bound the query list.
    /// 
    private void Databound()
    {
        _logic.CheckWorkplan(lem.GetEmployee.ID, Calendar1.SelectedDate.ToString(),true);  
        GridView1.DataSource = _logic.GetLWorkplan.getWorkplanDetails(lem.GetEmployee.ID,Calendar1.SelectedDate.ToString());
        GridView1.DataBind();
        UpdateSession();
     //   btnSubmit.Enabled = false;
    //    ClearDetail();
    }
    private void SessionSeting()
    {
        if (IsPostBack)
        {
            _logic = (LWorkplan)Session[appScope.PagelevelObj];
        }
    }
    private void UpdateSession()
    {
        Session[appScope.PagelevelObj] = _logic;
    }
    /// 
    /// set data from shift object to page
    /// 
    private void DetailDataBind()
    {
        DataDetail.Visible = true;
        txtPlanContent.Text = _logic.GetLWorkplanDetail.wpd_content;
        txtPlanMem.Text = _logic.GetLWorkplanDetail.wpd_mem;
       
//    chkStatus.Checked = _logic.GetRole.dr;
    }
    /************************************** protected methods (event listeners) ********************************/
    protected void btnAddnew_Click(object sender, EventArgs e)
    {
        chgOperTxt(true);
        // _logic.Initialize();   _logic.CheckWorkplan 将初始化工作计划主对象
        _logic.InitializeDetail();
        UpdateSession();
        DetailDataBind();
        txtPlanContent.Focus();
        GridView1.SelectedIndex = -1;
        
        Databound();
    }
    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(); 
        txtPlanContent.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
    
        int oper = 0;
        oper = _logic.GetLWorkplanDetail.ID > 0 ? _logic.GetLWorkplanDetail.Update() : _logic.GetLWorkplanDetail.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.GetLWorkplanDetail.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)
    {
        
    }
    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.GetLWorkplanDetail.ID > 0)
        //    _logic.GetLWorkplanDetail.lastmodified = DateTime.Now.ToString();
        _logic.GetLWorkplanDetail.wpd_content = txtPlanContent.Text;
        _logic.GetLWorkplanDetail.wpd_mem = txtPlanMem.Text;
        _logic.GetLWorkplanDetail.wpd_workplan = _logic.GetLWorkplan.ID;
        _logic.GetLWorkplanDetail.wpd_number = _logic.getNexNumber(_logic.GetLWorkplan.ID);
    }
    private void chgOperTxt(bool addNew)
    { 
        btnSubmit.Enabled = true;
         DataDetail.Visible = true;
    }
    private void ClearDetail()
    {
        txtPlanMem.Text = "";
        txtPlanContent.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));
        }
        DetailDataBind();
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        Databound();
    }
    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
      //  e.Cell.Controls.Add(new LiteralControl(" 发工资 "));
     //   e.Cell.Font.Size = FontUnit.XXLarge;
    }
}