///<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" />
///<reference  path="/CuXiao/CuXiaoSheZhi_Base/W_CXFASZ_Edit.win.js" /> 

//注意,需要在对象定义后面，手工增加以下代码实现对象继承
// W_Mmxystdsp_Edit.prototype = new W_CXFASZ_Edit( ); 

function W_Mmxystdsp_Edit() {

    var self = this;
    this.Instance = null;
    var requestor = new PBWindow();
    var dw_top = new DataWindow(requestor);
    var tab_1 = new TabControl(requestor);
    var dw_bm = new DataWindow(requestor);
    var cb_import = 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 (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_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.DeleteRow) == "function")
            win.AttachEvent("cb_5", "Clicked", self.DeleteRow);

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

        if (typeof (self.Import) == "function")
            win.AttachEvent("cb_import", "Clicked", self.Import);

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

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

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

    }

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

        dw_top.Detach(); dw_top = null;
        tab_1.Detach(); tab_1 = null;
        dw_bm.Detach(); dw_bm = null;
        requestor.Detach(); requestor = null;

        setTimeout(CollectGarbage, 100);
    }

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

    var curDw_list = "";
    var curDw_key = "";
    var dw_zssp = new DataWindow(requestor);
    var curFocusDw = "dw_bm";
    var cur_dw_indwx = "1";
    var curW_OPEN_BMDA_NEW = new OpenWindowQueryNewBase();
    var curW_OPEN_BMDA_URL = "OpenWindowQueryNewBase";
    var curW_OPEN_BMDA = new OpenWindowQueryBase();
    var curW_CXFADR = new W_CXFSSPDR()
    var webReq = new WebRequestor();
    webReq.SetWebServiceUrl("/Service/ServiceCuXiaoFangAn.ashx");
    var LsjOrDpj = "";
    //    var curLsjOrDpj = "1"; // 默认1是零售价 0 是吊牌价
    this.SetDataWindow = function (dw_1, keyValue) {
        curDw_list = dw_1;
        curDw_key = keyValue;
    }

    this.PostOpen = function () {
        //促销商品
        if (dw_zssp == null) dw_zssp = new DataWindow(requestor);
        dw_zssp.Attach(requestor.Control("dw_zssp"));

        if (typeof (self.dw_zssp_click) == "function")
            requestor.AttachEvent("dw_zssp", "Clicked", self.dw_zssp_click);
        if (typeof (self.Key_Down_zssp) == "function")
            requestor.AttachEvent("dw_zssp", "KeyDown", self.Key_Down_zssp);
        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_top.Modify("kssj.Protect='1' kssj.color='8421504'");
            dw_top.SetColumn("hdmc");
            dw_top.SetFocus();

        }
        if (opFlag == "EDIT") {
            dw_top.RetrieveEx("pzh='" + curDw_key + "'");
            dw_bm.RetrieveEx("pzh='" + curDw_key + "'");
            dw_zssp.RetrieveEx("pzh='" + curDw_key + "'");

        }
    }

    this.Xzkh = function () {
        //alert("xxxxx+第三方的身份")
        curW_OPEN_BMDA_NEW.SetDataWindow(dw_zssp, "spkh", "ksmc", "ZSTDSP", 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);
        //alert("xxxx" + dw_zssp.RowCount())
        //        var countRow = dw_zssp.RowCount();
        //        requestor.MessageBox("提示", "目前总共选择" + countRow + "款", ICON.Information);
    }
    //#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&treeRootLabel=部门分类&treeData=flbh&treeLabel=flmc&treeParentData=fflbh&treeLeave=islast&curQxlx=all&selectStyle=multiple"), curW_OPEN_BMDA_NEW, null);
    }
    //#endregion
    this.SelectionChanging = function (index_old, index_new) {
        cur_dw_indwx = index_new
    }
    this.DeleteRow = function () {
        eval(curFocusDw + ".DeleteRow(" + curFocusDw + ".GetRow())");
    }
    this.dw_bm_click = function (x, y, row, dwcNme, sobj) {
        curFocusDw = "dw_bm";
    }
    this.dw_zssp_click = function (x, y, row, dwcNme, sobj) {
        curFocusDw = "dw_zssp";
    }
    this.Key_Down_zssp = function (key) {
        if (dw_zssp.GetColumnName() == "je" && key == "13") {
            if (dw_zssp.GetItemNumber(dw_zssp.GetRow(), "je").length < 1) {
                requestor.MessageBox("提示", "请给当前行添加买满金额", ICON.Information);
                return 1
            }
            //         
            //            var oldRow = dw_zssp.GetItemString(dw_zssp.RowCount(), "xh");
            //            var rows = dw_zssp.InsertRow(0)
            //            dw_zssp.ScrollToRow(rows);
            //            var newRow = (parseInt(oldRow) + 1);
            //            dw_zssp.SetItem(rows, "xh", (newRow).toString());
            dw_zssp.SetColumn("je");
            dw_zssp.SetFocus();
            return 1;
        }
    }

    this.SaveAll = function () {
        if (dw_bm.AcceptText() != 1)
            return;
        if (dw_top.AcceptText() != 1)
            return;
        if (dw_zssp.AcceptText() != 1)
            return;

        var msg = "";
        if (dw_top.GetItemString(dw_top.GetRow(), "hdmc").length < 1) {
            msg += "活动名称不能为空\r\n";
        }
        if (dw_zssp.RowCount() < 1) {
            msg += "请选择赠送款号\r\n";
        }

        var curMaxXhForInsert = 0
        if (requestor.GetParm("opFlag") == "EDIT") {


            dw_zssp.SetSort("xh_number A");
            dw_zssp.Sort();

            var curMaxXh = dw_zssp.GetItemNumber(dw_zssp.RowCount(), "xh_number");

            curMaxXhForInsert = parseInt(curMaxXh)
        }

        for (var i = 1; i <= dw_zssp.RowCount(); i++) {
            if (dw_zssp.AcceptText() != 1)
                return;
            //alert([dw_fajb.GetItemNumber(i, "rlsl"), dw_fajb.GetItemNumber(i, "mmsl")])

            if (dw_zssp.GetItemNumber(i, "je") == 0) {
                requestor.MessageBox("提示", "请给当前行添加买满金额", ICON.Information);
                return 1
            }

            dw_zssp.SetItem(i, "xh", parseInt(curMaxXhForInsert) + i);
        }
        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(), "hdmc", dw_top.GetItemString(dw_top.GetRow(), "hdmc"));
        dw_bm.SetItem(1, "bmbh", dw_bm.GetItemString(1, "bmbh"));
        dw_zssp.SetItem(1, "spkh", dw_zssp.GetItemString(1, "spkh"));
        var dwChange1 = dw_top.GetChanges();
        var dwChange2 = dw_bm.GetChanges();
        var dwChange3 = dw_zssp.GetChanges();
        webReq.Invoke("W_MMXYSTDSPSheZhi_Edit_Save", "dwObject1=" + dw_top.GetDataObject() + "&dwChange1=" + dwChange1 + "&dwObject2=" + dw_bm.GetDataObject() + "&dwChange2=" + dwChange2 + "&dwObject3=" + dw_zssp.GetDataObject() + "&dwChange3=" + dwChange3 + "&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_zssp.RetrieveEx("pzh='" + curPzh + "'");
                dw_bm.RetrieveEx("pzh='" + curPzh + "'");
                requestor.MessageBox("提示", "数据保存成功!", ICON.Information);
                //dw_top.SetItemStatus(1, "pzh", DWBUFFER.Primary, true);
                requestor.Close();
            } else if ("EDIT" == opFlag) {
                dw_top.RetrieveEx("pzh='" + curPzh + "'");
                dw_zssp.RetrieveEx("pzh='" + curPzh + "'");
                dw_bm.RetrieveEx("pzh='" + curPzh + "'");
                requestor.MessageBox("提示", "数据保存成功!", ICON.Information);
                //requestor.Close();
            }

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

    this.Import = function () {

        curW_CXFADR.SetObj(dw_zssp, cb_import);
        requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.import.W_CXFSSPDR&curcxfamc=mmxystdsp&cur_dw_indwx=1"), curW_CXFADR, null);
    }

    //#region 导出
    this.Emportsp = function () {

        //获取当前的数据窗口
        //  alert("cur_dw_indwx" + cur_dw_indwx)
        if (cur_dw_indwx == "1") {

            if (dw_zssp.RowCount() > 0) {
                dw_zssp.Export();
            }
        }

        if (cur_dw_indwx == "2") {
            if (dw_zsmx.RowCount() > 0) {
                dw_zsmx.Export();
            }
        }
    }
    // #endregion

    this.EvalQuDpjOrLsj = function (data) {
        eval(data);
    }
}
W_Mmxystdsp_Edit.prototype = new W_CXFASZ_Edit()