using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Newtonsoft.Json.Linq;
namespace FineUIPro.Web.ZHGL.DataSync
{
    public partial class HJGLData_HJGLEdit : PageBase
    {
        #region  
        /// 
        /// 主键
        /// 
        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.HJGLData_HJGL model = BLL.HJGLData_HJGLService.GetHJGLData_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_CWCEC);
                    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
            }
        }
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (txtReportDate.SelectedDate == null)
            {
                ShowNotify("请选择上报日期!");
                return;
            }
            Model.HJGLData_HJGL table = new Model.HJGLData_HJGL();
            table.UnitId = BLL.Const.UnitId_CWCEC;
            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.HJGLData_HJGL));
                BLL.HJGLData_HJGLService.AddHJGLData_HJGL(table);
            }
            else
            {
                table.Id = this.Id;
                BLL.HJGLData_HJGLService.UpdateHJGLData_HJGL(table);
            }
            BLL.HJGLData_DefectService.DeleteHJGLData_DefectByDate(table.ReportDate);
            JArray EditorArr = Grid1.GetMergedData();
            if (EditorArr.Count > 0)
            {
                Model.HJGLData_Defect defect = new Model.HJGLData_Defect();
                for (int i = 0; i < EditorArr.Count; i++)
                {
                    JObject objects = (JObject)EditorArr[i];
                    defect.UnitId = table.UnitId;
                    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.HJGLData_Defect));
                    BLL.HJGLData_DefectService.AddHJGLData_Defect(defect);
                }
            }
            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }
    private void BindGrid()
    {
        if (!string.IsNullOrEmpty(this.Id))
        {
                Model.HJGLData_HJGL model = BLL.HJGLData_HJGLService.GetHJGLData_HJGLById(this.Id);
                if (model != null)
                {
                  var dt=  BLL.HJGLData_DefectService.GetHJGLData_DefectByDate(model.ReportDate);
                    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.HJGLData_HJGLService.IsReportByDate(dt))
                {
                    txtReportDate.SelectedDate = null;
                    ShowNotify("该日期已上报!");
                }
            }
        }
        protected void btnSyn_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.Id))
            {
                var responedata = 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 获取按钮权限
    /// 
    /// 获取按钮权限
    /// 
    /// 
    /// 
    private void GetButtonPower()
    {
        var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.HJGLData_HJGLMenuId);
        if (buttonList.Count() > 0)
        {
            if (buttonList.Contains(BLL.Const.BtnSave))
            {
                this.btnSave.Hidden = false;
            }
        }
    }
    #endregion
}
}