using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using AMESCoreStudio.WebApi.Models.AMES;
using AMESCoreStudio.CommonTools.Result;
using AMESCoreStudio.WebApi.DTO.AMES;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace AMESCoreStudio.WebApi.Controllers.AMES
{
    [Route("api/[controller]")]
    [ApiController]
    public class MatPropertyController : ControllerBase
    {
        private readonly AMESContext _context;

        /// <summary>
        /// 料號属性代碼基本檔
        /// </summary>
        /// <param name="context"></param>
        public MatPropertyController(AMESContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 料號属性代碼基本檔資料
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult<IEnumerable<MatProperty>>> GetMatProperty()
        {
            IQueryable<MatProperty> q = _context.MatPropertys;
            q = q.OrderBy(p => p.MatPropertyNo);
            var MatProperty = await q.ToListAsync();
            return MatProperty;
        }

        /// <summary>
        /// 料號属性代碼基本檔
        /// </summary>
        /// <param name="id">MatPropertyID</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ActionResult<IEnumerable<MatProperty>>> GetMatProperty(string id)
        {
            IQueryable<MatProperty> q = _context.MatPropertys;
            var MatProperty = await q.Where(p => p.MatPropertyNo == id).ToListAsync();

            //if (result.Data.Count() == 0)
            //{
            //    return NotFound();
            //}

            return MatProperty;
        }

        ///// <summary>
        ///// 工單鎖定資料 to 工單號碼
        ///// </summary>
        ///// <param name="id">工單號碼</param>
        ///// <returns></returns>
        //[HttpGet("ByWipNO/{id}")]
        //public async Task<ResultModel<MatProperty>> GetMatPropertyByWipNO(string id)
        //{
        //    IQueryable<MatProperty> q = _context.MatPropertys;

        //    ResultModel<MatProperty> result = new ResultModel<MatProperty>();
        //    result.Data = await q.Where(p => p.WipNO == id).ToListAsync();

        //    //if (result.Data.Count() == 0)
        //    //{
        //    //    return NotFound();
        //    //}

        //    return result;
        //}



        /// <summary>
        /// 新增料號属性代碼基本檔
        /// </summary>
        /// <param name="MatProperty"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<MatProperty>> PostMatProperty([FromBody] MatProperty MatProperty)
        {
            ResultModel<MatProperty> result = new ResultModel<MatProperty>();

            _context.MatPropertys.Add(MatProperty);
            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>
        /// <returns></returns>
        [HttpPut]
        public async Task<ResultModel<MatProperty>> PutMatProperty([FromBody] MatProperty MatProperty)
        {
            ResultModel<MatProperty> result = new ResultModel<MatProperty>();
            _context.Attach(MatProperty);
            //MatProperty.LockStatus = "1";
            //MatProperty.UnLockUserID = 0;
            //MatProperty.UnLockDate = DateTime.Now;
            // 指定更新某個欄位
            //_context.Entry(MatProperty).Property(p => p.UnLockReason).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;
        }
    }
}