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

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace AMESCoreStudio.WebApi.Controllers.AMES
{
    /// <summary>
    /// 補印資料表
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class RLabelReprintController : ControllerBase
    {

        private readonly AMESContext _context;

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

        /// <summary>
        /// 查詢補印資料表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult<IEnumerable<RLabelReprint>>> GetRLabelReprint()
        {
            IQueryable<RLabelReprint> q = _context.RLabelReprints;
            return await q.ToListAsync();
        }


        /// <summary>
        /// 補印資料表 by 序號查詢
        /// </summary>
        /// <param name="id">BarcodeID</param>
        /// <returns></returns>
        [HttpGet("BySN/{id}")]
        public async Task<ActionResult<IEnumerable<RLabelReprint>>> GetRLabelReprintBySN(string id)
        {
            IQueryable<RLabelReprint> q = _context.RLabelReprints.Where(w => w.SerialNumber == id );

            return await q.ToListAsync();
        }

        /// <summary>
        /// 新增補印資料表
        /// </summary>
        /// <param name="rLabelReprint"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<RLabelReprint>> PostRLabelReprint([FromBody] RLabelReprint rLabelReprint)
        {
            ResultModel<RLabelReprint> result = new ResultModel<RLabelReprint>();
            _context.RLabelReprints.Add(rLabelReprint);
            try
            {
                await _context.SaveChangesAsync();
                result.Success = true;
                result.Msg = "OK";
            }
            catch (DbUpdateException ex)
            {

                result.Success = false;
                result.Msg = ex.InnerException.Message;
            }
         
            return result;
        }

       
    }
}