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

namespace UniMarc
{
    public partial class From_User_manage_List : Form
    {
        Helper_DB mDb = new Helper_DB();
        public From_User_manage_List()
        {
            InitializeComponent();
        }
        private void CheckText()
        {
            string[] tNeedText = {
                tb_sangho.Text, tb_tel.Text, tb_email.Text
            };

            foreach (string tCheckText in tNeedText)
            {
                if (tCheckText == "")
                {
                    UTIL.MsgE("전화번호, 상호, 이메일, IP, 고유번호는 필수 입력 사항입니다.");
                    return;
                }
            }
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            CheckText();

            string tCmd = string.Format("SELECT * FROM `Comp` WHERE `comp_name` = \"{0}\"", tb_sangho.Text);
            string tResult = mDb.DB_Send_CMD_Search(tCmd);
            if (tResult != "")
            {
                UTIL.MsgE("상호명이 중복되었습니다.\n다시 확인해주세요.");
                return;
            }

            tCmd = string.Format("SELECT * FROM `comp` WHERE `compidx` = \"{0}\"", nudCompIDX.Value.ToString());
            tResult = mDb.DB_Send_CMD_Search(tCmd);
          
            if (tResult != "")
            {
                UTIL.MsgE("고유번호가 중복되었습니다.\n다시 확인해주세요.");
                return;
            }

            string[] tInsertCol = {
                "comp_name", "boss", "bubin", "cobin", "uptae",
                "jongmok", "zip", "addr", "tel", "fax",
                "bank_comp", "bank_no", "email", "barea", "grade"
                ,"compidx", "IP"
            };  // 15

            string[] tInsertData = {
                tb_sangho.Text, tb_boss.Text, tb_bubin.Text, tb_cobin.Text, tb_uptae.Text,
                tb_jongmok.Text, tb_zip.Text, tb_addr.Text, tb_tel.Text, tb_fax.Text,
                tb_bank_comp.Text, tb_bank_no.Text, tb_email.Text, tb_barea.Text, "외부업체"
                ,nudCompIDX.Value.ToString(),tbIP.Text
            };  // 15
            tCmd = mDb.DB_INSERT("Comp", tInsertCol, tInsertData);
            Helper_DB.ExcuteNonQuery(tCmd);

            //// IP 적용
            //string[] IP_Col = { "compidx", "comp", "IP" };
            //string[] IP_Data = { mDb.chk_comp(tb_sangho.Text), tb_sangho.Text, tbIP.Text };//cb_IPList.Text
            //tCmd = mDb.DB_INSERT("Comp_IP", IP_Col, IP_Data);
            //Helper_DB.ExcuteNonQuery(tCmd);

            RefreshList();

        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (dgvList.CurrentCell == null || dgvList.CurrentCell.RowIndex < 0 || dgvList.SelectedRows.Count == 0)
            {
                UTIL.MsgE("수정할 행을 선택해 주세요.");
                return;
            }
            string tText = string.Format("{0} 를 수정 하시겠습니까?", dgvList.SelectedRows[0].Cells["comp_name"].Value.ToString());
            if (UTIL.MsgQ(tText) == DialogResult.Yes)
            {
                CheckText();
                string[] tEdit_tbl = {
                    "comp_name", "boss", "bubin", "cobin", "uptae",
                "jongmok", "zip", "addr", "tel", "fax",
                "bank_comp", "bank_no", "email", "barea", "grade" };
                string[] tEdit_col = {
                    tb_sangho.Text, tb_boss.Text, tb_bubin.Text, tb_cobin.Text, tb_uptae.Text,
                tb_jongmok.Text, tb_zip.Text, tb_addr.Text, tb_tel.Text, tb_fax.Text,
                tb_bank_comp.Text, tb_bank_no.Text, tb_email.Text, tb_barea.Text, "외부업체"
                };
                string[] tSearch_tbl = { "idx" };
                string[] tSearch_col = { dgvList.SelectedRows[0].Cells["dbIDX"].Value.ToString() };
                string U_cmd = mDb.More_Update("Comp", tEdit_tbl, tEdit_col, tSearch_tbl, tSearch_col);

                Helper_DB.ExcuteNonQuery(U_cmd);
            }
            RefreshList();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {

            if (dgvList.CurrentCell == null || dgvList.CurrentCell.RowIndex < 0 || dgvList.SelectedRows.Count == 0)
            {
                UTIL.MsgE("삭제할 행을 선택해 주세요.");
                return;
            }
            string tText = string.Format("{0} 를 삭제 하시겠습니까?", dgvList.SelectedRows[0].Cells["comp_name"].Value.ToString());
            if (UTIL.MsgQ(tText) == DialogResult.Yes)
            {
                string tD_cmd = mDb.DB_Delete("Comp", "idx", dgvList.SelectedRows[0].Cells["dbIDX"].Value.ToString(), "comp_name", dgvList.SelectedRows[0].Cells["comp_name"].Value.ToString());
                Helper_DB.ExcuteNonQuery(tD_cmd);
                TextClear();
            }
            RefreshList();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void RefreshList()
        {
            mDb.DBcon();
            dgvList.Rows.Clear();
            string tArea = string.Format("idx,compidx,ip,comp_name, boss, bubin, cobin, uptae,jongmok, zip, addr, tel, fax,bank_comp, bank_no, email, barea, grade");
            string tCmd = string.Format("SELECT {0} FROM Comp", tArea);
            List<string> tData = mDb.DB_Send_CMD_Search(tCmd).Split('|').ToList();

            for (int i = 0; i < tData.Count; i = i + 18)
            {
                if (i + 18 > tData.Count) break;
                string[] tDgvData = { tData[i], tData[i+1], tData[i+2], tData[i+3], tData[i+4]
                ,tData[i+5],tData[i+6],tData[i+7],tData[i+8],tData[i+9]
                ,tData[i+10],tData[i+11],tData[i+12],tData[i+13],tData[i+14],tData[i+15],tData[i+16],tData[i+17]};
                dgvList.Rows.Add(tDgvData);
            }
            dgvList.ClearSelection();
        }
        private void From_User_manage_List_Load(object sender, EventArgs e)
        {
            RefreshList();


        }
        public class CompIpData
        {
            public string mIP = string.Empty;
            public string mCompName = string.Empty;
            public string mCompIDX = string.Empty;
            public CompIpData(string pIP, string pCompName, string pCompIDX)
            {
                mIP = pIP;
                mCompName = pCompName;
                mCompIDX = pCompIDX;
            }
        }


        private void dgvList_MouseClick(object sender, MouseEventArgs e)
        {
            if (dgvList.CurrentCell == null || dgvList.CurrentCell.RowIndex < 0 || dgvList.SelectedRows.Count==0) return;
            tb_sangho.InvokeText(dgvList.SelectedRows[0].Cells["comp_name"].Value.ToString());
            tb_boss.InvokeText(dgvList.SelectedRows[0].Cells["boss"].Value.ToString());
            tb_bubin.InvokeText(dgvList.SelectedRows[0].Cells["bubin"].Value.ToString());
            tb_cobin.InvokeText(dgvList.SelectedRows[0].Cells["cobin"].Value.ToString());
            tb_uptae.InvokeText(dgvList.SelectedRows[0].Cells["uptae"].Value.ToString());
            tb_jongmok.InvokeText(dgvList.SelectedRows[0].Cells["jongmok"].Value.ToString());
            tb_zip.InvokeText(dgvList.SelectedRows[0].Cells["zip"].Value.ToString());
            tb_addr.InvokeText(dgvList.SelectedRows[0].Cells["addr"].Value.ToString());
            tb_tel.InvokeText(dgvList.SelectedRows[0].Cells["tel"].Value.ToString());
            tb_fax.InvokeText(dgvList.SelectedRows[0].Cells["fax"].Value.ToString());
            tb_bank_comp.InvokeText(dgvList.SelectedRows[0].Cells["bank_comp"].Value.ToString());
            tb_bank_no.InvokeText(dgvList.SelectedRows[0].Cells["bank_no"].Value.ToString());
            tb_email.InvokeText(dgvList.SelectedRows[0].Cells["email"].Value.ToString());
            tb_barea.InvokeText(dgvList.SelectedRows[0].Cells["barea"].Value.ToString());
            tbIP.InvokeText(dgvList.SelectedRows[0].Cells["ip"].Value.ToString());
            int tValue = 0;
            int.TryParse(dgvList.SelectedRows[0].Cells["compidx"].Value.ToString(), out tValue);
            nudCompIDX.InvokeNumeric(tValue);
        }
        private void TextClear()
        {
            tb_sangho.InvokeText("");
            tb_boss.InvokeText("");
            tb_bubin.InvokeText("");
            tb_cobin.InvokeText("");
            tb_uptae.InvokeText("");
            tb_jongmok.InvokeText("");
            tb_zip.InvokeText("");
            tb_addr.InvokeText("");
            tb_tel.InvokeText("");
            tb_fax.InvokeText("");
            tb_bank_comp.InvokeText("");
            tb_bank_no.InvokeText("");
            tb_email.InvokeText("");
            tb_barea.InvokeText("");
            tbIP.InvokeText("");
        }
    }

}
