using FCOMMON;
using NetOffice.Extensions.Invoker;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AR;
using NetOffice.OutlookApi;

namespace FBS0000.Holiday
{
    public partial class fTimeTransfer : fBase
    {
        public fTimeTransfer()
        {
            InitializeComponent();
            Properties.Settings.Default["gwcs"] = FCOMMON.info.CS;

            //dtSD.KeyDown += dtSD_KeyDown;
            //dtED.KeyDown += dtSD_KeyDown;
        }
        private void fErrorChk_Load(object sender, EventArgs e)
        {
            //기본 이번달 설정한다
            dtSD.Value = DateTime.Now.AddMonths(-1);
            dateTimePicker1.Value = new DateTime(DateTime.Now.Year, 1, 1);
            EnsureVisibleAndUsableSize();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //검사버튼
            var sd = DateTime.Parse(dtSD.Text);
            var stime = new DateTime(sd.Year, 1, 1);
            var etime = new DateTime(sd.Year, sd.Month, 1).AddMonths(1).AddDays(-1);

            var gcode = FCOMMON.info.Login.gcode;
            //var id = FCOMMON.info.Login.no;

            var cn = FCOMMON.DBM.getCn();
            cn.Open();
            var cmd = new System.Data.SqlClient.SqlCommand("", cn);
            cmd.Parameters.Add("gcode", SqlDbType.VarChar).Value = gcode;
            cmd.Parameters.Add("sd", SqlDbType.VarChar).Value = stime.ToString("yyyy-MM-dd");
            cmd.Parameters.Add("ed", SqlDbType.VarChar).Value = etime.ToString("yyyy-MM-dd");

            //근태입력자료확인
            cmd.CommandText = "SELECT  holyday.uid, Users.name,\r\nsum( Holyday.DrTime) AS Dr," +
                " sum(Holyday.CrTime) AS Cr, sum( Holyday.DrTime) - sum(holyday.crtime) as jan" +
                " FROM     Holyday WITH (NOLOCK)" +
                " LEFT OUTER JOIN EETGW_GroupUser WITH (NOLOCK) " +
                " ON Holyday.gcode = EETGW_GroupUser.gcode AND Holyday.uid = EETGW_GroupUser.uid " +
                " LEFT OUTER JOIN Users WITH (NOLOCK) ON Holyday.uid = Users.id" +
                " WHERE  (Holyday.gcode = @gcode) AND  (Holyday.sdate BETWEEN @sd AND @ed) and  EETGW_GroupUser.useUserState = 1 " +
                " AND (ISNULL(Holyday.DrTime, 0) + ISNULL(Holyday.CrTime, 0) <> 0)" +
                " group by holyday.uid,Users.name" +
                " having sum( Holyday.DrTime) - sum(holyday.crtime) <> 0" +
                " order by Users.name";

            var dar = cmd.ExecuteReader();
            this.listView1.Items.Clear();
            double sum_dr, sum_cr, sum_jan;
            sum_dr = sum_cr = sum_jan = 0.0;
            while (dar.Read())
            {
                var str_uid = dar[0].ToString();
                var str_name = dar[1].ToString();
                var str_dr = (double)dar[2];
                var str_cr = (double)dar[3];
                var str_jan = (double)dar[4];

                sum_dr += str_dr;
                sum_cr += str_cr;
                sum_jan += str_jan;

                var lv = this.listView1.Items.Add(str_uid);
                lv.SubItems.Add(str_name);
                lv.SubItems.Add(str_dr.ToString());
                lv.SubItems.Add(str_cr.ToString());
                lv.SubItems.Add(str_jan.ToString());
                lv.Tag = $"{str_uid}|{str_jan}";
                lv.Checked = true;
            }

            var lvsum = this.listView1.Items.Add("Total");
            lvsum.SubItems.Add("");
            lvsum.SubItems.Add(sum_dr.ToString());
            lvsum.SubItems.Add(sum_cr.ToString());
            lvsum.SubItems.Add(sum_jan.ToString());
            lvsum.Checked = false;
            lvsum.BackColor = Color.Gray;

            dar.Close();
            cn.Dispose();

        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            dtSD.Value = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var 시작달 = DateTime.Parse(dtSD.Value.ToString("yyyy-MM") + "-01");
            dtSD.Value = 시작달.AddMonths(-1);
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            var 다음달 = DateTime.Parse(dtSD.Value.ToString("yyyy-MM") + "-01");
            dtSD.Value = 다음달.AddMonths(1);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var chkitems = this.listView1.CheckedItems;
            if (chkitems.Count < 1)
            {
                UTIL.MsgE("등록 가능한 아이템이 없습니다");
                return;
            }

            var ed = dateTimePicker1.Value;
            if (UTIL.MsgQ($"{chkitems.Count}건의 자료를 {ed.ToString("yyyy-MM-dd")}일자로 등록할까요?\n구분은 '대체'로 입력됩니다") != DialogResult.Yes)
                return;

            var sumjan = 0.0;
            var ta = new dsMSSQLTableAdapters.HolydayTableAdapter();
            var dt = new dsMSSQL.HolydayDataTable();
            foreach (ListViewItem lv in chkitems)
            {
                if (lv.Tag == null) continue;
                var buf = lv.Tag.ToString().Split('|');
                var uid = buf[0];
                var jan = double.Parse(buf[1]);
                sumjan += jan;

                var dr = dt.NewHolydayRow();
                dr.DrTime = jan;
                dr.sdate = ed;
                dr.edate = ed;
                dr.uid = uid;   
                dr.title = "대체이월";
                dr.contents = "대체이월";
                dr.tag = "iwol";
                dr.wuid = FCOMMON.info.Login.no;
                dr.wdate = DateTime.Now;
                dr.cate = "대체";
                dr.iwol = true;
                dr.gcode = FCOMMON.info.Login.gcode;
                dt.AddHolydayRow(dr);
            }
            var cnt = ta.Update(dt);
            if (cnt > 0)
                UTIL.MsgI($"{cnt}건의 자료가 추가 되었습니다");
            else UTIL.MsgE("저장된 자료가 없습니다");
        }
    }
}
