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;

namespace FCM0000
{
    public partial class fLovUser : FCOMMON.fBase
    {
        string fn_fpcolsize = "";
        string keyword = string.Empty;

        public string userEmail { get; set; }
        public string userID { get; set; }
        public string userName { get; set; }
        public string userTel { get; set; }

        
        public fLovUser(string search_)
        {
            InitializeComponent();
            Properties.Settings.Default["gwcs"] = FCOMMON.info.CS;
            fn_fpcolsize = System.IO.Path.Combine(FCOMMON.Util.CurrentPath, "formSetting", "fp_" + this.Name + ".ini");
            this.keyword = search_;
            this.fpSpread1.KeyDown += fpSpread1_KeyDown;
            this.fpSpread1.CellDoubleClick += fpSpread1_CellDoubleClick;
        
            //clear data
            userEmail = string.Empty;
            userID = string.Empty;
            userName = string.Empty;
            userTel = string.Empty;
        }

        void fpSpread1_CellDoubleClick(object sender, FarPoint.Win.Spread.CellClickEventArgs e)
        {
            btOK.PerformClick();
        }
   
        void fpSpread1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btOK.PerformClick();
            }
        }

        private void fLovItem_Load(object sender, EventArgs e)
        {
            EnsureVisibleAndUsableSize();
            this.Text = string.Format("사용자선택({0})",this.keyword);
            this.Show();
            Application.DoEvents();
            refreshData();
        }

        void refreshData()
        {

            try
            {
                if (keyword == "" || keyword == "%" || keyword == "%%") this.ta.Fill(this.dSUser.Users);
                else this.ta.FillBySearch(this.dSUser.Users, keyword );
                FPUtil.ColSizeLoad(ref this.fpSpread1, fn_fpcolsize);
            }
            catch (Exception eX)
            {
                FCOMMON.Util.MsgE(eX.Message);
            }
        }


        private void btSelect_Cliock(object sender, EventArgs e)
        {
            var drv = bs.Current as DataRowView;
            if (drv == null) return;
            else
            {
                var dr = drv.Row as DSUser.UsersRow;//.CustomsRow;
                userID = dr.id;
                userName = dr.name;
                userEmail = dr.email;
                userTel = dr.tel;
            }
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

       
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FPUtil.ColsizeSave(this.fpSpread1, fn_fpcolsize);
        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FPUtil.ColSizeLoad(ref this.fpSpread1, fn_fpcolsize);
        }

        private void exportListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sd = new SaveFileDialog();
            sd.Filter = "excel|*.xls";
            sd.FileName = "userlist.xls";
            if (sd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                fpSpread1.SaveExcel(sd.FileName,
                    FarPoint.Excel.ExcelSaveFlags.SaveAsViewed
                    | FarPoint.Excel.ExcelSaveFlags.SaveAsFiltered
                    | FarPoint.Excel.ExcelSaveFlags.NoFormulas
                    | FarPoint.Excel.ExcelSaveFlags.SaveCustomColumnHeaders);
                FCOMMON.Util.MsgI("다음 파일이 생성 되었습니다.\n\n" + sd.FileName);
            }
        }
    }
}
