using System;
using System.Data;
using System.Data.SqlClient;
using Newtonsoft.Json;
using FCOMMON;

namespace Project.Web
{
    public partial class MachineBridge
    {
        #region Dashboard API

        /// <summary>
        /// 오늘 휴가 인원 수 조회
        /// </summary>
        public string TodayCountH()
        {
            try
            {
                var sql = "select count(*) from EETGW_HolydayRequest WITH (nolock) " +
                    " where gcode = @gcode and isnull(conf,0) = 1 " +
                    " and sdate <= convert(varchar(10),GETDATE(),120) and edate >= convert(varchar(10),GETDATE(),120)";

                var cn = DBM.getCn();
                cn.Open();
                var cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add("gcode", SqlDbType.VarChar).Value = info.Login.gcode;
                var cnt = (int)cmd.ExecuteScalar();
                cmd.Dispose();
                cn.Dispose();

                return cnt.ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"TodayCountH 오류: {ex.Message}");
                return "0";
            }
        }

        /// <summary>
        /// 휴가요청 대기 건수 조회
        /// </summary>
        public string GetHolydayRequestCount()
        {
            try
            {
                var sql = "select count(*) from EETGW_HolydayRequest WITH (nolock) " +
                    " where gcode = @gcode and isnull(conf,0) = 0";

                var cn = DBM.getCn();
                cn.Open();
                var cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add("gcode", SqlDbType.VarChar).Value = info.Login.gcode;
                var cnt = (int)cmd.ExecuteScalar();
                cmd.Dispose();
                cn.Dispose();

                return JsonConvert.SerializeObject(new { HOLY = cnt, Message = "" });
            }
            catch (Exception ex)
            {
                return JsonConvert.SerializeObject(new { HOLY = 0, Message = ex.Message });
            }
        }

        /// <summary>
        /// 현재 출근 대상 인원 수 조회
        /// </summary>
        public string GetCurrentUserCount()
        {
            try
            {
                var sql = "select count(*) from vGroupUser WITH (nolock) " +
                    " where gcode = @gcode and useUserState = 1 and useJobReport = 1" +
                    " and id not in (select uid from vEETGW_TodayNoneWorkUser where gcode = @gcode and kunmu = 0)";

                var cn = DBM.getCn();
                cn.Open();
                var cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add("gcode", SqlDbType.VarChar).Value = info.Login.gcode;
                var cnt = (int)cmd.ExecuteScalar();
                cmd.Dispose();
                cn.Dispose();

                return JsonConvert.SerializeObject(new { Count = cnt, Message = "" });
            }
            catch (Exception ex)
            {
                return JsonConvert.SerializeObject(new { Count = 0, Message = ex.Message });
            }
        }

        /// <summary>
        /// 구매요청 대기 건수 조회 (NR, CR)
        /// </summary>
        public string GetPurchaseWaitCount()
        {
            try
            {
                DBM.GetPurchaseWaitCount(info.Login.gcode, out int cnt1, out int cnt2);
                return JsonConvert.SerializeObject(new { NR = cnt1, CR = cnt2, Message = "" });
            }
            catch (Exception ex)
            {
                return JsonConvert.SerializeObject(new { NR = 0, CR = 0, Message = ex.Message });
            }
        }

