using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using WK_KLI_LAPS_Service.App_Data;
using WK_KLI_LAPS_Service.Models.Master;


namespace WK_KLI_LAPS_Service.Controllers.Master.Vendor
{
    [EnableCors("CorsPolicy")]
    [Authorize]
    [ApiController]
    [Route("api/[controller]")]


    public class VendorApiController : ControllerBase
    {
        CommonRepository oCommonRepository = new CommonRepository();

        private IVendorService _VendorService;
        public VendorApiController(IVendorService VendorService)
        {
            _VendorService = VendorService;
        }

        [Authorize]
        [HttpGet]
        //Code to retrieve all data
        public IActionResult GetAll()
        {
            var _list = _VendorService.GetAll();
            return Ok(_list);
        }

        [Authorize]
        [HttpGet("GetActiveApplication")]
        //Code to retrieve active data
        public IActionResult GetActiveApplication()
        {
            var _list = _VendorService.GetActiveVendor();
            return Ok(_list);
        }

        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] vendorModel ovendorModel)
        {
            string retval = "0";
            try
            {
                retval = _VendorService.Add(ovendorModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }

        //Code to update specific record
        [Authorize]
        [HttpPost("Update")]
        public IActionResult Update([FromBody] vendorModel ovendorModel)
        {
            string retval = "0";
            try
            {
                retval = _VendorService.Edit(ovendorModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }


        //Code to activate or deactivate specific record
        [Authorize]
        [HttpPost("activeordeactive")]
        public IActionResult Delete([FromBody] vendorModel ovendorModel)
        {
            int retval = 0;
            try
            {
                retval = _VendorService.Delete(ovendorModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }



    }
}