using LYFZ.WeixinServers.WeiXinAPP;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace LYFZ.WeixinServers.DZKJ
{
    public partial class DZKJ_DistributorAccount : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CommonHandler.CheckLoginJump(this);
            if (!IsPostBack)
            {
                string Action = Request.QueryString["a"];
                if (Action != null)
                {
                    switch (Action) {
                        case "recharge": 
                            recharge(); 
                            break;
                        default: 
                            showMsegesBox("参数错误"); 
                            break;
                    }
                    return;
                }
                if (Request.QueryString["page"] != null)
                {
                    try
                    {
                        pageIndex = Convert.ToInt32(Request.QueryString["page"].ToString());
                        if (pageIndex <= 0)
                        {
                            pageIndex = 1;
                        }
                    }
                    catch { }
                }
                if (Request.QueryString["Keyword"] != null)
                {
                    Keyword = Request.QueryString["Keyword"].ToString().Trim();
                }
                if (Request.QueryString["IsEnabled"] != null)
                {
                    IsEnabled = Request.QueryString["IsEnabled"].ToString().Trim();
                }
                if (Request.QueryString["PeriodOfValidity"] != null)
                {
                    PeriodOfValidity = Request.QueryString["PeriodOfValidity"].ToString().Trim();
                }
                if (Request.QueryString["del"] != null && Request.QueryString["del"].ToString().Length > 0)
                {
                    try
                    {
                        if (CommonHandler.CheckAdmin(this))
                        {
                            long delID = Convert.ToInt64(Request.QueryString["del"].ToString());

                            deleteDZKJ_Customer(delID);

                        }

                    }
                    catch (Exception ex)
                    {
                        showMsegesBox("删除失败,请重试.错误原因:" + ex.Message);
                    }
                }
            }
            bindUserList();
        }

        private void recharge()
        {
            long Id = long.Parse(Request.Form["distributorId"].Trim('\''));
            int Amount = int.Parse(Request.Form["amount"]);
            LYFZ.WeixinServiceDate.DAL.DAL_DZKJ_DistributorAccount DDzkjDistAccount = new WeixinServiceDate.DAL.DAL_DZKJ_DistributorAccount();
            DDzkjDistAccount.Recharge(Id, CommonHandler.GetAdminUser(this).Account, 0, Amount, "续费充值");
        }

        void deleteDZKJ_Customer(long id)
        {
            LYFZ.WeixinServiceDate.Model.Model_DZKJ_DistributorAccount MDistAccount = DZKJ_Dal.GetModel(id);
            if (DZKJ_Dal.Delete(id))
            {
                LYFZ.WeixinServiceDate.DAL.DAL_AdminUser DAdminUser = new WeixinServiceDate.DAL.DAL_AdminUser();
                DAdminUser.Delete(MDistAccount.AccountID);
                this.Response.Redirect("DZKJ_DistributorAccount.aspx");
            }
            else
            {
                showMsegesBox("删除失败,请重试.");
            }
        }

        void showMsegesBox(string msg)
        {
            Response.Write("<script>alert('" + msg + "');document.location='DZKJ_DistributorAccount.aspx';</script>"); return;
        }

        LYFZ.WeixinServiceDate.DAL.DAL_DZKJ_DistributorAccount DZKJ_Dal = new WeixinServiceDate.DAL.DAL_DZKJ_DistributorAccount();
        public System.Text.StringBuilder CustomerAccountListHtmlTb = new System.Text.StringBuilder();
        string Keyword = "";
        string IsEnabled = "";
        string PeriodOfValidity = "";
        int pageIndex = 1;
        int pageSize = Global.PageSize;
        public string PagingInfoHtml = "";
        void bindUserList()
        {
            string whereStr = String.Empty;
            if (String.IsNullOrWhiteSpace(Keyword))
            {
                whereStr = " [AccountId] like '%" + Keyword + "%' or [CompanyName] like '%" + Keyword + "%' or [Contacts] like '%" + Keyword + "%' or [Telephone] like '%" + Keyword + "%' ";
            }
            else if (!String.IsNullOrEmpty(IsEnabled.Trim()))
            {
                whereStr = "IsEnabled=" + IsEnabled;
            }

            int pageCount = 0;
            int sumCount = DZKJ_Dal.GetRecordCount(whereStr); ;
            DataSet ds = DZKJ_Dal.GetListByPage(whereStr, "RegistrationTime desc", pageIndex, pageSize, ref pageCount);
            if (pageIndex > pageCount)
            {
                pageIndex = pageCount;
            }
            string countInfo = "当前" + pageIndex.ToString() + "/" + pageCount.ToString() + "页|" + pageSize.ToString() + "条/页|共" + sumCount.ToString() + "条记录";
            PagingInfoHtml = LYFZ.WXLibrary.PageTabList.GetPagingInfo_Web(pageCount, pageIndex, countInfo);

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                int DistAccountId = (int)row["AccountID"];

                LYFZ.WeixinServiceDate.DAL.DAL_DZKJ_CustomerAccount DCustomerAccount = new LYFZ.WeixinServiceDate.DAL.DAL_DZKJ_CustomerAccount();
                /* 注意:DistributorID 存的是分销商的登录帐号Id 不是分销商Id*/
                int used = DCustomerAccount.GetUsed(DistAccountId);
                string distUsername = (new LYFZ.WeixinServiceDate.DAL.DAL_AdminUser()).GetModel(DistAccountId).Account;
                CustomerAccountListHtmlTb.AppendFormat(@"
                    <tr>
                        <td>{0}</td><td><a href=""/DZKJ/DZKJ_CustomerAccount.aspx?DistId={12}"">{1}</a></td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td><td>{8}</td><td>{9}</td><td>{10}</td>
                        <td>
                            <a href=""#ChargeModal"" class=""btn-charge"" data-distributor-id=""'{11}'"" data-toggle=""modal""><i class=""fa fa-money""></i></a>
                            <a href=""DZKJ_AddDistributor.aspx?eid={11}""><i class=""fa fa-pencil""></i></a>
                            <a href=""#myModal"" role=""button"" onclick=""setModalValue('{11}')"" data-toggle=""modal""><i class=""fa fa-remove""></i></a>
                        </td>
                    </tr>",
                        GetIsEnabled(Convert.ToInt32(row["IsEnabled"].ToString())),
                        row["CompanyName"],
                        distUsername,
                        row["Contacts"],
                        GetSex(Convert.ToInt32(row["Sex"].ToString())),
                        row["Telephone"],
                        row["StartSimulatorCount"],
                        (int)row["StartSimulatorCount"] - used,
                        Convert.ToDateTime(row["RegistrationTime"]).ToString("yyyy-MM-dd HH:mm:ss"),
                        row["ip"],
                        GetLatestLoginTime(row["LatestLoginTime"]),
                        row["id"],
                        row["AccountId"]);
            }
        }
        string GetLatestLoginTime(object LatestLoginTime)
        {
            try
            {
                DateTime tempt;

                if (LatestLoginTime != null)
                {
                    if (DateTime.TryParse(LatestLoginTime.ToString(), out tempt))
                    {
                        return tempt.ToString("yyyy-MM-dd HH:mm:ss");
                    }
                    else
                    {
                        return "";
                    }
                }
            }
            catch { }
            return "";
        }
        string GetIsEnabled(int IsEnabled)
        {
            if (IsEnabled == 1)
            {
                return "正常";
            }
            else
            {
                return "已禁用";
            }
        }
        string GetSex(int sex)
        {
            if (sex == 1)
            {
                return "男";
            }
            else
            {
                return "女";
            }
        }

        /**
            <th>状态</th>
            <th>企业名</th>
            <th>帐号</th>
            <th>联系人</th>
            <th>性别</th>
            <th>联系电话</th>
            <th>有效期</th>
            <th>模拟器个数</th>
            <th>注册时间</th>
            <th>最新登录IP</th>
            <th>最新登录时间</th>
         *
              [ID]
             ,[Account]
             ,[Password]
             ,[Contacts]
             ,[Telephone]
             ,[Sex]
             ,[CompanyName]
             ,[CompanyAddress]
             ,[PeriodOfValidity]
             ,[StartSimulatorCount]
             ,[HardwareCode]
             ,[IP]
             ,[RegistrationTime]
             ,[LatestLoginTime]
             ,[IsEnabled] 
         **/

    }
}