using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using System.Xml;
using System.IO;
using System.Text;
using System.Configuration;
using ABC.File;
using System.Data;

namespace ABC.KetNoi
{

    public partial class CauHinh_File : DevExpress.XtraEditors.XtraUserControl
    {
        public CauHinh_File()
        {
            InitializeComponent();
            try
            {
               
                duyet();

            }
            catch (Exception)
            {
            }

        }
        Class_KetNoi cls = new Class_KetNoi();     // kết nối cơ sở dữ liệu sql
        #region button ok
        void simpleButton_ok_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn muốn lưu không?", "Thông báo!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                DataTable dt = Main_Form._dtCauHinh;
                string sql = "";
                if (dt.Rows.Count > 0)
                {
                    DataRow row = dt.Rows[0];
                   
                    sql = "update CauHinh set ThuMucFileTemp='"+txtFileTemp.Text.Trim() + "',ThuMucPDF1='"+ txtthumucpdf1.Text.Trim() + "',ThuMucPDF2='"+ txtthumucpdf2.Text.Trim() + "',";
                    sql += "',ThuMucSaoLuu='" + txtsauluu.Text.Trim() + "',ThuMucPhucHoi='"+ txtphuchoi.Text.Trim() + "' where TaiKhoan='"+ Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()+"'";
                    cls.T_exc(sql);
                }
                else
                {
                    sql = "insert into CauHinh(ThuMucFileTemp,ThuMucPDF1,ThuMucPDF2,ThuMucSaoLuu,ThuMucPhucHoi,TaiKhoan)values(";
                    sql += "'"+ txtFileTemp.Text.Trim()+"',";
                    sql += "'" +txtthumucpdf1.Text.Trim() + "',";
                    sql += "'" + txtthumucpdf2.Text.Trim() + "',";
                    sql += "'" + txtsauluu.Text.Trim() + "',";
                    sql += "'" + txtphuchoi.Text.Trim() + "',";
                    sql += "'" + Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper() + "')";
                    cls.T_exc(sql);
                }
               Main_Form._dtCauHinh = ThongTinCauHinh();
                //     this.Close();
            }
        }
        private DataTable ThongTinCauHinh()
        {
            Class_KetNoi db = new Class_KetNoi();
            int thamso = 1;
            string[] arr_name = new string[thamso];
            object[] arr_value = new object[thamso];
            arr_name[0] = "@ID";
            arr_value[0] = Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper();
            return db.T_opendataset_store("sp_CauHinh_TheoID", arr_name, arr_value, thamso).Tables[0];
        }
        #endregion

        #region event button
        public void duyet()
        {
            simpleButton_pdf1.Click += new EventHandler(simpleButton_pdf1_Click);
            simpleButton_pdf2.Click += new EventHandler(simpleButton_pdf2_Click);
            simpleButton_saoluu.Click += new EventHandler(simpleButton_sharedata_Click);
            simpleButton_phuchoi.Click += new EventHandler(simpleButton_updatedata_Click);
        }
        FolderBrowserDialog f = new FolderBrowserDialog();
        void simpleButton_updatedata_Click(object sender, EventArgs e)
        {
            if (f.ShowDialog() == DialogResult.OK)
            {
                txtphuchoi.Text = f.SelectedPath;
            }
        }

        void simpleButton_sharedata_Click(object sender, EventArgs e)
        {
            if (f.ShowDialog() == DialogResult.OK)
            {
                txtsauluu.Text = f.SelectedPath;
            }
        }

        void simpleButton_pdf2_Click(object sender, EventArgs e)
        {
            if (f.ShowDialog() == DialogResult.OK)
            {
                txtthumucpdf2.Text = f.SelectedPath;
            }
        }

        void simpleButton_pdf1_Click(object sender, EventArgs e)
        {
            if (f.ShowDialog() == DialogResult.OK)
            {
                txtthumucpdf1.Text = f.SelectedPath;
            }
        }
        #endregion

        private void CauHinh_Form_Load(object sender, EventArgs e)
        {
            try
            {
                display();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        #region hiển thị dữ liệu
        public void display()
        {
            DataTable dt = Main_Form._dtCauHinh;
            if(dt.Rows.Count>0)
            {
                DataRow row = dt.Rows[0];
                txtFileTemp.Text = row["ThuMucFileTemp"].ToString();
                txtthumucpdf1.Text = row["ThuMucPDF1"].ToString();
                txtthumucpdf2.Text = row["ThuMucPDF2"].ToString();
                txtsauluu.Text = row["ThuMucSaoLuu"].ToString();
                txtphuchoi.Text = row["ThuMucPhucHoi"].ToString();
            }
            else
            {
                txtFileTemp.Text ="";
                txtthumucpdf1.Text = "";
                txtthumucpdf2.Text = "";
                txtsauluu.Text = "";
                txtphuchoi.Text = "";
            }
        }

        #endregion

        private void btnFileTemp_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
                txtFileTemp.Text = dialog.SelectedPath;
        }

        private void txtFileTemp_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void txtphuchoi_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void txtthumucpdf2_EditValueChanged(object sender, EventArgs e)
        {

        }
    }
}
