using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FPJ0000
{
    public partial class fSaveCostData : FCOMMON.fBase
    {
        dsPRJ.EETGW_SaveCostRow dr = null;
        public fSaveCostData(dsPRJ.EETGW_SaveCostRow pidx_)
        {
            InitializeComponent();
            this.UseFormSetting = false;
            this.dr = pidx_;
            this.dsPRJ.ProjectsHistory.TableNewRow += ProjectsHistory_TableNewRow;
        }

        void ProjectsHistory_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["wuid"] = FCOMMON.info.Login.no;
            e.Row["wdate"] = DateTime.Now;
            e.Row["pidx"] = this.dr.idx;
            e.Row["div"] = "";
            e.Row["remark"] = string.Empty;
            e.Row["pdate"] = DateTime.Now.ToShortDateString();
        }
        private void fProjectData_Load(object sender, EventArgs e)
        {
            //상태
            var dt_eetproc = FCOMMON.DBM.getCodeTable("10");
            this.cmbProcess.DisplayMember = "Value";
            this.cmbProcess.ValueMember = "Value";
            this.cmbProcess.DataSource = dt_eetproc;

            //파트
            var dt_part = FCOMMON.DBM.getCodeTable("11");
            this.cmbPart.DisplayMember = "Value";
            this.cmbPart.ValueMember = "Value";
            this.cmbPart.DataSource = dt_part;


            var db = new EEEntities();

            //담당파트목록
            var lst_uid = db.EETGW_SaveCost.Where(t => string.IsNullOrEmpty(t.eepart) == false).OrderBy(t => t.eepart).GroupBy(t => t.eepart).Select(t => t.FirstOrDefault());
            this.cmbEEPart.Items.AddRange(lst_uid.Select(t => t.eepart).ToArray());

            //WorkGroup
            var lst_wgp = db.EETGW_SaveCost.Where(t => string.IsNullOrEmpty(t.workgroup) == false).OrderBy(t => t.workgroup).GroupBy(t => t.workgroup).Select(t => t.FirstOrDefault());
            this.cmbWorkgrp.Items.AddRange(lst_wgp.Select(t => t.workgroup).ToArray());

            //Site
            var lst_sit = db.EETGW_SaveCost.Where(t => string.IsNullOrEmpty(t.site) == false).OrderBy(t => t.site).GroupBy(t => t.site).Select(t => t.FirstOrDefault());
            this.cmbSite.Items.AddRange(lst_sit.Select(t => t.site).ToArray());

            //category
            var lst_cat = db.EETGW_SaveCost.Where(t => string.IsNullOrEmpty(t.category) == false).OrderBy(t => t.category).GroupBy(t => t.category).Select(t => t.FirstOrDefault());
            this.cmbCate.Items.AddRange(lst_cat.Select(t => t.category).ToArray());

            //Sub Cate
            var lst_sct = db.EETGW_SaveCost.Where(t => string.IsNullOrEmpty(t.subcate) == false).OrderBy(t => t.subcate).GroupBy(t => t.subcate).Select(t => t.FirstOrDefault());
            this.cmbsubCate.Items.AddRange(lst_sct.Select(t => t.subcate).ToArray());


            //site 기본값
            var prelist = new string[] { "ATK", "ATC", "ATEP", "ATP", "J-Device" };
            foreach (var item in prelist)
                if (cmbSite.Items.Contains(item) == false) cmbSite.Items.Add(item);

            //기본값(담당파트)
            prelist = new string[] { "EE1", "EE2", "K5EE" };
            foreach (var item in prelist)
                if (cmbEEPart.Items.Contains(item) == false) cmbEEPart.Items.Add(item);


            //기본값(워크그룹)
            prelist = new string[] { "Automation", "Bump", "Development", "FOL", "EOL", "K5EE", "Machine Shop", "SMT", "TCDS" };
            foreach (var item in prelist)
                if (cmbWorkgrp.Items.Contains(item) == false) cmbWorkgrp.Items.Add(item);


            //기본값추가            
            prelist = new string[] { "Capex", "R&M" };
            foreach (var item in prelist)
                if (cmbCate.Items.Contains(item) == false) cmbCate.Items.Add(item);

            //sub category
            prelist = new string[] { "비용절감", "생산성", "자동화", "품질", "환경안전" };
            foreach (var item in prelist)
                if (cmbsubCate.Items.Contains(item) == false) cmbsubCate.Items.Add(item);


            this.bs.DataSource = dr;

            this.cmbProcess.Text = dr.process;
            this.cmbPart.Text = dr.part;

            //일반사용자의경우에는 상태를 변경하지 못한다.
            int curLevel = Math.Max(FCOMMON.info.Login.level, FCOMMON.DBM.getAuth(FCOMMON.DBM.eAuthType.project));
            if (curLevel >= 5)
            {

            }
            else
            {

            }
        }


        private void btSave_Click(object sender, EventArgs e)
        {
            this.dr.process = cmbProcess.Text;
            this.dr.part = cmbPart.Text;
            this.dr.edate = edateTextBox.Text;



            this.Validate();
            this.bs.EndEdit();

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            //this.tableAdapterManager.UpdateAll(this.dsPRJ);
        }



        void SelectDate(TextBox ctl)
        {
            DateTime dt;
            var dateStr = ctl.Text.Trim();
            if (dateStr == "") dateStr = DateTime.Now.ToShortDateString();
            else
            {
                if (DateTime.TryParse(dateStr, out dt)) dateStr = dt.ToShortDateString();
                else
                {
                    FCOMMON.Util.MsgE("날짜 형식으로 변환할 수 없는 문자열 입니다.\n" +
                        "기준일자가 금일로 변경 됩니다");
                    dateStr = DateTime.Now.ToShortDateString();
                }
            }
            dt = DateTime.Parse(dateStr);
            var f = new FCOMMON.fSelectDay(dt);
            if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                ctl.Text = f.dtPick.SelectionStart.ToShortDateString();
            }
        }
        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            SelectDate(sdateTextBox);
        }

        private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            SelectDate(edateTextBox);
        }


        private void bs_CurrentChanged(object sender, EventArgs e)
        {

        }

    }
}
