using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using AMESCoreStudio.WebApi;
using AMESCoreStudio.WebApi.Models.AMES;
using AMESCoreStudio.CommonTools.Result;

namespace AMESCoreStudio.WebApi.Controllers.AMES
{
    /// <summary>
    /// 抽驗係數資料檔
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class QcQuotController : ControllerBase
    {
        private readonly AMESContext _context;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public QcQuotController(AMESContext context)
        {
            _context = context;
        }

        // GET: api/QcQuot
        [HttpGet]
        public async Task<ActionResult<IEnumerable<QcQuot>>> GetQcQuots()
        {
            return await _context.QcQuots.ToListAsync();
        }

        // GET: api/QcQuot/5
        [HttpGet("{id}")]
        public async Task<ActionResult<QcQuot>> GetQcQuot(int id)
        {
            var qcQuot = await _context.QcQuots.FindAsync(id);

            if (qcQuot == null)
            {
                return NotFound();
            }

            return qcQuot;
        }

        [HttpGet("QcQuotQuery")]
        public async Task<ResultModel<QcQuot>> GetQcQuotQuery(int page = 0, int limit = 10)
        {
            IQueryable<QcQuot> q = _context.QcQuots;
            ResultModel<QcQuot> result = new ResultModel<QcQuot>();

            // 紀錄筆數
            result.DataTotal = q.Count();

            // Table 頁數
            //if (page > 0)
            //{
            //    q = q.Skip((page - 1) * limit).Take(limit);
            //}
            result.Data = await q.ToListAsync();
            return result;
        }

        /// <summary>
        /// 更新抽驗係數
        /// </summary>
        /// <param name="qcQuot"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ResultModel<QcQuot>> PutQcQuot(QcQuot qcQuot)
        {
            ResultModel<QcQuot> result = new ResultModel<QcQuot>();
            _context.Entry(qcQuot).State = EntityState.Modified;
            qcQuot.UpdateDate = DateTime.Now;
            qcQuot.UpdateUserID = 0;

            try
            {
                await _context.SaveChangesAsync();
                result.Success = true;
                result.Msg = "OK";
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.InnerException.Message;
            }
            return result;
        }

        /// <summary>
        /// 新增抽驗係數
        /// </summary>
        /// <param name="qcQuot"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<QcQuot>> PostQcQuot(QcQuot qcQuot)
        {
            ResultModel<QcQuot> result = new ResultModel<QcQuot>();
            Helper helper = new Helper(_context);
            qcQuot.QuotID = helper.GetIDKey("QUOT_ID").Result;
            _context.QcQuots.Add(qcQuot);
            try
            {
                await _context.SaveChangesAsync();
                result.Success = true;
                result.Msg = "OK";
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.InnerException.Message;
            }
            return result;
        }

        /// <summary>
        /// 刪除抽驗係數
        /// </summary>
        /// <param name="id">QuotID</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ResultModel<string>> DeleteQcQuot(int id)
        {
            ResultModel<string> result = new ResultModel<string>();
            var qcQuot = await _context.QcQuots.FindAsync(id);

            try
            {
                if (qcQuot == null)
                {
                    result.Success = false;
                    result.Msg = "找不到要刪除資料";
                }
                else
                {
                    _context.QcQuots.Remove(qcQuot);
                    await _context.SaveChangesAsync();
                    result.Success = true;
                    result.Msg = "OK";
                }
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.InnerException.Message;
            }
            return result;
        }

        private bool QcQuotExists(int id)
        {
            return _context.QcQuots.Any(e => e.QuotID == id);
        }
    }
}