using AR;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Project.Dialog
{
    public partial class fSelectSID : Form
    {

        public string Value = string.Empty;
        public fSelectSID(List<string> list)
        {
            InitializeComponent();
            //this.listView1.Columns[1].Text = aftercolumnname;

            this.listView1.Columns[0].Text = "Old SID";
            this.listView1.Columns[1].Text = "New SID";
            //this.listView1.Columns[2].Text = "106";
            //this.listView1.Columns[3].Text = "--";

            this.listView1.Items.Clear();
            foreach (var item in list)
            {
                var buf = item.Split(';');

                var lv = this.listView1.Items.Add(buf[0].Trim());    //101
                if (buf.Length > 1) lv.SubItems.Add(buf[1].Trim());    //103;
                else lv.SubItems.Add(string.Empty);
                //if (buf.Length > 2) lv.SubItems.Add(buf[2].Trim());    //103;
                //else lv.SubItems.Add(string.Empty);
                //if (buf.Length > 3) lv.SubItems.Add(buf[3].Trim());    //103;
                //lv.SubItems.Add(string.Empty);
            }
            this.listView1.FocusedItem = null;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Value = string.Empty;
            if (this.listView1.SelectedItems != null && this.listView1.SelectedItems.Count == 1)
            {
                var lv = this.listView1.SelectedItems[0];
                Value = string.Format("{0};{1}", lv.SubItems[0].Text, lv.SubItems[1].Text);
            }
            else if (this.listView1.FocusedItem != null)
            {
                var lv = this.listView1.FocusedItem;
                Value = string.Format("{0};{1}", lv.SubItems[0].Text, lv.SubItems[1].Text);
            }

            if (Value.isEmpty() == false)
                DialogResult = DialogResult.OK;
        }

        private void fSelectSID_Load(object sender, EventArgs e)
        {
            // 모든데이터를 확인하고 마지막 customer 정보를 확인하.ㄴㄷ

            foreach (ListViewItem lv in this.listView1.Items)
            {
                var sidNew = lv.SubItems[1].Text;
                if (sidNew.isEmpty()) lv.SubItems.Add("--");
                else
                {
                    using (var db = new DataSet1TableAdapters.K4EE_Component_Reel_SID_InformationTableAdapter())
                    {
                        var list = db.GetbySIDNoCustCode(PUB.MCCode, sidNew).ToList();
                        if (list.Any() == true)
                        {
                            var buffer = new System.Text.StringBuilder();
                            foreach (var custinfo in list)
                            {
                                if (buffer.Length > 0) buffer.Append(",");
                                buffer.Append(string.Format("[{0}] {1}", custinfo.CustCode,custinfo.CustName));
                            }
                            lv.SubItems.Add(buffer.ToString()); //cust info
                        }
                        else
                        {
                            ///자료가 없다
                            lv.SubItems.Add("No information");
                        }
                    }
                }

            }

        }
    }
}
