using BLL;
using BLL.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using ThoughtWorks.QRCode.Codec;
using AspNet = System.Web.UI.WebControls;
namespace FineUIPro.Web.HSSE.QualityAudit
{
    public partial class GeneralEquipmentQuality : PageBase
    {
        #region 项目主键
        /// 
        /// 项目主键
        /// 
        public string ProjectId
        {
            get
            {
                return (string)ViewState["ProjectId"];
            }
            set
            {
                ViewState["ProjectId"] = value;
            }
        }
        #endregion
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Funs.DropDownPageSize(this.ddlPageSize);
                ////权限按钮方法
                this.GetButtonPower();
                this.ProjectId = this.CurrUser.LoginProjectId;
                if (!string.IsNullOrEmpty(Request.Params["projectId"]) && Request.Params["projectId"] != this.CurrUser.LoginProjectId)
                {
                    this.ProjectId = Request.Params["projectId"];
                }
                this.btnNew.OnClientClick = Window1.GetShowReference("GeneralEquipmentQualityEdit.aspx") + "return false;";
                if (this.CurrUser != null && this.CurrUser.PageSize.HasValue)
                {
                    Grid1.PageSize = this.CurrUser.PageSize.Value;
                }
                this.ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
                // 绑定表格
                this.BindGrid();
            }
        }
        /// 
        /// 绑定数据
        /// 
        private void BindGrid()
        {
            string strSql = @"SELECT GeneralEquipmentQuality.GeneralEquipmentQualityId,"
                          + @"GeneralEquipmentQuality.ProjectId,"
                          + @"CodeRecords.Code AS GeneralEquipmentQualityCode,"
                          + @"GeneralEquipmentQuality.UnitId,"
                          + @"GeneralEquipmentQuality.SpecialEquipmentId,"
                          + @"GeneralEquipmentQuality.EquipmentCount,"
                          + @"GeneralEquipmentQuality.InDate,"
                          + @"GeneralEquipmentQuality.IsQualified,"
                          + @"GeneralEquipmentQuality.Remark,"
                          + @"GeneralEquipmentQuality.CompileMan,"
                          + @"GeneralEquipmentQuality.CompileDate,"
                          + @"Unit.UnitName,"
                          + @"SpecialEquipment.SpecialEquipmentName"
                          + @" FROM QualityAudit_GeneralEquipmentQuality AS GeneralEquipmentQuality "
                          + @" LEFT JOIN Sys_CodeRecords AS CodeRecords ON GeneralEquipmentQuality.GeneralEquipmentQualityId = CodeRecords.DataId "
                          + @" LEFT JOIN Base_Unit AS Unit ON Unit.UnitId = GeneralEquipmentQuality.UnitId "
                          + @" LEFT JOIN Base_SpecialEquipment AS SpecialEquipment ON SpecialEquipment.SpecialEquipmentId = GeneralEquipmentQuality.SpecialEquipmentId "
                          + @" LEFT JOIN Sys_User AS Users ON GeneralEquipmentQuality.CompileMan = Users.UserId WHERE 1=1 ";
            List listStr = new List();
            if (!string.IsNullOrEmpty(this.CurrUser.LoginProjectId))
            {
                strSql += " AND GeneralEquipmentQuality.ProjectId = @ProjectId";
                listStr.Add(new SqlParameter("@ProjectId", this.CurrUser.LoginProjectId));
            }
            if (BLL.ProjectUnitService.GetProjectUnitTypeByProjectIdUnitId(this.ProjectId, this.CurrUser.UnitId))
            {
                strSql += " AND GeneralEquipmentQuality.UnitId = @UnitId";  ///状态为已完成
                listStr.Add(new SqlParameter("@UnitId", this.CurrUser.UnitId));
            }
            if (!string.IsNullOrEmpty(this.txtGeneralEquipmentQualityCode.Text.Trim()))
            {
                strSql += " AND GeneralEquipmentQualityCode LIKE @GeneralEquipmentQualityCode";
                listStr.Add(new SqlParameter("@GeneralEquipmentQualityCode", "%" + this.txtGeneralEquipmentQualityCode.Text.Trim() + "%"));
            }
            if (!string.IsNullOrEmpty(this.txtUnitName.Text.Trim()))
            {
                strSql += " AND UnitName LIKE @UnitName";
                listStr.Add(new SqlParameter("@UnitName", "%" + this.txtUnitName.Text.Trim() + "%"));
            }
            if (!string.IsNullOrEmpty(this.txtSpecialEquipmentName.Text.Trim()))
            {
                strSql += " AND SpecialEquipmentName LIKE @SpecialEquipmentName";
                listStr.Add(new SqlParameter("@SpecialEquipmentName", "%" + this.txtSpecialEquipmentName.Text.Trim() + "%"));
            }
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            Grid1.RecordCount = tb.Rows.Count;
            var table = this.GetPagedDataTable(Grid1, tb);
            Grid1.DataSource = table;
            Grid1.DataBind();
        }
        /// 
        /// 改变索引事件
        /// 
        /// 
        /// 
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            BindGrid();
        }
        /// 
        /// 分页下拉选择事件
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Grid1.PageSize = Convert.ToInt32(this.ddlPageSize.SelectedValue);
            BindGrid();
        }
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e)
        {
            this.BindGrid();
        }
        #endregion
        #region 查询
        /// 
        /// 查询
        /// 
        /// 
        /// 
        protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            this.BindGrid();
        }
        #endregion
        #region 编辑
        /// 
        /// 双击事件
        /// 
        /// 
        /// 
        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            this.EditData();
        }
        /// 
        /// 右键编辑事件
        /// 
        /// 
        /// 
        protected void btnMenuEdit_Click(object sender, EventArgs e)
        {
            this.EditData();
        }
        /// 
        /// 编辑数据方法
        /// 
        private void EditData()
        {
            if (Grid1.SelectedRowIndexArray.Length == 0)
            {
                Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning);
                return;
            }
            string id = Grid1.SelectedRowID;
            var generalEquipmentQuality = BLL.GeneralEquipmentQualityService.GetGeneralEquipmentQualityById(id);
            if (generalEquipmentQuality != null)
            {
                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("GeneralEquipmentQualityEdit.aspx?GeneralEquipmentQualityId={0}", id, "编辑 - ")));
            }
        }
        #endregion
        #region 删除
        /// 
        /// 右键删除事件
        /// 
        /// 
        /// 
        protected void btnMenuDelete_Click(object sender, EventArgs e)
        {
            if (Grid1.SelectedRowIndexArray.Length > 0)
            {
                foreach (int rowIndex in Grid1.SelectedRowIndexArray)
                {
                    string rowID = Grid1.DataKeys[rowIndex][0].ToString();
                    var generalEquipmentQuality = BLL.GeneralEquipmentQualityService.GetGeneralEquipmentQualityById(rowID);
                    if (generalEquipmentQuality != null)
                    {
                        BLL.LogService.AddSys_Log(this.CurrUser, generalEquipmentQuality.GeneralEquipmentQualityCode, generalEquipmentQuality.GeneralEquipmentQualityId, BLL.Const.GeneralEquipmentQualityMenuId, BLL.Const.BtnDelete);
                        BLL.GeneralEquipmentQualityService.DeleteGeneralEquipmentQualityById(rowID);
                    }
                }
                this.BindGrid();
                ShowNotify("删除数据成功!", MessageBoxIcon.Success);
            }
        }
        #endregion
        #region 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private void GetButtonPower()
        {
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.GeneralEquipmentQualityMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnAdd))
                {
                    this.btnNew.Hidden = false;
                }
                if (buttonList.Contains(BLL.Const.BtnModify))
                {
                    this.btnMenuEdit.Hidden = false;
                }
                if (buttonList.Contains(BLL.Const.BtnDelete))
                {
                    this.btnMenuDelete.Hidden = false;
                }
            }
        }
        #endregion
        #region 导出按钮
        /// 导出按钮
        /// 
        /// 
        /// 
        protected void btnOut_Click(object sender, EventArgs e)
        {
            Response.ClearContent();
            string filename = Funs.GetNewFileName();
            Response.AddHeader("content-disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("一般机具设备资质" + filename, System.Text.Encoding.UTF8) + ".xls");
            Response.ContentType = "application/excel";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            this.Grid1.PageSize = this.Grid1.RecordCount;
            BindGrid();
            Response.Write(GetGridTableHtml(Grid1));
            Response.End();
        }
        #endregion
        protected void btnQR_Click(object sender, EventArgs e)
        {
            string strSql = @"SELECT GeneralEquipmentQuality.GeneralEquipmentQualityId,"
                          + @"CodeRecords.Code AS GeneralEquipmentQualityCode,"
                          + @"GeneralEquipmentQuality.GeneralEquipmentQualityCode,"
                          + @"GeneralEquipmentQuality.QRCodeAttachUrl,"
                          + @"SpecialEquipment.SpecialEquipmentName"
                          + @" FROM QualityAudit_GeneralEquipmentQuality AS GeneralEquipmentQuality "
                          + @" LEFT JOIN Base_SpecialEquipment AS SpecialEquipment ON SpecialEquipment.SpecialEquipmentId = GeneralEquipmentQuality.SpecialEquipmentId "
                          + @" LEFT JOIN Sys_CodeRecords AS CodeRecords ON GeneralEquipmentQuality.GeneralEquipmentQualityId = CodeRecords.DataId  WHERE 1=1 ";
            List listStr = new List();
            if (!string.IsNullOrEmpty(this.CurrUser.LoginProjectId))
            {
                strSql += " AND GeneralEquipmentQuality.ProjectId = @ProjectId";
                listStr.Add(new SqlParameter("@ProjectId", this.CurrUser.LoginProjectId));
            }
            if (BLL.ProjectUnitService.GetProjectUnitTypeByProjectIdUnitId(this.ProjectId, this.CurrUser.UnitId))
            {
                strSql += " AND GeneralEquipmentQuality.UnitId = @UnitId";  ///状态为已完成
                listStr.Add(new SqlParameter("@UnitId", this.CurrUser.UnitId));
            }
            if (!string.IsNullOrEmpty(this.txtGeneralEquipmentQualityCode.Text.Trim()))
            {
                strSql += " AND GeneralEquipmentQualityCode LIKE @GeneralEquipmentQualityCode";
                listStr.Add(new SqlParameter("@GeneralEquipmentQualityCode", "%" + this.txtGeneralEquipmentQualityCode.Text.Trim() + "%"));
            }
            if (!string.IsNullOrEmpty(this.txtUnitName.Text.Trim()))
            {
                strSql += " AND UnitName LIKE @UnitName";
                listStr.Add(new SqlParameter("@UnitName", "%" + this.txtUnitName.Text.Trim() + "%"));
            }
            if (!string.IsNullOrEmpty(this.txtSpecialEquipmentName.Text.Trim()))
            {
                strSql += " AND SpecialEquipmentName LIKE @SpecialEquipmentName";
                listStr.Add(new SqlParameter("@SpecialEquipmentName", "%" + this.txtSpecialEquipmentName.Text.Trim() + "%"));
            }
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            NPOIExcel excel = new NPOIExcel();
            excel.SetColumnWidth(0, 50);
            excel.SetColumnWidth(1, 50);
            excel.SetColumnWidth(2, 50);
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                if (i % 3 == 0)
                {
                    excel.SetRowHeight(2 * (int)(i / 3), 200);
                }
                string QRCodeAttachUrl = tb.Rows[i]["QRCodeAttachUrl"].ToString();
                string GeneralEquipmentQualityCode = tb.Rows[i]["GeneralEquipmentQualityCode"].ToString();
                if (string.IsNullOrEmpty(QRCodeAttachUrl))
                {
                    try
                    {
                        var equipmentQuality = Funs.DB.QualityAudit_GeneralEquipmentQuality.FirstOrDefault(x => x.GeneralEquipmentQualityId == tb.Rows[i]["GeneralEquipmentQualityId"].ToString());
                        equipmentQuality.QRCodeAttachUrl = CreateCode_Simple("equipments$" + tb.Rows[i]["GeneralEquipmentQualityId"].ToString());
                        QRCodeAttachUrl = equipmentQuality.QRCodeAttachUrl;
                        Funs.DB.SubmitChanges();
                    }
                    catch (Exception e1) { }
                }
                //add picture data to this workbook.
                excel.SetPicValue(2 * (int)(i / 3), i % 3, Server.MapPath("~/") + QRCodeAttachUrl);
                excel.SetValue(2 * (int)(i / 3) + 1, i % 3, GeneralEquipmentQualityCode);
            }
            string initTemplatePath = Const.ExcelUrl + Funs.GetNewFileName() + ".xls";
            string uploadfilepath = Server.MapPath("~/") + initTemplatePath;
            string fileName = Path.GetFileName(initTemplatePath);
            excel.Save(uploadfilepath);
            FileInfo info = new FileInfo(uploadfilepath);
            long fileSize = info.Length;
            Response.ClearContent();
            Response.ContentType = "application/x-zip-compressed";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            Response.AddHeader("Content-Length", fileSize.ToString());
            Response.TransmitFile(uploadfilepath, 0, fileSize);
            Response.Flush();
            Response.End();
        }
        private string CreateCode_Simple(string nr)
        {
            try
            {
                string imageUrl = string.Empty;
                QRCodeEncoder qrCodeEncoder = new QRCodeEncoder
                {
                    QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE,
                    QRCodeScale = nr.Length,
                    QRCodeVersion = 0,
                    QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M
                };
                System.Drawing.Image image = qrCodeEncoder.Encode(nr, Encoding.UTF8);
                string filepath = Server.MapPath("~/") + UploadFileService.QRCodeImageFilePath;
                //如果文件夹不存在,则创建
                if (!Directory.Exists(filepath))
                {
                    Directory.CreateDirectory(filepath);
                }
                string filename = DateTime.Now.ToString("yyyymmddhhmmssfff").ToString() + ".jpg";
                imageUrl = filepath + filename;
                FileStream fs = new FileStream(imageUrl, FileMode.OpenOrCreate, FileAccess.Write);
                image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
                fs.Close();
                image.Dispose();
                return UploadFileService.QRCodeImageFilePath + filename;
            }
            catch (Exception ex)
            {
            }
            return "";
        }
    }
}