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 System.Data.SqlClient;

public partial class WTOSYSadmin_System_PayWayDO : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["WtoRightName"] == null)
        {
            Response.Redirect("~/SYSADMIN/main/error.aspx?err=非法登陆!或系统超时，请刷新后重新登录");
        }
            this.databind();
        
    }
    protected void databind()
    {
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        if (!IsPostBack)
        {
            //SqlDataAdapter sda = new SqlDataAdapter("select * from ServiceArea order by sort", conn);
            //DataSet ds = new DataSet();
            //sda.Fill(ds);
            ////this.araer.DataSource = ds;
            ////this.araer.DataTextField ="0";
            ////this.araer.DataValueField = "id";
            ////this.araer.DataBind();
            //ds.Dispose();
            SqlDataAdapter sda2 = new SqlDataAdapter("select * from pay", conn);
            DataSet ds2 = new DataSet();
            sda2.Fill(ds2);
            this.InJ.DataSource = ds2;
            this.InJ.DataTextField = "way";
            this.InJ.DataValueField = "id";
            this.InJ.DataBind();
            ds2.Dispose();
        }
        string type = Request.QueryString["type"].ToString().Trim();
        switch(type)
        {
            case "add":
                this.LinkButton1.Click+=new EventHandler(Add_Click);
                this.Label1.Text = "支付方式添加";
                break;
            case "edit":
                if (!IsPostBack)
                {
                    SqlCommand smd = new SqlCommand("select * from Payway where id=" + Request.QueryString["id"], conn);
                    SqlDataReader sdr = smd.ExecuteReader();
                    sdr.Read();
                    this.Label1.Text = "支付方式编辑";            
                    this.title.Text = sdr.GetString(5);
                    this.InJ.SelectedValue = Convert.ToString(sdr.GetInt32(3).ToString().Trim());
                    this.MerchantNumber.Text = sdr.GetString(10);
                    this.MD5Pwd.Text = sdr.GetString(11);
                    this.sort.Text =Convert.ToString(sdr.GetInt32(9));
                    this.Description.Text = Server.HtmlDecode(sdr.GetString(7));
                    if (sdr.GetString(8) == "y")
                    {
                        this.y.Checked = true;
                    }
                    else
                    {
                        this.n.Checked = true;
                    }
                    sdr.Close();
                }
                this.LinkButton1.Click+=new EventHandler(edit_Click);
                break;
            case "change":
                switch (Request.QueryString["sta"])
                { 
                    case "open":

                        if (Doadmin.Exec("update Payway set isshow='y' where id=" + Request.QueryString["id"].Trim()))
                        {
                            Response.Write("<script>alert('开启成功');window.location.href='../System/PaySet.aspx'</script>");
                        }
                        else
                        {
                            Response.Write("<script>alert('此次操作失败');window.location.href='../System/PaySet.aspx'</script>");
                        }
                        break;
                    case "close":
                        if (Doadmin.Exec("update Payway set isshow='n' where id=" + Request.QueryString["id"].Trim()))
                        {
                            Response.Write("<script>alert('操作成功');window.location.href='../System/PaySet.aspx'</script>");
                        }
                        else
                        {
                            Response.Write("<script>alert('此次操作失败');window.location.href='../System/PaySet.aspx'</script>");
                        }
                        break;
                }
                break;
        }
        string huobi = this.InJ.SelectedValue.ToString().Trim();
        if (huobi != null)
        {
            SqlCommand smdH = new SqlCommand("select * from pay where id=" + huobi, conn);
            SqlDataReader sdrH = smdH.ExecuteReader();
            sdrH.Read();
            this.pic.ImageUrl = "~/images/payway/" + sdrH.GetString(2);
            this.cont.Text = sdrH.GetString(4);
            this.HyperLink1.NavigateUrl = sdrH.GetString(3);
            if (sdrH.GetString(5) == "y")
            {
                this.CNY.Visible = true;
            }
            else
            { this.CNY.Visible = false; }
            if (sdrH.GetString(6) == "y")
            {
                this.USD.Visible = true;
            }
            else { this.USD.Visible = false; }
            if (sdrH.GetString(7) == "y")
            {
                this.EUR.Visible = true;
            }
            else { this.EUR.Visible = false; }
            if (sdrH.GetString(8) == "y")
            {
                this.GBP.Visible = true;
            }
            else { this.GBP.Visible = false; }
            if (sdrH.GetString(9) == "y")
            {
                this.CAD.Visible = true;
            }
            else { this.CAD.Visible = false; }
            if (sdrH.GetString(10) == "y")
            {
                this.AUD.Visible = true;
            }
            else { this.AUD.Visible = false; }
            if (sdrH.GetString(11) == "y")
            {
                this.RUB.Visible = true;
            }
            else { this.RUB.Visible = false; }
            if (sdrH.GetString(12) == "y")
            {
                this.HKD.Visible = true;
            }
            else { this.HKD.Visible = false; }
            if (sdrH.GetString(13) == "y")
            {
                this.TWD.Visible = true;
            }
            else
            {
                this.TWD.Visible = false;
            }
            if (sdrH.GetString(14) == "y")
            {
                this.KRW.Visible = true;
            }
            else
            {
                this.KRW.Visible = false;
            }
            if (sdrH.GetString(15) == "y")
            {
                this.SGD.Visible = true;
            }
            else
            {
                this.SGD.Visible = false;
            }
            if (sdrH.GetString(16) == "y")
            {
                this.NZD.Visible = true;
            }
            else
            {
                this.NZD.Visible = false;
            }
            if (sdrH.GetString(17) == "y")
            {
                this.JPY.Visible = true;
            }
            else
            {
                this.JPY.Visible = false;
            }


            sdrH.Close();

        }
        conn.Close();


    }
    protected void edit_Click(object sender, EventArgs e)
    {
        //取值开始
        string arvlu, altext, paywayID, payway, title, Payment, sort, Description, isshow, MerchantNumber, MD5Pwd;
        arvlu ="1001";
        altext = "长安";
        paywayID = this.InJ.SelectedItem.Value;
        payway = this.InJ.SelectedItem.Text;
        title = this.title.Text;
        Payment = "0";
        MerchantNumber = this.MerchantNumber.Text;
        MD5Pwd = this.MD5Pwd.Text;
        sort = this.sort.Text;
        Description = Server.HtmlEncode(this.Description.Text.Replace("'", "''"));
        if (this.y.Checked)
        {
            isshow = "y";
        }
        else
        {
            isshow = "n";
        }
        string sqlstr = "update Payway set bigId=" + arvlu + ",bigName='" + altext + "',paywayID=" + paywayID + ",payway='" + payway + "',title='" + title + "',Payment=" + Payment + ",MerchantNumber='" + MerchantNumber + "',MD5Pwd='" + MD5Pwd + "',Description='" + Description + "',isshow='" + isshow + "',sort=" + sort + " where id=" + Request.QueryString["id"].Trim();

        if (Doadmin.Exec(sqlstr))
        {
            Response.Write("<script>alert('提交成功');window.location.href='../System/PaySet.aspx'</script>");
        }
        else
        {
            Response.Write("<script>alert('此次操作失败');window.location.href='PayWayDO.aspx'</script>");
        }
    }
    protected void Add_Click(object sender, EventArgs e)
    {
        string arvlu, altext, paywayID, payway, title, Payment, sort, Description, isshow, MerchantNumber, MD5Pwd;
        arvlu = "1001";
        altext = "长安";
        paywayID = this.InJ.SelectedItem.Value;
        payway = this.InJ.SelectedItem.Text;
        title = this.title.Text;
        Payment = "0";
        MerchantNumber = this.MerchantNumber.Text;
        MD5Pwd = this.MD5Pwd.Text;
        sort = this.sort.Text;
        Description = Server.HtmlEncode(this.Description.Text.Replace("'", "''"));
        if (this.y.Checked)
        {
            isshow = "y";
        }
        else
        {
            isshow = "n";
        }
        string sqlstr = "insert into Payway (bigId,bigName,paywayID,payway,title,Payment,MerchantNumber,MD5Pwd,Description,isshow,sort) values(" + arvlu + ",'" + altext + "'," + paywayID + ",'" + payway + "','" + title + "'," + Payment + ",'" + MerchantNumber + "','"+MD5Pwd+"','" + Description + "','" + isshow + "'," + sort + ")";

        if (Doadmin.Exec(sqlstr))
        {
            Response.Write("<script>alert('添加成功');window.location.href='../System/PaySet.aspx'</script>");
        }
        else
        {
            Response.Write("<script>alert('此次操作失败');window.location.href='PayWayDO.aspx'</script>");
        }

    }
}
