using System;
using System.Web.UI.WebControls;
using TopWinCMS.BLL;
using TopWinCMS.Common;
using TopWinCMS.Model;

namespace TopWinCMS.WebUI.Admin.ControlPanel.Financial
{
    public partial class PaymentMethod : System.Web.UI.Page
    {
        PaymentMethodManage MyClassBLL = new PaymentMethodManage();
        PaymentMethodInfo model = new PaymentMethodInfo();
        SQLModel s_model = new SQLModel();

        protected void Page_Load(object sender, System.EventArgs e)
        {

            //RightsCheck.CheckWorkPower("P_4");

            if (!IsPostBack)
            {
                string Action = Request["Action"];
                if (Action == "Add")
                {
                    Panel1.Visible = true;
                    Panel2.Visible = false;
                    BT_Edit.Visible = false;
                }
                else if (Action == "Edit")
                {
                    Panel1.Visible = true;
                    Panel2.Visible = false;
                    BT_Edit.Visible = true;
                    BT_OK.Visible = false;


                    model = new PaymentMethodManage().EditLoad(int.Parse( Request["ID"]));
                    TB_PaymentMethodName.Text = model.MethodName;
                    CB_Pass.Checked = model.Pass == 1 ? true : false;
                    TB_AccountsID.Text = model.AccountsID;
                    TB_MD5.Text = model.MD5;
                    TB_Rate.Text = model.Rate.ToString();
                    CB_IsDefault.Checked = model.IsDefault == 1 ? true : false;
                    DDL_Interface.SelectedValue = model.Interface.ToString();
                    TB_ParentID.Text = model.ParentID;
                    TB_eMail.Text = model.eMail;
                }
                else
                {

                    BindList();
                }
            }
        }

        private void BindList()
        {
            GV_List.EmptyDataTemplate = TopWinCMS.WebFactory.PageBase.GetTemplate(this);
            s_model.Fields = "*";
            s_model.TableName = "TopWinCMS_PaymentMethod";
            s_model.OrderField = "ID Desc";


            s_model.PageSize = 15;

            MyClassBLL.BindList(GV_List, PageNavigation1, s_model);

            if (GV_List.Rows.Count <= 0)
            {

                DelSeletedID.Enabled = false;
            }
        }

        protected void DelSeletedID_Click(object sender, System.EventArgs e)
        {
            MyClassBLL.Delete(GetSelectedRow.IDs(GV_List), UpdatePanel1);
            BindList();
        }

        protected void PageNavigation1_PageChanging(object src, TopWinCMS.WebControls.PageChangingEventArgs e)
        {
            s_model.PageNo = e.NewPageIndex;
            BindList();
        }

        protected void BT_OK_Click(object sender, EventArgs e)
        {

            model.ID = 0;
            model.MethodName = TB_PaymentMethodName.Text.Trim();
            model.AccountsID = TB_AccountsID.Text.Trim();
            model.MD5 = TB_MD5.Text.Trim();
            model.Rate = decimal.Parse(TB_Rate.Text);
            model.Pass = CB_Pass.Checked == true ? 1 : 0;
            model.IsDefault = CB_IsDefault.Checked == true ? 1 : 0;
            model.Admin = AdminLoginInfo.UserName;
            model.Interface = int.Parse(DDL_Interface.SelectedValue);
            model.ParentID = TB_ParentID.Text.Trim();
            model.eMail = TB_eMail.Text.Trim();

            string Err = null;
            MyClassBLL.Add(model, ref Err);

            if (string.IsNullOrEmpty(Err))
            {
                Panel1.Visible = false;
                Panel2.Visible = true;

                s_model.PageNo = 1;
                BindList();
            }
            else
            {
                MsgBox.Alert(Err, this.UpdatePanel1);
            }
        }

