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.File;
using ABC.GiaoDich;
using ABC.Report;
using DevExpress.XtraReports.UI;
namespace ABC.BaoCao
{
    public partial class BaoCao_SoBaoCao_Si_Tra : DevExpress.XtraEditors.XtraUserControl
    {
        NhapSi_Class _NhapSi_Class = new NhapSi_Class();
        public BaoCao_SoBaoCao_Si_Tra()
        {
            InitializeComponent();
            //them
           
            //them
            event_click();
            gv_daluu.CellValueChanged+=new DevExpress.XtraGrid.Views.Base.CellValueChangedEventHandler(gv_daluu_CellValueChanged);

            txtngay.Text = Get_CauHinh.ngay_text;
            load_combo();
            this.Load += new EventHandler(BaoCao_SoBaoCao_Si_Tra_Load);
            //khai bai report truoc
          
        }

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

        void BaoCao_SoBaoCao_Si_Tra_FormClosed(object sender, FormClosedEventArgs e)
        {
            DataRow[] dr = DuLieu.List_KhachHang.Tables[0].Select("ID='' or ID is null");
            if (dr.Length > 0)
            {
                for (int d = 0; d < dr.Length; d++)
                {
                    DuLieu.List_KhachHang.Tables[0].Rows.Remove(dr[d]);
                }
                DuLieu.List_KhachHang.Tables[0].AcceptChanges();
            }
        }
        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,NHOM_NCC_ID from NhapSi_DaLuu group by ID_NCC,Tbl_NCC_Name,Date_Num_Final_UpDate,NHOM_NCC_ID having NHOM_NCC_ID = '080BB6A0-D01B-4F35-97CB-D32F680DE728' and 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,NHOM_NCC_ID from NhapSi_DaLuu group by ID_NCC,Tbl_NCC_Name,Date_Num_Final_UpDate,NHOM_NCC_ID having NHOM_NCC_ID = '080BB6A0-D01B-4F35-97CB-D32F680DE728' and 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];
            }
        }
        Label Thongkesoluong = new Label();
        Label thongketien = new Label();
        Label ThongkeGiaBQ = new Label();
        #region gv_daluu
        void gv_daluu_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (Pow.kiemtra("BaoCao_SoBaoCao_Si_Tra", "Sua", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                if (e.Column.FieldName != "Select")
                {
                    

                        #region
                        DataRow dt_update = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle);
                        string colum = e.Column.FieldName;
                        string value = e.Value.ToString();

                        DataRowView row_ncc = look_thuonglai_daluu.GetDataSourceRowByKeyValue(dt_update["Tbl_NCC_Name"].ToString()) as DataRowView;
                        string id_ncc = row_ncc["ID"].ToString().ToUpper();

                        DataRowView row_hang = look_loaica_daluu.GetDataSourceRowByKeyValue(dt_update["Tbl_Hang_Name"].ToString()) as DataRowView;
                        string id_hang = row_hang["ID"].ToString().ToUpper();


                        string id_kh;
                        if (e.Column.FieldName == "KH_View")
                        {

                            DataRowView row_khachhang = look_khachhang_daluu.GetDataSourceRowByKeyValue(dt_update["KH_View"].ToString()) as DataRowView;
                            id_kh = row_khachhang["ID"].ToString().ToUpper();
                            if (id_kh == null || id_kh=="")
                            {

                                for (int i = gv_daluu.FocusedRowHandle; i >= 0; i--)
                                {
#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                                    if (gv_daluu.GetDataRow(i)["KH_View"] != "        ''        ")
                                    {
                                        id_kh = gv_daluu.GetDataRow(i)["ID_KhachHang"].ToString();
                                        break;
                                    }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast
                                }
                            }

                        }
                        else
                        {
                            id_kh = dt_update["ID_KhachHang"].ToString();
                        }
                        //update id referent
                        #region
                        dt_update["ID_NCC"] = row_ncc["ID"].ToString().ToUpper();
                        dt_update["ID_KhachHang"] = id_kh;
                        dt_update["ID_Hang"] = row_hang["ID"].ToString().ToUpper();
                        #endregion

                        dt_update[colum] = value;
                        DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].AcceptChanges();
                        //cap nhat thanh tien
                        dt_update["ThanhTien"] = decimal.Parse(dt_update["SL"].ToString()) * decimal.Parse(dt_update["Gia"].ToString());
                        DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].AcceptChanges();
                        gv_daluu.RefreshData();

                        Thongkesoluong.Text = clum_sl.SummaryText;
                        thongketien.Text = clum_thanhtien.SummaryText;
                        ThongkeGiaBQ.Text = clum_Gia.SummaryText;

                        #region save to database
                        #region
                        //save and add vao muc du lieu da luu
                        string sql = "NhapSi ";
                        //  @STT
                        sql += "N'" + dt_update["STT"].ToString().ToUpper() + "',";
                        //,@ID
                        sql += "N'" + dt_update["ID"].ToString().ToUpper() + "',";
                        //,@ID_Hang
                        sql += "N'" + dt_update["ID_Hang"].ToString().ToUpper() + "',";
                        //,@ID_KhachHang
                        sql += "N'" + dt_update["ID_KhachHang"].ToString().ToUpper() + "',";
                        //,@ID_NCC
                        sql += "N'" + dt_update["ID_NCC"].ToString().ToUpper() + "',";
                        //,@Date_Num_Final_UpDate
                        sql += "N'" + dt_update["Date_Num_Final_UpDate"].ToString().ToUpper() + "',";
                        //,@User_ID_Final_UpDate
                        sql += "N'" + Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper() + "',";
                        //,@IS_User
                        sql += "N'1',";
                        //,@NguonGoc
                        sql += "N'SI',";
                        //,@SL
                        sql += "N'" + dt_update["SL"].ToString() + "',";
                        //,@Gia_0 --------SE THAY DOI VI LA GIA UU TIEN
                        sql += "N'" + dt_update["Gia"].ToString() + "',";
                        //,@Gia_1            
                        sql += "N'" + dt_update["Gia_1"].ToString() + "',";
                        //,@Gia_2
                        sql += "N'" + dt_update["Gia_2"].ToString() + "',";
                        //,@Sot
                        sql += "N'" + dt_update["Sot"].ToString() + "',";
                        //,@Ket
                        sql += "N'" + dt_update["Ket"].ToString() + "',";
                        //,@TienMat
                        sql += "N'0',";

                        //,@Toa_ID
                        sql += "Null,";
                        //,@Toa_SL
                        sql += "Null,";
                        //,@Toa_Gia
                        sql += "Null,";
                        //,@Toa_HHSL
                        sql += "Null,";
                        //,@Toa_HHPC
                        sql += "Null,";
                        //,@Toa_Xoa
                        sql += "Null,";
                        //,@_Text1
                        sql += "Null,";
                        //,@_Text2
                        sql += "Null,";
                        //,@_Text3
                        sql += "Null,";
                        //,@_Text4
                        sql += "Null,";
                        //,@_Text5
                        sql += "Null,";
                        //,@_Num1
                        sql += "Null,";
                        //,@_Num2
                        sql += "Null,";
                        //,@_Num3
                        sql += "Null,";
                        //,@_ID1
                        sql += "Null,";
                        //,@Lu
                        sql += "N'" + dt_update["Lu"].ToString() + "',";
                        //,@Cat
                        sql += "N'" + dt_update["Cat"].ToString() + "',";
                        //,@Bom
                        sql += "N'" + dt_update["Bom"].ToString() + "'";
                        _NhapSi_Class.set(sql);
                        #endregion
                        #endregion
                        #endregion
                   
                }
                else
                {
                    if ((bool)e.Value == true)
                    {
                        DataRow dr = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle);
                        dr["Select"] = true;
                        DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].AcceptChanges();
                    }
                    else
                    {
                        DataRow dr = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle);
                        dr["Select"] = false;
                        DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].AcceptChanges();
                    }
                    gv_daluu.RefreshData();
                }
            }
            else
            {
                gv_daluu.CancelUpdateCurrentRow();
            }

        }
        #endregion

        #region lệnh dùng chung
        public void excel()
        {
        }
        public void xoa()
        {
            if (Pow.kiemtra("BaoCao_SoBaoCao_Si_Tra", "Xoa", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                if (msgbox.dialog_yes_no("Xóa dòng chọn?") == DialogResult.Yes)
                {
              
                    DataRow[] dr = DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].Select("Select = " + true + "");
                    for (int i = 0; i < dr.Length; i++)
                    {
                        string sql = "NhapSi_Xoa '" + dr[i]["ID"].ToString().ToUpper() + "'";
                        _NhapSi_Class.set(sql);
                        DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].Rows.Remove(dr[i]);
                    }
                    gv_daluu.RefreshData();
                    Thongkesoluong.Text = clum_sl.SummaryText;
                    thongketien.Text = clum_thanhtien.SummaryText;
                    ThongkeGiaBQ.Text = clum_Gia.SummaryText;

                }
            }
        }
        BaoCao_SoBaoCao_Si_Tra_Edit _BaoCao_SoBaoCao_Si_Tra_Edit;
        public void sua()
        {
            if (Pow.kiemtra("BaoCao_SoBaoCao_Si_Tra", "Sua", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region
                _BaoCao_SoBaoCao_Si_Tra_Edit = new BaoCao_SoBaoCao_Si_Tra_Edit();
                _BaoCao_SoBaoCao_Si_Tra_Edit.FormClosed += new FormClosedEventHandler(_BaoCao_SoBaoCao_Si_Tra_Edit_FormClosed);
                _BaoCao_SoBaoCao_Si_Tra_Edit.ShowDialog();
                #endregion
            }
        }

        void _BaoCao_SoBaoCao_Si_Tra_Edit_FormClosed(object sender, FormClosedEventArgs e)
        {
            tailai();
        }
        public void chon()
        {
            for (int i = 0; i < DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].Rows.Count; i++)
            {
                DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].Rows[i]["Select"] = true;
            }
            DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].AcceptChanges();
            gv_daluu.RefreshData();
        }
        public void bochon()
        {
            for (int i = 0; i < DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].Rows.Count; i++)
            {
                DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].Rows[i]["Select"] = false;
            }
            DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].AcceptChanges();
            gv_daluu.RefreshData();
        }

        public void tailai()
        {
            string filter_string = "select * from NhapSi_DaLuu where khoa is null  and NHOM_NCC_ID = '080BB6A0-D01B-4F35-97CB-D32F680DE728'";
            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
                if (check_kytu_thuonglai.Checked == true && txtthuonglai.Text != null && txtthuonglai.Text != "")
                {
                    filter_string += " And Tbl_NCC_Name like '" + txtthuonglai.Text + "%'";
                }
            }
            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
                if (check_kytu_thuonglai.Checked == true && txtthuonglai.Text != null && txtthuonglai.Text != "")
                {
                    filter_string += " And Tbl_NCC_Name like '" + txtthuonglai.Text + "%'";
                }
            }
            filter_string += " order by Tbl_NCC_Name asc,Tbl_KhachHang_Name asc,Tbl_Hang_Name asc,Gia desc";
            DuLieu.BaoCao_SoBaoCao_Si_Tra = _NhapSi_Class.get_dataset(filter_string);
            DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].Columns.Add("Select", typeof(bool));
            DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].Columns.Add("Lever", typeof(int));
            DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].Columns.Add("KH_View");
            lever1("Tbl_KhachHang_Name","KH_View");
            //DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].Columns.Add("TL_View");
            //lever1("Tbl_NCC_Name", "TL_View");
            data();
        }
        public void lever1(string filename,string colum)
        {
            if (DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].Rows.Count > 0)
            {
                string lever1 = DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].Rows[0][filename].ToString();
                DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].Rows[0][colum] = lever1;
                DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].AcceptChanges();
                for (int i = 1; i < DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].Rows.Count; i++)
                {
                    if (lever1 == DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].Rows[i][filename].ToString())
                    {
                        //xoa trang          
                        DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].Rows[i][colum] = "        ''        ";
                        DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].AcceptChanges();
                        
                    }
                    else
                    {
                         lever1 = DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].Rows[i][filename].ToString();
                         DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].Rows[i][colum] = lever1;
                         DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].AcceptChanges();
                    }
                }
            }
        }
        public void print()
        {
           
            if (Pow.kiemtra("BaoCao_SoBaoCao_Si_Tra", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                tailai();
                #region
                //string filter_string = "select * from Print_BaoCaoSi_Tra where NHOM_NCC_ID = '080BB6A0-D01B-4F35-97CB-D32F680DE728'";
                //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 + "";
                //    if (check_ten_thuonglai.Checked == true && Look_tenlai.EditValue != null)
                //    {
                //        filter_string += " And ID_NCC = '" + Look_tenlai.EditValue.ToString().ToUpper() + "'";
                //    }
                //    if (check_kytu_thuonglai.Checked == true && txtthuonglai.Text != null && txtthuonglai.Text != "")
                //    {
                //        filter_string += " And Tbl_NCC_Name like '" + txtthuonglai.Text + "%'";
                //    }
                //}
                //else
                //{
                //    filter_string += "AND Date_Num_Final_UpDate = " + Get_CauHinh.Ngay_DN + "";
                //    if (check_ten_thuonglai.Checked == true && Look_tenlai.EditValue != null)
                //    {
                //        filter_string += " And ID_NCC = '" + Look_tenlai.EditValue.ToString().ToUpper() + "'";
                //    }
                //    if (check_kytu_thuonglai.Checked == true && txtthuonglai.Text != null && txtthuonglai.Text != "")
                //    {
                //        filter_string += " And Tbl_NCC_Name like '" + txtthuonglai.Text + "%'";
                //    }
                //}
                //DataSet dts = _NhapSi_Class.get_dataset(filter_string);
             
                //---------------------------------------

                Get_CauHinh.T_Baocaotonghop_tra = "BÁO CÁO NHẬP SĨ (TRA) NGÀY " + txtngay.Value.ToShortDateString();
                T_Baocaotonghop_tra rapport = new T_Baocaotonghop_tra();
                rapport.DataSource = DuLieu.BaoCao_SoBaoCao_Si_Tra;
                rapport.LocationFloat = new DevExpress.Utils.PointFloat(10, 10);
                rapport.ShowPreview();  
                #endregion
            }
        }
        public void print_bill()
        {
            if (Pow.kiemtra("BaoCao_SoBaoCao_Si_Tra", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region
              Get_CauHinh.T_Baocaotonghop_tra2 = "BÁO CÁO NHẬP SĨ (TRA) NGÀY " + txtngay.Value.ToShortDateString();
              T_baocao_tra rapport = new T_baocao_tra();
                rapport.DataSource = DuLieu.BaoCao_SoBaoCao_Si_Tra;
                rapport.LocationFloat = new DevExpress.Utils.PointFloat(10, 10);
                rapport.ShowPreview();  
                #endregion
                
            }
        }
        #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_sua.Click += new EventHandler(btn_sua_Click);
            btn_inso1.Click += new EventHandler(btn_inso1_Click);
        }

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

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

   

        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       
        public void data()
        {
            gc_daluu.DataSource = DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0];
            gv_daluu.ExpandAllGroups();
            gv_daluu.ExpandAllGroups();
            look_thuonglai_daluu.DataSource = DuLieu.List_NCC.Tables[0];
            look_loaica_daluu.DataSource = DuLieu.List_Hang.Tables[0];
            look_khachhang_daluu.DataSource = DuLieu.List_KhachHang.Tables[0];
            Thongkesoluong.Text = clum_sl.SummaryText;
            thongketien.Text = clum_thanhtien.SummaryText;
            ThongkeGiaBQ.Text = clum_Gia.SummaryText;
            load_combo();
        }
        #endregion

        private void BaoCao_ToaSi_Load(object sender, EventArgs e)
        {
            //string filter_string = "select * from NhapSi_DaLuu where Date_Num_Final_UpDate = " + Get_CauHinh.Ngay_DN + " And NHOM_NCC_ID = '080BB6A0-D01B-4F35-97CB-D32F680DE728'";

            //DuLieu.BaoCao_SoBaoCao_Si_Tra = _NhapSi_Class.get_dataset(filter_string);
            //DuLieu.BaoCao_SoBaoCao_Si_Tra.Tables[0].Columns.Add("Select", typeof(bool));
            //data();
            tailai();
        }

    }
}
