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 ABC.File;

namespace ABC.BaoCao
{
    public partial class BaoCao_ToaLe : DevExpress.XtraEditors.XtraUserControl
    {
        NhapSi_Class _NhapSi_Class = new NhapSi_Class();
        public BaoCao_ToaLe()
        {
            InitializeComponent();            
           // data();
            event_click();
            gv_daluu.CellValueChanged+=new DevExpress.XtraGrid.Views.Base.CellValueChangedEventHandler(gv_daluu_CellValueChanged);
            gv_chitiet.CellValueChanged += new DevExpress.XtraGrid.Views.Base.CellValueChangedEventHandler(gv_chitiet_CellValueChanged);
            link_xoachitiet.Click += new EventHandler(link_xoachitiet_Click);
            //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
            
        }

        void link_xoachitiet_Click(object sender, EventArgs e)
        {
            xoa_chitiet();
        }
        #region hamg xóa 1 dòng trong chi tiết
        public void xoa_chitiet()
        {
            if (Pow.kiemtra("BaoCao_ToaLe", "Xoa", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {

                DevExpress.XtraGrid.Views.Base.ColumnView bw = (DevExpress.XtraGrid.Views.Base.ColumnView)gc_daluu.FocusedView;
                if (bw.IsDetailView)
                {
                    int detailrowhandle = bw.FocusedRowHandle;
                    DataRow dr1 = bw.GetDataRow(detailrowhandle);
                    string ID = dr1["ID"].ToString();
                    select_dong = gv_daluu.FocusedRowHandle;
                    if (msgbox.dialog_yes_no("Xóa dòng chọn ?") == DialogResult.Yes)
                    {
                        string sql = "";
                        if (dr1["ID_KhachHang"].ToString().Length > 10)
                        {
                            sql += "N'" + dr1["ID_KhachHang"].ToString().ToUpper() + "',";
                            sql = "NhapLe_Xoa_ChiTiet '" + ID + "','" + dr1["ID_KhachHang"].ToString().ToUpper() + "','" + dr1["_ID_HD_LE"].ToString().ToUpper() + "','" + dr1["Date_Num_Final_UpDate"].ToString().ToUpper() + "'";
                        }
                        else
                        {
                            sql = "NhapLe_Xoa_ChiTiet '" + ID + "',Null,'" + dr1["_ID_HD_LE"].ToString().ToUpper() + "','" + dr1["Date_Num_Final_UpDate"].ToString().ToUpper() + "'";
                        }
                        db.T_exc(sql);
                        tailai();
                    }
                }
            }
            else
            {
                gv_daluu.CancelUpdateCurrentRow();
            }


        }
        #endregion
        
        #region edit chitiet
        int select_dong;
        void gv_chitiet_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
           
                select_dong = gv_daluu.FocusedRowHandle;
                if (Pow.kiemtra("BaoCao_ToaLe", "Sua", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
                {
                    #region
                    DevExpress.XtraGrid.Views.Base.ColumnView bw = (DevExpress.XtraGrid.Views.Base.ColumnView)gc_daluu.FocusedView;
                    if (bw.IsDetailView)
                    {
                        int detailrowhandle = bw.FocusedRowHandle;
                        DataRow dr1 = bw.GetDataRow(detailrowhandle);

                        DataRowView row = look_loaica_daluu.GetDataSourceRowByKeyValue(dr1["Tbl_Hang_Name"].ToString()) as DataRowView;
                        DataRowView row_ncc = look_thuonglai_daluu.GetDataSourceRowByKeyValue(dr1["Tbl_NCC_Name"].ToString()) as DataRowView;


                        #region
                        try
                        {
                            #region
                            string sql = "Tbl_GIAODICH_LE_insert ";
                            // @STT INT,
                            sql += "N'" + dr1["STT"].ToString() + "',";
                            //@ID uniqueidentifier,
                            sql += "N'" + dr1["ID"].ToString().ToUpper() + "',";
                            //@ID_Hang uniqueidentifier,
                            sql += "N'" + row["ID"].ToString().ToUpper() + "',";
                            //@ID_KhachHang uniqueidentifier,
                            if (dr1["ID_KhachHang"].ToString().Length > 10)
                            {
                                sql += "N'" + dr1["ID_KhachHang"].ToString().ToUpper() + "',";
                            }
                            else
                            {
                                sql += "Null,";
                            }
                            //@ID_NCC uniqueidentifier,
                            sql += "N'" + row_ncc["ID"].ToString() + "',";

                            //@Date_Num_Final_UpDate int,
                            sql += "N'" + Get_CauHinh.Ngay_DN.ToString() + "',";
                            //@User_ID_Final_UpDate uniqueidentifier,
                            sql += "N'" + Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper() + "',";
                            //@IS_User  bit,
                            sql += "N'1',";
                            //@NguonGoc nvarchar(100),
                            sql += "N'LE',";


                            //@SL decimal,
                            sql += "N'" + dr1["SL"].ToString() + "',";
                            //@Gia_0 decimal,
                            sql += "Null,";
                            //@Gia_1 decimal,
                            sql += "N'" + dr1["Gia_1"].ToString() + "',";
                            //@Gia_2 decimal,
                            sql += "Null,";
                            //@Sot int,
                            sql += "Null,";
                            //@Ket int,
                            sql += "Null,";
                            //@TienMat decimal,
                            sql += "Null,";

                            //@Toa_ID uniqueidentifier,
                            sql += "Null,";
                            //@Toa_SL decimal,
                            sql += "Null,";
                            //@Toa_Gia decimal,
                            sql += "Null,";
                            //@Toa_HHSL decimal,
                            sql += "Null,";
                            //@Toa_HHPC decimal,
                            sql += "Null,";
                            //@Toa_Xoa bit,
                            sql += "Null,";

                            //@_Text1 nvarchar(100),
                            sql += "Null,";
                            //@_Text2 nvarchar(100),
                            sql += "Null,";
                            //@_Text3 nvarchar(100),
                            sql += "Null,";
                            //@_Text4 nvarchar(100),
                            sql += "Null,";
                            //@_Text5 nvarchar(100),
                            sql += "Null,";


                            //@_Num1 decimal,
                            sql += "N'" + dr1["_Num1"].ToString() + "',";
                            //@_Num2 decimal,

                            sql += "N'" + dr1["_Num2"].ToString() + "',";


                            //@_Num3 decimal,
                            sql += "Null,";
                            //@_ID1 uniqueidentifier,
                            sql += "Null,";

                            //@_SoHD_LE int,
                            sql += "N'" + dr1["_SoHD_LE"].ToString() + "',";
                            //@_ID_HD_LE uniqueidentifier,
                            sql += "N'" + dr1["_ID_HD_LE"].ToString() + "',";
                            //@_Node_LE nvarchar(100),
                            sql += "N'" + dr1["_Node_LE"].ToString() + "',";
                            //@_ID_Lai_TienMat uniqueidentifier
                            sql += "Null,"; //kiem tran duoi sql
                            //,@Lu
                            sql += "Null,";
                            //,@Cat
                            sql += "Null,";
                            //,@Bom
                            sql += "Null";
                            //toa la lay 2 cot cong lai
                            db.T_exc(sql);
                            #endregion
                        }
                        catch
                        {
                        }
                        #endregion

                        tailai();
                    }
                    #endregion
                }
                else
                {
                    gv_chitiet.CancelUpdateCurrentRow();
                }
            
            
        }
        #endregion

        #region gv_daluu
        void gv_daluu_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (Pow.kiemtra("BaoCao_ToaLe", "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();
                    if (colum == "Tbl_KhachHang_Name")
                    {
                        DataRowView row_khachhang = look_khachhang_daluu.GetDataSourceRowByKeyValue(dt_update["Tbl_KhachHang_Name"].ToString()) as DataRowView;
                        if (row_khachhang != null)
                        {
                            if (gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_KhachHang_Truoc"].ToString() == null || gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_KhachHang_Truoc"].ToString() == "")
                            {
                                string sql = "NhapLe_Edit_Toa '" + dt_update["_ID_HD_LE"].ToString().ToUpper() + "','" + row_khachhang["ID"].ToString().ToUpper() + "','" + dt_update["TienMat"].ToString().Replace(",", "") + "','" + dt_update["Date_Num_Final_UpDate"].ToString() + "',Null";
                                db.T_exc(sql);
                            }
                            else
                            {
                                string sql = "NhapLe_Edit_Toa '" + dt_update["_ID_HD_LE"].ToString().ToUpper() + "','" + row_khachhang["ID"].ToString().ToUpper() + "','" + dt_update["TienMat"].ToString().Replace(",", "") + "','" + dt_update["Date_Num_Final_UpDate"].ToString() + "','" + gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_KhachHang_Truoc"].ToString().ToUpper() + "'";
                                db.T_exc(sql);
                            }
                          
                        }
                        else
                        {
                        }
                    }
                    else// cap nhat tien mat
                    {
                        DataRowView row_khachhang = look_khachhang_daluu.GetDataSourceRowByKeyValue(dt_update["Tbl_KhachHang_Name"].ToString()) as DataRowView;
                        if (row_khachhang != null)
                        {

                            string sql = "NhapLe_Edit_Toa '" + dt_update["_ID_HD_LE"].ToString().ToUpper() + "','" + row_khachhang["ID"].ToString().ToUpper() + "','" + dt_update["TienMat"].ToString().Replace(",", "") + "','" + dt_update["Date_Num_Final_UpDate"].ToString() + "','" + gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_KhachHang_Truoc"].ToString().ToUpper() + "'";
                            db.T_exc(sql);
                        }
                        else
                        {
                            // toa vang lai khoong cho phep cap nhat tien mat
                            msgbox.dialog_OK_info("Không cập nhật tiền mặt của khách Vãng Lai.");
                        }
                    }

                    tailai();
                    #endregion
                }
                else
                {
                   // int key = 0;
                    string _ToaLe = gv_daluu.GetRowCellValue(gv_daluu.FocusedRowHandle,"ToaLe").ToString().Trim();
                    for (int i = 0; i < DuLieu.ToaLe_Full.Tables[0].Rows.Count; i++)
                    {
                        if (DuLieu.ToaLe_Full.Tables[0].Rows[i]["ToaLe"].ToString()== _ToaLe)
                        {
                            if ((bool)e.Value == true)
                                DuLieu.ToaLe_Full.Tables[0].Rows[i]["Select"] = true;
                            else
                                DuLieu.ToaLe_Full.Tables[0].Rows[i]["Select"] = false;
                            break;
                        }    
                    }
                    //if ((bool)e.Value == true)
                    //{
                    //    //DataRow dr = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle);
                    //   // dr["Select"] = true;
                    //    DuLieu.ToaLe_Full.Tables[0].Rows[gv_daluu.FocusedRowHandle]["Select"] = true;
                    //    DuLieu.ToaLe_Full.Tables[0].AcceptChanges();
                    //}
                    //else
                    //{
                    //   // DataRow dr = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle);
                    //  //  dr["Select"] = false;
                    //    DuLieu.ToaLe_Full.Tables[0].Rows[gv_daluu.FocusedRowHandle]["Select"] = false;
                    //    DuLieu.ToaLe_Full.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_ToaLe", "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.ToaLe_Full.Tables[0].Select("Select = " + true + "");
                    for (int i = 0; i < dr.Length; i++)
                    {
                        string sql = "NhapLe_Xoa_Toa '" + dr[i]["_ID_HD_LE"].ToString().ToUpper() + "',";
                        if (dr[i]["ID_KhachHang"].ToString().Length > 10)
                        {
                            sql += "N'" + dr[i]["ID_KhachHang"].ToString().ToUpper() + "',";
                            
                        }
                        else
                        {
                            sql+="Null,";
                        }
                        sql+="'" + dr[i]["Date_Num_Final_UpDate"].ToString() + "'";
                        db.T_exc(sql);
                    }
                    tailai();

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

        public void tailai()
        {
            //load hoa don
            #region
            string filter_string = "Hoadon_le_Full " + Get_CauHinh.Ngay_DN + ",";
            if (check_ten_khachhang.Checked == true && look_ten_kh.EditValue != null)
            {
                filter_string += "'" + look_ten_kh.EditValue.ToString().ToUpper() + "',";
            }
            else
            {
                filter_string += "Null,";
            }
            if (check_kytu_khachhang.Checked == true && txtkh.Text != null && txtkh.Text != "")
            {
                filter_string += " '" + txtkh.Text + "%'";
            }
            else
            {
                filter_string += "Null";
            }

            #endregion
            dt1= db.T_opendataset(filter_string).Tables[0];
            dt1.Columns.Add("Select",typeof(bool));
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                dt1.Rows[i]["Select"] = false;
            }
           // dt1.Columns.Add("_ID_HD_LE");
            DuLieu.ToaLe_Full = db.T_opendataset(filter_string);
            DuLieu.ToaLe_Full.Tables[0].Columns.Add("Select", typeof(bool));
            DuLieu.ToaLe_Full.Relations.Add("Orders",
            DuLieu.ToaLe_Full.Tables[0].Columns["_ID_HD_LE"],
            DuLieu.ToaLe_Full.Tables[1].Columns["_ID_HD_LE"]);
            data();
        }
        Class_KetNoi db = new Class_KetNoi();
        public void print()
        {
           
            if (Pow.kiemtra("BaoCao_ToaLe", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
               
            }
        }
        public void print_bill()
        {
            if (Pow.kiemtra("BaoCao_ToaLe", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
               
            }
        }
        #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)
        {
            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
        DataTable dt1;
        #region data    
        Label Thongkesoluong = new Label();
        Label thongketien = new Label();
        Label Thongketienmat = new Label();
        Label Thongkeno = new Label();
        public void data()
        {
            DataTable dt = dt1;
            DataView view = dt.DefaultView;
            view.RowFilter = "khoa is null";
            gc_daluu.DataSource = view.ToTable(); 
           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];
            gv_daluu.CollapseAllDetails();            
            gv_daluu.ExpandMasterRow(select_dong);
            Thongkesoluong.Text = clum_sl.SummaryText;
            thongketien.Text = clum_tongtien.SummaryText;
            Thongketienmat.Text = clum_tienmat.SummaryText;
            Thongkeno.Text = clum_tienno.SummaryText;
        }
        #endregion

        private void BaoCao_ToaSi_Load(object sender, EventArgs e)
        {
            tailai();
            data();
        }

        private void gv_daluu_CustomRowCellEditForEditing(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
        {
           
        }

        private void gv_daluu_CellValueChanged_1(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
           /* try
            {
                if (e.Column.FieldName == "Select")
                {
                    bool chk = bool.Parse(gv_daluu.GetFocusedRowCellValue("Select").ToString());
                    if (chk)
                        DuLieu.ToaLe_Full.Tables[0].Rows[gv_daluu.FocusedRowHandle]["Select"] = true;
                    else
                        DuLieu.ToaLe_Full.Tables[0].Rows[gv_daluu.FocusedRowHandle]["Select"] = false;
                }

            }
            catch (Exception)
            {
            }
           */
        }
    }
}
