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.WeiXinAPP
{
    public partial class CustomerList : System.Web.UI.Page
    {
        LYFZ.WeixinServiceDate.DAL.DAL_CustomerInterfaces cusDal = new WeixinServiceDate.DAL.DAL_CustomerInterfaces();
        protected void Page_Load(object sender, EventArgs e)
        {
            CommonHandler.CheckLoginJump(this);
            if (!IsPostBack) {

                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["del"] != null && Request.QueryString["del"].ToString().Length > 0)
                {
                    try
                    {
                        if (CommonHandler.CheckAdmin(this))
                        {
                            string[] tempQuerys = Request.QueryString["del"].ToString().Split('@');
                            int delID = Convert.ToInt32(tempQuerys[0]);
                            if (cusDal.Delete(delID))
                            {
                                if (tempQuerys.Length == 2)
                                {
                                    Global.DeleteCustInfo(tempQuerys[1]);
                                }
                                showMsegesBox("已删除成功");
                            }
                        }

                    }
                    catch (Exception ex)
                    {
                        showMsegesBox("删除失败,请重试.错误原因:" + ex.Message);
                    }
                }
                else if (Request.QueryString["delwx"] != null && Request.QueryString["delwx"].ToString()=="1")
                {
                    LYFZ.Helper.SQLHelper.ExecuteSql("delete [tb_CustomerInterfaces] where [JMGDomainName] like '%????%' or [JMGDomainName] not in (SELECT [JMGDomainName] FROM [tb_authorization_info])");
                    showMsegesBox("已清除无效数据");
                }
                else if (Request.QueryString["eid"] != null)
                {
                    if (CommonHandler.CheckAdmin(this))
                    {
                        int eid = Convert.ToInt32(Request.QueryString["eid"].ToString());
                        SetISEnabled(eid);
                    }
                }
                else
                {
                    bindCustomerList();
                }
            }
        }
        void SetISEnabled(int eID)
        {
            try
            {
                LYFZ.WeixinServiceDate.Model.Model_CustomerInterfaces cusModel = cusDal.GetModel(eID);
                if (cusModel.ID > 0)
                {
                    string msg = "";
                    if (cusModel.ISEnabled == 1)
                    {
                        cusModel.ISEnabled = 0;
                        msg = "禁用";
                    }
                    else
                    {
                        cusModel.ISEnabled = 1;
                        msg = "启用";
                    }
                    if (cusDal.Update(cusModel))
                    { 
                        Global.UpdateCustInfo(cusModel.JMGDomainName, cusModel);
                        showMsegesBox(msg + "成功");
                          
                    }
                    else
                    {
                        showMsegesBox(msg + "失败");
                    }
                }
                else {
                    showMsegesBox("要设置的记录不存在,请刷新后重试");
                }
            }
            catch(Exception ex) {
                showMsegesBox("操作失败,原因:"+ex.Message);
            }
        }
        void showMsegesBox(string msg)
        {
            Response.Write("<script>alert('" + msg + "');document.location='CustomerList.aspx';</script>"); return;
        }
        string Keyword = "";
        public string PagingInfoHtml = "";
        int pageIndex = 1;
        int pageSize = Global.PageSize;
        public System.Text.StringBuilder CustomerListHtmlTb = new System.Text.StringBuilder();
        void bindCustomerList()
        {
            string whereStr = "";
            if (Keyword.Length > 0) {
                whereStr = " [JMGDomainName] like '%" + Keyword + "%' or [ShellDomainName] like '%" + Keyword + "%' or [IPAddress] like '%" + Keyword + "%' or [CompanyName] like '%" + Keyword + "%' ";
            }
            int pageCount = 0;
            int sumCount = cusDal.GetRecordCount(whereStr); ;
            DataSet ds = cusDal.GetListByPage(whereStr, "id 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);
            
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow row = ds.Tables[0].Rows[i];
                string ISEnabled = "禁用"; 
                string iconName = "lock";
                if (row["ISEnabled"].ToString() == "1")
                {
                    ISEnabled = "启用";
                    iconName = "ok";
                }
              
                string thisUrl = row["ShellDomainName"].ToString();

                CustomerListHtmlTb.Append("<tr>"
                                     + " <td>" + (i + 1).ToString() + "</td>"
                                     + " <td>" + row["CompanyName"].ToString() + "</td>"
                                     + " <td>" + row["JMGDomainName"].ToString() + "</td>"
                                     + " <td>" + row["ShellDomainName"].ToString() + "</td>"
                                     + " <td>" + row["AppSecret"].ToString() + "</td>"
                                     + " <td>" + row["CallFrequency"].ToString() + "</td>"
                                     + "<td>" + Convert.ToDateTime(row["RefreshTime"]).ToString("yyyy-MM-dd HH:mm") + "</td>"
                                     + "<td>" + Convert.ToDateTime(row["CreateTime"]).ToString("yyyy-MM-dd HH:mm") + "</td>"
                                     + " <td>" + ISEnabled + "</td>"
                                     + " <td>"
                                     + "<a  target=\"_blank\" href=\"" + thisUrl + "\"  style=\"margin-right:10px;\"><i class=\"icon-home\"></i></a>"
                                     + "<a href=\"CustomerList.aspx?eid=" + row["id"].ToString() + "\"  style=\"margin-right:10px;\"><i class=\"icon-" + iconName + "\"></i></a>"
                                     + "<a href=\"#myModal\" role=\"button\"   onclick=\"setModalValue('" + row["id"].ToString() + "@" + row["JMGDomainName"].ToString() + "')\"  data-toggle=\"modal\"><i class=\"icon-remove\"></i></a>"
                                     + "</td>"
                                     + " </tr>");
            }
        }
    }
}