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;

using ABC.GiaoDich;
using ABC.Report;
using ABC.KetNoi;
using DevExpress.XtraReports.UI;
using ABC.File;

namespace ABC.BaoCao
{
    public partial class BaoCao_SoBaoCao_Si_Le : DevExpress.XtraEditors.XtraUserControl
    {
        NhapSi_Class _NhapSi_Class = new NhapSi_Class();
        public BaoCao_SoBaoCao_Si_Le()
        {
            InitializeComponent();            
           // data();
            event_click();
           // gv_daluu.CellValueChanged+=new DevExpress.XtraGrid.Views.Base.CellValueChangedEventHandler(gv_daluu_CellValueChanged);

            txtngay.Text = Get_CauHinh.ngay_text;  
            Look_tenlai.Properties.DataSource = DuLieu.List_NCC.Tables[0];
            lool_nhomLai.Properties.DataSource = DuLieu.List_Nhom_NCC.Tables[0];
            this.Load += new EventHandler(BaoCao_SoBaoCao_Si_Le_Load);
            //khai bai report truoc
            
        }

        void BaoCao_SoBaoCao_Si_Le_Load(object sender, EventArgs e)
        {
            DataRow[] dr = DuLieu.List_Hang.Tables[0].Select("ID='' or ID is null");
            if (dr.Length == 0)
            {
                DataRow dr_new = DuLieu.List_Hang.Tables[0].NewRow();
                dr_new["Name"] = "        ''        ";
                DuLieu.List_Hang.Tables[0].Rows.Add(dr_new);
            }
            //this.FormClosed += new FormClosedEventHandler(BaoCao_SoBaoCao_Si_Le_FormClosed);
            tailai();
            
        }

        void BaoCao_SoBaoCao_Si_Le_FormClosed(object sender, FormClosedEventArgs e)
        {
            DataRow[] dr = DuLieu.List_Hang.Tables[0].Select("ID='' or ID is null");
            if (dr.Length > 0)
            {
                for (int d = 0; d < dr.Length; d++)
                {
                    DuLieu.List_Hang.Tables[0].Rows.Remove(dr[d]);
                }
                DuLieu.List_Hang.Tables[0].AcceptChanges();
            }
        }

       

        #region lệnh dùng chung
        public void excel()
        {
        }
        public void xoa()
        {
            
        }
        
        public void sua()
        {
            
        }

        void _BaoCao_SoBaoCao_Si_Bien_Edit_FormClosed(object sender, FormClosedEventArgs e)
        {
            tailai();
        }
        public void chon()
        {
          
        }
        public void bochon()
        {
          
        }
        public void load_combo()
        {
            if (check_ngay.Checked == true)
            {
                int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;

                string load_tl = "select ID_NCC as ID,Tbl_NCC_Name as Name,Date_Num_Final_UpDate from DuLieu_SI_LE_Extend group by ID_NCC,Tbl_NCC_Name,Date_Num_Final_UpDate having  Date_Num_Final_UpDate = " + ng + " ORDER BY Tbl_NCC_Name asc";
                DuLieu.List_NCC_Tra = _NhapSi_Class.get_dataset(load_tl);
                Look_tenlai.Properties.DataSource = DuLieu.List_NCC_Tra.Tables[0];
            }
            else
            {
                string load_tl = "select ID_NCC as ID,Tbl_NCC_Name as Name,Date_Num_Final_UpDate from DuLieu_SI_LE_Extend group by ID_NCC,Tbl_NCC_Name,Date_Num_Final_UpDate having Date_Num_Final_UpDate = " + Get_CauHinh.Ngay_DN + " ORDER BY Tbl_NCC_Name asc";
                DuLieu.List_NCC_Tra = _NhapSi_Class.get_dataset(load_tl);
                Look_tenlai.Properties.DataSource = DuLieu.List_NCC_Tra.Tables[0];
            }
        }

