using NFine.Application.WMS;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NFine.Code;
using System.Data.SqlClient;
using NFine.Data.Extensions;
using System.Data.OleDb;
using System.Configuration;
using ICS.Application.Entity;

namespace NFine.Web.Areas.WMS.Controllers
{
    public class ICSRCVIQCsController : ControllerBase
    {

        ICSRCVIQCsApp App = new ICSRCVIQCsApp();
        // GET: WMS/ICSRCVIQCs
        public ActionResult ICSRCVIQCs()
        {
            return View();
        }
        public ActionResult Rejection()
        {
            return View();
        }
        public ActionResult WWRejection()
        {
            return View();
        }
        public ActionResult ICSInspectionDetail()
        {
            return View();
        }
        public ActionResult ICSLookFiles()
        {
            return View();
        }
        

        //采购
        [HttpGet]
        public ActionResult GetICSInspection(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetICSInspection(ref pagination, queryJson);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }
        [HttpGet]
        public ActionResult GetInspectionFile(Pagination pagination, string JYID, string queryJson)
        {
            DataTable ListData = App.GetInspectionFile(ref pagination, queryJson, JYID);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }
        //委外
        [HttpGet]
        public ActionResult GetICSInspection2(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetICSInspection2(ref pagination, queryJson);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        //工单
        [HttpGet]
        public ActionResult GetICSInspection3(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetICSInspection3(ref pagination, queryJson);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        //其他入库
        [HttpGet]
        public ActionResult GetICSInspection4(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetICSInspection4(ref pagination, queryJson);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        //生产退料
        [HttpGet]
        public ActionResult GetICSInspection5(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetICSInspection5(ref pagination, queryJson);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        //委外退料
        [HttpGet]
        public ActionResult GetICSInspection6(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetICSInspection6(ref pagination, queryJson);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        //销售退货
        [HttpGet]
        public ActionResult GetICSInspection7(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetICSInspection7(ref pagination, queryJson);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }


        //不良原因
        [HttpGet]
        public ActionResult Select_ICSBadReason(string BCCode)
        {
            var data = App.Select_ICSBadReason(BCCode);
            return Content(data.ToJson());
        }


        
        [HttpGet]
        public ActionResult Select_ChangeA(string BCCode)
        {
            var data = App.Select_ChangeA(BCCode);
            return Content(data.ToJson());
        }

        
        [HttpGet]
        public ActionResult Select_ChangeB(string BRCode)
        {
            var data = App.Select_ChangeB(BRCode);
            return Content(data.ToJson());
        }


        //不良原因组
        [HttpGet]
        public ActionResult GetSelectICSBadCode(string BRCode)
        {
            var data = App.GetSelectICSBadCode(BRCode);
            return Content(data.ToJson());
        }

        /// <summary>
        /// 创建检验表
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult CreateICSInspection(string keyValue, string ICSInspections)
        {
            string msg = App.CreateICSInspection(keyValue, ICSInspections);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("添加成功!");
            }
        }

        //生成拒收单
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult CreateRejection(string keyValue,string Type)
        {
            string msg = "";
            if (Type=="1")
            {
                msg = App.CreateRejection(keyValue);
            }
            else if(Type == "8")
            {
                msg = App.CreateWLYRejection(keyValue);
            }
            else if (Type == "9")
            {
                msg = App.CreateWWWLYRejection(keyValue);
            }
            else
            {
                msg = App.CreateWWRejection(keyValue);
            }
            
            if (string.IsNullOrEmpty(msg))
            {
                return Success("操作成功!");
            }
            else
            {
                return Error("" + msg + "");
            }
        }


        /// <summary>
        /// 删除拒收单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DelectRejection(string keyValue)
        {
            string msg = App.DelectRejection(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("删除成功!");
            }
            else
            {
                return Error(msg);
            }
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DelectWWRejection(string keyValue)
        {
            string msg = App.DelectWWRejection(keyValue);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("删除成功!");
            }
            else
            {
                return Error(msg);
            }
        }

        [HttpGet]
        public ActionResult GetInventoryInspection(Pagination pagination, string InvCode, int SampleQuantity, string ResultINp)
        {
            DataTable ListData = App.GetInventoryInspection(ref pagination, InvCode, SampleQuantity, ResultINp);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult SaveICSInspectionDetail(string ICSMTDOC, string InvCode, string ResultINp)
        {
            string msg = App.SaveICSInspectionDetail(ICSMTDOC, InvCode, ResultINp);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("保存成功!");
            }
        }
        /// <summary>
        /// 上传文件(检验信息)
        /// </summary>
        /// <param name="BidCode"></param>
        /// <param name="ID"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpLoadFileImport(string ID,string DNCode)
        {
            string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;

            string UploadfileName = "";
            string fileName = "";
            //获取上传的文件集合
            try
            {
                HttpFileCollection httpFile = System.Web.HttpContext.Current.Request.Files;
                for (int i = 0; i < httpFile.Count; i++)
                {
                    //获取送货单号
                    //string STNO = Request.Form["STNO"].ToString();
                    UploadfileName = Path.GetFileNameWithoutExtension(httpFile[i].FileName)
                         + Path.GetExtension(httpFile[i].FileName);
                     fileName = DNCode + Path.GetExtension(httpFile[i].FileName);
                    string Paths = System.Web.HttpContext.Current.Server.MapPath("~\\File\\InspectionFile\\");
                    if (!Directory.Exists(Paths))
                    {
                        Directory.CreateDirectory(Paths);
                    }
                    string filePath = System.Web.HttpContext.Current.Server.MapPath("~\\File\\InspectionFile\\" + UploadfileName);

                    int iLen = httpFile[0].ContentLength;
                    byte[] bData = new byte[iLen];
                    httpFile[0].InputStream.Read(bData, 0, iLen);
                    FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate);
                    fs.Write(bData, 0, bData.Length);
                    fs.Flush();
                    fs.Close();
                    fs.Dispose();
                    App.InsertICSInspectionFile(ID, UploadfileName, fileName);
                }
                return Success("上传成功!");
            }
            catch (Exception ex)
            {

                return Error(ex.Message);
            }
        }

        //采购到货单检验
        [HttpGet]
        public ActionResult GetICSInspection8(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetICSInspection8(ref pagination, queryJson);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }


        //委外采购到货单检验
        [HttpGet]
        public ActionResult GetICSInspection9(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetICSInspection9(ref pagination, queryJson);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }
        

    }
}