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

	 var self=this;
	 var requestor= new PBWindow();
	 var dw_1= new DataWindow( 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_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_2","Clicked",self.Query); 

		 if(typeof(self.Save)=="function") 
			 win.AttachEvent("cb_1","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() {
	 if(typeof(this.Release)=="function") this.Release(); 
	 if(typeof(baseUnBind)=="function") baseUnBind(); 

		 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 curPzh = "";
    var dw_list = null;
    var curW_OPEN = new OpenWindowQueryBase();
    var webReq = new WebRequestor();
    webReq.SetWebServiceUrl("/Service/ServiceCaiWu.ashx");

    this.PostOpen = function () {
        CurUserId = requestor.GetParm("CurUserId");
        curEditModel = requestor.GetParm("EditModel");
        if (curEditModel == "EDIT") {
            dw_list.CopyDataEx(dw_list.GetRow(), dw_1, 0);
            dw_1.ResetUpdate();
            dw_1.Modify(" pzh.protect='1'");

        } else {
            self.Add();
        }
        dw_1.SetColumn("bmbh");
        dw_1.SetFocus();
    }

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

    this.Add = function () {
        dw_1.InsertRow(0);
        webReq.Invoke("GetTZDPzh", "flag=ST&table=cw_ysktzd");
        if (webReq.StatusCode() == 200) {
            curPzh = webReq.ResponseText();
            dw_1.SetItem(dw_1.GetRow(), "pzh", curPzh);
            dw_1.SetItem(dw_1.GetRow(), "zdr", CurUserId);
         
        } else {
            requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
            return;
        }
    }

    this.DoubleClicked = function (x, y, row, dwcName) {

        if (dwcName == "khbh") {
            curW_OPEN.SetDataWindow(dw_1, "khbh", "khmc", "M", self)   // 这个用来给子窗口Set当前数据窗口
            requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.DangAn.Base.OpenWindowQueryBase&treeDw=dw_da_khda_fl_tree&listDw=dw_pub_khda_list&treeRootLabel=客户分类&treeData=flbh&treeLabel=flmc&treeParentData=fflbh&treeLeave=islast&selectStyle=multiple"), curW_OPEN, null);
        }
        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&selectStyle=multiple&curQxlx=cwcx"), curW_OPEN, null);
        }
    }

    this.itemChanged = function (row, dwoName, data) {

        if (dwoName == "khbh") {
            dw_1.GetChild(dwoName, dwc);
            var findRow = dwc.Find("khbh='" + data + "' ", 1, dwc.RowCount());
            if (findRow <= 0) {
                requestor.MessageBox("提示", "未找到客户编号为[" + data + "]的记录，请检查!", ICON.StopSign);
                return 1;
            } else {
                dw_1.SetItem(row, "khmc", dwc.GetItemString(findRow, "khmc"));
            }
           
        }
        if (dwoName == "bmbh") {
            dw_1.GetChild(dwoName, dwc);
            var findRow = dwc.Find("bmbh='" + data + "' ", 1, dwc.RowCount());
            if (findRow <= 0) {
                requestor.MessageBox("提示", "未找到部门编号为[" + data + "]的记录，请检查!", ICON.StopSign);
                return 1;
            } else {
                dw_1.SetItem(row, "bmmc", dwc.GetItemString(findRow, "bmmc"));
            }

        }
    }


    this.EditChanged = function (row, col, data) {
        CurUserId = requestor.GetParm("CurUserId");
        if (col == "bmbh") {
            dw_1.GetChild(col, dwc);
            if (data != "" || data != null) {
                // dwc.RetrieveEx("Upper(bmbh) like '%" + data.toUpperCase() + "%' or Upper(bmmc) like '%" + data + "%'");
                dwc.Retrieve(CurUserId, data);
            }
            else {
                dwc.Retrieve(CurUserId, "%");
            }
        }

        if (col == "khbh") {
            dw_1.GetChild(col, dwc);

            if (data != "" || data != null) {
                dwc.RetrieveEx("Upper(khbh) like '%" + data.toUpperCase() + "%' or Upper(khmc) like '%" + data + "%'");

            } else {
                dwc.Retrieve();
            }
        }
    }

    this.Save = function () {

        if (!dw_1.CheckData("*", "")) {
            return
        }
        var dwChanges = dw_1.GetChanges();
        if (dwChanges == "") {
            return;
        }
        
        webReq.Invoke("SaveAsYSTZD", "dwObjects=dw_cw_ysktzd_edit&dwChanges=" + dwChanges);

        if (webReq.StatusCode() == 200) {

            dw_list.Retrieve();
            if (curEditModel == "ADD") {

                requestor.MessageBox("提示", "数据保存成功!", ICON.Information);
                dw_1.ScrollToRow(dw_1.InsertRow(0));
                dw_1.ResetUpdate();
                self.Add();
            }
            else {

                requestor.MessageBox("提示", "数据保存成功!", ICON.Information);
                requestor.Close();
            }

        }
        else {
            requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
        }
    }

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