        public void tailai()
        {
            string filter_string = "select * from DuLieu_SI_LE_Extend where Date_Num_Final_UpDate >0 ";
            if (check_ngay.Checked == true)
            {
                int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
                filter_string += "AND Date_Num_Final_UpDate = " + ng + "";
#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (check_ten_thuonglai.Checked == true && Look_tenlai.EditValue != null && Look_tenlai.EditValue != "")
                {
                    string nhomchon = "'" + Look_tenlai.Properties.GetCheckedItems().ToString().Replace(", ", "','") + "'";

                    filter_string += " And ID_NCC IN (" + nhomchon + ")";
                }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast
               
#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (check_nhom_lai.Checked == true && lool_nhomLai.EditValue != null && lool_nhomLai.EditValue != "")
                {
                    filter_string += " And NHOM_NCC_ID = '" + lool_nhomLai.EditValue.ToString().ToUpper() + "'";
                }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast
            }
            else
            {
                filter_string += "AND Date_Num_Final_UpDate = " + Get_CauHinh.Ngay_DN + "";
#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (check_ten_thuonglai.Checked == true && Look_tenlai.EditValue != null && Look_tenlai.EditValue != "")
                {
                    string nhomchon = "'" + Look_tenlai.Properties.GetCheckedItems().ToString().Replace(", ", "','") + "'";

                    filter_string += " And ID_NCC IN (" + nhomchon + ")";
                }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast
               
#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (check_nhom_lai.Checked == true && lool_nhomLai.EditValue != null && lool_nhomLai.EditValue != "")
                {
                    filter_string += " And NHOM_NCC_ID = '" + lool_nhomLai.EditValue.ToString().ToUpper() + "'";
                }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast
            }
            filter_string += " and  (Date_Num_Final_UpDate in(select Date_Num_Final_UpDate from Tbl_GIAODICH where khoa is null) or ";
            filter_string += " Date_Num_Final_UpDate in(select Date_Num_Final_UpDate from Tbl_GIAODICH_LE where khoa is null)) ";
            filter_string += " order by Tbl_NCC_Name asc,Tbl_Hang_Name asc,Tbl_KhachHang_Name asc,Gia desc";
            DuLieu.BaoCaoSi_Le = _NhapSi_Class.get_dataset(filter_string);
            DuLieu.BaoCaoSi_Le.Tables[0].Columns.Add("CA_View");
            //lever1("Tbl_Hang_Name", "CA_View");
            // Duyệt qua từng hàng và gán giá trị từ cột "Tbl_Hang_Name" vào cột "CA_View"
            foreach (DataRow row in DuLieu.BaoCaoSi_Le.Tables[0].Rows)
            {
                row["CA_View"] = row["Tbl_Hang_Name"];
            }

            data();
        }
        public void lever1(string filename, string colum)
        {
            if (DuLieu.BaoCaoSi_Le.Tables[0].Rows.Count > 0)
            {
                string lever1 = DuLieu.BaoCaoSi_Le.Tables[0].Rows[0][filename].ToString();
                DuLieu.BaoCaoSi_Le.Tables[0].Rows[0][colum] = lever1;
                DuLieu.BaoCaoSi_Le.Tables[0].AcceptChanges();
                for (int i = 1; i < DuLieu.BaoCaoSi_Le.Tables[0].Rows.Count; i++)
                {
                    if (lever1 == DuLieu.BaoCaoSi_Le.Tables[0].Rows[i][filename].ToString())
                    {
                        //xoa trang    
                        DuLieu.BaoCaoSi_Le.Tables[0].Rows[i][colum] = "        ''        ";
                        DuLieu.BaoCaoSi_Le.Tables[0].AcceptChanges();

                    }
                    else
                    {
                        lever1 = DuLieu.BaoCaoSi_Le.Tables[0].Rows[i][filename].ToString();
                        DuLieu.BaoCaoSi_Le.Tables[0].Rows[i][colum] = lever1;
                        DuLieu.BaoCaoSi_Le.Tables[0].AcceptChanges();
                    }
                }
            }
        }
        public void print()
        {
           
            if (Pow.kiemtra("BaoCao_SoBaoCao_Si_Le", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region
                
                //------------------



                Get_CauHinh.T_Baocaotonghop_2 = "SỔ BÁO CÁO BÁN SĨ & LẼ NGÀY " + txtngay.Value.ToShortDateString();
                T_Baocaotonghop2 rapport = new T_Baocaotonghop2();
                rapport.DataSource = DuLieu.BaoCaoSi_Le;
                rapport.LocationFloat = new DevExpress.Utils.PointFloat(10, 10);
                rapport.ShowRibbonPreview();

                #endregion
            }
        }
        public void print_bill()// in tong hop
        {
            if (Pow.kiemtra("BaoCao_SoBaoCao_Si_Le", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                Get_CauHinh.T_Baocaotonghop_2 = "SỔ BÁO CÁO BÁN SĨ & LẼ NGÀY " + txtngay.Value.ToShortDateString();
                T_baocao_si_le rapport = new T_baocao_si_le();
                rapport.DataSource = DuLieu.BaoCaoSi_Le.Tables[0];
                rapport.ShowRibbonPreview();
            }
        }
        #endregion

        #region menu click
        public void event_click()
        {
           
            //btn_del.Click += new EventHandler(btn_del_Click);          
            //btn_all.Click += new EventHandler(btn_all_Click);
            //btn_none.Click += new EventHandler(btn_none_Click);
           // btn_close.Click += new EventHandler(btn_close_Click);
            btn_load.Click += new EventHandler(btn_load_Click);
            btn_in.Click += new EventHandler(btn_in_Click);
            btn_inso1.Click += new EventHandler(btn_inso1_Click);
           // btn_sua.Click += new EventHandler(btn_sua_Click);
        }

        void btn_inso1_Click(object sender, EventArgs e)
        {
            print_bill();
        }

        void btn_sua_Click(object sender, EventArgs e)
        {
            print_bill();
        }

   

        void btn_in_Click(object sender, EventArgs e)
        {
            print();
        }

        void btn_load_Click(object sender, EventArgs e)
        {
            tailai();
        }

        void btn_close_Click(object sender, EventArgs e)
        {
          //  this.Close();
        }

        void btn_none_Click(object sender, EventArgs e)
        {
            bochon();
        }

        void btn_all_Click(object sender, EventArgs e)
        {
            chon();
        } 

        void btn_del_Click(object sender, EventArgs e)
        {
            xoa();
        }




        #endregion

        #region data       
        Label Thongkesoluong = new Label();
        Label thongketien = new Label();
        Label ThongkeGiaBQ = new Label();
        public void data()
        {
            load_combo();
            gc_daluu.DataSource = DuLieu.BaoCaoSi_Le.Tables[0];
            gv_daluu.ExpandAllGroups();
            gv_daluu.ExpandAllGroups();
   
            Thongkesoluong.Text = clum_sl.SummaryText;
            thongketien.Text = clum_thanhtien.SummaryText ;
            ThongkeGiaBQ.Text = clum_Gia.SummaryText;
            
            
        }






        #endregion

        private void gv_daluu_DoubleClick(object sender, EventArgs e)
        {
           
        }
    }
}
