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;
using DevExpress.XtraSplashScreen;

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

            lool_nhomLai.Properties.DataSource = DuLieu.List_Nhom_NCC.Tables[0];
            Look_nhom_khachhang.Properties.DataSource = DuLieu.List_Nhom_KhachHang.Tables[0];
            Look_nhomhang.Properties.DataSource = DuLieu.List_Nhom_Hang.Tables[0];

            Look_tenlai.Properties.DataSource = DuLieu.List_NCC.Tables[0];
            look_ten_kh.Properties.DataSource = DuLieu.List_KhachHang.Tables[0];
            look_tenhang.Properties.DataSource = DuLieu.List_Hang.Tables[0];
            //khai bai report truoc
            
        }
        Label Thongkesoluong = new Label();
        Label thongketien = new Label();
        Label Thongkesot = new Label();
        Label Thongkeket = new Label();
        #region gv_daluu
        void gv_daluu_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (Pow.kiemtra("BaoCao_ToaSi", "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;
                    DataRowView row_khachhang = look_khachhang_daluu.GetDataSourceRowByKeyValue(dt_update["Tbl_KhachHang_Name"].ToString()) as DataRowView;
                    DataRowView row_hang = look_loaica_daluu.GetDataSourceRowByKeyValue(dt_update["Tbl_Hang_Name"].ToString()) as DataRowView;

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

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

                    Thongkesoluong.Text = clum_sl.SummaryText;
                    thongketien.Text = clum_thanhtien.SummaryText;
                    Thongkesot.Text = clum_sot.SummaryText;
                    Thongkeket.Text = clum_ket.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_ToaSi.Tables[0].AcceptChanges();
                    }
                    else
                    {
                        DataRow dr = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle);
                        dr["Select"] = false;
                        DuLieu.BaoCao_ToaSi.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_ToaSi", "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_ToaSi.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_ToaSi.Tables[0].Rows.Remove(dr[i]);
                    }
                    gv_daluu.RefreshData();
                    Thongkesoluong.Text = clum_sl.SummaryText;
                    thongketien.Text = clum_thanhtien.SummaryText;
                    Thongkesot.Text = clum_sot.SummaryText;
                    Thongkeket.Text = clum_ket.SummaryText;

                }
            }
        }
        public void sua()
        {
        }
        public void chon()
        {
            for (int i = 0; i < DuLieu.BaoCao_ToaSi.Tables[0].Rows.Count; i++)
            {
                DuLieu.BaoCao_ToaSi.Tables[0].Rows[i]["Select"] = true;
            }
            DuLieu.BaoCao_ToaSi.Tables[0].AcceptChanges();
            gv_daluu.RefreshData();
        }
        public void bochon()
        {
            for (int i = 0; i < DuLieu.BaoCao_ToaSi.Tables[0].Rows.Count; i++)
            {
                DuLieu.BaoCao_ToaSi.Tables[0].Rows[i]["Select"] = false;
            }
            DuLieu.BaoCao_ToaSi.Tables[0].AcceptChanges();
            gv_daluu.RefreshData();
        }

        public void tailai()
        {
           // _NhapSi_Class.get_dataset("select * from NhapSi_DaLuu where Date_Num_Final_UpDate = " + Get_CauHinh.Ngay_DN + "");
            string filter_string = "select * from NhapSi_DaLuu where khoa is null 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_ten_khachhang.Checked == true && look_ten_kh.EditValue != null)
            {
                filter_string += " And ID_KhachHang = '" + look_ten_kh.EditValue.ToString().ToUpper() + "'";
            }
            if (check_tenhang.Checked == true && look_tenhang.EditValue != null)
            {
                filter_string += " And ID_Hang = '" + look_tenhang.EditValue.ToString().ToUpper() + "'";
            }
            //NHOM
            if (check_nhom_lai.Checked == true && lool_nhomLai.EditValue != null)
            {
                filter_string += " And NHOM_NCC_ID = '" + lool_nhomLai.EditValue.ToString().ToUpper() + "'";
            }
            if (check_nhom_khachhang.Checked == true && Look_nhom_khachhang.EditValue != null)
            {
                filter_string += " And NHOM_KHACHHANG_ID = '" + Look_nhom_khachhang.EditValue.ToString().ToUpper() + "'";
            }
            if (check_nhom_hang.Checked == true && Look_nhomhang.EditValue != null)
            {
                filter_string += " And NHOM_HANG_ID = '" + Look_nhomhang.EditValue.ToString().ToUpper() + "'";
            }
            //KY TU
            if (check_kytu_thuonglai.Checked == true && txtthuonglai.Text != null && txtthuonglai.Text != "")
            {
                filter_string += " And Tbl_NCC_Name like '" + txtthuonglai.Text + "%'";
            }
            if (check_kytu_khachhang.Checked == true && txtkh.Text != null && txtkh.Text != "")
            {
                filter_string += " And Tbl_KhachHang_Name like '" + txtkh.Text + "%'";
            }
            if (check_kytu_hang.Checked == true && txthang.Text != null && txthang.Text != "")
            {
                filter_string += " And Tbl_Hang_Name like '" + txthang.Text + "%'";
            }

            DuLieu.BaoCao_ToaSi = _NhapSi_Class.get_dataset(filter_string);
            DuLieu.BaoCao_ToaSi.Tables[0].Columns.Add("Select", typeof(bool));
            data();
        }
        public void print()
        {
           
            if (Pow.kiemtra("BaoCao_ToaSi", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region
                MessageBoxManager.OK = "In Thường";
                MessageBoxManager.Cancel = "In Chi Tiết";
                MessageBoxManager.Register();
                DialogResult da = MessageBox.Show("Chọn kiểu in", "Phần mềm quản lý vựa", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                DataSet dsSelectData = new DataSet();
                if (da == DialogResult.Cancel)
                {
                    dsSelectData = DuLieu.BaoCao_ToaSi;
                }
                else
                {
                    dsSelectData = DuLieu.BaoCao_ToaSi;
                    dsSelectData.Tables[0].Columns.Remove("ThanhTien");

                }
                MessageBoxManager.Unregister();
                T_PhieuXuat rapport = new T_PhieuXuat();
                rapport.DataSource = dsSelectData;
                rapport.ShowRibbonPreview();

                #endregion
            }
        }
        public void print_bill()
        {
            if (Pow.kiemtra("BaoCao_ToaSi", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region
                T_bill rapport = new T_bill();
                rapport.DataSource = DuLieu.BaoCao_ToaSi;
                rapport.ShowRibbonPreview();
                #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_bill.Click += new EventHandler(btn_bill_Click);
        }

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

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

        void btn_load_Click(object sender, EventArgs e)
        {
            SplashScreenManager.ShowForm(typeof(SplashScreen1));
            tailai();
            SplashScreenManager.CloseForm();
        }

        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()
        {
            if (DuLieu.BaoCao_ToaSi != null)
            {
                gc_daluu.DataSource = DuLieu.BaoCao_ToaSi.Tables[0];
                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;
                Thongkesot.Text = clum_sot.SummaryText;
                Thongkeket.Text = clum_ket.SummaryText;
            }
        }
        #endregion

        private void BaoCao_ToaSi_Load(object sender, EventArgs e)
        {
            DuLieu.BaoCao_ToaSi = DuLieu.List_NhapSi_DaLuu;
            data();
        }

    }
}
