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 FPJ0000.OtConfirm
{
    public partial class fOTConfirmOK : fBase
    {
        dsPRJ.JobReportRow[] drows;
        //Boolean binit = false;

        public fOTConfirmOK(dsPRJ.JobReportRow[] dr_)
        {
            InitializeComponent();
            drows = dr_;
            this.bs.DataSource = drows;
            //this.dv1.DataSource = dr_;
        }

        int curLevel = 0;
        private void fHolyRequestAdd_Load(object sender, EventArgs e)
        {
            EnsureVisibleAndUsableSize();
            curLevel = Math.Max(FCOMMON.info.Login.level, FCOMMON.DBM.getAuth(FCOMMON.DBM.eAuthType.holyreq));
            button1.Enabled = curLevel >= 5;
            //binit = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //초과시간이 입력된 데이터만 확인한다.
            var cnt = drows.Where(t => t.RowState == DataRowState.Modified).Count();
            var dlg = FCOMMON.Util.MsgQ($"{cnt}건의 자료를 저장할까요?\n" +
                "승인시간이 입력된 자료는 근태현황에 자동 입력됩니다\n" +
                "승인시간이 0으로 초기화된 자료는 근태현황이 자동 삭제됩니다");
            if (dlg != DialogResult.Yes) return;

            this.Validate();
            foreach (var row in this.drows)
            {
                row.otwuid = FCOMMON.info.Login.no; //220501
                row.ottime = DateTime.Now;
                row.EndEdit();
            }

            DialogResult = DialogResult.OK;
        }

        private void btInputAll_Click(object sender, EventArgs e)
        {
            using (var f = new fOTConfirmValue())
            {
                if (f.ShowDialog() != DialogResult.OK) return;
                foreach (var dr in drows)
                {
                    if (f.radAll.Checked)
                    {
                        dr.ot2 = dr.ot;
                        dr.otPMS = 0;
                    }
                    else if (f.radAllPMS.Checked)
                    {
                        dr.ot2 = 0;
                        dr.otPMS = dr.ot;
                    }
                    else if (f.radInput.Checked)
                    {
                        var ot = (float)f.nudValue.Value;
                        var otPMS = (float)f.nudValuePMS.Value;
                        var multi = f.checkBox1.Checked;
                        var multiPMS = f.checkBox2.Checked;

                        if (multi) dr.ot2 = Math.Round( dr.ot * ot,1); //입력값 배수 ㅊㅓ리
                        else dr.ot2 = ot;

                        if (multiPMS) dr.otPMS = Math.Round( dr.ot * otPMS,1); //입력값 배수 ㅊㅓ리
                        else dr.otPMS = otPMS;
                    }
                    dr.otReason = f.tbRemark.Text.Trim();
                    dr.EndEdit();
                }
                this.bs.DataSource = drows;
                this.Invalidate();
                this.dv1.Refresh();
            }
        }
    }
}
