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 util = FCOMMON.Util;

namespace FPJ0000
{
    public partial class fMailList : FCOMMON.fBase
    {
        string fn_fpcolsize = "";
        int ProjectIndex = -1;


        public fMailList(int idx_)
        {
            InitializeComponent();
            fn_fpcolsize = util.MakeFilePath(util.CurrentPath, "formSetting", "fp_" + this.Name + ".ini");
            this.ProjectIndex = idx_;
            this.FormClosed += fPartList_FormClosed;
            this.KeyPreview = true;
            this.KeyDown += fPartList_KeyDown;
            this.dsPRJ.ProjectsMailList.TableNewRow += ProjectsPart_TableNewRow;
        }
   private void fPartList_Load(object sender, EventArgs e)
        {
            EnsureVisibleAndUsableSize();
            //열번호찾기
            //foreach(FarPoint.Win.Spread.Column col in this.fpSpread1.ActiveSheet.Columns)
            //{
            //    string colname = col.DataField.ToLower();
            //    if (colname == "itemmodel") colidx_model = col.Index;
            //    else if (colname == "itemname") colidx_itemname = col.Index;
            //    else if (colname == "item") colidx_item = col.Index;
            //    else if (colname == "qty") colidx_qty = col.Index;
            //    else if (colname == "price") colidx_price = col.Index;
            //    else if (colname == "amt") colidx_amt = col.Index;
            //    else if (colname == "supplyidx") colidx_supplyidx = col.Index;
            //    else if (col.Label.ToLower() == "supply") colidx_supply = col.Index;
            //}
            RefreshData();
            FPUtil.ColSizeLoad(ref this.fpSpread1, fn_fpcolsize);
        }
        void ProjectsPart_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["wuid"] = FCOMMON.info.Login.no;
            e.Row["wdate"] = DateTime.Now;
            e.Row["Project"] = this.ProjectIndex;
            e.Row["enable"] = true;
            e.Row["div"] = "TO";
        }

        void fPartList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape) this.Close();
        }

        void fPartList_FormClosed(object sender, FormClosedEventArgs e)
        {

        }

     
        
        void RefreshData()
        {
            if(dsPRJ.HasChanges())
            {
                var dlg = FCOMMON.Util.MsgQ("변경 사항이 있습니다. 갱신하면 변경 내용이 손실 됩니다.\n지금 갱신 하겠습니까?");
                if (dlg != System.Windows.Forms.DialogResult.Yes) return;
            }

            try {
                this.taPrj.FillByIDX(this.dsPRJ.Projects, this.ProjectIndex);
                this.ta.Fill(this.dsPRJ.ProjectsMailList, this.ProjectIndex);
            }
            catch (Exception ex)
            {
                FCOMMON.Util.MsgE(ex.Message);
            }

        }



        private void exportListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sd = new SaveFileDialog();
            sd.Filter = "excel|*.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);
            }
            
            //dv1.ExportData("partlist.csv");
        }


        private void autoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.fpSpread1.ActiveSheet.DataAutoSizeColumns = true;

            for (int i = 0; i < this.fpSpread1.ActiveSheet.Rows.Count; i++)
                this.fpSpread1.ActiveSheet.SetRowHeight(i, 25);
            //dv1.AutoResizeColumns();
        }

        private void resetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (FarPoint.Win.Spread.Column col in this.fpSpread1.ActiveSheet.Columns)
            {
                col.Width = 100;
            }
        }

        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 fpSpread1_EditModeOff(object sender, EventArgs e)
        {
            int Colidx = this.fpSpread1.ActiveSheet.ActiveColumnIndex;
            int Rowidx = this.fpSpread1.ActiveSheet.ActiveRowIndex;

            var taItem = new FCM0000.dsMSSQLTableAdapters.ItemsTableAdapter();
        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.bs.EndEdit();
            try
            {
                this.ta.Update(this.dsPRJ.ProjectsMailList);
                this.dsPRJ.ProjectsMailList.AcceptChanges();
            }
            catch(Exception ex)
            {
                FCOMMON.Util.MsgE("Save error\n\n" +ex.Message);
            }
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {

        }

        private void pdateLabel_Click(object sender, EventArgs e)
        {

        }
    }
}
