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 WipFqcItemController : ControllerBase
    {
        private readonly AMESContext _context;

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

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

        // GET: api/WipFqcItem/5
        [HttpGet("{id}")]
        public async Task<ActionResult<WipFqcItem>> GetWipFqcItem(int id)
        {
            var wipFqcItem = await _context.WipFqcItems.FindAsync(id);

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

            return wipFqcItem;
        }

        /// <summary>
        /// 工單號碼查詢綁定檢驗工項
        /// </summary>
        /// <param name="id">工單號碼</param>
        /// <returns></returns>
        [HttpGet("ByWipNo/{id}")]
        public async Task<ActionResult<IEnumerable<WipFqcItem>>> GetWipFqcItemByWipNo(string id)
        {
            return await _context.WipFqcItems.Where(w => w.WipNo == id).ToListAsync();
        }

        /// <summary>
        /// 更新工單對應工項資料檔
        /// </summary>
        /// <param name="wipFqcItem"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ResultModel<WipFqcItem>> PutWipFqcItem(WipFqcItem wipFqcItem)
        {
            ResultModel<WipFqcItem> result = new ResultModel<WipFqcItem>();
            _context.Entry(wipFqcItem).State = EntityState.Modified;
            wipFqcItem.UpdateDate = DateTime.Now;

            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="wipFqcItem"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<WipFqcItem>> PostWipFqcItem(WipFqcItem wipFqcItem)
        {
            ResultModel<WipFqcItem> result = new ResultModel<WipFqcItem>();
            Helper helper = new Helper(_context);
            wipFqcItem.WipFqcitemID = helper.GetIDKey("WIP_FQCITEM_ID").Result;
            wipFqcItem.CreateDate = DateTime.Now;
            wipFqcItem.UpdateDate = DateTime.Now;
            _context.WipFqcItems.Add(wipFqcItem);

            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/WipFqcItem/5
        [HttpDelete("{id}")]
        public async Task<ActionResult<WipFqcItem>> DeleteWipFqcItem(int id)
        {
            var wipFqcItem = await _context.WipFqcItems.FindAsync(id);
            if (wipFqcItem == null)
            {
                return NotFound();
            }

            _context.WipFqcItems.Remove(wipFqcItem);
            await _context.SaveChangesAsync();

            return wipFqcItem;
        }

        private bool WipFqcItemExists(int id)
        {
            return _context.WipFqcItems.Any(e => e.WipFqcitemID == id);
        }
    }
}