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.IO;



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

        SqlConnection con;
        SqlCommand cmd;





        private void BtnBackUp_Click(object sender, EventArgs e)
        {

            if (TxtPatch.Text == "")
            {
                errorProvider1.SetError(TxtPatch, "مسیر را انتخاب کنید");
                return;
            }

            errorProvider1.Clear();

            if (String.IsNullOrWhiteSpace(TxtName.Text))
            {
                errorProvider1.SetError(TxtName, "نام را انتخاب کنید");
                return;
            }
             errorProvider1.Clear();

            if (File.Exists(TxtPatch.Text + "\\" + TxtName.Text + ".bak"))
            {
                errorProvider1.SetError(TxtName, "فایلی با همین نام در این مسیر وجود دارد");
                return;
            }
             errorProvider1.Clear();

            BtnBackUp.Enabled =false;
            BtnBrowse.Enabled = false;
            TxtName.Enabled = false;



                cmd = new SqlCommand("DoBackUp", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Patch",TxtPatch.Text + "\\" + TxtName.Text + ".bak" );
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();



                FrmMessage Fm = new FrmMessage();
                Fm.LblText.Text = "پشتیبان گیری با موفقیت انجام شد";
                Fm.PicClose.Visible = true;
                Fm.PicNO.Visible = false;
                Fm.PicYes.Visible = false;
                Fm.ShowDialog();


              BtnBackUp.Enabled =true;
              BtnBrowse.Enabled = true;
              TxtName.Enabled = true;







        }

        private void FrmBackUp_Load(object sender, EventArgs e)
        {
            con = new SqlConnection("Server = . ; DataBase = DbHotel ; Integrated Security = True");
        }

        private void BtnBrowse_Click(object sender, EventArgs e)
        {
            
            if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                TxtPatch.Text = folderBrowserDialog1.SelectedPath;
            }
        }
    }
}
