using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Authorization;

using System.Web;
using MySystem.MainModels;
using LitJson;
using Library;

namespace MySystem.Areas.Api.Controllers.v1
{
    [Area("Api")]
    [Route("Api/v1/[controller]/[action]")]
    public class ServiceCenterController : BaseController
    {
        public ServiceCenterController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
        {
        }




        #region 商户-常见问题列表
        // [Authorize]
        public JsonResult List(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            List<Dictionary<string, object>> dataList = ListDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
        }
        public List<Dictionary<string, object>> ListDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int CategoryId = int.Parse(function.CheckInt(data["CategoryId"].ToString())); //问答类型
            int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
            int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
            List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
            string condition = "";
            if (CategoryId > 0)
            {
                condition += " and CategoryId=" + CategoryId;
            }
            List<Dictionary<string, object>> querys = new ServiceCenterService().List(new List<FieldItem>(), condition, PageNum, PageSize);
            foreach (Dictionary<string, object> subdata in querys)
            {
                Dictionary<string, object> curData = new Dictionary<string, object>();
                curData.Add("Title", subdata["Title"].ToString()); //标题
                curData.Add("Id", subdata["Id"].ToString()); //Id
                dataList.Add(curData);
            }
            return dataList;
        }
        #endregion



        #region 商户-常见问题搜索
        [Authorize]
        public JsonResult Search(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            List<Dictionary<string, object>> dataList = SearchDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
        }
        public List<Dictionary<string, object>> SearchDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            string SearchKey = data["SearchKey"].ToString(); //搜索关键词
            int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
            int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
            List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
            string condition = "";
            if (!string.IsNullOrEmpty(SearchKey))
            {
                condition += " and Title like '%" + SearchKey + "%'";
            }
            List<Dictionary<string, object>> querys = new ServiceCenterService().List(new List<FieldItem>(), condition, PageNum, PageSize);
            foreach (Dictionary<string, object> subdata in querys)
            {
                Dictionary<string, object> curData = new Dictionary<string, object>();
                curData.Add("Title", subdata["Title"].ToString()); //标题
                curData.Add("Id", subdata["Id"].ToString()); //Id
                dataList.Add(curData);
            }
            return dataList;
        }
        #endregion



        #region 商户-常见问题详情
        // [Authorize]
        public JsonResult Detail(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> Obj = DetailDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
        }
        public Dictionary<string, object> DetailDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
            ServiceCenter query = new ServiceCenterService().Query(Id);
            Obj.Add("Title", query.Title); //标题
            Obj.Add("Content", CheckContentImage(query.Content)); //内容
            return Obj;
        }
        #endregion



        #region 检查签名是否合法，合法返回1，不合法返回提示信息

        /// <summary>
        /// 检查签名是否合法，合法返回1，不合法返回提示信息
        /// </summary>
        /// <param name="value">请求的参数(json字符串)</param>
        /// <param name="signField">要签名的字段</param>
        /// <returns></returns>
        private string CheckSign(string value, string[] signField)
        {
            JsonData json = JsonMapper.ToObject(value);
            Dictionary<string, string> dic = new Dictionary<string, string>();
            for (int i = 0; i < signField.Length; i++)
            {
                dic.Add(signField[i], json[signField[i]].ToString());
            }
            string sign = json["sign"].ToString(); //客户端签名字符串
            return new Sign().sign(dic, sign);
        }

        #endregion

    }
}
