using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PageAdmin.BLL;
using PageAdmin.Filter;
using PageAdmin.Common;
using System.IO;
using System.Data;
using System.Configuration;

namespace PageAdmin.Web.Areas.Admin.Controllers
{
    public class DatabackupController : BaseController
    {
        private DatabackupBLL _DatabackupBLL;
        private DataBaseTypeEnum _DbType;
        private string _BackPath;

        public DatabackupController() : base("Tool/")
        {
            _DatabackupBLL = new DatabackupBLL();
            _DbType = ConfigHelper.DefaultDataBaseType;
            _BackPath = "/App_Data/Backup/";
        }

        [HttpGet]
        public ActionResult Index()
        {
            ViewBag.DBType = _DbType.ToString();
            ViewBag.Path = _BackPath;
            return View();
        }
        public JsonResult ListData()
        {
            IEnumerable<dynamic> dt = IOHelper.DirectoryList(_BackPath).OrderByDescending(a => a.id).ToList();
            return base.Json(dt);
        }

        [HttpPost]
        public JsonResult Delete(FormCollection form)
        {
            string backupPath = _BackPath + form["file"];
            IOHelper.DelFile(backupPath);
            TipsInfo tipInfo = new TipsInfo();
            tipInfo.Msg = "删除成功！";
            return Json(tipInfo);
        }

        [HttpPost]
        public JsonResult Backups(FormCollection form)
        {
            var date = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
            string aimPath = path + date + ".mdb";
            if (_DbType==DataBaseTypeEnum.Access)
            {
                string srcPath = "/DataBase/v4.mdb";
                return Json(_DatabackupBLL.BackUp(srcPath, aimPath));
            }
            if (_DbType == DataBaseTypeEnum.MsSql)
            {
                aimPath = path + date + ".bak";
                return Json(_DatabackupBLL.SQLServerBackup(aimPath));
            }
            else
            {
                return Json(new TipsInfo());
            }

        }

        [HttpPost]
        public JsonResult ClearLog()
        {
            return Json(_DatabackupBLL.SQLServerClearLog());
        }
    }
}