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
{
    /// <summary>
    /// 檔案用途    條碼工單資料檔
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class BarcodeWipController : ControllerBase
    {
        private readonly AMESContext _context;

        public BarcodeWipController(AMESContext context)
        {
            _context = context;
        }

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

        // GET: api/BarcodeWip/5
        [HttpGet("{id}/{wipid}")]
        public async Task<ActionResult<BarcodeWip>> GetBarcodeWipByTwoKey(int id, int wipid)
        {
            var barcodeWip = await _context.BarcodeWips.Where(w => w.BarcodeID == id && w.WipID == wipid).FirstOrDefaultAsync();
            return barcodeWip;
        }

        // PUT: api/BarcodeWip/5
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
        [HttpPut("{id}")]
        public async Task<ResultModel<BarcodeWip>> PutBarcodeWip([FromBody] BarcodeWip barcodeWip)
        {
            ResultModel<BarcodeWip> result = new ResultModel<BarcodeWip>();
            _context.Attach(barcodeWip);
            barcodeWip.UpdateDate = DateTime.Now;
            // 指定更新某個欄位
            _context.Entry(barcodeWip).Property(p => p.UpdateDate).IsModified = true;

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

        // POST: api/BarcodeWip
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
        [HttpPost]
        public async Task<ResultModel<BarcodeWip>> PostBarcodeWip([FromBody] BarcodeWip barcodeWip)
        {
            ResultModel<BarcodeWip> result = new ResultModel<BarcodeWip>();

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

        // DELETE: api/BarcodeWip/5
        [HttpDelete("{id}")]
        public async Task<ActionResult<BarcodeWip>> DeleteBarcodeWip(decimal id)
        {
            var barcodeWip = await _context.BarcodeWips.FindAsync(id);
            if (barcodeWip == null)
            {
                return NotFound();
            }

            _context.BarcodeWips.Remove(barcodeWip);
            await _context.SaveChangesAsync();

            return barcodeWip;
        }

        private bool BarcodeWipExists(decimal id)
        {
            return _context.BarcodeWips.Any(e => e.WipID == id);
        }
    }
}