using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;

using CRM.BLL;
using CRM.Model;
using CRM.Common;

public partial class Pages_Client_ClientRemind : System.Web.UI.Page
{
    protected void Init_Data()
    {

        ((GridView)ClientList1.FindControl("GridView1")).Columns[1].Visible = false;
        //((Button)((GridView)ClientList1.FindControl("GridView1")).Controls[0].FindControl("DEL")).Visible = false;
        //ClientList1.GridView1_DataBound(new object(),new EventArgs());
        //((GridView)ClientList1.FindControl("GridView1")).Controls.Count = false;
        if (Request.QueryString["ClientID"] != null)
        {
            string ClientName = Request.QueryString["ClientName"];
            //string SelectedIndex=((GridView)ClientList1.FindControl("GridView1")).SelectedIndex.ToString();
            string SelectedIndex = Request.QueryString["SelectedIndex"].ToString();
            ClientRemindListControl1.Bind(Request.QueryString["ClientID"], ClientName, SelectedIndex);
            if (!String.IsNullOrEmpty(SelectedIndex))
            {
                ((GridView)ClientList1.FindControl("GridView1")).SelectedIndex = Int32.Parse(SelectedIndex);
            }
        }
    }

    protected void ClientList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string id = ClientList1.ClientID;
        string ClientName = ((LinkButton)((GridView)ClientList1.FindControl("GridView1")).Rows[ClientList1.GridviewSelectedIndex].FindControl("LinkButton1")).Text;
        string SelectedIndex = ((GridView)ClientList1.FindControl("GridView1")).SelectedIndex.ToString();
        //ClientRemindList1.Bind(id, ClientName, SelectedIndex);
    }
    protected void ClientList1_RowCommand(object sender, EventArgs e)
    {
        //新增客户
        if (ClientList1.CommandName == "ADD")
        {
            string id = ClientList1.ClientID;
            string SelectedIndex = ((GridView)ClientList1.FindControl("GridView1")).SelectedIndex.ToString();
            if (!ClientScript.IsClientScriptBlockRegistered("AddClient"))
                RegisterClientScriptBlock("AddClient", "<script language=javascript>location.href='ClientRemindInfo.aspx?ClientID=" + id + "&SelectedIndex=" + SelectedIndex + "';</script>");
        }
        #region 删除客户 只有本人才可以删除客户资料
        if (ClientList1.CommandName == "DEL")
        {
            for (int i = 0; i < ((GridView)ClientList1.FindControl("GridView1")).Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)(((GridView)ClientList1.FindControl("GridView1")).Rows[i].FindControl("CheckBox2"));
                if (cb.Checked == true)
                {
                    string id = ((GridView)ClientList1.FindControl("GridView1")).DataKeys[i].Values["id"].ToString();
                    IList<ClientBasicInfo> clientbasicinfo = new List<ClientBasicInfo>();

                    ClientBasic clientBasic = new ClientBasic();
                    clientbasicinfo = clientBasic.GetClientBasicById(id);

                    if (clientbasicinfo.Count > 0)
                    {
                        if (clientbasicinfo[0].业务员.ToString() == Session["LoginID"].ToString())
                        {
                            //ClientBasic clientBasic = new ClientBasic();
                            clientBasic.ClientBasicDeleteById(id);
                        }
                    }

                }
            }
            ClientList1.Bind();

        }
        #endregion
    }
    protected void ClientList1_RowDataBound(object sender, EventArgs e)
    {
        ((GridView)ClientList1.FindControl("GridView1")).Controls[0].Controls[0].Visible = false;
    }
    protected void ClientRemindListControl1_RowCommand(object sender, EventArgs e)
    {

        //新增客户
        if (ClientRemindListControl1.CommandName == "ADD")
        {
            string id = ClientList1.ClientID;
            int SelectedIndex = ((GridView)ClientList1.FindControl("GridView1")).SelectedIndex;
            if (SelectedIndex > -1)
            {
                if (!ClientScript.IsClientScriptBlockRegistered("AddRemind"))
                    RegisterClientScriptBlock("ADDRemind", "<script language=javascript>location.href='ClientRemindInfo.aspx?ClientID=" + id + "&SelectedIndex=" + SelectedIndex + "';</script>");
            }
            else
            {
                if (!ClientScript.IsClientScriptBlockRegistered("SelectClient"))
                    RegisterClientScriptBlock("SelectClient", "<script language=javascript>alert('请选择客户')</script>");
                return;
            }
        }
        #region 删除客户 只有本人才可以删除客户资料
        if (ClientRemindListControl1.CommandName == "DEL")
        {
            for (int i = 0; i < ((GridView)ClientRemindListControl1.FindControl("GridView1")).Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)(((GridView)ClientRemindListControl1.FindControl("GridView1")).Rows[i].FindControl("CheckBox2"));
                if (cb.Checked == true)
                {
                    string id = ((GridView)ClientRemindListControl1.FindControl("GridView1")).DataKeys[i].Values["id"].ToString();

                    IList<ClientRemindInfo> ClientRemindinfo = new List<ClientRemindInfo>();
                    ClientRemind clientRemind = new ClientRemind();
                    ClientRemindinfo = clientRemind.GetClientRemindByID(id);

                    if (ClientRemindinfo.Count > 0)
                    {
                        if (ClientRemindinfo[0].业务员.ToString() == Session["LoginID"].ToString())
                        {
                            //ClientBasic clientBasic = new ClientBasic();
                            clientRemind.ClientRemindDeleteByID(id);
                        }
                    }

                }
            }
            string Id = ClientList1.ClientID;
            string ClientName = "";
            if (ClientList1.GridviewSelectedIndex > -1)
            {
                ClientName = ((LinkButton)((GridView)ClientList1.FindControl("GridView1")).Rows[ClientList1.GridviewSelectedIndex].FindControl("LinkButton1")).Text;
            }
            string SelectedIndex = ((GridView)ClientList1.FindControl("GridView1")).SelectedIndex.ToString();
            ClientRemindListControl1.Bind(Request.QueryString["ClientID"], ClientName, SelectedIndex);
        }
        #endregion


    }
    protected void ClientRemindListControl1_PageIndexChanging(object sender, EventArgs e)
    {
        string id = ClientList1.ClientID;
        string ClientName = "";
        if (ClientList1.GridviewSelectedIndex > -1)
        {
            ClientName = ((LinkButton)((GridView)ClientList1.FindControl("GridView1")).Rows[ClientList1.GridviewSelectedIndex].FindControl("LinkButton1")).Text;
        }
        string SelectedIndex = ((GridView)ClientList1.FindControl("GridView1")).SelectedIndex.ToString();
        ((GridView)ClientRemindListControl1.FindControl("GridView1")).PageIndex = ClientRemindListControl1.PageIndex;
        ClientRemindListControl1.Bind(id, ClientName, SelectedIndex);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Init_Data();
        }
    }
}
