///<reference path="/Scripts/ExtPB_Net.js" />
///<reference path="/Scripts/MyExtPB.js" />
///<reference path="/Scripts/ExtPB_Dll_Pb_Http.js" />
///<reference path="/Scripts/MyExtPb_Input.js" />

function W_XJYinHangPZ() {

	 var self=this;
	 this.Instance=null;
	 var requestor= new PBWindow();
	 var dw_1= new DataWindow( requestor );
	 var tb_1= new ToolStrip( requestor );
	 var ddlb_1= new DropDownListBox( requestor );
	 var dw_fhjb= new DataWindow( requestor );
	 var ContextMenuStrip1= new ContextMenu( requestor );

	 if(typeof(this.bind)=="function") var baseBind=this.bind;
	 this.bind=function(win){
	 self.Instance=win;
	 if(typeof( baseBind )=="function") baseBind(win);

		 if(requestor == null ) requestor= new PBWindow();
		 requestor.Attach(win);
		 if(dw_1 == null ) dw_1= new DataWindow( requestor );
		 dw_1.Attach( win.Control( "dw_1") ); 
		 if(tb_1 == null ) tb_1= new ToolStrip( requestor );
		 tb_1.Attach( win.Control( "tb_1") ); 
		 if(ddlb_1 == null ) ddlb_1= new DropDownListBox( requestor );
		 ddlb_1.Attach( win.Control( "ddlb_1") ); 
		 if(dw_fhjb == null ) dw_fhjb= new DataWindow( requestor );
		 dw_fhjb.Attach( win.Control( "dw_fhjb") ); 
		 if(ContextMenuStrip1 == null ) ContextMenuStrip1= new ContextMenu( requestor );
		 ContextMenuStrip1.Attach( win.Control( "ContextMenuStrip1") ); 

		 if(typeof(self.PostOpen)=="function") 
			 win.AttachEvent("window","PostOpen",self.PostOpen); 

		 if(typeof(self.DoubleClick)=="function") 
			 win.AttachEvent("dw_1","DoubleClicked",self.DoubleClick); 

		 if(typeof(self.RowFocusChanged)=="function") 
			 win.AttachEvent("dw_1","RowFocusChanged",self.RowFocusChanged); 

		 if(typeof(self.ChaXun)=="function") 
			 win.AttachEvent("btn_ChaXun","Clicked",self.ChaXun); 

		 if(typeof(self.SelectionChanged)=="function") 
			 win.AttachEvent("ddlb_1","SelectionChanged",self.SelectionChanged); 

		 if(typeof(self.Add)=="function") 
			 win.AttachEvent("btn_Add","Clicked",self.Add); 

		 if(typeof(self.Edit)=="function") 
			 win.AttachEvent("btn_Edit","Clicked",self.Edit); 

		 if(typeof(self.Delete)=="function") 
			 win.AttachEvent("btn_Delete","Clicked",self.Delete); 

		 if(typeof(self.Audit)=="function") 
			 win.AttachEvent("btn_Audit","Clicked",self.Audit); 

		 if(typeof(self.UnAudit)=="function") 
			 win.AttachEvent("btn_UnAudit","Clicked",self.UnAudit); 

		 if(typeof(self.Tally)=="function") 
			 win.AttachEvent("btn_Tally","Clicked",self.Tally); 

		 if(typeof(self.Exit)=="function") 
			 win.AttachEvent("btn_Exit","Clicked",self.Exit); 

		 if(typeof(self.savegs)=="function") 
			 win.AttachEvent("格式保存ToolStripMenuItem","Clicked",self.savegs); 

		 if(typeof(self.qcgs)=="function") 
			 win.AttachEvent("清除格式ToolStripMenuItem","Clicked",self.qcgs); 

	 }

	 if(typeof(this.unbind)=="function") var baseUnBind=this.unbind; 
	 this.unbind=function() {
	 self.Instance=null;
	 if(typeof(this.Release)=="function") this.Release(); 
	 if(typeof(baseUnBind)=="function") baseUnBind(); 

		 dw_1.Detach(); dw_1=null; 
		 tb_1.Detach(); tb_1=null; 
		 ddlb_1.Detach(); ddlb_1=null; 
		 dw_fhjb.Detach(); dw_fhjb=null; 
		 ContextMenuStrip1.Detach(); ContextMenuStrip1=null; 
		 requestor.Detach(); requestor=null; 

		 setTimeout(CollectGarbage, 100); 
	 }

	 //以上代码由设计器自动生成,请不要手工修改
	 //ExtPB.Net JavaScript End  设计器查找标志,请不要删除或修改

   
    var curYhbh = "";
    var FunctionID = "";

    var CurXJYH_Edit = new W_XJYinHangPZ_Edit();
    var curW_CaiWuChaXun = new W_CaiWuChaXun();
    var webReq = new WebRequestor();
    webReq.SetWebServiceUrl("/Service/ServiceCaiWu.ashx");
    var webReqTimestamp = new WebRequestor();
    webReqTimestamp.SetWebServiceUrl("/Service/GetPublicData.ashx");
    var isAdd = false;

    var curSelDays = ""; //默认该模块可查询天数
    var curSelSQL = "";

    this.PostOpen = function () {

        curYhbh = requestor.GetParm("curYhbh");
        curSelDays = requestor.GetParm("seldays");
        self.SelectionChanged();
    }

    //#region 无数据时按钮处理
    this.SetButtomStatus = function () {
        // 汇总无数据时
        if (dw_1.RowCount() < 1) {

            tb_1.SetEnabled("btn_Edit", false);
            tb_1.SetEnabled("btn_Delete", false);
            tb_1.SetEnabled("btn_Audit", false);
            tb_1.SetEnabled("btn_UnAudit", false);
            tb_1.SetEnabled("btn_Tally", false);
            tb_1.SetEnabled("btn_Print", false);
            tb_1.SetEnabled("btn_Export", false);

            tb_1.SetEnabled("btn_Save", false);
        }
    }
    //#endregion

    this.Add = function () {
        curSelSQL = "";
        ddlb_1.SetText("未记账");
        var djzt = ddlb_1.GetText();
        if (djzt == "未记账") {
            djzt = "%0%";
        } else if (djzt == "已记账") {
            djzt = "%1%";
        } else if (djzt == "全部") {
            djzt = "%";
        } else {
            djzt = "%" + djzt + "%";
        }
        isAdd = true;
        CurXJYH_Edit.SetDataWindow(self,dw_1);
        requestor.Open("/Service/LoadWindow.ashx?WinName=A8ERP.CaiWu.XianJinPinZheng.W_XJYinHangPZ_Edit&curParm1=ADD&curParm2=" + djzt, CurXJYH_Edit, null);
    }

    //#region edit
    this.Edit = function () {
        var row = dw_1.GetRow();
        if (row < 1) {
            requestor.MessageBox("提示", "请选择要编辑的单据行！", ICON.Information);
            return;
        }
//        self.SelectionChanged();
//        dw_1.ScrollToRow(row);
        var CurPzh = dw_1.GetItemString(row, "pzh");
        var CurDqzt = dw_1.GetItemString(row, "dqzt");
        CurXJYH_Edit.SetDataWindow(self, dw_1);
        requestor.Open("/Service/LoadWindow.ashx?WinName=A8ERP.CaiWu.XianJinPinZheng.W_XJYinHangPZ_Edit&curParm1=EDIT&curParm2=" + CurPzh + "&CurDqzt=" + CurDqzt + "&CURGNQXSTR=" + requestor.GetParm("CURGNQXSTR"), CurXJYH_Edit, null);
    }

    this.DoubleClick = function () {
        self.Edit();
    }
    //#endregion

    //#region Delete
    this.Delete = function () {
        //判断审核标志-------------

        if (requestor.MessageBox("提示", "确定删除所选记录行吗？", ICON.Question, BUTTON.YesNo) == 2) {
            return;
        }
        var curRow = dw_1.GetRow();
        if (curRow < 1) {
            requestor.MessageBox("提示", "请选择要删除的记录行!", ICON.Information);
            return;
        }
        if (self.CheckJzbz()) {
            return;
        }
        var curPzh = dw_1.GetItemString(curRow, "pzh");
        webReq.Invoke("XJYHPZDelete", "CurPzh=" + curPzh);
        if (webReq.StatusCode() == 200) {
            requestor.MessageBox("提示", "记录删除成功！", ICON.Information);
            dw_1.DeleteRow(curRow);
            if (dw_1.RowCount() > 0) {
                dw_1.ScrollToRow(dw_1.GetRow());
                self.RowFocusChanged(dw_1.GetRow());
            } else {
                self.SetButtomStatus();
            }
        } else {
            requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
        }
    }
    //#endregion

    //#region Audit
    this.Audit = function () {
        var curRow = dw_1.GetRow();
        if (curRow > 0) {
            var curPzh = dw_1.GetItemString(curRow, "pzh");
            if (requestor.MessageBox("提示", "请选择要审核单据【" + curPzh + "】吗？", ICON.Question, BUTTON.YesNo) == 2) {
                return;
            }

            if (self.CheckJzbz()) {
                return;
            }
            var nextZt = self.GetNextZt(dw_1.GetItemString(curRow, "dqzt"));
            webReq.Invoke("XJPZ_Audit", "PZH=" + curPzh + "&DJZT=" + nextZt + "&curFunId=SJ298");
            if (webReq.StatusCode() == 200) {
                requestor.MessageBox("提示", "审核成功!", ICON.Information);
               
                //#region 刷新汇总表
                self.SelectionChanged(dw_1.GetPageIndex());
                if (dw_1.RowCount() == 0) {
                    self.SetButtomStatus();
                    return;
                }
                //找到相应的记录
                var findRow = dw_1.Find("pzh='" + curPzh + "' ", 1, dw_1.RowCount());
                if (findRow > 0) {
                    dw_1.ScrollToRow(findRow);
                    var curFindRow = dw_fhjb.Find("fhjb='" + nextZt + "'", 1, dw_fhjb.RowCount());
                    var curToButtonShowName = dw_fhjb.GetItemString(curFindRow, "dygn");
                    tb_1.SetText("btn_Audit", curToButtonShowName);
                } else {
                    self.RowFocusChanged(dw_1.GetRow());
                }
                //#endregion 
              
            } else {
                requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
            }
        } else {
            requestor.MessageBox("提示", "请选择要审核的记录行!", ICON.Information);
        }
    }
    //#endregion

    //#region UnAudit 
    this.UnAudit = function () {
        var curRow = dw_1.GetRow();
        if (curRow > 0) {
            var curPzh = dw_1.GetItemString(curRow, "pzh");
            if (requestor.MessageBox("提示", "请选择要退审单据【" + curPzh + "】吗？", ICON.Question, BUTTON.YesNo) == 2) {
                return;
            }
            if (self.CheckJzbz()) {
                return;
            }
            webReq.Invoke("XJPZ_UnAudit", "PZH=" + curPzh + "&curFunId=SJ298");
            if (webReq.StatusCode() == 200) {
                requestor.MessageBox("提示", "退审成功!", ICON.Information);
                //#region 刷新汇总表
                dw_1.SetRedraw(false);
                dw_1.SetProcessing(true);

                //刷新
                self.SelectionChanged(dw_1.GetPageIndex());
                //找到相应的记录
                var findRow = dw_1.Find("pzh='" + curPzh + "' ", 1, dw_1.RowCount());
                if (findRow > 0) {
                    dw_1.ScrollToRow(findRow);
                } else {
                    self.RowFocusChanged(dw_1.GetRow());
                }
                dw_1.SetProcessing(false);
                dw_1.SetRedraw(true);
                //#endregion 
                
            } else {
                requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
            }
        } else {
            requestor.MessageBox("提示", "请选择要退审的记录行!", ICON.Information);
        }
    }
    //#endregion

    //#region GetNextZt
    this.GetNextZt = function (curZt) {
        if ("F" == curZt) {
            var nextRow = dw_fhjb.Find(" fhjb='1' ", 1, dw_fhjb.RowCount());
            if (nextRow > 0) {
                return "1";
            } else {
                return "T";
            }
        } else {
            var nextZt = parseInt(curZt) + 1;
            var nextRow = dw_fhjb.Find(" fhjb='" + nextZt + "' ", 1, dw_fhjb.RowCount());
            if (nextRow > 0) {
                return nextZt + "";
            } else {
                return "T";
            }
        }
    }
    //#endregion

    //#region RowFocusChanged
    this.RowFocusChanged = function (row) {
        if (row > 0) {
            var curShzt = dw_1.GetItemString(row, "dqzt");
            var curJzbz = dw_1.GetItemString(row, "jzbz");

            if (curShzt == "F") {
                tb_1.SetEnabled("btn_Audit", true);
                tb_1.SetEnabled("btn_UnAudit", false);
                tb_1.SetEnabled("btn_Tally", false);
                tb_1.SetEnabled("btn_Delete", true);
                tb_1.SetEnabled("btn_Edit", true);
            } else if (curShzt == "T") {
                tb_1.SetEnabled("btn_Audit", false);
                if (curJzbz == "1") {
                    tb_1.SetEnabled("btn_UnAudit", false);
                    tb_1.SetEnabled("btn_Tally", false);
                    tb_1.SetEnabled("btn_Delete", false);
                    tb_1.SetEnabled("btn_Edit", false);
                } else {
                    tb_1.SetEnabled("btn_UnAudit", true);
                    tb_1.SetEnabled("btn_Tally", true);
                    tb_1.SetEnabled("btn_Delete", false);
                    tb_1.SetEnabled("btn_Edit", false);
                }
            } else {
                tb_1.SetEnabled("btn_Audit", true);
                tb_1.SetEnabled("btn_UnAudit", true);
                tb_1.SetEnabled("btn_Tally", false);
                tb_1.SetEnabled("btn_Delete", false);
                tb_1.SetEnabled("btn_Edit", false);
            }
            //tb_1.SetEnabled("btn_PriorRow", true);
            //tb_1.SetEnabled("btn_NextRow", true);
            //if (row <= 1) {
            //    tb_1.SetEnabled("btn_PriorRow", false);
            //}
            //if (row >= dw_1.RowCount()) {
            //    tb_1.SetEnabled("btn_NextRow", false);
            //}

            var fhjbRow = dw_fhjb.Find(" fhjb='" + curShzt + "' ", 1, dw_fhjb.RowCount());
            tb_1.SetText("btn_Audit", dw_fhjb.GetItemString(fhjbRow, "dygn"));
        }
    }
    //#endregion

    //#region 记账
    this.Tally = function () {

        if (dw_1.GetRow < 1) {
            alert("请先选择一笔数据进行审核");
            return;
        }
        var pzh = dw_1.GetItemString(dw_1.GetRow(), "pzh");

        if (pzh.length < 1) {
            alert("凭证号异常");
            return;
        }
        if (!confirm("您确定要记账凭证号为[" + pzh + "]的单据吗？")) {
            return;
        }
        //#region 检查本单据是否已经记账
        if (self.CheckJzbz()) {
            return;
        }
        //#endregion
        webReq.Invoke("Bill_CW_DJJZ_Check", "PZH=" + pzh + "&DJFL=" + "7");
        if (webReq.ResponseText() != "0") {
            requestor.MessageBox("提示", webReq.ResponseText(), ICON.Information);
            return;
        }
        webReq.Invoke("XJPZ_Tally", "curPzh=" + pzh + "&curFunId=SJ298");
        if (webReq.StatusCode() == 200) {
            requestor.MessageBox("提示", webReq.ResponseText(), ICON.Information);
            //#region 刷新汇总表
            dw_1.SetRedraw(false);
            dw_1.SetProcessing(true);

            //刷新
            self.SelectionChanged(dw_1.GetPageIndex());
            //找到相应的记录
            var findRow = dw_1.Find("pzh='" + pzh + "' ", 1, dw_1.RowCount());
            if (findRow > 0) {
                dw_1.ScrollToRow(findRow);
                tb_1.SetEnabled("btn_Audit", false);
                tb_1.SetEnabled("btn_UnAudit", false);
                tb_1.SetEnabled("btn_Tally", false);
                tb_1.SetEnabled("btn_Delete", false);
                tb_1.SetEnabled("btn_Edit", false);
            } else {
                self.RowFocusChanged(dw_1.GetRow());
            }
            dw_1.SetProcessing(false);
            dw_1.SetRedraw(true);
            //#endregion 
           
        } else {
            requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
        }

    }
    //#endregion   

    this.Exit = function () {
        setTimeout("window.close()", 100);
    }

    this.SelectionChanged = function (val) {
        var djzt = ddlb_1.GetText();
        //        var SqlWhere = "";
        //        if (djzt == "未记账") {
        //            djzt = "%0%";
        //        } else if (djzt == "已记账") {
        //            djzt = "%1%";
        //        } else if (djzt == "全部") {
        //            djzt = "%";
        //        } else {
        //            djzt = "%" + djzt + "%";
        //        }
        //dw_1.Retrieve(curYhbh, djzt);

        if (djzt == "未记账") {
            djzt = "isnull(jzbz,'0')<>'1'";
        } else if (djzt == "已记账") {
            djzt = "isnull(jzbz,'0')='1'";
        } else if (djzt == "全部") {
            djzt = "";
        }
        else {
            djzt = "fhmc='" + djzt + "'";
        }

        if (curSelSQL.length > 0 && !isAdd) {
            SqlWhere = curSelSQL;
        }
        else {
            SqlWhere = "  a.bmbh in(select bmbh  from da_qx_bmqxsz where yhbh = '" + curYhbh + "'and cw = '1') ";
            if (curSelDays != "-1") {
                SqlWhere += " and CONVERT(date,kdsj,120) >='" + curSelDays + "'";
            }
        }

        if (djzt.length > 0) {
            SqlWhere += " and " + djzt;
        }
       
        dw_1.RetrieveEx(SqlWhere);

        var curPageCount = dw_1.GetPageCount();
        try {
            if (arguments.length == 1) {
                if (parseInt(val) > curPageCount) {

                    dw_1.SetPageIndex(curPageCount);

                } else {
                    dw_1.SetPageIndex(val);

                }
                dw_1.Refresh();
            }
        } catch (e) {
            alert("this.SelectionChanged Error.\r\n");
        }
    }

    //#region 查询界面
    this.ChaXun = function () {
        curW_CaiWuChaXun.SetDataWindow(self, dw_1, "TT", curYhbh, ddlb_1.GetText(), curSelDays);
        requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.CaiWu.ChaXun.W_CaiWuChaXun&flag=TT"), curW_CaiWuChaXun, null);

    }
    //#endregion

    //#region this.EvalJs
    this.EvalJs = function (jsScript) {
        //alert(jsScript);
        eval(jsScript);
    }
    //#endregion

    //#region 检查记账标志
    this.CheckJzbz = function () {
        var curTName = "";
        var curpzh = "";
        curpzh = dw_1.GetItemString(dw_1.GetRow(), "pzh");
        curTName = "cw_xjyhzhtzd";

        webReqTimestamp.Invoke("YW_GTE_PZH", "curTName=" + curTName + "&curPzh=" + curpzh);
        if (webReqTimestamp.StatusCode() == 200) {
            var curjzbz = webReqTimestamp.ResponseText();
            if (curjzbz == "1") {
                requestor.MessageBox("提示", "该记录已经记账！不能修改！！", ICON.StopSign);
                //#region 刷新汇总表
                dw_1.SetRedraw(false);
                dw_1.SetProcessing(true);

                //刷新
                self.SelectionChanged();
                //找到相应的记录
                var findRow = dw_1.Find("pzh='" + curpzh + "' ", 1, dw_1.RowCount());
                if (findRow > 0) {
                    dw_1.ScrollToRow(findRow);
                } else {
                    self.RowFocusChanged(dw_1.GetRow());
                }
                dw_1.SetProcessing(false);
                dw_1.SetRedraw(true);
                //#endregion 

                tb_1.SetEnabled("btn_Save", false);
                tb_1.SetEnabled("btn_Audit", false);
                tb_1.SetEnabled("btn_UnAudit", false);
                tb_1.SetEnabled("btn_Tally", false);
                tb_1.SetEnabled("btn_Delete", false);
                tb_1.SetEnabled("btn_Edit", false);
                return true;
            } else {
                return false;
            }
        } else {
            requestor.MessageBox("获取记账标志失败", webReqTimestamp.ResponseText(), ICON.StopSign);
            return true;
        }

    }
    //#endregion

    this.print = function () { 
    
//      alert(1)
    }
    //#region 格式保存
    this.savegs = function () {
        var msg = savegs(dw_1, curYhbh, requestor.GetParm("FunctionID"));
        alert(msg);
    }
    //#endregion

    //#region 清除格式
    this.qcgs = function () {
        var msg = qcgs(curYhbh, requestor.GetParm("curFunctionId"));
        alert(msg);

    }
    //#endregion
}