        /// <summary>
        /// 휴가자 목록 조회
        /// </summary>
        public string GetHolyUser()
        {
            try
            {
                var sql = " select uid,type,cate,sdate,edate,title,dbo.getusername(uid) as name " +
                    " from vEETGW_TodayNoneWorkUser WITH (nolock)" +
                    " where gcode = @gcode and kunmu=0";

                var cs = Properties.Settings.Default.gwcs;
                var cn = new SqlConnection(cs);
                var cmd = new SqlCommand(sql, cn);
                cmd.Parameters.AddWithValue("gcode", info.Login.gcode ?? "");
                var da = new SqlDataAdapter(cmd);
                var dt = new DataTable();
                da.Fill(dt);
                da.Dispose();
                cmd.Dispose();
                cn.Dispose();

                return JsonConvert.SerializeObject(dt, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
            }
            catch (Exception ex)
            {
                Console.WriteLine($"GetHolyUser 오류: {ex.Message}");
                return "[]";
            }
        }

        /// <summary>
        /// 휴가요청 목록 조회
        /// </summary>
        public string GetHolyRequestUser()
        {
            try
            {
                var sql = " select uid,cate,sdate,edate,HolyReason,Users.name,holydays,holytimes,remark " +
                    " from EETGW_HolydayRequest WITH (nolock) INNER JOIN " +
                    " Users ON EETGW_HolydayRequest.uid = Users.id " +
                    " where EETGW_HolydayRequest.gcode = @gcode" +
                    " and isnull(conf,0) = 0";

                var cs = Properties.Settings.Default.gwcs;
                var cn = new SqlConnection(cs);
                var cmd = new SqlCommand(sql, cn);
                cmd.Parameters.AddWithValue("gcode", info.Login.gcode);
                var da = new SqlDataAdapter(cmd);
                var dt = new DataTable();
                da.Fill(dt);
                da.Dispose();
                cmd.Dispose();
                cn.Dispose();

                return JsonConvert.SerializeObject(dt, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
            }
            catch (Exception ex)
            {
                Console.WriteLine($"GetHolyRequestUser 오류: {ex.Message}");
                return "[]";
            }
        }

        /// <summary>
        /// 출근 대상자 목록 조회
        /// </summary>
        public string GetPresentUserList()
        {
            try
            {
                var sql = "select * from vGroupUser WITH (nolock) " +
                    " where gcode = @gcode and useUserState = 1 and useJobReport = 1" +
                    " and id not in (select uid from vEETGW_TodayNoneWorkUser where gcode = @gcode and kunmu = 0)";

                var cs = Properties.Settings.Default.gwcs;
                var cn = new SqlConnection(cs);
                var cmd = new SqlCommand(sql, cn);
                cmd.Parameters.AddWithValue("gcode", info.Login.gcode);
                var da = new SqlDataAdapter(cmd);
                var dt = new DataTable();
                da.Fill(dt);
                da.Dispose();
                cmd.Dispose();
                cn.Dispose();

                return JsonConvert.SerializeObject(dt, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
            }
            catch (Exception ex)
            {
                Console.WriteLine($"GetPresentUserList 오류: {ex.Message}");
                return "[]";
            }
        }

        /// <summary>
        /// 구매요청(NR) 목록 조회
        /// </summary>
        public string GetPurchaseNRList()
        {
            try
            {
                var sql = "select pdate, process, pumname, pumscale, pumunit, pumqtyreq, pumprice, pumamt from Purchase WITH (nolock) where gcode = @gcode and state = '---' order by pdate desc";

                var cs = Properties.Settings.Default.gwcs;
                var cn = new SqlConnection(cs);
                var cmd = new SqlCommand(sql, cn);
                cmd.Parameters.AddWithValue("gcode", info.Login.gcode);
                var da = new SqlDataAdapter(cmd);
                var dt = new DataTable();
                da.Fill(dt);
                da.Dispose();
                cmd.Dispose();
                cn.Dispose();

                return JsonConvert.SerializeObject(dt, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
            }
            catch (Exception ex)
            {
                Console.WriteLine($"GetPurchaseNRList 오류: {ex.Message}");
                return "[]";
            }
        }

        /// <summary>
        /// 구매요청(CR) 목록 조회
        /// </summary>
        public string GetPurchaseCRList()
        {
            try
            {
                var sql = "select pdate, process, pumname, pumscale, pumunit, pumqtyreq, pumprice, pumamt " +
                    " from EETGW_PurchaseCR WITH (nolock) " +
                    " where gcode = @gcode and state = '---'" +
                    " order by pdate desc";

                var cs = Properties.Settings.Default.gwcs;
                var cn = new SqlConnection(cs);
                var cmd = new SqlCommand(sql, cn);
                cmd.Parameters.AddWithValue("gcode", info.Login.gcode);
                var da = new SqlDataAdapter(cmd);
                var dt = new DataTable();
                da.Fill(dt);
                da.Dispose();
                cmd.Dispose();
                cn.Dispose();

                return JsonConvert.SerializeObject(dt, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
            }
            catch (Exception ex)
            {
                Console.WriteLine($"GetPurchaseCRList 오류: {ex.Message}");
                return "[]";
            }
        }

        #endregion
    }
}
