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 fNewSID : Form
    {
        public int FindSIDCount = -1;
        public string NewSID { get; set; }
        public fNewSID(string presid)
        {
            InitializeComponent();
            this.tbOldSid.Text = presid;

        }

        private void fNewSID_Load(object sender, EventArgs e)
        {
            //해당 sid 로 101,103,106 가능한 sid 목록을 표시한다			
            var sid = tbOldSid.Text.Trim();
            var presid = this.tbOldSid.Text;
            var db = new EEEntities();
            var dr = db.Component_Reel_SIDConv.AsNoTracking().Where(t => t.SIDFrom == presid).ToList();
            if (dr.Any())
            {
                if (dr.Count() == 1)
                {
                    tb1.Text = dr.First().SIDTo != null ? dr.First().SIDTo.Trim() : string.Empty;
                }
                else
                {
                    //데이터가 여러개 있다.
                    FindSIDCount = dr.Count();
                    //var befcolname = "101";
                    //var aftcolname = "103";
                    //if (Pub.Result.JobType == "16") { befcolname = "101"; aftcolname = "106"; }
                    //else if (Pub.Result.JobType == "13") { befcolname = "101"; aftcolname = "103"; }
                    //else if (Pub.Result.JobType == "31") { befcolname = "103"; aftcolname = "101"; }
                    //else if (Pub.Result.JobType == "61") { befcolname = "106"; aftcolname = "101"; }
                    //else if (Pub.Result.JobType == "18") { befcolname = "101"; aftcolname = "108"; }
                    //else if (Pub.Result.JobType == "81") { befcolname = "108"; aftcolname = "101"; }

                    var lst = new List<String>();
                    foreach (var item in dr)
                    {
                        var msidto = string.Empty;
                        if (item.SIDTo != null) msidto = presid + ";" + item.SIDTo.Trim();
                        lst.Add(msidto);
                    }
                    var f = new Dialog.fSelectSID(lst);
                    if (f.ShowDialog() == DialogResult.OK)
                    {
                        var v = f.Value.Split(';'); //0은 old .1=new
                        tb1.Text = v[1].Trim();
                    }
                }

            }
            else
            {
                tb1.Text = string.Empty;
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.NewSID = tb1.Text.Trim();

            if (NewSID.isEmpty())
            {
                Util.MsgE("SID값이 입력(선택) 되지 않았습니다");
                return;
            }

            if (NewSID == tbOldSid.Text)
            {
                Util.MsgE($"기존 SID와 동일한 SID값이 확인되었습니다\n\n" +
                    $"기존:{tbOldSid.Text}\n" +
                   $"신규:{NewSID}");
                return;
            }

            DialogResult = DialogResult.OK;
        }

        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Util.TouchKeyShow(tb1, "Input SID");
        }


    }
}
