///<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_YingFuKuanTZD() {

	 var self=this;
	 var requestor= new PBWindow();
	 var dw_fhjb= new DataWindow( requestor );
	 var dw_1= new DataWindow( requestor );
	 var tb_1= new ToolStrip( requestor );

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

		 if(requestor == null ) requestor= new PBWindow();
		 requestor.Attach(win);
		 if(dw_fhjb == null ) dw_fhjb= new DataWindow( requestor );
		 dw_fhjb.Attach( win.Control( "dw_fhjb") ); 
		 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(typeof(self.DoubleClicked)=="function") 
			 win.AttachEvent("dw_1","DoubleClicked",self.DoubleClicked); 

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

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

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

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

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

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

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

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

	 }

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

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

		 setTimeout(CollectGarbage, 100); 
	 }

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

    var CurYFTZD_Edit = new W_YFKTZD_Edit();
    var webReq = new WebRequestor();
    webReq.SetWebServiceUrl("/Service/ServiceCaiWu.ashx");

    this.Add = function () {

        CurYFTZD_Edit.SetDataWindow(dw_1);
        requestor.Open("/Service/LoadWindow.ashx?WinName=A8ERP.CaiWu.TiaoZhengHeFeiYongDan.W_YFKTZD_Edit&curParm1=ADD", CurYFTZD_Edit, null);
    }

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

    this.DoubleClicked = function () {
        self.Edit();
    }

    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;
        }
        var curPzh = dw_1.GetItemString(curRow, "pzh");
        webReq.Invoke("YFTZDDelete", "CurPzh=" + curPzh);
        if (webReq.StatusCode() == 200) {
            requestor.MessageBox("提示", "记录删除成功！", ICON.Information);
            dw_1.DeleteRow(curRow);
            curRow = 0;
        } else {
            requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
        }
    }

    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;
            }
            var nextZt = self.GetNextZt(dw_1.GetItemString(curRow, "dqzt"));
            webReq.Invoke("SetDjShzt", "PZH=" + curPzh + "&DJZT=" + nextZt);
            if (webReq.StatusCode() == 200) {
                requestor.MessageBox("提示", "审核成功!", ICON.Information);
                dw_1.SetItem(curRow, "dqzt", nextZt);
                self.RowFocusChanged(curRow);
            } else {
                requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
            }
        } else {
            requestor.MessageBox("提示", "请选择要审核的记录行!", ICON.Information);
        }
    }

    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;
            }
            webReq.Invoke("DjUnAudit", "PZH=" + curPzh);
            if (webReq.StatusCode() == 200) {
                requestor.MessageBox("提示", "退审成功!", ICON.Information);
                dw_1.SetItem(curRow, "dqzt", "F");
                self.RowFocusChanged(curRow);
            } else {
                requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
            }
        } else {
            requestor.MessageBox("提示", "请选择要退审的记录行!", ICON.Information);
        }
    }

    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";
            }
        }
    }

    this.RowFocusChanged = function (row) {
        var curShzt = dw_1.GetItemString(row, "dqzt");
        var curJzbz = dw_1.GetItemString(row, "jzbz");
        if (curShzt == "F") {
            tb_1.SetEnabled("btn_4", true);
            tb_1.SetEnabled("btn_5", false);
            tb_1.SetEnabled("btn_6", false);
            tb_1.SetEnabled("btn_3", true);
            tb_1.SetEnabled("btn_2", true);
        } else if (curShzt == "T") {
            tb_1.SetEnabled("btn_4", false);
            if (curJzbz == "1") {
                tb_1.SetEnabled("btn_5", false);
                tb_1.SetEnabled("btn_6", false);
                tb_1.SetEnabled("btn_3", false);
                tb_1.SetEnabled("btn_2", false);
            } else {
                tb_1.SetEnabled("btn_5", true);
                tb_1.SetEnabled("btn_6", true);
                tb_1.SetEnabled("btn_3", true);
                tb_1.SetEnabled("btn_2", true);
            }
        } else {
            tb_1.SetEnabled("btn_4", true);
            tb_1.SetEnabled("btn_5", true);
            tb_1.SetEnabled("btn_6", false);
            tb_1.SetEnabled("btn_3", true);
            tb_1.SetEnabled("btn_2", true);
        }
        var fhjbRow = dw_fhjb.Find(" fhjb='" + curShzt + "' ", 1, dw_fhjb.RowCount());
        tb_1.SetText("btn_4", dw_fhjb.GetItemString(fhjbRow, "dygn"));
    }

    //#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;
        }
     
        webReq.Invoke("YFTZD_Tally", "curPzh=" + pzh);
        if (webReq.StatusCode() == 200) {
            dw_1.Retrieve();
            alert(webReq.ResponseText());

           
            tb_1.SetEnabled("btn_4", false);
            tb_1.SetEnabled("btn_5", false);
            tb_1.SetEnabled("btn_6", false);
            tb_1.SetEnabled("btn_3", false);
            tb_1.SetEnabled("btn_2", false);
        } else {
            alert(webReq.ResponseText());
        }

    }
    //#endregion   

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