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 ICSMTDOCController : ControllerBase
    {
        ICSMTDOCApp App = new ICSMTDOCApp();
        // GET: WMS/ICSMTDOC
        public ActionResult Metamorphosis()
        {
            return View();
        }
        
        public ActionResult ICSMTDOCAdd()
        {
            return View();
        }
        public ActionResult GetICSInventory()
        {
            return View();
        }
        public ActionResult GetBatchCode()
        {
            return View();
        }
        
        public ActionResult MetamorphosisToLead()
        {
            return View();
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJson(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetGridJson(queryJson, ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSubGridJson(string MTDOCCode)
        {
            DataTable ListData = App.GetSubGridJson(MTDOCCode);
            var JsonData = new
            {
              
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        //在库条码
        public ActionResult GetRepertory(string LotNo ,Pagination pagination)
        {
            DataTable ListData = App.GetRepertory(LotNo, ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }


        public ActionResult GetInvcode(string Invcode, Pagination pagination)
        {
            DataTable ListData = App.GetInvcode(Invcode, ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }



        public ActionResult GetCode(string Type, string Common ,Pagination pagination)
        {
            DataTable ListData = App.GetCode(Type, Common, ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }


   

        /// <summary>
        /// 创建形态转换
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult CreateICSMTDOC(string ICSMTDOC, string InvCode, string Memo)
        {
             string msg = App.CreateICSMTDOC(ICSMTDOC, InvCode, Memo);
            if (!string.IsNullOrEmpty(msg))
            {
                return Error(msg);
            }
            else
            {
                return Success("添加成功!");
            }
        }

        //审核 
        public ActionResult ICSMTDOCAudit(string MTDOCCode)
        {
            string msg = App.ICSMTDOCAudit(MTDOCCode);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("形态转换成功!");
            }
            else
            {
                return Error("" + msg + "");
            }
        }




        [HttpPost]
        /// <summary>
        /// 文件上传到本地
        /// </summary>
        public string UploadFile()
        {
            try
            {
                string str_Year = Request.Form["txt_Year"];
                String UPLoadType = Request.Form["UPLoadType"];
                HttpFileCollection hpFiles = System.Web.HttpContext.Current.Request.Files;
                if (hpFiles != null && hpFiles.Count > 0)
                {

                    string IsXls = System.IO.Path.GetExtension(hpFiles[0].FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名
                    if (IsXls != ".xls" && IsXls != ".xlsx")
                    {

                        return "只可以选择Excel(.xls .xlsx)文件";//当选择的不是Excel文件时,返回
                    }
                    string filename = DateTime.Now.ToString("yyyyMMddhhmmss") + UPLoadType + IsXls;             //获取Execle文件名 DateTime日期函数
                    string savePath = System.Web.HttpContext.Current.Server.MapPath("~\\File\\UPLoadFile\\" + filename);//Server.MapPath 获得虚拟服务器相对路径

                    int iLen = hpFiles[0].ContentLength;
                    if (Directory.Exists(savePath)) return "文件已存在";
                    byte[] bData = new byte[iLen];
                    hpFiles[0].InputStream.Read(bData, 0, iLen);


                    FileStream newFile = new FileStream(savePath, FileMode.OpenOrCreate);
                    newFile.Write(bData, 0, bData.Length);
                    newFile.Flush();
                    int _FileSizeTemp = hpFiles[0].ContentLength;

                    newFile.Close();
                    newFile.Dispose();
                    //bool del = false;
                    string mess = "";
                    mess = App.ToLead(savePath, str_Year);
                    if (System.IO.File.Exists(savePath))//删除文件
                    {

                        System.IO.File.Delete(savePath);
                    }
                    return mess;

                }
                else
                {
                    return "获取文件失败";
                }
            }
            catch (Exception ex)
            {

                return ex.ToString();
            }
        }



    }
}