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

	 var self=this;
	 this.Instance=null;
	 var requestor= new PBWindow();
	 var tb_1= new ToolStrip( requestor );
	 var dw_1= new DataWindow( 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(tb_1 == null ) tb_1= new ToolStrip( requestor );
		 tb_1.Attach( win.Control( "tb_1") ); 
		 if(dw_1 == null ) dw_1= new DataWindow( requestor );
		 dw_1.Attach( win.Control( "dw_1") ); 

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

		 if(typeof(self.Query)=="function") 
			 win.AttachEvent("cb_Query","Clicked",self.Query); 

		 if(typeof(self.Save)=="function") 
			 win.AttachEvent("cb_Save","Clicked",self.Save); 

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

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

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

	 }

	 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(); 

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

		 setTimeout(CollectGarbage, 100); 
	 }

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

    var dwc = new DataWindowChild();
    var curEditModel = "";
    var CurUserId = "";
    var isAdd = false;
    var curPzh = "";
    var dw_list = null;
    var curParentSelf = null;
    var curW_OPEN = new OpenWindowQueryBase();
    var webReq = new WebRequestor();
    webReq.SetWebServiceUrl("/Service/ServiceCaiWu.ashx");
    var webReqTimestamp = new WebRequestor();
    webReqTimestamp.SetWebServiceUrl("/Service/GetPublicData.ashx");
    var webReq1 = new WebRequestor();
    webReq1.SetWebServiceUrl("/Service/ServiceKuCunGuanLi.ashx");
    var CurTzlx = "";

    this.PostOpen = function () {
        CurUserId = requestor.GetParm("CurUserId");
        curEditModel = requestor.GetParm("EditModel");
        if (curEditModel == "EDIT") {
            self.Edit();
        } else {
            self.Add();
            isAdd = true;
        }
    }

    this.SetDataWindow = function (selfs, dw) {
        curParentSelf = selfs;
        dw_list = dw;
    }

    this.Add = function () {
        dw_1.Reset();
        dw_1.ResetUpdate();
        var CurRow = dw_1.InsertRow(0);
        webReq.Invoke("GetTZDPzh", "flag=TT&table=cw_xjyhzhtzd");
        if (webReq.StatusCode() == 200) {
            curPzh = webReq.ResponseText();
            dw_1.SetItem(CurRow, "pzh", curPzh);
            dw_1.SetItem(CurRow, "kdr", CurUserId);
        } else {
            requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
            return;
        }
    }

    this.Edit = function () {
        CurTzlx = dw_1.GetItemString(dw_1.GetRow(), "tzlx");
        self.SetZh(CurTzlx, false);
    }

    this.DoubleClicked = function (x, y, row, dwcName) {
        if (dwcName == "bmbh") {
            curW_OPEN.SetDataWindow(dw_1, "bmbh", "bmmc", "", self)  // 这个用来给子窗口Set当前数据窗口
            requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.DangAn.Base.OpenWindowQueryBase&treeDw=dw_da_bmda_fl_tree&listDw=dw_pub_list_bmda&treeRootLabel=部门分类&treeData=flbh&treeLabel=flmc&treeParentData=fflbh&treeLeave=islast&curQxlx=cw"), curW_OPEN, null);
            self.ClearZh();
        }
    }

    this.SetZh = function (tzlx, isClear) {
        //        dw_1.GetChild("tzyhzh", dwc);
        //        dw_1.GetChild("zryhzh", dwc);
        //        dwc.Retrieve("", "0");
        //        dwc.Retrieve("", "1");
        //        dw_1.GetChild("tzlx", dwc);
        //        dwc.Retrieve("xjyh_tzlx");
        var CurBmbh = dw_1.GetItemString(1, "bmbh");
        var CurBmbhR = dw_1.GetItemString(1, "rbmbh");
        if (CurBmbh == "") {
            requestor.MessageBox("提示", "请选择部门！", ICON.Information);
            return 0;
        }

        if (isClear) {
            dw_1.SetItem(dw_1.GetRow(), "tzyhzh", "");
            dw_1.SetItem(dw_1.GetRow(), "zryhzh", "");
            dw_1.SetItem(dw_1.GetRow(), "tzyhzhao", "");
            dw_1.SetItem(dw_1.GetRow(), "zryhzhao", "");
            dw_1.SetItem(dw_1.GetRow(), "dqyez", "0");
            dw_1.SetItem(dw_1.GetRow(), "dqyer", "0");
        }
        if (tzlx == "ck") {
            if (CurBmbhR == "") {
                requestor.MessageBox("提示", "请选择入方部门！", ICON.Information);
                return 0;
            }
            dw_1.Modify("tzyhzh_t.Text='现金账户:'");
            dw_1.Modify("tzyhzhao_t.Text='------:'");
            dw_1.Modify("dqyez_t.Text='余额:'");
            dw_1.Modify("zryhzh_t.Text='银行账户:' ");
            dw_1.Modify("zryhzhao_t.Text='银行账号:'");
            dw_1.Modify("dqyer_t.Text='余额:'");
            dw_1.GetChild("tzyhzh", dwc);
            dwc.Retrieve(CurBmbh, "0");
            dw_1.GetChild("zryhzh", dwc);
            dwc.Retrieve(CurBmbhR, "1");
        } else if (tzlx == "tx") {
            if (CurBmbhR == "") {
                requestor.MessageBox("提示", "请选择入方部门！", ICON.Information);
                return 0;
            }
            dw_1.Modify("tzyhzh_t.Text='银行账户:'");
            dw_1.Modify("tzyhzhao_t.Text='银行账号:'");
            dw_1.Modify("dqyez_t.Text='余额:'");
            dw_1.Modify("zryhzh_t.Text='现金账户:'  ");
            dw_1.Modify("zryhzhao_t.Text='------:'");
            dw_1.Modify("dqyer_t.Text='余额:'");

            dw_1.GetChild("tzyhzh", dwc);
            dwc.Retrieve(CurBmbh, "1");
            dw_1.GetChild("zryhzh", dwc);
            dwc.Retrieve(CurBmbhR, "0");
        } else if (tzlx == "yhzz") {
            if (CurBmbhR == "") {
                requestor.MessageBox("提示", "请选择入方部门！", ICON.Information);
                return 0;
            }
            dw_1.Modify("tzyhzh_t.Text='转出账户:'");
            dw_1.Modify("tzyhzhao_t.Text='转出账号:'");
            dw_1.Modify("dqyez_t.Text='余额:'");
            dw_1.Modify("zryhzh_t.Text='转入账户:' ");
            dw_1.Modify("zryhzhao_t.Text='转入账号:'");
            dw_1.Modify("dqyer_t.Text='余额:'");

            dw_1.GetChild("tzyhzh", dwc);
            dwc.Retrieve(CurBmbh, "1");
            dw_1.GetChild("zryhzh", dwc);
            dwc.Retrieve(CurBmbhR, "1");
        } else if (tzlx == "xjzz") {
            if (CurBmbhR == "") {
                requestor.MessageBox("提示", "请选择入方部门！", ICON.Information);
                return 0;
            }
            dw_1.Modify("tzyhzh_t.Text='转出账户:'");
            dw_1.Modify("tzyhzhao_t.Text='------:'");
            dw_1.Modify("dqyez_t.Text='余额:'");
            dw_1.Modify("zryhzh_t.Text='转入账户:' ");
            dw_1.Modify("zryhzhao_t.Text='------:'");
            dw_1.Modify("dqyer_t.Text='余额:'");

            dw_1.GetChild("tzyhzh", dwc);
            dwc.Retrieve(CurBmbh, "0");
            dw_1.GetChild("zryhzh", dwc);
            dwc.Retrieve(CurBmbhR, "0");
        } else if (tzlx == "yhtz") {
            dw_1.Modify("tzyhzh_t.Text='调整账户:'");
            dw_1.Modify("tzyhzhao_t.Text='调整账号:'");
            dw_1.Modify("dqyez_t.Text='余额:'");
            dw_1.Modify("zryhzh_t.Text='------:' ");
            dw_1.Modify("zryhzhao_t.Text='------:'");
            dw_1.Modify("dqyer_t.Text='------:'");

            dw_1.GetChild("tzyhzh", dwc);
            dwc.Retrieve(CurBmbh, "1");
            dw_1.GetChild("zryhzh", dwc);
            dwc.Retrieve("", "");
        } else if (tzlx == "xjtz") {
            dw_1.Modify("tzyhzh_t.Text='转出账户:'");
            dw_1.Modify("tzyhzhao_t.Text='------:'");
            dw_1.Modify("dqyez_t.Text='余额:'");
            dw_1.Modify("zryhzh_t.Text='------:' ");
            dw_1.Modify("zryhzhao_t.Text='------:'");
            dw_1.Modify("dqyer_t.Text='------:'");

            dw_1.GetChild("tzyhzh", dwc);
            dwc.Retrieve(CurBmbh, "0");
            dw_1.GetChild("zryhzh", dwc);
            dwc.Retrieve("", "");
        }
        if (curEditModel == "ADD") {
            self.ClearZh();
        }
        return 1;
    }


    this.ClearZh = function () {
        dw_1.SetItem(1, "tzyhzh", "");
        dw_1.SetItem(1, "zryhzh", "");
        dw_1.SetItem(1, "tzyhzhao", "");
        dw_1.SetItem(1, "zryhzhao", "");
        dw_1.SetItem(1, "dqyez", 0);
        dw_1.SetItem(1, "dqyer", 0);
    }

    this.ItemChanged = function (row, dwoName, data) {
        if (dwoName == "bmbh") {
            // self.ClearZh();
            dw_1.SetItem(1, "tzyhzh", "");
            dw_1.SetItem(1, "tzyhzhao", "");
            dw_1.SetItem(1, "dqyez", 0);
            dw_1.GetChild("tzyhzh", dwc);
            dwc.Retrieve("", "1");

        } else if (dwoName == "rbmbh") {
            dw_1.SetItem(1, "zryhzh", "");
            dw_1.SetItem(1, "zryhzhao", "");
            dw_1.SetItem(1, "dqyer", 0);
            dw_1.GetChild("zryhzh", dwc);
            dwc.Retrieve("", "");
        } else if (dwoName == "tzlx") {
            if (self.SetZh(data, true) == 0) {
                dw_1.SetItem(row, dwoName, "");
                dw_1.AcceptText();
                dw_1.SetColumn("bmbh");
                dw_1.SetFocus();
                return 1;
            }
            CurTzlx = data;
        } else if (dwoName == "tzyhzh") {
            dw_1.GetChild(dwoName, dwc);
            dw_1.SetItem(row, "tzyhzhao", dwc.GetItemString(dwc.GetSelectedRow(), "yhkh"));
            dw_1.SetItem(row, "dqyez", dwc.GetItemNumber(dwc.GetSelectedRow(), "dqye"));
        } else if (dwoName == "zryhzh") {
            dw_1.GetChild(dwoName, dwc);
            dw_1.SetItem(row, "zryhzhao", dwc.GetItemString(dwc.GetSelectedRow(), "yhkh"));
            dw_1.SetItem(row, "dqyer", dwc.GetItemNumber(dwc.GetSelectedRow(), "dqye"));
        }
    }

    this.Save = function () {
        dw_1.AcceptText();
        if (!dw_1.CheckData("*", "")) {
            return
        }
        dw_1.SetItem(dw_1.GetRow(), "kdsj", UtcDateFormatToString(dw_1.GetItemDate(dw_1.GetRow(), "kdsj")));

        var dwChanges = dw_1.GetChanges();
        if (dwChanges == "") {
            requestor.MessageBox("提示", "没有数据修改", ICON.Information);
            return;
        }

        var bmbh = dw_1.GetItemString(dw_1.GetRow(), "bmbh");
        var rq = UtcDateTimeFormatToString(dw_1.GetItemDate(dw_1.GetRow(), "kdsj"));
        webReq1.Invoke("GetTallystatus", "bmbh=" + bmbh + "&tzrq=" + rq + "&flag=2");
        if (webReq1.StatusCode() == 200) {
            requestor.MessageBox("提示", "本部门库存已结转,不可保存！", ICON.StopSign);
            return;
        }

        //#region 检查记账标志
        if (isAdd) {
            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);
                    dw_1.ResetUpdate();
                    return;
                }
            } else {
                requestor.MessageBox("获取记账标志失败", webReqTimestamp.ResponseText(), ICON.StopSign);
                return;
            }
        }
        //#endregion
        if (!(CurTzlx == "xjtz" || CurTzlx == "yhtz") && dw_1.GetItemString(dw_1.GetRow(), "zryhzh") == "") {
            requestor.MessageBox("提示", "请选择" + dw_1.Describe("zryhzh_t.Text"), ICON.Information);
            return;
        }

        //新增加账户判断
        if (CurTzlx == "yhzz" || CurTzlx == "xjzz") {
            if (dw_1.GetItemString(dw_1.GetRow(), "tzyhzh") == dw_1.GetItemString(dw_1.GetRow(), "zryhzh")) {
                requestor.MessageBox("提示", "转出和转入账户不能相同", ICON.Information);
                return;
            }
        }

        //判断余额问题
        if (CurTzlx == "tx") {
            var CurBmbh = dw_1.GetItemString(1, "bmbh");
            if (CurBmbh == "") {
                requestor.MessageBox("提示", "请选择部门！", ICON.Information);
                return 0;
            }
            dw_1.GetChild("tzyhzh", dwc);
            dwc.Retrieve(CurBmbh, "1");
            var zhbh = dw_1.GetItemString(dw_1.GetRow(), "tzyhzh");
            var findRow = dwc.Find("yhzh='" + zhbh + "'", 1, dwc.RowCount());
            var curYe = dwc.GetItemNumber(findRow, "dqye");
            var curFsje = dw_1.GetItemNumber(dw_1.GetRow(), "fsje");
            if (GetXiTongCanShu("fydyxqk", "xtcs", "qybz") == "0") {
                if (curYe < curFsje) {
                    requestor.MessageBox("提示", "账户余额不足", ICON.Information);
                    return;
                }
            }
        } else if (CurTzlx == "yhzz") {
            var CurBmbh = dw_1.GetItemString(1, "bmbh");
            if (CurBmbh == "") {
                requestor.MessageBox("提示", "请选择部门！", ICON.Information);
                return 0;
            }
            dw_1.GetChild("tzyhzh", dwc);
            dwc.Retrieve(CurBmbh, "1");
            var zhbh = dw_1.GetItemString(dw_1.GetRow(), "tzyhzh");
            var findRow = dwc.Find("yhzh='" + zhbh + "'", 1, dwc.RowCount());
            var curYe = dwc.GetItemNumber(findRow, "dqye");
            var curFsje = dw_1.GetItemNumber(dw_1.GetRow(), "fsje");

            if (GetXiTongCanShu("fydyxqk", "xtcs", "qybz") == "0") {
                if (curYe < curFsje) {
                    requestor.MessageBox("提示", "账户余额不足", ICON.Information);
                    return;
                }
            }
        } else if (CurTzlx == "xjzz") {
            var CurBmbh = dw_1.GetItemString(1, "bmbh");
            if (CurBmbh == "") {
                requestor.MessageBox("提示", "请选择部门！", ICON.Information);
                return 0;
            }
            dw_1.GetChild("tzyhzh", dwc);
            dwc.Retrieve(CurBmbh, "0");
            var zhbh = dw_1.GetItemString(dw_1.GetRow(), "tzyhzh");
            var findRow = dwc.Find("yhzh='" + zhbh + "'", 1, dwc.RowCount());
            var curYe = dwc.GetItemNumber(findRow, "dqye");
            var curFsje = dw_1.GetItemNumber(dw_1.GetRow(), "fsje");
            if (GetXiTongCanShu("fydyxqk", "xtcs", "qybz") == "0") {
                if (curYe < curFsje) {
                    requestor.MessageBox("提示", "账户余额不足", ICON.Information);
                    return;
                }
            }
        }
        else if (CurTzlx == "ck") {
            var CurBmbh = dw_1.GetItemString(1, "bmbh");
            if (CurBmbh == "") {
                requestor.MessageBox("提示", "请选择部门！", ICON.Information);
                return 0;
            }
            dw_1.GetChild("tzyhzh", dwc);
            dwc.Retrieve(CurBmbh, "0");
            var zhbh = dw_1.GetItemString(dw_1.GetRow(), "tzyhzh");
            var findRow = dwc.Find("yhzh='" + zhbh + "'", 1, dwc.RowCount());
            var curYe = dwc.GetItemNumber(findRow, "dqye");
            var curFsje = dw_1.GetItemNumber(dw_1.GetRow(), "fsje");
            if (GetXiTongCanShu("fydyxqk", "xtcs", "qybz") == "0") {
                if (curYe < curFsje) {
                    requestor.MessageBox("提示", "账户余额不足", ICON.Information);
                    return;
                }
            }
        }
        //#region 写日志
        var curPzh = dw_1.GetItemString(dw_1.GetRow(), "pzh");
        if (curPzh.length > 0) {
            curPzh = dw_1.GetItemString(dw_1.GetRow(), "pzh")
        } else {
            curPzh = "###";
        }
        var curLogStr = "";
        if (curEditModel == "Add") {
            curLogStr = A8erp_Common.WriteLog(dw_1, "Add", "DB");
        } else {
            curLogStr = "【凭证号：" + curPzh + "】" + A8erp_Common.WriteLog(dw_1, "Edit", "DB");
        }
        //#endregion
        webReq.Invoke("SaveAsXJYHPZ", "dwObjects=dw_cw_xjyhzhtzd_edit&dwChanges=" + dwChanges + "&recordFlag=" + curLogStr + "&curPzh=" + curPzh);

        if (webReq.StatusCode() == 200) {
            //dw_list.Retrieve(CurUserId);
            //#region 刷新
            curParentSelf.SelectionChanged(dw_list.GetPageIndex());
            //#endregion
            if (curEditModel == "ADD") {
                requestor.MessageBox("提示", "数据保存成功!", ICON.Information);
                curParentSelf.EvalJs("isAdd = false");
                isAdd = false;
            }
            else {
                requestor.MessageBox("提示", "数据保存成功!", ICON.Information);
                dw_1.ResetUpdate();
            }
            self.Query();
        }
        else {
            requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
        }
    }

    this.Query = function () {
        requestor.Close();
    }
}