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.Threading.Tasks;
using System.Windows.Forms;

namespace FEQ0000.Purchase
{
    public partial class fSIDCheckNR : fBase
    {
        public List<int> chklist = new List<int>();
        public fSIDCheckNR(List<dsPurchase.PurchaseRow> items)
        {
            InitializeComponent();

            //자료를 추가한다
            this.dsPurchase.PurchaseCheck.Clear();
            foreach (var item in items)
            {
                var newdr = dsPurchase.PurchaseCheck.NewPurchaseCheckRow();
                newdr.idx = item.idx;
                newdr.pdate = item.pdate;
                newdr.sid = item.sid;
                newdr.pumname = item.pumname;
                newdr.pumscale = item.pumscale;
                newdr.pumunit = item.pumunit;
                newdr.pumqty = item.pumqtyReq;
                newdr.pumprice = item.pumprice;
                newdr.pumamt = item.pumamt;
                newdr.supply = item.supply;
                newdr.project = item.project;
                newdr.bigo = item.bigo;
                newdr.gcode = item.gcode;
                newdr.requestName = item.requestName;
                newdr.chk1 = item.chk1;
                newdr.chk2 = item.chk2;
                newdr.EndEdit();
                this.dsPurchase.PurchaseCheck.AddPurchaseCheckRow(newdr);
            }
            this.dsPurchase.PurchaseCheck.AcceptChanges();
            this.Text = $"NR구매품목 SID재고 검사";
        }
        public fSIDCheckNR(List<dsPurchase.EETGW_PurchaseCRRow> items)
        {
            InitializeComponent();

            //자료를 추가한다
            this.dsPurchase.PurchaseCheck.Clear();
            foreach (var item in items)
            {
                var newdr = dsPurchase.PurchaseCheck.NewPurchaseCheckRow();
                newdr.idx = item.idx;
                newdr.pdate = item.pdate;
                newdr.sid = item.sid;
                newdr.pumname = item.pumname;
                newdr.pumscale = item.pumscale;
                newdr.pumunit = item.pumunit;
                newdr.pumqty = item.pumqtyReq;
                newdr.pumprice = item.pumprice;
                newdr.pumamt = item.pumamt;
                newdr.supply = item.supply;
                newdr.project = item.project;
                newdr.bigo = item.bigo;
                newdr.gcode = item.gcode;
                newdr.requestName = item.requestName;
                newdr.chk1 = item.chk1;
                newdr.chk2 = item.chk2;
                newdr.EndEdit();
                this.dsPurchase.PurchaseCheck.AddPurchaseCheckRow(newdr);
            }
            this.dsPurchase.PurchaseCheck.AcceptChanges();
            this.Text = $"CR구매품목 SID재고 검사";
        }

        public fSIDCheckNR(List<dsPurchase.EETGW_PurchaseEBRow> items)
        {
            InitializeComponent();

            //자료를 추가한다
            this.dsPurchase.PurchaseCheck.Clear();
            foreach (var item in items)
            {
                var newdr = dsPurchase.PurchaseCheck.NewPurchaseCheckRow();
                newdr.idx = item.idx;
                newdr.pdate = item.pdate;
                newdr.sid = item.sid;
                newdr.pumname = item.pumname;
                newdr.pumscale = item.pumscale;
                newdr.pumunit = item.pumunit;
                newdr.pumqty = item.pumqtyReq;
                newdr.pumprice = item.pumprice;
                newdr.pumamt = item.pumamt;
                newdr.supply = item.supply;
                newdr.project = item.project;
                newdr.bigo = item.bigo;
                newdr.gcode = item.gcode;
                newdr.requestName = item.requestName;
                newdr.chk1 = item.chk1;
                newdr.chk2 = item.chk2;
                newdr.EndEdit();
                this.dsPurchase.PurchaseCheck.AddPurchaseCheckRow(newdr);
            }
            this.dsPurchase.PurchaseCheck.AcceptChanges();
            this.Text = $"EB구매품목 SID재고 검사";
        }


        private void fSIDCheckNR_Load(object sender, EventArgs e)
        {
            EnsureVisibleAndUsableSize();
            dv1.AutoResizeColumns();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            var dlg = FCOMMON.Util.MsgQ("모든 자료의 SPR/NR 재고를 검색 할까요?");
            if (dlg != DialogResult.Yes) return;
            var cnt = 0;
            foreach (var dr in this.dsPurchase.PurchaseCheck)
            {
                var sid = dr.sid;
                if (sid.Length != 9 || sid.StartsWith("10") == false)
                {
                    dr.sitecnt = "SID오류";
                    dr.chk2 = true;
                    cnt += 1;
                }
                else
                {
                    var dt = AmkorRestfulService.SPMSIDSearch(sid);
                    if (dt.Complete)
                    {
                        dr.sitecnt = $"{dt.Result.Rows.Count}건";
                    }
                    else
                    {
                        dr.sitecnt = "없음";
                        dr.chk2 = true;
                        cnt += 1;
                    }
                }
                dr.EndEdit();
            }
            FCOMMON.Util.MsgI($"전체 {this.dsPurchase.PurchaseCheck.Count}건 중 {cnt}건의 자료가 [구매담당확인] 체크 되었습니다");
        }

        private void dv1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex < 0 || e.RowIndex < 0) return;
            var col = this.dv1.Columns[e.ColumnIndex];
            if (col.DataPropertyName == "sitecnt")
            {
                //site검색 해야한다
                var drv = this.dv1.Rows[e.RowIndex].DataBoundItem as DataRowView;
                var dr = drv.Row as dsPurchase.PurchaseCheckRow;
                var sid = dr.sid;
                if (sid.Length != 9 || sid.StartsWith("10") == false)
                {
                    dr.sitecnt = "SID오류";
                    dr.chk2 = true;
                }
                else
                {
                    var dt = AmkorRestfulService.SPMSIDSearch(sid);
                    if (dt.Complete)
                    {
                        dr.sitecnt = $"{dt.Result.Rows.Count}건";
                        var f = new FCM0000.fSIDListSelect(sid, dt.Result);
                        if (f.ShowDialog() == DialogResult.OK)
                        {
                            dr.chk2 = true;
                        }
                        else
                        {
                            dr.chk2 = false;
                        }
                    }
                    else
                    {
                        dr.sitecnt = "없음";
                        dr.chk2 = true;
                    }
                }
                dr.EndEdit();
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            var cnt = this.dsPurchase.PurchaseCheck.Where(t => t.chk2).Count();

            if (cnt == 0)
            {
                this.Close();
            }
            else
            {
                var dlg = FCOMMON.Util.MsgQ($"{cnt}건의 항목의 [구매담당확인] 상태를 전환 할까요?");
                if (dlg != DialogResult.Yes) return;

                var lists = this.dsPurchase.PurchaseCheck.Where(t => t.chk2 == true).ToList();
                this.chklist.AddRange(lists.Select(t => t.idx));
                DialogResult = DialogResult.OK;
            }

        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            foreach (var dr in this.dsPurchase.PurchaseCheck)
                dr.chk2 = true;
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            foreach (var dr in this.dsPurchase.PurchaseCheck)
                dr.chk2 = false;
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            var url = @"https://atknet.amkor.co.kr/#/department/ee/spm";
            FCOMMON.Util.RunExplorer(url);
        }
    }
}
