using System;
using System.Web;
using System.Web.Mvc;
using PageAdmin.Common;
using System.IO;
using PageAdmin.BLL;
using System.Linq;

namespace PageAdmin.Web.Areas.Admin.Controllers
{
    public class ExcleOperationController : BaseController
    {
        ExcleOperationBLL bll=null;

        public ExcleOperationController() :base("Common/")
        {
            bll = new ExcleOperationBLL();
        }

        #region 会员表的导入和导出
        [HttpGet]
        public ActionResult MemberExport()
        {
            ViewBag.FieldList = bll.GetFields("pa_member");
            return View();
        }

        [HttpGet]
        public ActionResult MemberImport()
        {
            return View();
        }

        [ValidateAntiForgeryToken]
        [HttpPost]
        public JsonResult MemberExport(FormCollection form)
        {
            string filePath=bll.MemberExport(form);
            TipsInfo tipsInfo = new TipsInfo()
            {
                State = 1,
                Msg = "",
                Description = filePath
            };
            return base.Json(tipsInfo);
        }

        [ValidateAntiForgeryToken]
        [HttpPost]
        public JsonResult MemberImport(FormCollection form)
        {
            bll.MemberImport(form);
            TipsInfo tipsInfo = new TipsInfo()
            {
                State = 1,
                Msg = ""
            };
            return base.Json(tipsInfo);
        }

        #endregion


        private void Init()
        {
            int tableId = StringHelper.Format<int>(Request.QueryString["table_id"]);
            User currentUser = ViewBag.CurrentUser;
            int uid = currentUser.Uid;
            if (tableId < 0)
            {
                throw new Exception("无效的table_id参数!");
            }
            ViewBag.TableId = tableId;
            ViewBag.Uid = uid;
            ViewBag.FieldList = bll.GetFields(tableId);
        }


        [HttpGet]
        public ActionResult Export()
        {
            Init();
            return View();
        }

        [HttpGet]
        public ActionResult Import()
        {
            Init();
            return View();
        }

        [ValidateAntiForgeryToken]
        [HttpPost]
        public JsonResult Export(FormCollection form)
        {
            ExcleOperationBLL bll = new ExcleOperationBLL();
            string filePath = bll.Export(form);
            TipsInfo tipsInfo = new TipsInfo()
            {
                State = 1,
                Msg = "",
                Description = filePath
            };
            return base.Json(tipsInfo);
        }

        [ValidateAntiForgeryToken]
        [HttpPost]
        public JsonResult Import(FormCollection form)
        {
            User currentUser = ViewBag.CurrentUser;
            ExcleOperationBLL bll = new ExcleOperationBLL();
            bll.Import(form, currentUser);
            TipsInfo tipsInfo = new TipsInfo()
            {
                State = 1,
                Msg = ""
            };
            return base.Json(tipsInfo);
        }

        
    }
}