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 AccessDB : Form
    {
        public AccessDB()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var dba = new dsAccessTableAdapters.GrpListTableAdapter();
            var grplist = dba.GetData();

            comboBox1.Items.Clear();
            foreach (var item in grplist)
                this.comboBox1.Items.Add(item.PersonUser2);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //부서등록
            var ta = new dsMSSQLTableAdapters.UserGroupTableAdapter();
            var usergrp = ta.GetData();

            //이 부서에 속하지 않은 데이터는 추가한다.
            var cnt = 0;
            foreach (var item in comboBox1.Items)
            {
                var dept = item.ToString();
                if (usergrp.Where(t => t.dept == dept).Any() == false)
                {
                    var newdr = usergrp.NewUserGroupRow();
                    newdr.dept = dept;
                    newdr.permission = 511;
                    usergrp.AddUserGroupRow(newdr);
                    cnt += 1;
                }
            }
            FCOMMON.Util.MsgI(cnt.ToString() + "건의 자료가 추가됨");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //인원업데이트
            var dept = this.comboBox1.Text;
            var tauser = new dsMSSQLTableAdapters.UsersTableAdapter();

            var ta = new dsAccessTableAdapters.VIEW_CARD_PERSONTableAdapter();
            var userlist = ta.GetData(dept);
            var cnt = 0;
            var cnte = 0;
            foreach (dsAccess.VIEW_CARD_PERSONRow dr in userlist)
            {
                //이 사용자를 user 테이블에서 찾는다.
                if (dr.IsSabunNull()) continue;
                var dtUser = tauser.GetID(dr.Sabun);
                if (dtUser.Rows.Count == 0)
                {
                    //사라없음
                    var newdr = dtUser.NewUsersRow();
                    newdr.name = dr.Name;
                    if (dr.GradeName.Length > 5) newdr.grade = dr.GradeName.Substring(0, 5);
                    else  newdr.grade = dr.GradeName;
                    newdr.dept = dr.PersonUser2;
                    newdr.hp = dr.Mobile;
                    if (dr.Tel.Length > 20) newdr.tel = dr.Tel.Substring(0, 15);
                    else newdr.tel = dr.Tel;
                    newdr.id = dr.Sabun;
                    newdr.password = "B6589FC6AB0DC82CF12099D1C2D40AB994E8410C";
                    newdr.wuid = "DEV";
                    newdr.wdate = DateTime.Now;
                    dtUser.AddUsersRow(newdr);
                    tauser.Update(dtUser);
                    cnt += 1;
                }
                else
                {
                    //있으니 데이터 변경
                    foreach(dsMSSQL.UsersRow druser in dtUser.Rows)
                    {
                        druser.name = dr.Name;
                        druser.dept = dr.PersonUser2;
                        if (dr.GradeName.Length > 5) druser.grade = dr.GradeName.Substring(0, 5);
                        else druser.grade = dr.GradeName;
                        druser.EndEdit();
                    }
                    cnte += 1;
                    tauser.Update(dtUser);
                }
            }
            FCOMMON.Util.MsgI($"A{cnt},I:{cnte}");
        }

    }
}

