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 UserList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CommonHandler.CheckLoginJump(this);
            if (!IsPostBack)
            {
                if (Request.QueryString["del"] != null && Request.QueryString["del"].ToString().Length>0)
                {
                    try
                    {
                        if (CommonHandler.CheckAdmin(this))
                        {
                            int delID = Convert.ToInt32(Request.QueryString["del"].ToString());
                            if (CommonHandler.GetAdminUser(this).ID != delID)
                            {
                                deleteUser(delID);
                            }
                            else {
                                showMsegesBox("对不起,当前登录帐号不能删除!");
                            }
                        }
                        
                    }
                    catch (Exception ex){
                        showMsegesBox("删除失败,请重试.错误原因:"+ex.Message);
                    }
                }
            }
            bindUserList();
        }

        void deleteUser(int id)
        {
            if (userDal.Delete(id))
            {
                this.Response.Redirect("UserList.aspx");
            }
            else {
                 showMsegesBox("删除失败,请重试.");
            }
        }

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

        LYFZ.WeixinServiceDate.DAL.DAL_AdminUser userDal = new WeixinServiceDate.DAL.DAL_AdminUser();
        public System.Text.StringBuilder UserListHtmlTb = new System.Text.StringBuilder();
        void bindUserList()
        {
            DataSet ds = userDal.GetList("Competence <> 2");
            int index = 0;
            foreach (DataRow row in ds.Tables[0].Rows) {
                UserListHtmlTb.AppendFormat(@"
                    <tr>
                        <td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td>
                        <td>
                            <a href=""AddUser.aspx?eid={8}"" style=""margin-right:10px;""><i class=""fa fa-pencil""></i></a>
                            <a href=""#myModal"" role=""button"" onclick=""setModalValue({8})""  data-toggle=""modal""><i class=""fa fa-remove""></i></a>
                        </td>
                   </tr>",
                   index++,
                   row["UserName"].ToString(),
                   row["Account"].ToString(),
                   row["sex"].ToString(),
                   row["Phone"].ToString(),
                   row["Email"].ToString(),
                   Convert.ToDateTime(row["CreateTime"]).ToString("yyyy-MM-dd hh:mm:ss"),
                   CommonHandler.I18NAccountType((int)row["Competence"]),
                   row["id"].ToString());
            }
        }

    }
}