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

    var self = this;
    this.Instance = null;
    var requestor = new PBWindow();
    var cb_4 = new CommandButton(requestor);
    var cb_3 = new CommandButton(requestor);
    var cb_1 = new CommandButton(requestor);
    var cb_2 = new CommandButton(requestor);
    var dw_top = new DataWindow(requestor);
    var dw_fajb = new DataWindow(requestor);
    var dw_BM = 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 (cb_4 == null) cb_4 = new CommandButton(requestor);
        cb_4.Attach(win.Control("cb_4"));
        if (cb_3 == null) cb_3 = new CommandButton(requestor);
        cb_3.Attach(win.Control("cb_3"));
        if (cb_1 == null) cb_1 = new CommandButton(requestor);
        cb_1.Attach(win.Control("cb_1"));
        if (cb_2 == null) cb_2 = new CommandButton(requestor);
        cb_2.Attach(win.Control("cb_2"));
        if (dw_top == null) dw_top = new DataWindow(requestor);
        dw_top.Attach(win.Control("dw_top"));
        if (dw_fajb == null) dw_fajb = new DataWindow(requestor);
        dw_fajb.Attach(win.Control("dw_fajb"));
        if (dw_BM == null) dw_BM = new DataWindow(requestor);
        dw_BM.Attach(win.Control("dw_BM"));

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

        if (typeof (self.Closed) == "function")
            win.AttachEvent("cb_4", "Clicked", self.Closed);

        if (typeof (self.DeleteBm) == "function")
            win.AttachEvent("cb_3", "Clicked", self.DeleteBm);

        if (typeof (self.SaveAll) == "function")
            win.AttachEvent("cb_1", "Clicked", self.SaveAll);

        if (typeof (self.Xzbm) == "function")
            win.AttachEvent("cb_2", "Clicked", self.Xzbm);

    }

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

        cb_4.Detach(); cb_4 = null;
        cb_3.Detach(); cb_3 = null;
        cb_1.Detach(); cb_1 = null;
        cb_2.Detach(); cb_2 = null;
        dw_top.Detach(); dw_top = null;
        dw_fajb.Detach(); dw_fajb = null;
        dw_BM.Detach(); dw_BM = null;
        requestor.Detach(); requestor = null;

        setTimeout(CollectGarbage, 100);
    }

    //以上代码由设计器自动生成,请不要手工修改
    //ExtPB.Net JavaScript End  设计器查找标志,请不要删除或修改
    var webReq = new WebRequestor();
    webReq.SetWebServiceUrl("/Service/HuiyuanJiFenchuli.ashx");
    var curW_OPEN_BMDA_NEW = new OpenWindowQueryNewBase();
    var curW_OPEN_BMDA_URL = "OpenWindowQueryNewBase";
    var curPzh = "";
    var curDw_list = "";
    this.PostOpen = function () {
        var opFlag = requestor.GetParm("opFlag");
        if (opFlag == "ADD") {
            dw_top.ScrollToRow(dw_top.InsertRow(0));
            dw_top.SetItem(dw_top.GetRow(), "kssj", new Date().getVarDate());
            dw_top.SetItem(dw_top.GetRow(), "jssj", GoNextMonth());
            dw_fajb.InsertRow(0)
        }
        if (opFlag == "EDIT") {
            dw_top.RetrieveEx("pzh='" + curPzh + "'");
            dw_BM.RetrieveEx("pzh='" + curPzh + "'");
            dw_fajb.RetrieveEx("pzh='" + curPzh + "'");
        }
    }
	 //#region 选择部门
	 this.Xzbm = function () {
	     curW_OPEN_BMDA_NEW.SetDataWindow(dw_BM, "bmbh", "bmmc", "C")  // 这个用来给子窗口Set当前数据窗口
	     requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.DangAn.Base." + curW_OPEN_BMDA_URL + "&treeDw=dw_da_bmda_fl_tree&listDw=dw_pub_list_bmda_hyhd&treeRootLabel=部门分类&treeData=flbh&treeLabel=flmc&treeParentData=fflbh&treeLeave=islast&curQxlx=all&selectStyle=multiple"), curW_OPEN_BMDA_NEW, null);
	 }
	 //#endregion
	 this.SaveAll = function () {
	     dw_top.AcceptText();
	     dw_BM.AcceptText();
	     dw_fajb.AcceptText();
	     var msg = "";
	     if (dw_top.GetItemString(dw_top.GetRow(), "hdmc").length < 1) {
	         msg += "活动名称不能为空\r\n";
	     }
	     if (dw_fajb.GetItemNumber(1, "je") == "") {
	         msg += "金额不能为空\r\n";
	     }
	     if (dw_fajb.GetItemNumber(1, "sl") == "") {
	         msg += "数量不能为空\r\n";
	     }
	     if (dw_fajb.GetItemNumber(1, "bl") == "") {
	         msg += "返现比例不能为空\r\n";
	     }
	     if (dw_BM.RowCount() < 1) {
	         msg += "请选择部门\r\n";
	     }
	     if (msg.length > 0) {
	         requestor.MessageBox("提示", msg.toString(), ICON.StopSign);
	         return;
	     }
	     dw_top.SetItem(dw_top.GetRow(), "bzxx", dw_top.GetItemString(dw_top.GetRow(), "bzxx"));
	     dw_BM.SetItem(dw_BM.GetRow(), "bmbh", dw_BM.GetItemString(dw_BM.GetRow(), "bmbh"));
	     dw_fajb.SetItem(dw_fajb.GetRow(), "pzh", dw_fajb.GetItemString(dw_fajb.GetRow(), "pzh"));
	    
	   
	     var dwChange1 = dw_top.GetChanges();
	     var dwChange2 = dw_BM.GetChanges();
	     var dwChange3 = dw_fajb.GetChanges();
	     webReq.Invoke("W_HY_MMXYYXFZY__Edit_Save", "dwObject1=" + dw_top.GetDataObject() + "&dwChange1=" + dwChange1 + "&dwObject2=" + dw_BM.GetDataObject() + "&dwChange2=" + dwChange2 + "&dwObject3=" + dw_fajb.GetDataObject() + "&dwChange3=" + dwChange3);
	     if (webReq.StatusCode() == 200) {
	         var curPzh = webReq.ResponseText();
	         curDw_list.Refresh();
	         var findRow = curDw_list.Find("pzh='" + curPzh + "'", 1, curDw_list.RowCount());
	         if (findRow > 0) {
	             curDw_list.ScrollToRow(findRow);
	         }
	         dw_top.SetItem(dw_top.GetRow(), "pzh", curPzh);
	         dw_top.ResetUpdate();
	         dw_fajb.RetrieveEx("pzh='" + curPzh + "'");
	         dw_BM.RetrieveEx("pzh='" + curPzh + "'");
	         requestor.MessageBox("提示", "数据保存成功!", ICON.Information);
	         return;
	     }
	     else {
	         requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
	     }
	 }
	 this.SetDataWindow = function (dw, keyValue) {
	     curPzh = keyValue;
	     curDw_list = dw;
	 }
	 this.Closed = function () {
	     requestor.Close();
	 }
	 this.DeleteBm = function () {
	     dw_BM.DeleteRow(dw_BM.GetRow());
	 }
 }