using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QMAPP.ServicesAgent;
using QMAPP.Entity.Sys;

namespace QMAPP.WinForm.Forms.Sys
{
    /// <summary>
    /// 用户管理
    /// </summary>
    public partial class ManageUserForm : Form
    {
        public ManageUserForm()
        {
            InitializeComponent();
        }

        #region 窗体载入

        private void ManageUserForm_Load(object sender, EventArgs e)
        {
            this.dataGridView1.AutoGenerateColumns = false;

            //绑定数据
            this.BindData();
        }

        #endregion

        #region 绑定数据

        private void BindData()
        {
            List<User> list = null;
            try
            {
                QMAPP.ServicesAgent.ServiceAgent agent = ClientContext.GetServiceAgent();

                list = agent.InvokeServiceFunction<List<User>>("UserManageBLL_GetAllUsers", new User() { UserName = txtUserName.Text, LoginUserID = txtLoginID.Text }); ;

                this.dataGridView1.DataSource = list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        private void tsbtnSearch_Click(object sender, EventArgs e)
        {
            this.BindData();
        }

        private void tsbtnPrint_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count <= 0)
            {
                MessageBox.Show("请选择要打印的用户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
                return;
            }
            List<User> userlist = new List<User>();
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                userlist.Add(row.DataBoundItem as User);
            }

            PrintLoginLabel(userlist, false);
        }

        private static void PrintLoginLabel(List<User> userlist,bool editTemplate)
        {
            string path = System.Configuration.ConfigurationManager.AppSettings["Template_Local_Path"];
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            FastReport.Report fr = new FastReport.Report();
            fr.RegisterData(userlist, "Users");
            if (!System.IO.File.Exists(string.Format("{0}\\{1}.frx", path, "LoginLabel")))
            {
                if (System.IO.File.Exists(string.Format("PrintTemplate\\{1}.frx", path, "LoginLabel")))
                {
                    System.IO.File.Copy(string.Format("PrintTemplate\\{1}.frx", path, "LoginLabel")
                        , string.Format("{0}\\{1}.frx", path, "LoginLabel"));
                }
                else
                {
                    fr.FileName = string.Format("{0}\\{1}.frx", path, "LoginLabel");
                    fr.Design(true);
                    fr.Save(string.Format("{0}\\{1}.frx", path, "LoginLabel"));
                }
            }
            fr.Load(string.Format("{0}\\{1}.frx", path, "LoginLabel"));
            if (editTemplate)
            {
                fr.Design(true);
            }
            else
            {
                fr.PrintSettings.ShowDialog = false;
                fr.Print();
            }
            fr.Dispose();
        }

        private void tsbtnEditTemplate_Click(object sender, EventArgs e)
        {
            List<User> userlist = new List<User>();

            if (dataGridView1.SelectedRows.Count <= 0)
            {
                userlist.Add(new User { LoginUserID = "CFAACCTEST11", EmployeeNo = "CFAACCTEST11", UserName = "测试用户", PassWord = "LWQJKQIWE123JKJ1FNQ291FJKJQ23" });

            }
            else
            {

                foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                {
                    userlist.Add(row.DataBoundItem as User);
                }
            }



            PrintLoginLabel(userlist, true);
        }
    }
}