using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.Text;
using System.IO;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using QMFrameWork.Common.Serialization;
using QMFrameWork.Common.ExcelOperation;
using QMAPP.Common.Web.Models;


namespace QMAPP.Common.Web.Controllers
{
    /// <summary>
    /// 信息输出控制器
    /// </summary>
    public class MessageOutputController : Controller
    {
        #region 输出导入数据提示信息

        public ActionResult OutputImportMessage(ImportMessageModel model)
        {
            string fileName = "";
            
            try
            {
                //获取导入配置信息
                SheetInfo mainInfo = new IEExcelHelper().GetMainInfo(model.InfoName);

                model.TotalRecordCount = model.ErrorDt.Rows.Count;
             
                //过滤异常数据
                for (int i = model.ErrorDt.Rows.Count - 1; i >= 0; i--)
                {
                    if (model.ErrorDt.Rows[i]["InfoError"] == System.DBNull.Value || model.ErrorDt.Rows[i]["InfoError"].ToString() == "")
                    {
                        model.ErrorDt.Rows.RemoveAt(i);
                    }
                }

                

                #region 构成异常数据表格

                model.TableHtml = "";
                model.TableHtml += "<div style=\"overflow-x: auto; overflow-y: auto; height: 100%; width: 100%;\">";
                model.TableHtml += "<table id=\"tblError\" style=\"width:100%;border: 1px solid #CCFFFF\" iconCls=\"icon-edit\">";
                model.TableHtml += "<thead class=\"datagrid-header\">";
                foreach (CellInfo item in mainInfo.ColInfos)
                {
                    model.TableHtml += string.Format("<th field=\"{0}\" nowrap=nowrap class=\"datagrid-header td\" align=\"center\" width=\"120px\">{1}</th>", item.ColumnName, item.ColumnTitle);
                }
                model.TableHtml += string.Format("<th field=\"InfoError\" nowrap=nowrap class=\"datagrid-header td\" align=\"center\" width=\"120px\">错误信息</th>");
                model.TableHtml += "</thead>";

                model.TableHtml += "<tbody class=\"datagrid-body_row\">";
                for (int i = 0; i < model.ErrorDt.Rows.Count; i++)
                {
                    model.TableHtml += "<tr>";
                    for (int j = 0; j < model.ErrorDt.Columns.Count; j++)
                    {
                        model.TableHtml += string.Format("<td nowrap=nowrap class=\"datagrid-body td\">{0}</td>", model.ErrorDt.Rows[i][model.ErrorDt.Columns[j].ColumnName].ToString());
                    }
                    model.TableHtml += "</tr>";
                }

                model.TableHtml += "</tbody>";                
                model.TableHtml += "</table>";
                model.TableHtml += "</div>";

                //model.TableHtml += "<script language=\"javascript\">$('#tblError').datagrid({nowrap:false;});</script>";

                #region 生成导出文件

                fileName = Guid.NewGuid().ToString() + ".xlsx";
                //生成导出文件
                QMFrameWork.WebUI.Util.IEFileTool efTool = new QMFrameWork.WebUI.Util.IEFileTool();
                byte[] fileContent = efTool.GetExcelFileContent(model.InfoName, fileName, model.ErrorDt);

                using (FileStream fs = System.IO.File.Create(QController.PhysicsRootPath + "Temp\\" + fileName))
                {
                    fs.Write(fileContent, 0, fileContent.Length);
                }
                model.ErrorFile = fileName;
                #endregion

                model.ErrorDt.Rows.Clear();
                model.ErrorDt.Dispose();
                #endregion



                #region 返回现实视图

                ViewResult vr = null;                

                vr = new ViewResult
                {
                    ViewName = "OutputImportMessage",
                    MasterName = "AppMaster",
                    ViewData = new ViewDataDictionary<ImportMessageModel>(model)
                };

                return vr;

                #endregion
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region 下载模板

        /// <summary>
        /// 下载导入模板
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public FilePathResult GetErrorFile()
        {
            try
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "Temp\\";
                string fileName = Request.Form["ErrorFile"];
                return File(path + fileName, "application/vnd.ms-excel", Url.Encode("错误信息列表.xlsx"));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}