using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using FineUIPro.Web.HJGL.BaseInfo;
using Newtonsoft.Json.Linq;

namespace FineUIPro.Web.ZHGL.DataSync.ProjectDataSync
{
    public partial class Project_HJGLData_HJGLEdit : PageBase
    {
        #region  
        /// <summary>
        /// 主键
        /// </summary>
        public string Id
    {
        get
        {
            return (string)ViewState["Id"];
        }
        set
        {
            ViewState["Id"] = value;
        }
    }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnClose.OnClientClick = ActiveWindow.GetHideReference();
                ////权限按钮方法
                this.GetButtonPower();
                this.Id = Request.Params["Id"];
                BLL.Base_DefectService.InitDefectDropDownList(DropDefect, false, null);
                if (!string.IsNullOrEmpty(this.Id))
                {
                    Model.Project_HJGLData_HJGL model = BLL.Project_HJGLData_HJGLService.GetProject_HJGLData_HJGLById(this.Id);
                    if (model != null)
                    {
                        //this.txtUnitId.Text = model.UnitId;
                        this.txtCollCropCode.Text = model.CollCropCode;
                        this.txtUnitName.Text = model.UnitName;
                        if (model.ReportDate != null)
                        {
                            this.txtReportDate.SelectedDate = model.ReportDate;
                        }
                        if (model.WelderNum.HasValue)
                        {
                            this.txtWelderNum.Text = model.WelderNum.Value.ToString();
                        }
                        if (model.TotalDineNum.HasValue)
                        {
                            this.txtTotalDineNum.Text = model.TotalDineNum.Value.ToString();
                        }
                        if (model.CompleteDineNum.HasValue)
                        {
                            this.txtCompleteDineNum.Text = model.CompleteDineNum.Value.ToString();
                        }
                        if (model.TotalFilmNum.HasValue)
                        {
                            this.txtTotalFilmNum.Text = model.TotalFilmNum.Value.ToString();
                        }
                        if (model.OKFilmNum.HasValue)
                        {
                            this.txtOKFilmNum.Text = model.OKFilmNum.Value.ToString();
                        }

                    }
                }
                else
                {
                    var base_Unit = BLL.UnitService.GetUnitByUnitId(BLL.Const.UnitId_TCC);
                    txtUnitName.Text = base_Unit.UnitName;
                    txtCollCropCode.Text = base_Unit.CollCropCode;
                    txtReportDate.SelectedDate = DateTime.Now;

                }
                BindGrid();
                #region Grid1
                // 删除选中单元格的客户端脚本
                string deleteScript = GetDeleteScript();

                JObject defaultObj = new JObject();
                defaultObj.Add("DefectName", "");
                defaultObj.Add("DefectNum", "");

