///<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_Mmxyrlyy_Edit.prototype = new W_CXFASZ_Edit( ); 

function W_Mmxyrlyy_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_mmxsrlyy)=="function") 
			 win.AttachEvent("window","PostOpen",self.PostOpen_mmxsrlyy); 

		 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.ItemChanged)=="function") 
			 win.AttachEvent("dw_top","ItemChanged",self.ItemChanged); 

		 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 curFocusDw = "dw_bm";
    var dw_cxsp = new DataWindow(requestor);
    var dw_zsmx = new DataWindow(requestor);
    var cb_6;
    var sle_1;
    var webReq = new WebRequestor();
    webReq.SetWebServiceUrl("/Service/ServiceCuXiaoFangAn.ashx");
    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 LsjOrDpj = "";
    this.SetDataWindow = function (dw__1, keyValue) {
        curDw_list = dw__1;
        curDw_key = keyValue;
    }
    this.PostOpen_mmxsrlyy = function () {
        if (dw_cxsp == null) dw_cxsp = new DataWindow(requestor);
        dw_cxsp.Attach(requestor.Control("dw_cxsp"));
        if (dw_cxsp == null) dw_cxsp = new DataWindow(requestor);
        dw_cxsp.Attach(requestor.Control("dw_cxsp"));
        if (typeof (self.dw_cxsp_click) == "function")
            requestor.AttachEvent("dw_cxsp", "Clicked", self.dw_cxsp_click);
        //特价明细
        if (dw_zsmx == null) dw_zsmx = new DataWindow(requestor);
        dw_zsmx.Attach(requestor.Control("dw_zsmx"));
        if (dw_zsmx == null) dw_zsmx = new DataWindow(requestor);
        dw_zsmx.Attach(requestor.Control("dw_zsmx"));
        if (typeof (self.Key_Down_zsmx) == "function")
            requestor.AttachEvent("dw_zsmx", "KeyDown", self.Key_Down_zsmx);
        if (typeof (self.dw_zsmx_click) == "function")
            requestor.AttachEvent("dw_zsmx", "Clicked", self.dw_zsmx_click);
        //增加明细的确定按钮
        if (cb_6 == null) cb_6 = new DataWindow(requestor);
        cb_6.Attach(requestor.Control("cb_6"));
        if (typeof (self.Add_zsmx) == "function")
            requestor.AttachEvent("cb_6", "Clicked", self.Add_zsmx);
        //
        if (sle_1 == null) sle_1 = new SingleLineEdit(requestor);
        sle_1.Attach(requestor.Control("sle_1"));
        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_zsmx.ScrollToRow(dw_zsmx.InsertRow(0))
            dw_zsmx.SetItem(dw_zsmx.GetRow(), "xh", "1");
        }
        if (opFlag == "EDIT") {
            dw_top.RetrieveEx("pzh='" + curDw_key + "'");
            dw_bm.RetrieveEx("pzh='" + curDw_key + "'");
            dw_cxsp.RetrieveEx("pzh='" + curDw_key + "'");
            dw_zsmx.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_zsmx.RowCount() < 1) {
                dw_zsmx.ScrollToRow(dw_zsmx.InsertRow(0))
                dw_zsmx.SetItem(dw_zsmx.GetRow(), "xh", "1");
            }
        }
    }
    this.Add_zsmx = function () {
        if (dw_zsmx.AcceptText() != 1)
            return;
        var curRow = sle_1.GetText();
        var curMmsl = dw_zsmx.GetItemNumber(1, "mmje");
        var curzq = dw_zsmx.GetItemNumber(1, "rlje");
        if (curMmsl == "" && curzq == "") {
            alert("请输入第一行！！")
            return 1
        }
        for (var i = 2; i <= curRow; i++) {

            var oldRow = dw_zsmx.GetItemString(dw_zsmx.RowCount(), "xh");
            var rows = dw_zsmx.InsertRow(oldRow + i)
            dw_zsmx.ScrollToRow(rows);
            var newRow = (parseInt(oldRow) + 1);
            dw_zsmx.SetItem(rows, "xh", (newRow).toString());
            dw_zsmx.SetItem(rows, "mmje", curMmsl * i);
            dw_zsmx.SetItem(rows, "rlje", curzq * i);
        }
    }
    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.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);
    }
    //#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) {
        //        if (index_new == "2") {
        //            var Cur_Row_tjmx = dw_tjmx.RowCount();
        //            if (Cur_Row_tjmx < 1) {
        //                dw_tjmx.ScrollToRow(dw_tjmx.InsertRow(0))
        //                dw_tjmx.SetItem(dw_tjmx.GetRow(), "xh", 1);
        //            }
        //        }
        cur_dw_indwx = index_new
    }
    this.DeleteRow = function () {
        if (curFocusDw == "dw_zsmx") {
            if (dw_zsmx.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_zsmx_click = function (x, y, row, dwcNme, sobj) {
        curFocusDw = "dw_zsmx";
    }
    this.Key_Down_zsmx = function (key) {
        if (dw_zsmx.GetColumnName() == "mmje" && key == "13") {
            if (dw_zsmx.GetItemNumber(dw_zsmx.GetRow(), "mmje").length < 1) {
                requestor.MessageBox("提示", "请给当前行添加买满金额", ICON.Information);
                return 1
            }
            if (dw_zsmx.GetItemNumber(dw_zsmx.GetRow(), "rlje").length < 1) {
                alert("请给当前行添加让利金额");
                return 1
            }
            var oldRow = dw_zsmx.GetItemString(dw_zsmx.RowCount(), "xh");
            var rows = dw_zsmx.InsertRow(0)
            dw_zsmx.ScrollToRow(rows);
            var newRow = (parseInt(oldRow) + 1);
            dw_zsmx.SetItem(rows, "xh", (newRow).toString());
            dw_zsmx.SetColumn("mmje");
            dw_zsmx.SetFocus();
            return 1;
        }
    }
    this.SaveAll = function () {
        if (dw_bm.AcceptText() != 1)
            return;
        if (dw_top.AcceptText() != 1)
            return;
        if (dw_cxsp.AcceptText() != 1)
            return;
        if (dw_zsmx.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";
        }
//        if (dw_zsmx.RowCount() < 1) {
//            msg += "请填写赠劵明细\r\n";
//        }
        for (var i = 1; i <= dw_zsmx.RowCount(); i++) {
            if (dw_zsmx.AcceptText() != 1)
                return;
            //alert([dw_fajb.GetItemNumber(i, "rlsl"), dw_fajb.GetItemNumber(i, "mmsl")])

            if (dw_zsmx.GetItemNumber(i, "mmje") == "") {
                requestor.MessageBox("提示", "请给当前行添加买满金额", ICON.Information);
                return 1
            }
            if (dw_zsmx.GetItemNumber(i, "rlje") == "") {
                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_zsmx.SetItem(1, "mmje", dw_zsmx.GetItemNumber(1, "mmje"));
        var dwChange1 = dw_top.GetChanges();
        var dwChange2 = dw_bm.GetChanges();
        var dwChange3 = dw_cxsp.GetChanges();
        var dwChange4 = dw_zsmx.GetChanges();
        webReq.Invoke("W_MXYRLYYCXSheZhi_Edit_Save", "dwObject1=" + dw_top.GetDataObject() + "&dwChange1=" + dwChange1 + "&dwObject2=" + dw_bm.GetDataObject() + "&dwChange2=" + dwChange2 + "&dwObject3=" + dw_cxsp.GetDataObject() + "&dwChange3=" + dwChange3 + "&dwObject4=" + dw_zsmx.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_zsmx.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_zsmx.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_cxsp,cb_import);
        requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.import.W_CXFSSPDR&curcxfamc=mmxyrlyy&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_cxsp.RowCount() > 0) {
                dw_cxsp.Export();
            }
        }

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

    this.EvalQuDpjOrLsj = function (data) {
        eval(data);
    }
}

W_Mmxyrlyy_Edit.prototype=new W_CXFASZ_Edit()