        protected void BT_Edit_Click(object sender, EventArgs e)
        {
            model.ID = int.Parse(Request["ID"]);
            model.MethodName = TB_PaymentMethodName.Text;
            model.Pass = CB_Pass.Checked == true ? 1 : 0;
            model.AccountsID = TB_AccountsID.Text;
            model.MD5 = TB_MD5.Text;
            model.Rate = decimal.Parse(TB_Rate.Text);
            model.IsDefault = CB_IsDefault.Checked == true ? 1 : 0;
            model.Admin = AdminLoginInfo.UserName;
            model.Interface = int.Parse(DDL_Interface.SelectedValue);
            model.ParentID = TB_ParentID.Text.Trim();
            model.eMail = TB_eMail.Text.Trim();

            string Err = null;
            MyClassBLL.Add(model, ref Err);
            if (string.IsNullOrEmpty(Err))
            {
                Panel1.Visible = false;
                Panel2.Visible = true;

                s_model.PageNo = 1;
                BindList();
            }
            else
            {
                MsgBox.Alert(Err, this.UpdatePanel1);
            }
        }
        protected void BT_Back_Click(object sender, EventArgs e)
        {
            Response.Redirect("PaymentMethod.aspx");
        }

        protected void GV_List_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Pass")
            {
                MyClassBLL.PassOrNo((int)GV_List.DataKeys[Convert.ToInt32(e.CommandArgument)].Value );
                s_model.PageNo = PageNavigation1.PageNo;
                BindList();
            }

        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            MyClassBLL.Delete(GV_List.DataKeys[e.RowIndex].Value.ToString(), UpdatePanel1);
            BindList();
        }

