using System;
using System.Collections.Generic;
using System.Common;
using System.IO;
using System.IRepositories;
using System.IRepositories.Call;
using System.Linq;
using System.Model;
using System.Model.Call;
using System.Net;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using System.Utility;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using SqlSugar;
using TVShoppingCallCenter_ZLJ.Models.Inputs;

namespace TVShoppingCallCenter_ZLJ.Controllers.CallCenter
{
    /// <summary>
    /// 通话记录
    /// </summary>
   // [Authorize]
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class CallRecordsController : BaseController
    {
        private readonly IConfiguration config;
        private readonly ISys_MobileDataRepository busMobileDataRepository;
        private readonly ICall_CallRecordsRepository busCallRecordsRepository;
        private readonly ICall_CallOutboundRepository busCallOutboundRepository;
        public CallRecordsController( ICall_CallRecordsRepository _busCallRecordsRepository, IConfiguration _configuration, ISys_MobileDataRepository _busMobileDataRepository,
            ICall_CallOutboundRepository _busCallOutboundRepository)
        {
            busCallRecordsRepository = _busCallRecordsRepository;
            config = _configuration;
            busMobileDataRepository = _busMobileDataRepository;
            busCallOutboundRepository = _busCallOutboundRepository;
        }

        /// <summary>
        /// 获取通话记录
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpPost("getlistbypage")]
        public async Task<IActionResult> GetListByPage(CallRecordsInput input)
        {
           
            List<IConditionalModel> ConditionalModels = new List<IConditionalModel>();
            #region 筛选条件
            if (input.SearchStartTime != null)
            {
                ConditionalModels.Add(new ConditionalModel { FieldName = "BeginTime", ConditionalType = ConditionalType.GreaterThanOrEqual, FieldValue = input.SearchStartTime.GetValueOrDefault().ToString("yyyy-MM-dd 00:00:01") });
            }
            if (input.SearchEndTime != null)
            {
                ConditionalModels.Add(new ConditionalModel { FieldName = "BeginTime", ConditionalType = ConditionalType.LessThanOrEqual, FieldValue = input.SearchEndTime.GetValueOrDefault().ToString("yyyy-MM-dd 23:59:59") });
            }
            if (!string.IsNullOrEmpty(input.CallNumber))
            {
                ConditionalModels.Add(new ConditionalModel { FieldName = "Caller", ConditionalType = ConditionalType.Like, FieldValue = input.CallNumber });
            }
            if (!string.IsNullOrEmpty(input.UserCode))
            {
                ConditionalModels.Add(new ConditionalModel { FieldName = "UserCode", ConditionalType = ConditionalType.Like, FieldValue = input.UserCode });
            }
            if (!string.IsNullOrEmpty(input.Location))
            {
                ConditionalModels.Add(new ConditionalModel { FieldName = "Location", ConditionalType = ConditionalType.Like, FieldValue = input.Location });
            }
            if (input.CallType > -1)
            {
                ConditionalModels.Add(new ConditionalModel { FieldName = "CallType", ConditionalType = ConditionalType.Equal , FieldValue = input.CallType.ToString () });
            }
            if (input.CallState >-1)
            {
                ConditionalModels.Add(new ConditionalModel { FieldName = "CallState", ConditionalType = ConditionalType.Equal, FieldValue = input.CallState.ToString() });
            }
            
            #endregion

            int count = 0;
            PageData<T_Call_CallRecords> pagelist=   await busCallRecordsRepository.GetListByPage(ConditionalModels, new MyPageModel { PageCount= count ,PageIndex= input.pageindex,PageSize=input.pagesize}, " BeginTime DESC");
            return Success("成功", pagelist);

        }
       
    
        /// <summary>
        /// 获取自动外呼通话记录
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpPost("getoutboundlist")]
        public async Task<IActionResult> GetOutboundListByPage(CallRecordsInput input)
        {

            List<IConditionalModel> ConditionalModels = new List<IConditionalModel>();
            #region 筛选条件
            if (input.SearchStartTime != null)
            {
                ConditionalModels.Add(new ConditionalModel { FieldName = "BeginTime", ConditionalType = ConditionalType.GreaterThanOrEqual, FieldValue = input.SearchStartTime.GetValueOrDefault().ToString("yyyy-MM-dd 00:00:01") });
            }
            if (input.SearchEndTime != null)
            {
                ConditionalModels.Add(new ConditionalModel { FieldName = "BeginTime", ConditionalType = ConditionalType.LessThanOrEqual, FieldValue = input.SearchEndTime.GetValueOrDefault().ToString("yyyy-MM-dd 23:59:59") });
            }
            if (!string.IsNullOrEmpty(input.CallNumber))
            {
                ConditionalModels.Add(new ConditionalModel { FieldName = "Caller", ConditionalType = ConditionalType.Like, FieldValue = input.CallNumber });
            }
            if (!string.IsNullOrEmpty(input.UserCode))
            {
                ConditionalModels.Add(new ConditionalModel { FieldName = "UserCode", ConditionalType = ConditionalType.Like, FieldValue = input.UserCode });
            }
            #endregion

            int count = 0;
            PageData<T_Call_CallOutbound> pagelist = await busCallOutboundRepository.GetListByPage(ConditionalModels, new MyPageModel { PageCount = count, PageIndex = input.pageindex, PageSize = input.pagesize }, " BeginTime DESC");
            return Success("成功", pagelist);

        }

    }
}