using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MS.Models;
using MS.Models.ViewModel;
using MS.Services;
using MS.Services.membershipGroup;
using MS.WebCore.Core;
using System.IO;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Threading.Tasks;
using System.Collections.Generic;
using MS.Entities.SQLite;

namespace MS.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class membershipGroupController : ControllerBase
    {
        private readonly ImembershipGroup _membershipGroup;
        public membershipGroupController(ImembershipGroup imembershipGroup)
        {
            _membershipGroup = imembershipGroup;
        }
        [Route("getGroupIdListAsync")]
        [HttpPost]
        public async Task<ExecuteResult> getGroupIdListAsync([FromBody] ReportRequest request)
        {
            return await  _membershipGroup.getGroupIdListAsync(request.currentBuId);
        }
        [Route("CreateAsync")]
        [EnableCors("AllowSpecificOrigin")]
        [HttpPost]
        public async Task<ExecuteResult> CreateAsync(BuidMembershipGroupViewModel viewModel)
        {
            return await _membershipGroup.CreateAsync(viewModel);
        }
        [Route("GetmembershipGroupAsync")]
        [EnableCors("AllowSpecificOrigin")]
        [HttpPost]
        public async Task<ExecuteResult> GetmembershipGroupAsync([FromBody] ReportRequest request)
        {
            return await _membershipGroup.GetmembershipGroupAsync(request.currentBuId);
        }
        [Route("Import")]
        [EnableCors("AllowSpecificOrigin")]
        [HttpPost]
        public async Task<IActionResult> ImportExcel()
        {
            if (!Request.HasFormContentType)
                return BadRequest("无效的请求");

            var file = Request.Form.Files[0]; // 假设只上传了一个文件

            if (file.Length <= 0)
                return BadRequest("空文件");

            using (var memoryStream = new MemoryStream())
            {
                await file.CopyToAsync(memoryStream);
                memoryStream.Position = 0; // 重置内存流位置

                var workbook = new XSSFWorkbook(memoryStream);
                var worksheet = workbook.GetSheetAt(0); // 假设数据在第一个工作表中
                List<MembershipGroupListViewModel> list = new List<MembershipGroupListViewModel>();
                // 遍历行并处理数据
                for (int row = 1; row <= worksheet.LastRowNum; row++) // 假设跳过标题行
                {
                    var currentRow = worksheet.GetRow(row);
                    if (currentRow == null)
                        continue;

                    // 从每个单元格读取数据
                    var cell1Value = currentRow.GetCell(0)?.ToString();
                    var cell2Value = currentRow.GetCell(1)?.ToString();
                    // 在这里添加您的逻辑来处理数据
                    list.Add(new MembershipGroupListViewModel() {  CurrentBuId = cell1Value ,Phone = cell2Value });
                }
                await _membershipGroup.CreateAsync(list);
            }

            return Ok("Excel 文件导入成功");
        }
        [Route("UpdateAsync")]
        [EnableCors("AllowSpecificOrigin")]
        [HttpPost]
        public async Task<ExecuteResult> UpdateAsync()
        {
            return await _membershipGroup.UpdateAsync("db3b74ff-412c-45fb-9887-9b10aa179704");
        }
        [Route("GetmembershipGroupListAsync")]
        [EnableCors("AllowSpecificOrigin")]
        [HttpPost]
        public async Task<ExecuteResult> GetmembershipGroupListAsync([FromBody] ReportRequest request)
        {
            return await _membershipGroup.GetmembershipGroupListAsync(request.currentBuId);
        }
    }
}
