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_CustomerAccount : System.Web.UI.Page
    {
        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["IsEnabled"] != null)
                {
                    IsEnabled = Request.QueryString["IsEnabled"].ToString().Trim();
                }
                if (!String.IsNullOrEmpty(Request.QueryString["DistId"]))
                {
                    DistId = Request.QueryString["DistId"].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();
        }

        void deleteDZKJ_Customer(long id)
        {
            LYFZ.WeixinServiceDate.DAL.DAL_DZKJ_TaskExecutionStatistics DDTes = new WeixinServiceDate.DAL.DAL_DZKJ_TaskExecutionStatistics();

            LYFZ.WeixinServiceDate.DAL.DAL_DZKJ_CustomerAccount DDCustAcount = new WeixinServiceDate.DAL.DAL_DZKJ_CustomerAccount();

            string account = DDCustAcount.GetModel(id).Account;

            LYFZ.WeixinServiceDate.DAL.DAL_DZKJ_TaskExecutionStatistics DDTeakExecStatistics = new WeixinServiceDate.DAL.DAL_DZKJ_TaskExecutionStatistics();

            if (DDTeakExecStatistics.Exists("Account", account) && !CommonHandler.isAdmin(this,false))
            {
                showMsegesBox("删除失败,客户已经开始使用中");
            }
            else {
                if (DZKJ_Dal.Delete(id))
                {
                    this.Response.Redirect("DZKJ_CustomerAccount.aspx");
                }
                else
                {
                    showMsegesBox("删除失败,请重试.");
                }
            }
        }

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

        LYFZ.WeixinServiceDate.DAL.DAL_DZKJ_CustomerAccount DZKJ_Dal = new WeixinServiceDate.DAL.DAL_DZKJ_CustomerAccount();
        public System.Text.StringBuilder CustomerAccountListHtmlTb = new System.Text.StringBuilder();
        string Keyword = String.Empty;
        string IsEnabled = String.Empty;
        string PeriodOfValidity = String.Empty;
        string DistId = String.Empty;
        int pageIndex = 1;
        int pageSize = Global.PageSize;
        public string PagingInfoHtml = String.Empty;
        void bindUserList()
        {
            string whereStr = String.Empty;
            if (!String.IsNullOrEmpty(Keyword))
            {
                whereStr = " ([Account] like '%" + Keyword + "%' or [CompanyName] like '%" + Keyword + "%' or [Contacts] like '%" + Keyword + "%' or [Telephone] like '%" + Keyword + "%' ) and ";
            }
            else if (!String.IsNullOrEmpty(IsEnabled))
            {
                whereStr = "IsEnabled=" + IsEnabled + " and ";
            }
            else if (!String.IsNullOrEmpty(PeriodOfValidity))
            {
                if (PeriodOfValidity.Trim() == "0")
                {
                    whereStr = "PeriodOfValidity<'" + DateTime.Now.ToString("yyyy-MM-dd") + "' and ";
                }
                else if (PeriodOfValidity.Trim() == "1")
                {
                    whereStr = "PeriodOfValidity>='" + DateTime.Now.ToString("yyyy-MM-dd") + "' and ";
                }
            }

            if (!String.IsNullOrEmpty(DistId)){
                whereStr += "DistributorID=" + DistId;
            }else{
                whereStr += CommonHandler.isAdmin(this, false) ? "1=1" : "DistributorID=" + CommonHandler.GetAdminUser(this).ID;
            }

            int pageCount = 0;
            int sumCount = DZKJ_Dal.GetRecordCount(whereStr);

            DataSet ds = DZKJ_Dal.GetListByPage(whereStr, "RegistrationTime desc", pageIndex, pageSize, ref pageCount);

            pageIndex = pageIndex > pageCount ? pageCount : pageIndex;

            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];
                CustomerAccountListHtmlTb.Append("<tr>"
                    + "<td>" + GetIsEnabled(Convert.ToInt32(row["IsEnabled"].ToString())) + "</td>"
                    + "<td>" + row["CompanyName"].ToString() + "</td>"
                    + "<td><a  href=\"/DZKJ/DZKJTaskOneView.aspx?viewtype=1&ExecDate=" + DateTime.Now.ToString("yyyy-MM-dd") + "&Account=" + row["Account"].ToString() + "\">" + row["Account"].ToString() + "</a></td>"
                    + "<td>" + row["Contacts"].ToString() + "</td>"
                    + "<td>" + GetSex(Convert.ToInt32(row["Sex"].ToString())) + "</td>"
                    + "<td>" + row["Telephone"].ToString() + "</td>"
                    + "<td>" + Convert.ToDateTime(row["PeriodOfValidity"]).ToString("yyyy-MM-dd") + "</td>"
                    + "<td>" + row["StartSimulatorCount"].ToString() + "</td>"
                    + "<td>" + Convert.ToDateTime(row["RegistrationTime"]).ToString("yyyy-MM-dd HH:mm:ss") + "</td>"
                    + "<td>" + GetLatestLoginTime(row["LatestLoginTime"]) + "</td>"
                    + "<td>"
                    + "<a href=\"DZKJ_AddCustomer.aspx?eid=" + row["id"].ToString() + "\"  style=\"margin-right:10px;\"><i class=\"fa fa-pencil\"></i></a>"
                    + "<a href=\"#myModal\" role=\"button\"   onclick=\"setModalValue('" + row["id"].ToString() + "')\"  data-toggle=\"modal\"><i class=\"fa fa-remove\"></i></a>"
                    + "</td>"
                    + " </tr>");
            }
        }
        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] */

    }
}