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 System.IO;
using ABC.KetNoi;

using DevExpress.XtraEditors;
using ABC.File;

namespace ABC
{
    public partial class T_Share :XtraUserControl
    {
        public T_Share()
        {
            InitializeComponent();
        }

        private void T_Share_Load(object sender, EventArgs e)
        {
            get_database();
            progess_in();
            string name = System.Environment.MachineName;
            
            txtthumuc.Text = "Data_" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year.ToString() +"_"+ DateTime.Now.Hour.ToString() + "h" + DateTime.Now.Minute.ToString()+" From ("+name+")";
        }
        T_Connect_master mt = new T_Connect_master();
        DataTable dt_main;
        public void get_database()
        {
            #region
            string sql = "SELECT DB_NAME(database_id) AS DatabaseName,Name AS Logical_Name,Physical_Name, (size*8)/1024 SizeMB FROM sys.master_files WHERE DB_NAME(database_id) = 'ABC'";
            dt_main = mt.T_open(sql).Tables[0];
            gc3.DataSource = dt_main;
            #endregion
        }
        public void progess_in()
        {
            Class_KetNoi cls = 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();
            DataTable dt = cls.T_opendataset_store("sp_CauHinh_TheoID", arr_name, arr_value, thamso).Tables[0];
            if (dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                txtduongdan.Text= row["ThuMucSaoLuu"].ToString();
            }
        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {
         //   this.DialogResult = DialogResult.Cancel;
           // this.Close();
        }
    
     

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            ok();
        }
        public void ok()
        {
            if (check_thumuc() == true)
            {
                if (check_va_taothumuc() == true)
                {
                    get_file_sql();
                    check_va_taothumuc_local();
                   
                    detach();
                    copy();
                    attach();
                    msgbox.dialog_OK_info("Thành Công");
                }
            }
        }
        string sql_file = "";
        public void get_file_sql()
        {
            if (dt_main.Rows.Count > 0)
            {
                sql_file = dt_main.Rows[0]["Physical_Name"].ToString();
                sql_file = System.IO.Path.GetDirectoryName(sql_file); folder_root = sql_file;

            }
        }
        public bool check_thumuc()
        {
            string path = txtduongdan.Text;
            if (!Directory.Exists(path))
            {
                msgbox.dialog_OK_warning("Không tìm thấy thư mục " + txtduongdan.Text + "");
                return false;
            }
            else
            {
                return true;
            }
        }
        public bool check_va_taothumuc()
        {
            string path = txtduongdan.Text+@"\"+txtthumuc.Text;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
                folder_copy1 = path;
                return true;
            }
            else
            {
                DialogResult a= msgbox.dialog_yes_no("Đã tồn tại thư mục " + path + " trong máy tính \n Bạn có muốn thay thế không?");
                if (a == DialogResult.Yes)
                {
                    Directory.Delete(path);
                    Directory.CreateDirectory(path);
                    folder_copy1 = path;
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        public void check_va_taothumuc_local()
        {
            string path =sql_file+ @"\Share_" + txtthumuc.Text;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
                folder_copy2 = path;
            }
            else
            {
               
                    Directory.Delete(path);
                    Directory.CreateDirectory(path);
                    folder_copy2 = path;
            }
        }
        public void attach()
        {
            
            string A3 = folder_root + @"\ABC_Data.mdf";
            string A4 = folder_root + @"\ABC_Log.ldf";
            
            string SQL2 = "exec sp_attach_db 'ABC', '" + A3 + "', '" + A4 + "'";
            
            mt.T_exc(SQL2);
        }
        public void detach()
        {
            string SQL = "sp_detach_db N'ABC'";
            
            
            string CLOSE1 = "ALTER DATABASE [ABC] SET OFFLINE WITH ROLLBACK IMMEDIATE";
            mt.T_exc(CLOSE1);
          
            mt.T_exc(SQL);
        }
        string folder_copy1, folder_copy2,folder_root;
        public void copy()
        {
           
            string A3 = folder_root + @"\ABC_Data.mdf";
          //  string A4 = folder_root + @"\ABC_Log.ldf";

            System.IO.File.Copy(A3, folder_copy1 + @"\ABC_Data.mdf");
           // System.IO.File.Copy(A4, folder_copy1 + @"\ABC_Log.ldf");


            System.IO.File.Copy(A3, folder_copy2 + @"\ABC_Data.mdf");
          //  System.IO.File.Copy(A4, folder_copy2 + @"\ABC_Log.ldf");
        }
    }
}
