using FCOMMON;
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 FEQ0000
{
    public partial class EQFilterApply : fBase
    {

        //fEquipment.eTabletype divtype;
        string eqfiletertype = "E";
        public EQFilterApply(string  type_)
        {
            InitializeComponent();
            Properties.Settings.Default["gwcs"] = FCOMMON.info.CS;
            Properties.Settings.Default["EEEntities"] = FCOMMON.info.CS;
            this.eqfiletertype = type_;
            this.FormClosed += __Closed;
        }

        void __Closed(object sender, FormClosedEventArgs e)
        {
           
        }
       
        private void __Load(object sender, EventArgs e)
        {
            EnsureVisibleAndUsableSize();
            try
            {
                ta.Fill(this.dsEQ.EquipmentFilter, eqfiletertype, FCOMMON.info.Login.gcode);

                //필터목록을 미리 가져온다.
                //var fn = "eqfilter.xml";
               // if (eqfiletertype != "E") fn = "eqfilterIng.xml";
                arUtil.XMLHelper xml = new arUtil.XMLHelper(FCOMMON.info.Path.MakeFilePath("Setting", "eqfilter.xml"));
                if (!xml.Exist()) xml.CreateFile();
                List<string> filterList = new List<string>();
                int filterCnt = int.Parse(xml.get_Data("filter", "count", "0"));
                for (int i = 0; i < filterCnt; i++)
                {
                  var data=  xml.get_Data("filter", "item" + (i + 1).ToString());
                  if (data=="") continue;
                  filterList.Add(data);
                }

                foreach (dsEQ.EquipmentFilterRow item in this.dsEQ.EquipmentFilter.Select("", "Title"))
                {
                    string title = item.Title;
                    if (title=="") continue;

                    var chk = filterList.IndexOf(title) != -1;
                    var lvitem = this.listView1.Items.Add(item.Title);
                    lvitem.Checked = chk;
                    lvitem.SubItems.Add(item.memo);
                    lvitem.SubItems.Add(item.Filter);
                    lvitem.SubItems.Add(item.Apply);
                }
            }
            catch (Exception ex)
            {
                FCOMMON.Util.MsgE(ex.Message);
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.Invalidate();
            this.bs.EndEdit();
            try
            {
                var cnt = ta.Update(this.dsEQ.EquipmentFilter);
                FCOMMON.Util.MsgI("update : " + cnt.ToString());

            }
            catch (Exception ex)
            {
                FCOMMON.Util.MsgE(ex.Message);
            }
        }

        public string filter = string.Empty;
        public string apply = string.Empty;


        private void button1_Click_1(object sender, EventArgs e)
        {
            //다음에 체크한것을 복원하기 위해 값을 기록한다.
            List<string> filterList = new List<string>();
            foreach (ListViewItem item in this.listView1.CheckedItems)
            {
                string title = item.SubItems[0].Text.Trim();
                if (title=="") continue;
                filterList.Add(title);
            }
            var fn = this.eqfiletertype == "E" ? "eqfilter.xml" : "eqfilterIng.xml";

            arUtil.XMLHelper xml = new arUtil.XMLHelper(FCOMMON.info.Path.MakeFilePath( "Setting", fn));
            if (!xml.Exist()) xml.CreateFile();
            xml.set_Data("filter", "count", filterList.Count.ToString());
            for (int i = 0; i < filterList.Count; i++)
            {
                xml.set_Data("filter", "item" + (i + 1).ToString(), filterList[i]);
            }
            xml.Save();

            DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
