using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App.ServiceApp.BidAnnouncementManager;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Controllers.ServiceController
{
    /// <summary>
    /// 招标公告
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BidAnnouncementController : ControllerBase
    {
        private readonly BidAnnouncementApp _app;
        public BidAnnouncementController(BidAnnouncementApp app)
        {
            _app = app;
        }

        /// <summary>
        /// 分页查询招标公告信息
        /// </summary>
        /// <param name="bidtype">招标信息类型（材料工程那些）</param>
        /// <param name="type">公告类型（招标公告，废标公告那些）</param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<dynamic>>>> LoadBidAnnouncement(string bidtype,short? type, int page = 1, int limit = 10)
        {
            var result = new Response<PageInfo<List<dynamic>>>();
            try
            {
                result = await _app.LoadBidAnnouncement(bidtype,type,page,limit);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 根据id查询单个招标公告--编辑招标公告用
        /// </summary>
        /// <param name="id">招标公告id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PpAnnouncement>> LoadBidAnnouncementById(string id)
        {
            var result = new Response<PpAnnouncement>();
            try
            {
                result = await _app.LoadBidAnnouncementById(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }


        /// <summary>
        /// 添加招标公告
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<string>> AddBidAnnouncement(PpAnnouncement info)
        {
            var result = new Response<string>();
            try
            {
                result = await _app.AddBidAnnouncement(info);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 编辑招标公告
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> EditBidAnnouncement(PpAnnouncement info)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.EditBidAnnouncement(info);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 删除招标公告
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> DeleteBidAnnouncement(string id)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.DeleteBidAnnouncement(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        #region 报名
        /// <summary>
        /// 添加报名信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> AddPpRegistration(string biddingInfoId)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.AddPpRegistration(biddingInfoId);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }
        #endregion

        /// <summary>
        /// 审核招标公告
        /// </summary>
        /// <param name="announcementid">招标公告id</param>
        /// <param name="isagree">是否通过</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> ReviewAnnouncement(string announcementid, bool isagree)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.ReviewAnnouncement(announcementid, isagree);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }
    }
}
