using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Product;
using System.Text;
using System.IO;
using Base;
using SqlOperate;

public partial class admin_IndustryType_IndustryManage : BasePage
{ 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }
    private void Bind()
    {
        string strURl = Request.RawUrl;
        strURl = strURl.Substring(0, strURl.IndexOf(".aspx")) + ".aspx";
        string strWhere = "1=1";
        PageNo1.Strurl = strURl;
        PageNo1.FldName = "Sequence";
        PageNo1.Table = "W_IndustryType";
        PageNo1.StrGetFields = "*";
        PageNo1.PageSize = 12;
        if (Request.QueryString["page"] != "" && Request.QueryString["page"] != null)
        {
            if (Session["strWhere"] != null)
                strWhere = Session["strWhere"].ToString();
            PageNo1.Where = strWhere;
            PageNo1.Pageindex = Convert.ToInt32(Request.QueryString["page"].ToString());
            DataTable dt = PageNo1.BindView();
            if (dt.Rows.Count == 0)
            {
                lblMessage.Visible = true;
                lblMessage.Text = "暂无数据信息";
            }
            else
                lblMessage.Visible = false;
            this.grdIndustryType.DataSource = dt.DefaultView;
            grdIndustryType.DataBind();
        }
        else {
            int ParentID = 0;
            if (Request.QueryString["ParentID"] != null && Request.QueryString["ParentID"] != "")
            {
                ParentID = int.Parse(Request.QueryString["ParentID"]);
            }
            strWhere += " and SuperiorsID=" + ParentID;
            PageNo1.Where = strWhere;
            PageNo1.Pageindex = 1;
            Session["strWhere"] = strWhere;
            DataTable dt=PageNo1.BindView();
            if (dt.Rows.Count == 0)
            {
                lblMessage.Visible = true;
                lblMessage.Text = "暂无数据信息";
            }
            else
                lblMessage.Visible = false;
            this.grdIndustryType.DataSource = dt.DefaultView;
            grdIndustryType.DataBind();
        }
    }
    protected void lbtnUpdateToRelease_Command(object sender, CommandEventArgs e)
    {
        TIndustryType TIndustry = new TIndustryType();
        string Content = e.CommandArgument.ToString();
        int index = Content.IndexOf("|");
        if (index < -1 || index + 1 > Content.Length)
            return;

        TIndustry.ID = int.Parse(Content.Substring(0, index));
        TIndustry.ToRelease = Convert.ToBoolean(Content.Substring(index + 1));
        IIndustryType RIndustry = new RIndustryType();
        RIndustry.UpdateToRelease(TIndustry);
        Bind();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Write("<script>location='javascript:history.go(-2)'</script>");
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (pnlIndustryType.Visible)
            pnlIndustryType.Visible = false;
        else
            pnlIndustryType.Visible = true;

    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        try
        {
            int parentid = 0;
            if (Request.QueryString["ParentID"] != null && Request.QueryString["ParentID"] != "")
            {
                parentid = int.Parse(Request.QueryString["ParentID"]);
            }
            TIndustryType TIndustry = new TIndustryType();
            TIndustry.Type = this.txtType.Text;
            TIndustry.ToRelease = Convert.ToBoolean(this.radlToRelease.SelectedValue);
            TIndustry.ID = parentid;
            TIndustry.Sequence = int.Parse(this.txtSequence.Text);
            IIndustryType RIndustry = new RIndustryType();
            int m = RIndustry.Insert(TIndustry);
            ShowMessage("添加成功！", "IndustryType/IndustryManage.aspx");
        }
        catch (Exception ex)
        {
            ShowMessage("添加失败！", "IndustryType/IndustryManage.aspx");
        }
    }
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        pnlIndustryType.Visible = false;
    }


    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ButJS_Click(object sender, EventArgs e)
    {
        bool b = WriteFile();
        if (b == true)
        {
            Response.Write("<script>alert('生成成功！');</script>");
        }
        else
        {
            Response.Write("<script>alert('生成失败！');</script>");
        }
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        string strId = "";
        ISupply Supply = new RSupply();
        IIndustryType RIndustry = new RIndustryType();
        for (int i = 0; i < grdIndustryType.Rows.Count; i++)
        {
            CheckBox cbox = (CheckBox)grdIndustryType.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {
                strId += grdIndustryType.DataKeys[i].Value.ToString()+",";
                int ID =int.Parse(grdIndustryType.DataKeys[i].Value.ToString());
                string strSql = "delete from  C_Supply where id in (SELECT id FROM C_Supply where patindex('%,'+rtrim( " + ID + ")+',%',','+ C_Supply.IndType+',')>0)";
                SqlHelper.ExecuteNonQuery(strSql);
                string strSql1 = "delete from  C_Purchase where id in (SELECT id FROM C_Purchase where patindex('%,'+rtrim( " + ID + ")+',%',','+ C_Purchase.IndType+',')>0)";
                SqlHelper.ExecuteNonQuery(strSql1);
            }
        }
        strId = strId.TrimEnd(new char[] { ',' });
        RIndustry.Delete(strId);
        Bind();
    }



    public static bool WriteFile()
    {
        string sPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "js\\industry.js";
        Encoding code = Encoding.GetEncoding("gb2312");
        // 读取模板文件 
        StreamWriter sw = null;
        // 替换内容 
        // 这时,模板文件已经读入到名称为str的变量中了 
        string str = Base.CreateJS.CreateIndustry(); //模板页中的ShowArticle 
        // 写文件 
        try
        {
            sw = new StreamWriter(sPath, false, code);
            sw.Write(str);
            
            sw.Flush();
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write(ex.Message);
            HttpContext.Current.Response.End();
        }
        finally
        {
            sw.Close();
        }
        return true;

    }
    protected void grdIndustryType_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(grdIndustryType.DataKeys[e.RowIndex].Value.ToString());
        string strSql = "delete from  C_Supply where id in (SELECT id FROM C_Supply where patindex('%,'+rtrim( " + id + ")+',%',','+ C_Supply.IndType+',')>0)";
        SqlHelper.ExecuteNonQuery(strSql);
        string strSql1 = "delete from  C_Purchase where id in (SELECT id FROM C_Purchase where patindex('%,'+rtrim( " + id + ")+',%',','+ C_Purchase.IndType+',')>0)";
        SqlHelper.ExecuteNonQuery(strSql1);
        IIndustryType RIndustry = new RIndustryType();
        RIndustry.Delete(id);
        Bind();
    }
    /// <summary>
    /// 排序
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow rows in this.grdIndustryType.Rows)
        {
            TextBox txt = rows.FindControl("txtSequence") as TextBox;
            System.Web.UI.WebControls.Label lbid = rows.FindControl("Lb_id") as System.Web.UI.WebControls.Label;
            int txtd = Convert.ToInt32(txt.Text.ToString());
            int lbxid = Convert.ToInt32(lbid.Text.ToString());
            IIndustryType RIndustry = new RIndustryType();
            RIndustry.UpdateSequence(txtd, lbxid);
        }
        Bind();
    }
    protected void grdIndustryType_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grdIndustryType.EditIndex = e.NewEditIndex;
        Bind();
    }
    protected void grdIndustryType_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = int.Parse(grdIndustryType.DataKeys[e.RowIndex].Value.ToString());
        string name = ((TextBox)grdIndustryType.Rows[e.RowIndex].FindControl("txtName")).Text;
        int Sequence=int.Parse(((TextBox)grdIndustryType.Rows[e.RowIndex].FindControl("txtSequence")).Text);
        TIndustryType TIndustry = new TIndustryType();
        TIndustry.ID = id;
        TIndustry.Type = name;
        TIndustry.Sequence = Sequence;
        IIndustryType RIndustry = new RIndustryType();
        RIndustry.Update(TIndustry);
        grdIndustryType.EditIndex = -1;
        Bind();
    }
    protected void grdIndustryType_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grdIndustryType.EditIndex = -1;
        Bind();
    }
    protected void grdIndustryType_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onMouseOver", "SetNewColor(this);");
            e.Row.Attributes.Add("onMouseOut", "SetOldColor(this)");
        }
    }

}
