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.KetNoi;
using ABC.Report;
using System.IO;
using DevExpress.XtraReports.UI;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid;
using DevExpress.XtraEditors;
using ABC.File;
using DevExpress.XtraSplashScreen;

namespace ABC.Toa
{
    public partial class Toa_ChuyenTienToa :XtraUserControl
    {
        public Toa_ChuyenTienToa()
        {
            InitializeComponent();
           
            event_click();
            txtngay.Text = Get_CauHinh.ngay_text;
            Class_KetNoi db = new Class_KetNoi();
            LoopNhomThuongLai.Properties.DataSource = db.T_opendataset("select * from Tbl_NhomToa").Tables[0];
            Look_tenlai.Properties.DataSource = DuLieu.List_NCC.Tables[0];
            lool_nhomLai.Properties.DataSource = DuLieu.List_Nhom_NCC.Tables[0];
            this.Load += new EventHandler(Toa_QuanLyDuLieu_Load);
            Link_add.Click += new EventHandler(Link_add_Click);
            link_in.Click += new EventHandler(link_in_Click);
            Link_xoa.Click += new EventHandler(Link_xoa_Click);
            link_pdf.Click += new EventHandler(link_pdf_Click);
        }

        void link_pdf_Click(object sender, EventArgs e)
        {
            xuatpdf();
        }

        void Link_xoa_Click(object sender, EventArgs e)
        {
            DataRow dr = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle);
            if (msgbox.dialog_yes_no("Xóa toa "+dr["TOA1"].ToString()+" ?") == DialogResult.Yes)
            {
                string sql = "Xoa_toa '"+dr["ID"].ToString().ToUpper()+"'";
                db.T_exc(sql);
                tailai();

            }
        }

        void link_in_Click(object sender, EventArgs e)
        {
            print1();
        }

        void Link_add_Click(object sender, EventArgs e)
        {
            // thiet lap
            //DuLieu.User_case = "Edit";
            //DataRow dr = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle);
            //DuLieu.Ma_Toa = dr["ID"].ToString();
            //DuLieu.KyTuToa = dr["KYTU"].ToString();
            //DuLieu.So_Toa = int.Parse(dr["SOTOA"].ToString());
            //DuLieu.Ngay_hienthi_Toa = int.Parse(dr["NGAYLAMTOA"].ToString());
            //DuLieu.Tungay_toa = int.Parse(dr["FROM_N"].ToString());
            //DuLieu.Denngay_toa =  int.Parse(dr["TO_N"].ToString());
            //DuLieu.Ngay_hienthi_Toa_text = Convert.ToDateTime(dr["NGAYLAMTOA_TEXT"].ToString()).ToShortDateString();
            //DuLieu.tungay_toa_text = Convert.ToDateTime(dr["FROM_TEXT"].ToString()).ToShortDateString();
            //DuLieu.DenNgay_toa_text = Convert.ToDateTime(dr["TO_TEXT"].ToString()).ToShortDateString();

            //DuLieu.ID_NCC_Toa = dr["NCC_ID"].ToString().ToUpper();
            //DuLieu.Ten_NCC_Toa = dr["Tbl_NCC_Name"].ToString();   
            ////load chi tra
            //DuLieu.TKHIEN = decimal.Parse(dr["TKHIEN"].ToString()); 
            //DuLieu.TNUOC = decimal.Parse(dr["TNUOC"].ToString()); 
            //DuLieu.TUNG = decimal.Parse(dr["TUNG"].ToString()); 
            //DuLieu.TXE = decimal.Parse(dr["TXE"].ToString());
            //DuLieu.TKY = decimal.Parse(dr["TKY"].ToString());
            //DuLieu.T_HH = decimal.Parse(dr["THH"].ToString()); 
             

            // DuLieu.TTHEM1 = decimal.Parse(dr["TTHEM1"].ToString());
            // DuLieu.TTHEM2 = decimal.Parse(dr["TTHEM2"].ToString());
            // DuLieu.TTHEM3 = decimal.Parse(dr["TTHEM3"].ToString());

            //DuLieu.TEN_THEM1 = dr["TEN_THEM1"].ToString();
            //DuLieu.TEN_THEM2 = dr["TEN_THEM2"].ToString();
            //DuLieu.TEN_THEM3 = dr["TEN_THEM3"].ToString();

            //DuLieu.x1 = dr["C1"].ToString();
            //DuLieu.x2 = dr["C2"].ToString();
            //DuLieu.x3 = dr["C3"].ToString();

         
            //_Toa_New = new Toa_New();
            //_Toa_New.FormClosed += new FormClosedEventHandler(_Toa_New_FormClosed);
            //_Toa_New.ShowDialog();
        }