                // 在第一行新增一条数据
                btnNew.OnClientClick = Grid1.GetAddNewRecordReference(defaultObj, true);
                // 删除选中行按钮
                btnDelete.OnClientClick = Grid1.GetNoSelectionAlertReference("请选择一条记录!") + deleteScript;
                #endregion

            }
        }
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (txtReportDate.SelectedDate == null)
            {

                ShowNotify("请选择上报日期!");
                return;

            }
            Model.Project_HJGLData_HJGL table = new Model.Project_HJGLData_HJGL();
            table.UnitId = BLL.Const.UnitId_TCC;
            table.ProjectId = this.CurrUser.LoginProjectId;
            table.CollCropCode = this.txtCollCropCode.Text;
            table.UnitName = this.txtUnitName.Text;
            table.ReportDate = this.txtReportDate.SelectedDate;
            table.WelderNum = Funs.GetNewInt(this.txtWelderNum.Text);
            table.TotalDineNum = Funs.GetNewInt(this.txtTotalDineNum.Text);
            table.CompleteDineNum = Funs.GetNewInt(this.txtCompleteDineNum.Text);
            table.TotalFilmNum = Funs.GetNewInt(this.txtTotalFilmNum.Text);
            table.OKFilmNum = Funs.GetNewInt(this.txtOKFilmNum.Text);
            if (string.IsNullOrEmpty(this.Id))
            {
                table.Id = SQLHelper.GetNewID(typeof(Model.Project_HJGLData_HJGL));
                BLL.Project_HJGLData_HJGLService.AddProject_HJGLData_HJGL(table);

            }
            else
            {
                table.Id = this.Id;
                BLL.Project_HJGLData_HJGLService.UpdateProject_HJGLData_HJGL(table);
            }
            BLL.Project_HJGLData_DefectService.DeleteProject_HJGLData_DefectByDate(table.ReportDate, this.CurrUser.LoginProjectId);
            JArray EditorArr = Grid1.GetMergedData();
            if (EditorArr.Count > 0)
            {
                Model.Project_HJGLData_Defect defect = new Model.Project_HJGLData_Defect();

                for (int i = 0; i < EditorArr.Count; i++)
                {
                    JObject objects = (JObject)EditorArr[i];
                    defect.UnitId = table.UnitId;
                    defect.ProjectId = table.ProjectId;
                    defect.CollCropCode = table.CollCropCode;
                    defect.UnitName = table.UnitName;
                    defect.ReportDate = table.ReportDate;
                    defect.DefectName = objects["values"]["DefectName"].ToString();
                    defect.DefectNum = Funs.GetNewInt(objects["values"]["DefectNum"].ToString());
                    defect.Id = SQLHelper.GetNewID(typeof(Model.Project_HJGLData_Defect));
                    BLL.Project_HJGLData_DefectService.AddProject_HJGLData_Defect(defect);
                }
            }
            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }


    private void BindGrid()
    {
        if (!string.IsNullOrEmpty(this.Id))
        {
                Model.Project_HJGLData_HJGL model = BLL.Project_HJGLData_HJGLService.GetProject_HJGLData_HJGLById(this.Id);
                if (model != null)
                {
                  var dt=  BLL.Project_HJGLData_DefectService.GetProject_HJGLData_DefectByDate(model.ReportDate,this.CurrUser.LoginProjectId);
                    Grid1.RecordCount = dt.Count;
                   // var table = this.GetPagedDataTable(Grid1, tb);
                    Grid1.DataSource = dt;
                    Grid1.DataBind();
                }

        }

    }
        protected void txtReportDate_DateSelect(object sender, EventArgs e)
        {
            if (txtReportDate.SelectedDate != null)
            {
                DateTime dt = (DateTime)txtReportDate.SelectedDate;
                if (BLL.Project_HJGLData_HJGLService.IsReportByDate(dt, this.CurrUser.LoginProjectId))
                {
                    txtReportDate.SelectedDate = null;
                    ShowNotify("该日期已上报!");

                }
            }
        }
        //protected void btnSyn_Click(object sender, EventArgs e)
        //{
        //    if (!string.IsNullOrEmpty(this.Id))
        //    {
        //        var responedata = Project_HJGLData_HJGLService.PushCNCEC(Id);
        //        if (responedata.code == 1)
        //        {
        //            ShowNotify("上报成功!");
        //            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());

        //        }
        //        else
        //        {
        //            ShowNotify(responedata.message);
        //            // PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        //        }

        //    }


        //}
        private string GetDeleteScript()
        {
            return Confirm.GetShowReference("确定删除当前数据吗?", String.Empty, MessageBoxIcon.Question, Grid1.GetDeleteSelectedRowsReference(), String.Empty);
        }
    #region 获取按钮权限
    /// <summary>
    /// 获取按钮权限
    /// </summary>
    /// <param name="button"></param>
    /// <returns></returns>
    private void GetButtonPower()
    {
        var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.Project_HJGLData_HJGLMenuId);
        if (buttonList.Count() > 0)
        {
            if (buttonList.Contains(BLL.Const.BtnSave))
            {
                this.btnSave.Hidden = false;
            }
        }
    }
    #endregion

}
}