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

    var self = this;
    this.Instance = null;
    var requestor = new PBWindow();
    var cb_3 = new CommandButton(requestor);
    var cb_2 = new CommandButton(requestor);
    var dw_top = new DataWindow(requestor);
    var tab_1 = new TabControl(requestor);
    var dw_cxsp = new DataWindow(requestor);
    var dw_tjmx = new DataWindow(requestor);
    var dw_bm = new DataWindow(requestor);
    var cb_1 = new CommandButton(requestor);
    var cb_save = new CommandButton(requestor);
    var cb_4 = new CommandButton(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_3 == null) cb_3 = new CommandButton(requestor);
        cb_3.Attach(win.Control("cb_3"));
        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 (tab_1 == null) tab_1 = new TabControl(requestor);
        tab_1.Attach(win.Control("tab_1"));
        if (dw_cxsp == null) dw_cxsp = new DataWindow(requestor);
        dw_cxsp.Attach(win.Control("dw_cxsp"));
        if (dw_tjmx == null) dw_tjmx = new DataWindow(requestor);
        dw_tjmx.Attach(win.Control("dw_tjmx"));
        if (dw_bm == null) dw_bm = new DataWindow(requestor);
        dw_bm.Attach(win.Control("dw_bm"));
        if (cb_1 == null) cb_1 = new CommandButton(requestor);
        cb_1.Attach(win.Control("cb_1"));
        if (cb_save == null) cb_save = new CommandButton(requestor);
        cb_save.Attach(win.Control("cb_save"));
        if (cb_4 == null) cb_4 = new CommandButton(requestor);
        cb_4.Attach(win.Control("cb_4"));

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

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

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

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

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

        if (typeof (self.dw_cxsp_click) == "function")
            win.AttachEvent("dw_cxsp", "Clicked", self.dw_cxsp_click);

        if (typeof (self.dw_tjmx_click) == "function")
            win.AttachEvent("dw_tjmx", "Clicked", self.dw_tjmx_click);

        if (typeof (self.Key_Down_tjmx) == "function")
            win.AttachEvent("dw_tjmx", "KeyDown", self.Key_Down_tjmx);

        if (typeof (self.dw_bm_click) == "function")
            win.AttachEvent("dw_bm", "Clicked", self.dw_bm_click);

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

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

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

    }

    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_3.Detach(); cb_3 = null;
        cb_2.Detach(); cb_2 = null;
        dw_top.Detach(); dw_top = null;
        tab_1.Detach(); tab_1 = null;
        dw_cxsp.Detach(); dw_cxsp = null;
        dw_tjmx.Detach(); dw_tjmx = null;
        dw_bm.Detach(); dw_bm = null;
        cb_1.Detach(); cb_1 = null;
        cb_save.Detach(); cb_save = null;
        cb_4.Detach(); cb_4 = null;
        requestor.Detach(); requestor = null;

        setTimeout(CollectGarbage, 100);
    }

    //以上代码由设计器自动生成,请不要手工修改
    //ExtPB.Net JavaScript End  设计器查找标志,请不要删除或修改
    var cur_dw_indwx = "1";
    var curFocusDw = "dw_bm";
    var curW_OPEN_BMDA_NEW = new OpenWindowQueryNewBase();
    var curW_OPEN_BMDA_URL = "OpenWindowQueryNewBase";
    var curW_OPEN_BMDA = new OpenWindowQueryBase();
    var webReq = new WebRequestor();
    var curDw_list ;
    var curDw_key;
    var LsjOrDpj = "";
    webReq.SetWebServiceUrl("/Service/ServiceCuXiaoFangAn.ashx");
    this.PostOpen = function () {
        var opFlag = requestor.GetParm("opFlag")
        if (opFlag == "ADD") {
            dw_top.ScrollToRow(dw_top.InsertRow(0));
            dw_top.SetItem(dw_top.GetRow(), "kdrq", new Date().getVarDate());
            dw_top.SetItem(dw_top.GetRow(), "ksrq", new Date().getVarDate());
            dw_top.SetItem(dw_top.GetRow(), "jsrq", GoNextMonth());
            dw_top.Modify("zzsj.Protect='1' zzsj.color='8421504'");
            dw_top.Modify("kssj.Protect='1' kssj.color='8421504'");
            dw_top.Modify("xq1.Protect='1' xq1_t.color='8421504'");
            dw_top.Modify("xq2.Protect='1' xq2_t.color='8421504'");
            dw_top.Modify("xq3.Protect='1' xq3_t.color='8421504'");
            dw_top.Modify("xq4.Protect='1' xq4_t.color='8421504'");
            dw_top.Modify("xq5.Protect='1' xq5_t.color='8421504'");
            dw_top.Modify("xq6.Protect='1' xq6_t.color='8421504'");
            dw_top.Modify("xq7.Protect='1' xq7_t.color='8421504'");
            dw_tjmx.ScrollToRow(dw_tjmx.InsertRow(0))
        }
        if (opFlag == "EDIT") {
            dw_top.RetrieveEx("pzh='" + curDw_key + "'");
            dw_bm.RetrieveEx("pzh='" + curDw_key + "'");
            dw_cxsp.RetrieveEx("pzh='" + curDw_key + "'");
            dw_tjmx.RetrieveEx("pzh='" + curDw_key + "'");
            if (dw_top.GetItemString(dw_top.GetRow(), "sdsdzt") == "1") {
                //可编辑
                dw_top.Modify("zzsj.Protect='0' zzsj.color='33554432'");
                dw_top.Modify("kssj.Protect='0' kssj.color='33554432'");
            } else {
                dw_top.Modify("zzsj.Protect='1' zzsj.color='8421504'");
                dw_top.Modify("kssj.Protect='1' kssj.color='8421504'");

            }

            if (dw_top.GetItemString(dw_top.GetRow(), "cxrsdzt") == "1") {
                //可编辑
                dw_top.Modify("xq1.Protect='0' xq1_t.color='33554432'");
                dw_top.Modify("xq2.Protect='0' xq2_t.color='33554432'");
                dw_top.Modify("xq3.Protect='0' xq3_t.color='33554432'");
                dw_top.Modify("xq4.Protect='0' xq4_t.color='33554432'");
                dw_top.Modify("xq5.Protect='0' xq5_t.color='33554432'");
                dw_top.Modify("xq6.Protect='0' xq6_t.color='33554432'");
                dw_top.Modify("xq7.Protect='0' xq7_t.color='33554432'");
            } else {

                dw_top.Modify("xq1.Protect='1' xq1_t.color='8421504'");
                dw_top.Modify("xq2.Protect='1' xq2_t.color='8421504'");
                dw_top.Modify("xq3.Protect='1' xq3_t.color='8421504'");
                dw_top.Modify("xq4.Protect='1' xq4_t.color='8421504'");
                dw_top.Modify("xq5.Protect='1' xq5_t.color='8421504'");
                dw_top.Modify("xq6.Protect='1' xq6_t.color='8421504'");
                dw_top.Modify("xq7.Protect='1' xq7_t.color='8421504'");

            }
            if (dw_tjmx.RowCount() < 1) {
                dw_tjmx.InsertRow(0)
            }
        }
    }
    this.SetDataWindow = function (dw__1, keyValue) {
        curDw_list = dw__1;
        curDw_key = keyValue;
    }
    this.ItemChanged = function (row, dwcName, date, sobj) {
        if (dwcName == "sdsdzt") {
            var datas = dw_top.GetItemString(dw_top.GetRow(), "sdsdzt")
            if (datas == "1") { // 要disable 
                dw_top.Modify("zzsj.Protect='1' zzsj.color='8421504'");
                dw_top.Modify("kssj.Protect='1' kssj.color='8421504'");
            } else {// 要enabled  
                dw_top.Modify("zzsj.Protect='0' zzsj.color='33554432'");
                dw_top.Modify("kssj.Protect='0' kssj.color='33554432'");
            }
        } else if (dwcName == "cxrsdzt") {
            var datas = dw_top.GetItemString(dw_top.GetRow(), "cxrsdzt")
            if (datas == "1") {
                dw_top.Modify("xq1.Protect='1' xq1_t.color='8421504'");
                dw_top.Modify("xq2.Protect='1' xq2_t.color='8421504'");
                dw_top.Modify("xq3.Protect='1' xq3_t.color='8421504'");
                dw_top.Modify("xq4.Protect='1' xq4_t.color='8421504'");
                dw_top.Modify("xq5.Protect='1' xq5_t.color='8421504'");
                dw_top.Modify("xq6.Protect='1' xq6_t.color='8421504'");
                dw_top.Modify("xq7.Protect='1' xq7_t.color='8421504'");
            } else {// 要enabled   

                dw_top.Modify("xq1.Protect='0' xq1_t.color='33554432'");
                dw_top.Modify("xq2.Protect='0' xq2_t.color='33554432'");
                dw_top.Modify("xq3.Protect='0' xq3_t.color='33554432'");
                dw_top.Modify("xq4.Protect='0' xq4_t.color='33554432'");
                dw_top.Modify("xq5.Protect='0' xq5_t.color='33554432'");
                dw_top.Modify("xq6.Protect='0' xq6_t.color='33554432'");
                dw_top.Modify("xq7.Protect='0' xq7_t.color='33554432'");
            }
        }
    }
    this.SelectionChanging = function (index_old, index_new) {
        cur_dw_indwx = index_new
    }
    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&treeRootLabel=部门分类&treeData=flbh&treeLabel=flmc&treeParentData=fflbh&treeLeave=islast&curQxlx=all&selectStyle=multiple"), curW_OPEN_BMDA_NEW, null);
    }
    this.Xzkh = function () {
        if (cur_dw_indwx == "1") {
            curW_OPEN_BMDA_NEW.SetDataWindow(dw_cxsp, "spkh", "ksmc", "CXFA", self);
        }
        // 这个用来给子窗口Set当前数据窗口
        requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.DangAn.Base." + curW_OPEN_BMDA_URL + "&treeDw=dw_da_sp_lbda_tree&listDw=dw_pub_sp_spda_open_list&treeRootLabel=商品分类&treeData=lbbh&treeLabel=lbmc&treeParentData=flbbh&treeLeave=islast&selectStyle=multiple"), curW_OPEN_BMDA_NEW, null);
        var countRow = dw_cxsp.RowCount();
        requestor.MessageBox("提示", "目前总共选择" + countRow + "款", ICON.Information);
    }
    this.Key_Down_tjmx = function (key) {

        if (dw_tjmx.GetColumnName() == "yz" && key == "13") {
            dw_tjmx.AcceptText();
            if (dw_tjmx.GetItemNumber(dw_tjmx.GetRow(), "yz") == "" || dw_tjmx.GetItemNumber(dw_tjmx.GetRow(), "yz").length < 1) {
                requestor.MessageBox("提示", "请给当前行添加规则", ICON.Information);
                return 1
            }
            if (dw_tjmx.GetItemNumber(dw_tjmx.GetRow(), "xj") == "" || dw_tjmx.GetItemNumber(dw_tjmx.GetRow(), "xj").length < 1) {
                requestor.MessageBox("提示", "请给当前行添加规则", ICON.Information);
                return 1
            }
            var oldRow = dw_tjmx.GetItemString(dw_tjmx.RowCount(), "xh");

            var rows = dw_tjmx.InsertRow(0)
            dw_tjmx.ScrollToRow(rows);
            var newRow = (parseInt(oldRow) + 1);
            dw_tjmx.SetItem(rows, "xh", (newRow).toString());
            dw_tjmx.SetColumn("yz");
            dw_tjmx.SetFocus();
            return 1;
        }
    }
    this.EvalQuDpjOrLsj = function (data) {
        eval(data);
    }
    this.DeleteRow = function () {
        if (curFocusDw == "dw_tjmx") {
            if (dw_tjmx.RowCount() == 1) {
                return 1
            }
            else {
                eval(curFocusDw + ".DeleteRow(" + curFocusDw + ".GetRow())");
            }
        }
        else {
            eval(curFocusDw + ".DeleteRow(" + curFocusDw + ".GetRow())");
        }
    }
    this.dw_bm_click = function (x, y, row, dwcNme, sobj) {
        curFocusDw = "dw_bm";
    }
    this.dw_cxsp_click = function (x, y, row, dwcNme, sobj) {
        curFocusDw = "dw_cxsp";
    }
    this.dw_tjmx_click = function (x, y, row, dwcNme, sobj) {
        curFocusDw = "dw_tjmx";
    }

    this.SaveAll = function () {
        if (dw_bm.AcceptText() != 1)
            return;
        if (dw_top.AcceptText() != 1)
            return;
        if (dw_cxsp.AcceptText() != 1)
            return;
        if (dw_tjmx.AcceptText() != 1)
            return;
        var msg = "";
        if (dw_top.GetItemString(dw_top.GetRow(), "hdmc").length < 1) {
            msg += "活动名称不能为空\r\n";
        }
        if (dw_cxsp.RowCount() < 1) {
            msg += "请选择促销款号\r\n";
        }
        for (var i = 1; i <= dw_tjmx.RowCount(); i++) {
            if (dw_tjmx.AcceptText() != 1)
                return;
            //alert([dw_fajb.GetItemNumber(i, "rlsl"), dw_fajb.GetItemNumber(i, "mmsl")])

            if (dw_tjmx.GetItemNumber(i, "xj") == "" || dw_tjmx.GetItemNumber(i, "yz") == "") {
                requestor.MessageBox("提示", "请给添加打折明细", ICON.Information);
                return 1
            }
            
        }
        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(), "hdyy", dw_top.GetItemString(dw_top.GetRow(), "hdyy"));
        dw_bm.SetItem(1, "bmbh", dw_bm.GetItemString(1, "bmbh"));
        dw_cxsp.SetItem(1, "spkh", dw_cxsp.GetItemString(1, "spkh"));
        dw_tjmx.SetItem(1, "xj", dw_tjmx.GetItemNumber(1, "xj"));
        var dwChange1 = dw_top.GetChanges();
        var dwChange2 = dw_bm.GetChanges();
        var dwChange3 = dw_cxsp.GetChanges();
        var dwChange4 = dw_tjmx.GetChanges();
        webReq.Invoke("W_Mdxjdyz_Edit_Save", "dwObject1=" + dw_top.GetDataObject() + "&dwChange1=" + dwChange1 + "&dwObject2=" + dw_bm.GetDataObject() + "&dwChange2=" + dwChange2 + "&dwObject3=" + dw_cxsp.GetDataObject() + "&dwChange3=" + dwChange3 + "&dwObject4=" + dw_tjmx.GetDataObject() + "&dwChange4=" + dwChange4 + "&curLsjOrDpj=" + LsjOrDpj);
        if (webReq.StatusCode() == 200) {
            var curPzh = webReq.ResponseText();
            //更新list数据  


            curDw_list.Refresh();
            var findRow = curDw_list.Find("pzh='" + curPzh + "'", 1, curDw_list.RowCount());
            if (findRow > 0) {
                curDw_list.ScrollToRow(findRow);
            }






            var opFlag = requestor.GetParm("opFlag")
            if ("ADD" == opFlag) {
                dw_top.RetrieveEx("pzh='" + curPzh + "'");
                dw_cxsp.RetrieveEx("pzh='" + curPzh + "'");
                dw_tjmx.RetrieveEx("pzh='" + curPzh + "'");
                dw_bm.RetrieveEx("pzh='" + curPzh + "'");
                requestor.MessageBox("提示", "数据保存成功!", ICON.Information);
                dw_top.SetItemStatus(1, "pzh", DWBUFFER.Primary, true);
                return 1;
            }
            if ("EDIT" == opFlag) {
                dw_top.RetrieveEx("pzh='" + curPzh + "'");
                dw_cxsp.RetrieveEx("pzh='" + curPzh + "'");
                dw_tjmx.RetrieveEx("pzh='" + curPzh + "'");
                dw_bm.RetrieveEx("pzh='" + curPzh + "'");
                requestor.MessageBox("提示", "数据保存成功!", ICON.Information);
                //requestor.Close();
            }

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

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