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;


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

          
          
            //khai bai report truoc
            
        }

        #region gv_daluu
        Label Thongkesrecord = new Label();

        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_SoBaoCao_Si_Bien.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_Bien.Tables[0].AcceptChanges();
                    gv_daluu.RefreshData();

                    Thongkesrecord.Text = stt_daluu.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";
                    _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_Bien.Tables[0].AcceptChanges();
                    }
                    else
                    {
                        DataRow dr = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle);
                        dr["Select"] = false;
                        DuLieu.BaoCao_SoBaoCao_Si_Bien.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("NCC_Forms", "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_Bien.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_Bien.Tables[0].Rows.Remove(dr[i]);
                    }
                    gv_daluu.RefreshData();
                    Thongkesrecord.Text = stt_daluu.SummaryText;
                  

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

    
        #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);
          
        }

  

        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_Bien.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];
            Thongkesrecord.Text = stt_daluu.SummaryText;
            
            
        }
        #endregion

        private void BaoCao_ToaSi_Load(object sender, EventArgs e)
        {
          
            data();
        }
        #region register event
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {

            if (keyData == (Keys.Control | Keys.D))
            {
                this.Close();
            }
            if (keyData == (Keys.Control | Keys.A))
            {
                chon();
            }
            if (keyData == (Keys.Control | Keys.Z))
            {
                bochon();
            }
           
            if (keyData == (Keys.Shift | Keys.Delete))
            {
                xoa();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

    
        #endregion

    }
}
