using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using XYY.API.Tracking.Models;
using XYY.Core.Standard.Mvc;
using XYY.Data.Model.Standard.Tracking;
using XYY.Service.Standard.TrackingService;

namespace XYY.API.Tracking.Controllers.TraceSupplementConfig
{
    [ApiController]
    [Route("tsc/[controller]")]
    public class ConfigController : ControllerBase
    {
        ITraceSupplementConfigService supplementConfigService;

        public ConfigController(ITraceSupplementConfigService supplementConfigService)
        {
            this.supplementConfigService = supplementConfigService;
        }

        [HttpPost]
        public async Task<ApiJsonModel> Add([FromBody] Logistics_Trace_SupplementConfig config)
        {
            await supplementConfigService.AddConfig(config);
            return ApiJsonModel.OK();
        }

        /// <summary>
        /// 更新接口
        /// </summary>
        /// <param name="config">Action:Update 更新单个实体Config,Disable 禁用IdList列表,Enable 启用 IdList列表</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiJsonModel> Put([FromBody] SupplementConfigPutModel config)
        {
            if (string.IsNullOrEmpty(config.Action))
                return ApiJsonModel.Error("必须传入修改方法");
            if (config.Action.Equals("Update"))
            {
                if (config.Config == null)
                    return ApiJsonModel.Error("当Action为Update时，必须传入Config对象");
                await supplementConfigService.EditConfig(config.Config);
            }
            else if (config.Action.Equals("Disable"))
            {
                if (config.IdList == null || config.IdList.Ids == null)
                    return ApiJsonModel.Error("当Action为Disable时，必须传入IdList对象");
                await supplementConfigService.Disable(config.IdList.Ids);
            }
            else if (config.Action.Equals("Enable"))
            {

                if (config.IdList == null || config.IdList.Ids == null)
                    return ApiJsonModel.Error("当Action为Enable时，必须传入IdList对象");
                await supplementConfigService.Enable(config.IdList.Ids);
            }
            return ApiJsonModel.OK();
        }

        [HttpDelete]
        public async Task<ApiJsonModel> Delete([FromBody] IdList list)
        {
            await supplementConfigService.Delete(list.Ids);
            return ApiJsonModel.OK();
        }
    }
}