        protected void GV_List_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TableCell tc = e.Row.Cells[3];
                LinkButton lb = tc.Controls[0] as LinkButton;
                if (lb.Text == "1")
                {
                    lb.Text = "<IMG alt=\"已审核\" src=\"../Images/success-true.gif\"> ";
                }
                else
                {
                    lb.Text = "<IMG alt=\"未审核\" src=\"../Images/failed-false.gif\"> ";
                }
            }
        }
        private void InitSupportCoins(string interfaceID)
        {
            switch (interfaceID)
            {
                case "0":
                    //this.cblSupportCoins.Visible = false;
                    //this.ltrPaymentInfo.Text = "";
                    this.tr_AccountsID.Visible = false;
                    //this.pnlAliPaySettings.Visible = false;
                    this.tr_eMail.Visible = false;
                    this.tr_ParentID.Visible = false;
                    this.HyperLink1.Visible = false;
                    break;

                case "1":

                    this.tr_AccountsID.Visible = false;

                    //this.pnlAliPaySettings.Visible = false;
                    this.tr_eMail.Visible = false;
                    this.tr_ParentID.Visible = false;
                    this.HyperLink1.Visible = false;
                    this.tr_MD5.Visible = false;
                    break;
                case "2":
                    //this.ddlAliPaySetting.Attributes.Add("onchange", "ShowAlipayButton()");

                    this.tr_AccountsID.Visible = true;
                    //this.pnlAliPaySettings.Visible = true;

                    this.tr_eMail.Visible = false;
                    this.tr_ParentID.Visible = true;
                    this.HyperLink1.Visible = true;
                    this.HyperLink1.Text = "<font color=red>支付宝的特约商家才可以使用此支付接口，详情请联系客服</font>";
                    this.HyperLink1.NavigateUrl = "#";
                    break;

                case "3":

                    this.tr_AccountsID.Visible = true;

                    //this.pnlAliPaySettings.Visible = false;
                    this.tr_eMail.Visible = false;
                    this.tr_ParentID.Visible = false;
                    this.HyperLink1.Visible = false;
                    break;

                case "4":

                    this.tr_AccountsID.Visible = false;

                    ////this.pnlAliPaySettings.Visible = false;
                    this.tr_eMail.Visible = true;
                    this.tr_ParentID.Visible = false;
                    this.HyperLink1.Visible = false;
                    break;

                case "5":

                    this.tr_AccountsID.Visible = false;

                    //this.pnlAliPaySettings.Visible = false;
                    this.tr_eMail.Visible = true;
                    this.tr_ParentID.Visible = false;
                    this.HyperLink1.Visible = false;
                    break;

                case "6":

                    this.tr_AccountsID.Visible = true;

                    //this.pnlAliPaySettings.Visible = false;
                    this.tr_eMail.Visible = false;
                    this.tr_ParentID.Visible = false;
                    this.HyperLink1.Visible = false;
                    break;

                case "7":

                    this.tr_AccountsID.Visible = true;

                    //this.pnlAliPaySettings.Visible = false;
                    this.tr_eMail.Visible = false;
                    this.tr_ParentID.Visible = false;
                    this.HyperLink1.Visible = false;

                    break;

                case "8":

                    this.tr_AccountsID.Visible = true;

                    //this.pnlAliPaySettings.Visible = false;
                    this.tr_eMail.Visible = false;
                    this.tr_ParentID.Visible = false;
                    this.HyperLink1.Visible = false;

                    break;

                case "9":

                    this.tr_AccountsID.Visible = true;

                    //this.pnlAliPaySettings.Visible = false;
                    this.tr_eMail.Visible = false;
                    this.tr_ParentID.Visible = false;

                    this.HyperLink1.Visible = true;
                    this.HyperLink1.Text = "<font color=red>[点击这里进行IPS商户注册]</font>";
                    this.HyperLink1.NavigateUrl = "http://express.ips.com.cn/merchant/register_xpshop.asp";
                    break;

                case "10":

                    this.tr_AccountsID.Visible = true;

                    //this.pnlAliPaySettings.Visible = false;
                    this.tr_eMail.Visible = false;
                    this.tr_ParentID.Visible = false;

                    this.HyperLink1.Visible = true;
                    this.HyperLink1.Text = "<font color=red>[点击这里进行财付通商户注册]</font>";
                    this.HyperLink1.NavigateUrl = "http://union.tenpay.com/mch/mch_index1.shtml?posid=1202677201";
                    break;

                case "11":
                    //this.ddlAliPaySetting.Attributes.Add("onchange", "ShowAlipayButton()");

                    this.tr_AccountsID.Visible = true;
                    //this.pnlAliPaySettings.Visible = true;

                    this.tr_eMail.Visible = false;
                    this.tr_ParentID.Visible = true;
                    this.HyperLink1.Visible = false;

                    break;

                case "12":

                    this.tr_AccountsID.Visible = true;

                    //this.pnlAliPaySettings.Visible = false;
                    this.tr_eMail.Visible = false;
                    this.tr_ParentID.Visible = false;

                    this.HyperLink1.Visible = true;
                    this.HyperLink1.Text = "<font color=red>[点击这里进行财付通商户注册]</font>";
                    this.HyperLink1.NavigateUrl = "http://union.tenpay.com/mch/mch_index1.shtml?posid=1202677201";
                    break;
            }
        }

        protected void DDL_Interface_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.InitSupportCoins(this.DDL_Interface.SelectedValue);
            switch (this.DDL_Interface.SelectedValue)
            {
                case "0":

                    this.tr_AccountsID.Visible = false;
                    break;

                case "1":

                    this.tr_AccountsID.Visible = false;
                    break;

                case "2":

                    this.tr_AccountsID.Visible = true;
                    break;

                case "3":

                    this.tr_AccountsID.Visible = true;
                    break;

                case "4":

                    this.tr_AccountsID.Visible = false;
                    this.tr_eMail.Visible = true;
                    break;

                case "5":

                    this.tr_AccountsID.Visible = false;
                    this.tr_eMail.Visible = true;
                    break;

                case "7":

                    this.tr_AccountsID.Visible = true;
                    break;

                case "8":

                    this.tr_AccountsID.Visible = true;
                    break;

                case "9":

                    this.tr_AccountsID.Visible = true;
                    this.tr_ParentID.Visible = false;
                    break;

                case "10":

                    this.tr_AccountsID.Visible = true;
                    break;

                case "11":

                    this.tr_AccountsID.Visible = true;
                    break;

                case "12":

                    this.tr_AccountsID.Visible = true;
                    break;
            }
        }

        protected string PayPlat(object obj)
        { 
            switch (obj.ToString())
            {
                case "1":
                    return "线下支付";
                case "2":
                    return "支付宝支付接口";
                case "3":
                    return "网银支付接口(国内)";
                case "4":
                    return "Paypal";
                case "5":
                    return "贝宝";
                case "6":
                    return "网银支付接口(国外)";
                case "7":
                    return "快钱支付";
                case "8":
                    return "YeePay易宝支付";
                case "9":
                    return "IPS易捷支付";

                case "10":
                    return "腾讯财付通即时到帐支付";
                case "11":
                    return "支付宝鲜花支付接口";
                case "12":
                    return "腾讯财付通中介担保支付";
            }
            return "";
        }
    }
}
