using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Service;
using Domain;
using Common;

namespace WebSite.Controllers
{
    public class ManualVisitController : BaseController
    {
        private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(ManualVisitController));
        public IManualVisitManager ManualVisitManager { get; set; }

        #region Action

        public ActionResult Index()
        {
			return View();
        }
        /// <summary>
        /// 获取验证码
        /// </summary>
        /// <param name="name"></param>
        /// <param name="companyName"></param>
        /// <param name="phoneNumber"></param>
        /// <returns></returns>
        public ActionResult GetValidateCode(string name, string companyName, string phoneNumber)
        {
            try
            {
                string validateCode = Tools.CreateValidateNumber(4);
                AliyunSMSHelper.SendVerifySMS(phoneNumber, validateCode);//发送短信验证码
                ManualVisit entity = new ManualVisit();
                entity.Name = name;
                entity.CompanyName = companyName;
                entity.PhoneNumber = phoneNumber;
                entity.ValidateCode = validateCode;
                entity.CreatedDate = DateTime.Now;
                ManualVisitManager.Save(entity);
                return Json(new { IsSuccess = true, Message = "验证码发送成功" });
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                return Json(new { IsSuccess = false, Message = "验证码发送失败" });
            }
        }
        /// <summary>
        /// 验证验证码
        /// </summary>
        /// <param name="phoneNumber"></param>
        /// <param name="validateCode"></param>
        /// <returns></returns>
        public ActionResult GetManual(string phoneNumber, string validateCode)
        {
            try
            {
                bool result = ManualVisitManager.ValidateCode(phoneNumber, validateCode);
                if (result)
                {
                    return Json(new { IsSuccess = true, Message = "验证码验证成功", Target = "/uploads/boonlive.pdf" });
                }
                else
                {
                    return Json(new { IsSuccess = false, Message = "验证码错误" });
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                return Json(new { IsSuccess = false, Message = "验证码验证失败" });
            }
        }

        public ActionResult List()
        {
            return View();
        }

        public ActionResult LoadAllByPage(int page, int rows, string order, string sort, DateTime? startDate, DateTime? endDate)
        {
			long total = 0;
            var list = this.ManualVisitManager.LoadAllByPage(out total, page, rows, order, sort, startDate, endDate);

            return Json(new { total = total, rows = list });
        }

        #endregion
    }
}
