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.Data.SqlClient;
using System.Globalization;
using Stimulsoft.Report.Dictionary;
using Stimulsoft.Report;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Rendering;
using System.IO;



namespace Library_Red_Diamonds
{
    public partial class backup : Form
    {
        public backup()
        {
            InitializeComponent();
        }

        static SqlConnection con = new SqlConnection(Connection.con);
        SaveFileDialog dlg = new SaveFileDialog();
        int _time = 60;

        private void backupdata()
        {
            if (txt_address.Text == "")
            {
                timer.Enabled = false;
                MessageBox.Show("آدرسی برای پشتیبان گیری انتخاب نشده است", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                timer.Enabled = true;
                MessageBox.Show("در حین پشتبان گیری به چیزی دست نزنید", "هشدار", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                string bk;
                bk = @"Backup DataBase [" + Application.StartupPath + "\\Red-Diamonds-DB.mdf] To Disk='" + dlg.FileName + "'";
                SqlCommand com = new SqlCommand(bk,con);
                con.Open();
                com.ExecuteNonQuery();
                con.Close();     
            }
        }
       
        private void btn_backup_Click_1(object sender, EventArgs e)
        {
            backupdata();
        }

        private void btn_browse_Click_1(object sender, EventArgs e)
        {
            dlg.DefaultExt = "BAK";
            dlg.Filter = "Backup Files(*.BAK)|*.BAK";
            dlg.FilterIndex = 1;
            dlg.OverwritePrompt = true;
            dlg.Title = "پشتیبان گیری";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                txt_address.Text = dlg.FileName.ToString();
            }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            if (_time < 60 && progressbar.Value < 97)
                progressbar.Value += 2;
            _time -= 1;
            lblTime.Text = _time.ToString();
            lblProgress.Text = progressbar.Value.ToString() + "%";
            if (_time == 0)
            {
                progressbar.Value = 100;
                lblProgress.Text = "100%";
                timer.Enabled = false;
                MessageBox.Show("عملیات پشتیبان گیری با موفقیت انجام شد", "پیغام", MessageBoxButtons.OK, MessageBoxIcon.Information);
                progressbar.Value = 0;
                lblProgress.Text = "0%";
                _time = 60;
            }
        }
     
       }
 }

