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.Windows.Forms;

namespace FPJ0000
{
    public partial class rPartList : fBase
    {
        int ProjectIndex;
        public rPartList(int idx_)
        {
            InitializeComponent();
            this.ProjectIndex = idx_;
        }

        private void fRPartList_Load(object sender, EventArgs e)
        {
            EnsureVisibleAndUsableSize();
            button1.PerformClick();
        }

        private void button1_Click(object sender, EventArgs e)
        {


        }

        private void button1_Click_1(object sender, EventArgs e)
        {


            try
            {
                //프로젝트 정보 채우기
                this.ta.FillByIDX(this.dsPRJ.Projects, this.ProjectIndex);


                //order by 절은 다시 생성한다.
                var SSQl = " select ItemGroup,option1,ItemSupply,sum(amt) as amt ,sum(amtn) as amtn ,sum(isnull(qtybuy,0)*isnull(price,0)) as amtb " +
                           " from projectspart" +
                           " where Project = " + this.ProjectIndex.ToString() +
                           " group by ItemGroup,option1,ItemSupply" +
                           " order by itemgroup, option1, ItemSupply";

                this.dsPRJ.ProjectsPart.Clear();
                var cmd = new System.Data.SqlClient.SqlCommand(SSQl, taPart.Connection);
                cmd.Parameters.Add("prj", SqlDbType.Int).Value = this.ProjectIndex;
                var da = new System.Data.SqlClient.SqlDataAdapter(cmd);
                this.dsReport1.PartSummary.Clear();
                da.Fill(this.dsReport1.PartSummary);

                //데이터를 회전해서 입력하고 소계를 넣는다
                string grp1 = "!";
                string grp2 = "!";

                decimal sum = 0;
                decimal sum2 = 0;
                decimal sum3 = 0;
                

                decimal sumn = 0;
                decimal sumn2 = 0;
                decimal sumn3 = 0;

                decimal sumb = 0;
                decimal sumb2 = 0;
                decimal sumb3 = 0;

                Boolean first = true;
                int grp2cnt = 0;
                int spanStart = 0;
                int spanStart2 = -1;
                int cnt = 0;

                this.fpSpread1_Sheet1.RowCount = 0;
                this.fpSpread1.SuspendLayout();
                foreach (dsReport.PartSummaryRow dr in dsReport1.PartSummary.Rows)
                {
                    sum3 += dr.amt;
                    sumn3 += dr.amtn;
                    sumb3 += dr.amtb;

                    if (first)
                    {
                        grp1 = dr.ItemGroup.Trim();
                        grp2 = dr.option1.Trim();

                        //한줄추가한다
                        this.fpSpread1_Sheet1.Rows.Add(this.fpSpread1_Sheet1.RowCount, 1);
                        this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 0].Value =  grp1;
                        this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 1].Value = grp2;
                        this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 2].Value = dr.ItemSupply;
                        this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 3].Value = dr.amt;
                        this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 4].Value = dr.amtn;
                        this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 5].Value = dr.amtb;
                        this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 1].HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Center;
                        sum = dr.amt;
                        sum2 = dr.amt;
                        

                        sumn = dr.amtn;
                        sumn2 = dr.amtn;

                        sumb = dr.amtb;
                        sumb2 = dr.amtb;

                        first = false;
                        grp2cnt = 0;
                    }
                    else
                    {
                        if (grp1 != dr.ItemGroup.Trim())
                        {

                            //소계항목이 남아있다면?
                            if (grp2cnt > 1)
                            {
                                this.fpSpread1_Sheet1.Rows.Add(this.fpSpread1_Sheet1.RowCount, 1);
                                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 0].Value = "";
                                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 1].Value = "소계(" + grp2 + ")";
                                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 2].Value = "";
                                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 3].Value = sum2;
                                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 4].Value = sumn2;
                                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 5].Value = sumb2;


                                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 1].BackColor = Color.FromArgb(240, 240, 240);
                                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 2].BackColor = Color.FromArgb(240, 240, 240);
                                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 3].BackColor = Color.FromArgb(240, 240, 240);
                                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 1].HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Center;
                                grp2cnt = 0;

                                //소분류가 변경되었으므로 처음 줄부터 지금 줄까지 스팬을 건다
                                if (spanStart2 != -1)
                                {
                                    var spanRows3 = (this.fpSpread1_Sheet1.RowCount - spanStart2 - 1);
                                    if (spanRows3 > 1)
                                    {
                                        this.fpSpread1_Sheet1.Cells[spanStart2, 1].RowSpan = spanRows3;
                                        this.fpSpread1_Sheet1.Cells[spanStart2, 1].HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Center;
                                    }
                                }


                            }

                            //중분류 합계를 추가한다
                            this.fpSpread1_Sheet1.Rows.Add(this.fpSpread1_Sheet1.RowCount, 1);
                            this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 0].Value = "합계(" + grp1 + ")";
                            this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 1].Value = "";
                            this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 2].Value = "";
                            this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 3].Value = sum;
                            this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 4].Value = sumn;
                            this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 5].Value = sumb;
                            this.fpSpread1_Sheet1.Rows[this.fpSpread1_Sheet1.RowCount - 1].BackColor = Color.FromArgb(220, 220, 220);

                            //중분류가 변경되었으므로 처음 줄부터 지금 줄까지 스팬을 건다
                            var spanRows = (this.fpSpread1_Sheet1.RowCount - spanStart - 1);
                            if(spanRows > 1)
                            {
                                this.fpSpread1_Sheet1.Cells[spanStart, 0].RowSpan = spanRows;
                                this.fpSpread1_Sheet1.Cells[spanStart, 0].HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Center;
                            }
                                
                            spanStart = this.fpSpread1_Sheet1.RowCount ;
                            spanStart2 = this.fpSpread1_Sheet1.RowCount;
                            sum = 0;
                            sum2 = 0;

                            sumn = 0;
                            sumn2 = 0;

                            sumb = 0;
                            sumb2 = 0;

                            grp1 = dr.ItemGroup.Trim();
                            grp2 = dr.option1.Trim();
                            grp2cnt = 0;
                        }

                        if (grp2 != dr.option1.Trim())
                        {
                            //소계를 추가한다
                            //그룹이 바뀌엇지만 항목이 1보다 클 때 처맇낟
                            if (grp2cnt > 1)
                            {
                                this.fpSpread1_Sheet1.Rows.Add(this.fpSpread1_Sheet1.RowCount, 1);
                                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 0].Value = "";
                                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 1].Value = "소계(" + grp2 + ")";// ;
                                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 2].Value = "";
                                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 3].Value = sum2;
                                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 4].Value = sumn2;
                                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 5].Value = sumb2;

                                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 1].BackColor = Color.FromArgb(240, 240, 240);
                                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 2].BackColor = Color.FromArgb(240, 240, 240);
                                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 3].BackColor = Color.FromArgb(240, 240, 240);
                                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 1].HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Center;
                                grp2cnt = 0;

                                //소분류가 변경되었으므로 처음 줄부터 지금 줄까지 스팬을 건다
                                if(spanStart2 != -1)
                                {
                                    var spanRows3 = (this.fpSpread1_Sheet1.RowCount - spanStart2 - 1);
                                    if (spanRows3 > 1)
                                    {
                                        this.fpSpread1_Sheet1.Cells[spanStart2, 1].RowSpan = spanRows3;
                                        this.fpSpread1_Sheet1.Cells[spanStart2, 1].HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Center;
                                    }
                                }
                            }
                            grp2cnt = 1;
                            spanStart2 = this.fpSpread1_Sheet1.RowCount;
                            sum2 = 0;
                            sumn2 = 0;
                            sumb2 = 0;
                            grp2 = dr.option1.Trim();
                            
                        }
                        else grp2cnt += 1;

                        this.fpSpread1_Sheet1.Rows.Add(this.fpSpread1_Sheet1.RowCount, 1);
                        this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 0].Value =   grp1;
                        this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 1].Value = grp2;
                        this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 2].Value = dr.ItemSupply;
                        this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 3].Value = dr.amt;
                        this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 4].Value = dr.amtn;
                        this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 5].Value = dr.amtb;

                        this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 1].HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Center;
                        cnt++;

                        sum += dr.amt;
                        sum2 += dr.amt;

                        sumn += dr.amtn;
                        sumn2 += dr.amtn;

                        sumb += dr.amtb;
                        sumb2 += dr.amtb;

                        //grp2cnt += 1;
                    }
                }


                //소계항목이 남아있다면?
                if (grp2cnt > 1)
                {
                    this.fpSpread1_Sheet1.Rows.Add(this.fpSpread1_Sheet1.RowCount, 1);
                    this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 0].Value = "";
                    this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 1].Value = "소계(" + grp2 + ")";
                    this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 2].Value = "";
                    this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 3].Value = sum2;
                    this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 4].Value = sumn2;
                    this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 5].Value = sumb2;

                    this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 1].BackColor = Color.FromArgb(240, 240, 240);
                    this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 2].BackColor = Color.FromArgb(240, 240, 240);
                    this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 3].BackColor = Color.FromArgb(240, 240, 240);
                    this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 1].HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Center;
                    grp2cnt = 0;

                    //소분류가 변경되었으므로 처음 줄부터 지금 줄까지 스팬을 건다
                    if (spanStart2 != -1)
                    {
                        var spanRows3 = (this.fpSpread1_Sheet1.RowCount - spanStart2 - 1);
                        if (spanRows3 > 1)
                        {
                            this.fpSpread1_Sheet1.Cells[spanStart2, 1].RowSpan = spanRows3;
                            this.fpSpread1_Sheet1.Cells[spanStart2, 1].HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Center;
                        }
                    }
                }



                //마지막 중분류 소계를 추가한다

                this.fpSpread1_Sheet1.Rows.Add(this.fpSpread1_Sheet1.RowCount, 1);
                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 0].Value = "합계(" + grp1 + ")";
                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 1].Value = "";
                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 2].Value = "";
                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 3].Value = sum;
                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 4].Value = sumn;
                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 5].Value = sumb;

                this.fpSpread1_Sheet1.Rows[this.fpSpread1_Sheet1.RowCount - 1].BackColor = Color.FromArgb(220, 220, 220);

                 var spanRows2 = (this.fpSpread1_Sheet1.RowCount - spanStart - 1);
                if (spanRows2 > 1)
                    this.fpSpread1_Sheet1.Cells[spanStart, 0].RowSpan = spanRows2;

                this.fpSpread1_Sheet1.Rows.Add(this.fpSpread1_Sheet1.RowCount, 1);
                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 0].Value = "총계";
                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 1].Value = "";
                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 2].Value = "";
                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 3].Value = sum3;
                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 4].Value = sumn3;
                this.fpSpread1_Sheet1.Cells[this.fpSpread1_Sheet1.RowCount - 1, 5].Value = sumb3;

                this.fpSpread1_Sheet1.Rows[this.fpSpread1_Sheet1.RowCount - 1].BackColor = Color.LightSkyBlue;
                
                this.fpSpread1.ResumeLayout();

            }
            catch (Exception ex)
            {
                FCOMMON.Util.MsgE(ex.Message);
            }


        }

        private void exportListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sd = new SaveFileDialog();
            sd.Filter = "excel|*.xls";
            var fn = nameTextBox.Text.Trim() + "_비용요약";
            sd.FileName = fn + ".xls";
            if (sd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                fpSpread1.SaveExcel(sd.FileName,
                    FarPoint.Excel.ExcelSaveFlags.SaveAsViewed
                    | FarPoint.Excel.ExcelSaveFlags.SaveAsFiltered
                    | FarPoint.Excel.ExcelSaveFlags.NoFormulas
                    | FarPoint.Excel.ExcelSaveFlags.SaveCustomColumnHeaders);
            }
        }
    }
}