        void Toa_QuanLyDuLieu_Load(object sender, EventArgs e)
        {
            //
           
            tailai();
        }
        #region lệnh dùng chung
        void _BaoCao_SoBaoCao_Si_Bien_Edit_FormClosed(object sender, FormClosedEventArgs e)
        {
            tailai();
        }
        public void chon()
        {
   
        }
        public void bochon()
        {
     
        }
        NhapSi_Class _NhapSi_Class = new NhapSi_Class();
        public void tailai()
        {
           
            //
            #region
            string filter_string = "select * from ThongKeToa_chuyen_tien_toa where khoa is null and NGAYLAMTOA >0 ";
            string filter_string_ketChuyen = "select * from Toa_Con_Thieu_chot where CON_LAI >0 ";
            if (check_ngay.Checked == true)
            {
                int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
                int ng2 = (int)(Convert.ToDateTime(txtngay2.Text) - new DateTime(1900, 1, 1)).TotalDays;
                filter_string += "AND NGAYLAMTOA >= " + ng + " and  NGAYLAMTOA <= " + ng2 + "";
               // filter_string_ketChuyen+= "AND NGAYLAMTOA >= " + ng + " and  NGAYLAMTOA <= " + ng2 + "";
                if (check_ten_thuonglai.Checked == true && Look_tenlai.EditValue != null)
                {
                    filter_string += " And NCC_ID = '" + Look_tenlai.EditValue.ToString().ToUpper() + "'";
                    filter_string_ketChuyen+= " And NCC_ID = '" + Look_tenlai.EditValue.ToString().ToUpper() + "'";
                }

                if (check_nhom_lai.Checked == true && lool_nhomLai.EditValue != null && lool_nhomLai.EditValue != "")
                {
                    filter_string += " And NHOM_NCC_ID = '" + lool_nhomLai.EditValue.ToString().ToUpper() + "'";
                    filter_string_ketChuyen+= " And NHOM_NCC_ID = '" + lool_nhomLai.EditValue.ToString().ToUpper() + "'";
                }
                //if (check_kytu.Checked == true && txtthuonglai.Text != null && txtthuonglai.Text != "")
                //{
                //    filter_string += " And Tbl_NCC_Name like N'" + txtthuonglai.Text.ToString().ToUpper() + "%'";
                //    filter_string_ketChuyen+= " And Tbl_NCC_Name like N'" + txtthuonglai.Text.ToString().ToUpper() + "%'";
                //}
                if (check_kytu.Checked == true && !string.IsNullOrEmpty(txtthuonglai.Text))
                {
                    filter_string += " And Tbl_NCC_Name like N'%" + txtthuonglai.Text.ToString().ToUpper() + "%'";
                    filter_string_ketChuyen += " And Tbl_NCC_Name like N'%" + txtthuonglai.Text.ToString().ToUpper() + "%'";
                }
            }
            else
            {
                filter_string += "AND NGAYLAMTOA = " + Get_CauHinh.Ngay_DN + "";
               // filter_string_ketChuyen+= "AND NGAYLAMTOA = " + Get_CauHinh.Ngay_DN + "";
                if (check_ten_thuonglai.Checked == true && Look_tenlai.EditValue != null)
                {
                    filter_string += " And NCC_ID = '" + Look_tenlai.EditValue.ToString().ToUpper() + "'";
                    filter_string_ketChuyen+= " And NCC_ID = '" + Look_tenlai.EditValue.ToString().ToUpper() + "'";
                }

                if (check_nhom_lai.Checked == true && lool_nhomLai.EditValue != null && lool_nhomLai.EditValue != "")
                {
                    filter_string += " And NHOM_NCC_ID = '" + lool_nhomLai.EditValue.ToString().ToUpper() + "'";
                    filter_string_ketChuyen += " And NHOM_NCC_ID = '" + lool_nhomLai.EditValue.ToString().ToUpper() + "'";
                }
                //if (check_kytu.Checked == true && txtthuonglai.Text != null && txtthuonglai.Text != "")
                //{
                //    filter_string += " And Tbl_NCC_Name like N'" + txtthuonglai.Text.ToString().ToUpper() + "%'";
                //    filter_string_ketChuyen+= " And Tbl_NCC_Name like N'" + txtthuonglai.Text.ToString().ToUpper() + "%'";
                //}
                if (check_kytu.Checked == true && !string.IsNullOrEmpty(txtthuonglai.Text))
                {
                    filter_string += " And Tbl_NCC_Name like N'%" + txtthuonglai.Text.ToString().ToUpper() + "%'";
                    filter_string_ketChuyen += " And Tbl_NCC_Name like N'%" + txtthuonglai.Text.ToString().ToUpper() + "%'";
                }
            }
            if(chkNhomToa.Checked)
                filter_string += " And IDNhomToa= N'" + LoopNhomThuongLai.EditValue.ToString().ToUpper() + "'";
            // string sql = filter_string + " union all " + filter_string_ketChuyen;
            DuLieu.ThongKeToan_chuyentientoa = _NhapSi_Class.get_dataset(filter_string);
            DuLieu.ThongKeToan_chuyentientoa.Tables[0].Columns.Add("Select", typeof(bool));
            data();
            #endregion
        }
        public void print()
        {
            if (Pow.kiemtra("ThongKeToa_chuyen_tien_toa", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region
              
                    DataSet dsSelectData = new DataSet();
                    dsSelectData = DuLieu.ThongKeToan_chuyentientoa;


                    Get_CauHinh.thongketoachitiet = "Từ ngày : " + txtngay.Value.ToShortDateString() + " Đến Ngày " + txtngay2.Value.ToShortDateString();
                    T_TL_ChuyenTienToa rapport = new T_TL_ChuyenTienToa();
                    rapport.DataSource = dsSelectData;
                    rapport.DataMember = "Tbl_NhapLieu";
                    rapport.LocationFloat = new DevExpress.Utils.PointFloat(10, 10);
                    rapport.ShowPreview();

                
                #endregion
            }
        }
        public void print_bill()// in tong hop
        {
            if (Pow.kiemtra("ThongKeToa_chuyen_tien_toa", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region
 
                #endregion
            }
        }
        #endregion

        #region menu click
        public void event_click()
        {
            //btn_close.Click += new EventHandler(btn_close_Click);
            btn_load.Click += new EventHandler(btn_load_Click);
            btn_print.Click += new EventHandler(btn_print_Click);
            
        }

        void btn_print_Click(object sender, EventArgs e)
        {
            print();
        }
        Toa_New _Toa_New;
        void btn_new_Click(object sender, EventArgs e)
        {
           
        }
        Class_KetNoi db = new Class_KetNoi();
        void _Toa_New_FormClosed(object sender, FormClosedEventArgs e)
        {
         
                //update SL,Gia,Num2,id_toa ve ban dau theo dieu kien chua co saved
           
            
        }
        Toa_Class _Toa_Class = new Toa_Class();
        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)
        {
            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();
        }

      




        #endregion

        #region data
        public void data()
        {
            gc_daluu.DataSource = DuLieu.ThongKeToan_chuyentientoa.Tables[0];
            gv_daluu.ExpandAllGroups();

            lbdoanhso.Text = cds.SummaryText + " (vnđ)";
            lbphaitra.Text = cpt.SummaryText + " (vnđ)";

            //lbhh.Text = chh.SummaryText + " (vnđ)";
        //    lbkhien.Text = ctk.SummaryText + " (vnđ)";
         //   lbnd.Text = cnd.SummaryText + " (vnđ)";

           // lbut.Text = cut.SummaryText + " (vnđ)";
        //    lbux.Text = cux.SummaryText + " (vnđ)";
         //   lbtky.Text = ctky.SummaryText + " (vnđ)";

            lbk1.Text = ck1.SummaryText + " (vnđ)";
            //lbk2.Text = ck2.SummaryText + " (vnđ)";
            //lbk3.Text = ck3.SummaryText + " (vnđ)";

            // Create and setup the first summary item.
            //GridGroupSummaryItem item = new GridGroupSummaryItem();
            //item.FieldName = "PhaiTra";
            //item.SummaryType = DevExpress.Data.SummaryItemType.Count;
            //gv_daluu.GroupSummary.Add(item);
        }
        #endregion


        #region in
        chang _chang = new chang();
        public void print1()
        {
            #region

            if (Pow.kiemtra("ThongKeToa_chuyen_tien_toa", "Inan") == true)
            {
                

            }
            #endregion

        }
        #endregion


        #region pdf
        public void xuatpdf()
        {
            DataRow dtrchon = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle);
            
                pdf1();
               
        }
        public void pdf1()
        {
            #region

            if (Pow.kiemtra("ThongKeToa_chuyen_tien_toa", "Inan") == true)
            {
                //load thiet lap
                #region
               
                #endregion

            }
            #endregion

        }
        public void check_va_taothumuc(string pa)
        {
            string path = pa;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);

            }
            else
            {

            }
        }
        #endregion

        private void gv_daluu_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            try
            {
                //thanh toan tien/ghi chu, check thanh toan het
                decimal _phaitra = (gv_daluu.GetFocusedRowCellValue("PhaiTra").ToString() == "") ? 0 : decimal.Parse(gv_daluu.GetFocusedRowCellValue("PhaiTra").ToString());
                decimal _thanh_toan = (gv_daluu.GetFocusedRowCellValue("THANH_TOAN1").ToString() == "") ? 0 : decimal.Parse(gv_daluu.GetFocusedRowCellValue("THANH_TOAN1").ToString());
                clsConnect cn = new clsConnect();
                if (e.Column.FieldName == "Chon")
                {
                    if (e.Value.ToString().ToLower() == "true")
                    {
                        gv_daluu.SetFocusedRowCellValue("THANH_TOAN", _phaitra);
                    }
                    else
                    {
                        gv_daluu.SetFocusedRowCellValue("THANH_TOAN", _thanh_toan);
                    }
                       
                }
                DateTime _NgayThanhToan = (gv_daluu.GetFocusedRowCellValue("NGAY_THANH_TOAN").ToString().Trim() == "") ? DateTime.Now : Convert.ToDateTime(gv_daluu.GetFocusedRowCellValue("NGAY_THANH_TOAN"));
                string _GhiChu = gv_daluu.GetFocusedRowCellValue("GHI_CHU").ToString();
                string _STT = gv_daluu.GetFocusedRowCellValue("STT").ToString();
                decimal _thanh_toan_toa = (gv_daluu.GetFocusedRowCellValue("THANH_TOAN").ToString() == "") ? 0 : decimal.Parse(gv_daluu.GetFocusedRowCellValue("THANH_TOAN").ToString());
                int para = 4;
                string[] name = new string[para];
                object[] value = new object[para];
                name[0] = "@STT";
                name[1] = "@THANH_TOAN";
                name[2] = "@NGAY_THANH_TOAN";
                name[3] = "@GHI_CHU";
                value[0] = _STT;
                value[1] = _thanh_toan_toa;
                value[2] = _NgayThanhToan;
                value[3] = _GhiChu;
                if (_thanh_toan_toa == 0)
                    value[2] = DBNull.Value;
                string sql = "update TOA set THANH_TOAN=@THANH_TOAN,NGAY_THANH_TOAN=@NGAY_THANH_TOAN,GHI_CHU=@GHI_CHU where STT=@STT";
                cn.UpdateTable(sql, name, value, para);
               
                tailai();
            }
            catch (Exception ex)
            {
               // MessageBox.Show(ex.Message);
            }
           
        }

        private void gc_daluu_Click(object sender, EventArgs e)
        {

        }

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

        private void repositoryItemchek_CheckedChanged(object sender, EventArgs e)
        {
            //if (repositoryItemchek.ValueChecked.ToString() == "True")
            //{
            //    decimal _phaitra = (gv_daluu.GetFocusedRowCellValue("PhaiTra").ToString() == "") ? 0 : decimal.Parse(gv_daluu.GetFocusedRowCellValue("PhaiTra").ToString());
            //    decimal _thanh_toan = (gv_daluu.GetFocusedRowCellValue("THANH_TOAN1").ToString() == "") ? 0 : decimal.Parse(gv_daluu.GetFocusedRowCellValue("THANH_TOAN1").ToString());
            //    clsConnect cn = new clsConnect();
            //    gv_daluu.SetFocusedRowCellValue("Chon", true);
            //    gv_daluu.SetFocusedRowCellValue("THANH_TOAN", _phaitra);
            //}
            //else
            //{
            //    gv_daluu.SetFocusedRowCellValue("Chon", false);
            //    gv_daluu.SetFocusedRowCellValue("THANH_TOAN", "0");
            //}
        }
    }
}
