using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;
using System.Data.SqlClient;
using System.IO;



namespace GymSystem
{
    public partial class FrmBackUp : Form
    {
        public FrmBackUp()
        {
            InitializeComponent();
        }

        private void PicBrowse_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            
            DateTime today = DateTime.Today;
            PersianCalendar pc = new PersianCalendar();

            sfd.FileName = pc.GetYear(today).ToString("0000") + pc.GetMonth(today).ToString("00") + pc.GetDayOfMonth(today).ToString("00");
            sfd.Filter = "BackUp Files (*.bak)|*.bak";

            if (sfd.ShowDialog() == DialogResult.OK)
            {

                TxtAdress.Text = sfd.FileName;

            }









        }

        private void BtnBackUp_Click(object sender, EventArgs e)
        {

            FrmMessage fm = new FrmMessage();

            if (TxtAdress.Text == "")
            {
                fm.Text = "پشتیبان گیری";
                fm.LblText.Text = "مسیر ذخیره فایل پشتیبان را انتخاب کنید";
                fm.PicOk.Visible = true;
                fm.PicHoshdar.Visible = true;

                fm.ShowDialog();
                return;
            }


            try
            {

                StreamReader sr = new StreamReader(Application.StartupPath + "\\ServerName.bas");
                string StrServerName = sr.ReadLine();
                sr.Close();
                

                SqlConnection con = new SqlConnection("Server = " + StrServerName + " ; DataBase = DbGym ; Integrated Security = True");
                SqlCommand cmd = new SqlCommand("DoBackUp", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Patch", TxtAdress.Text);

                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();


                fm.Text = "پشتیبان گیری";
                fm.LblText.Text = "عمل پشتیبان گیری با موفقیت انجام شد";
                fm.PicOk.Visible = true;
                fm.PicKhabar.Visible = true;
                fm.ShowDialog();


            }

            catch
            {
                fm.Text = "تهیه پشتیبان";
                fm.LblText.Text = "خطا در تهیه فایل پشتیبان";
                fm.PicOk.Visible = true;
                fm.PicHoshdar.Visible = true;
                fm.ShowDialog();

            }

           



        }

        private void groupPanel1_Click(object sender, EventArgs e)
        {

        }
    }
}
