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_ClientContact : System.Web.UI.Page
{
    private string ClientID;
    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();
            ClientContactList1.Bind(Request.QueryString["ClientID"], ClientName,SelectedIndex);
            if (!String.IsNullOrEmpty(SelectedIndex))
            {
                ((GridView)ClientList1.FindControl("GridView1")).SelectedIndex = Int32.Parse(SelectedIndex);
            }
        }
    }
    //protected void GridView1_SelectedIndexChanged(object sender,EventArgs e) {
    //    int index = GridView1.SelectedIndex;

    //    IList<ClientContactInfo> ClientContactinfo = new List<ClientContactInfo>();
    //    ClientContact clientcontact = new ClientContact();
    //    ClientContactinfo = clientcontact.GetClientContactByID(GridView1.DataKeys[index].Values["id"].ToString());

    //    if (ClientContactinfo.Count > 0) {
    //        txtDemand.Text = ClientContactinfo[0].需求意向;
    //        ddlSales.SelectedValue = ClientContactinfo[0].业务员.ToString();
    //        ddlFeedbackType.SelectedValue = ClientContactinfo[0].联系反馈类型.ToString();
    //        txtTitle.Text = ClientContactinfo[0].主题;
    //        txtContent.Text = ClientContactinfo[0].内容描述;
    //        txtDateTime1.Text = ClientContactinfo[0].添加日期.ToShortDateString();
    //        rblImportantance.SelectedValue = ClientContactinfo[0].重要程度.ToString();
    //        rblIsEnd.SelectedValue = ClientContactinfo[0].是否完成.ToString();
    //        txtDateTime2.Text = ClientContactinfo[0].完成日期.ToShortDateString();
    //    }

    //}

    //protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) {
    //    string id = GridView1.DataKeys[e.RowIndex].Values["id"].ToString();

    //    ClientContact clientcontact = new ClientContact();
    //    clientcontact.ClientContactDeleteByID(id);
    //    Bind();
    //}
    //protected void Button1_Click(object sender, EventArgs e)
    //{
    //    ClientContactInfo ClientContactinfo = new ClientContactInfo();

    //    ClientContactinfo.CID = Int32.Parse(Session["CID"].ToString());
    //    ClientContactinfo.客户ID = Int32.Parse(ClientList1.ClientID);
    //    ClientContactinfo.联系反馈类型 = Int32.Parse(ddlFeedbackType.SelectedValue);
    //    ClientContactinfo.主题 = txtTitle.Text.Trim();
    //    ClientContactinfo.内容描述 = txtContent.Text.Trim();
    //    ClientContactinfo.需求意向 = txtDemand.Text.Trim();
    //    ClientContactinfo.添加日期 = DateTime.Parse(txtDateTime1.Text.Trim());
    //    ClientContactinfo.重要程度 = Int32.Parse(rblImportantance.SelectedValue);
    //    ClientContactinfo.是否完成 = Int32.Parse(rblIsEnd.SelectedValue);
    //    ClientContactinfo.完成日期 = DateTime.Parse(txtDateTime2.Text.Trim());
    //    ClientContactinfo.创建日期 = DateTime.Parse(DateTime.Now.ToString());
    //    ClientContactinfo.业务员 = Int32.Parse(Session["LoginID"].ToString());

    //    ClientContact clientcontact = new ClientContact();
    //    clientcontact.ClientContactAdd(ClientContactinfo);


    //    Bind();
        
    //}
    //protected void Button2_Click(object sender, EventArgs e)
    //{
    //    ClientContactInfo ClientContactinfo = new ClientContactInfo();

    //    ClientContactinfo.CID = Int32.Parse(Session["CID"].ToString());
    //    ClientContactinfo.客户ID = Int32.Parse(ClientList1.ClientID);
    //    ClientContactinfo.联系反馈类型 = Int32.Parse(ddlFeedbackType.SelectedValue);
    //    ClientContactinfo.主题 = txtTitle.Text.Trim();
    //    ClientContactinfo.内容描述 = txtContent.Text.Trim();
    //    ClientContactinfo.需求意向 = txtDemand.Text.Trim();
    //    ClientContactinfo.添加日期 = DateTime.Parse(txtDateTime1.Text.Trim());
    //    ClientContactinfo.重要程度 = Int32.Parse(rblImportantance.SelectedValue);
    //    ClientContactinfo.是否完成 = Int32.Parse(rblIsEnd.SelectedValue);
    //    ClientContactinfo.完成日期 = DateTime.Parse(txtDateTime2.Text.Trim());
    //    ClientContactinfo.创建日期 = DateTime.Parse(DateTime.Now.ToString());
    //    ClientContactinfo.业务员 = Int32.Parse(Session["LoginID"].ToString());
    //    ClientContactinfo.ID = Int32.Parse(GridView1.DataKeys[GridView1.SelectedIndex].Values["id"].ToString());

    //    ClientContact clientcontact = new ClientContact();
    //    clientcontact.ClientContactEdit(ClientContactinfo);

    //    Bind();
        
    //}

    /// <summary>
    /// 根据选中客户绑定联系数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    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();
        ClientContactList1.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='ClientContactInfo.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 ClientContactList1_RowCommand(object sender, EventArgs e) {

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

                    IList<ClientContactInfo> ClientContactinfo = new List<ClientContactInfo>();
                    ClientContact clientContact = new ClientContact();
                    ClientContactinfo = clientContact.GetClientContactByID(id);

                    if (ClientContactinfo.Count > 0)
                    {
                        if (ClientContactinfo[0].业务员.ToString() == Session["LoginID"].ToString())
                        {
                            //ClientBasic clientBasic = new ClientBasic();
                            clientContact.ClientContactDeleteByID(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();
            ClientContactList1.Bind(Request.QueryString["ClientID"], ClientName, SelectedIndex);
        }
        #endregion


    }
    protected void ClientContactList_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)ClientContactList1.FindControl("GridView1")).PageIndex = ClientContactList1.PageIndex;
        ClientContactList1.Bind(id, ClientName, SelectedIndex);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {
            Init_Data();
        }
    }
}
