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

    var self = this;
    this.Instance = null;
    var requestor = new PBWindow();
    var cb_Emportsp = new CommandButton(requestor);
    var cb_import = new CommandButton(requestor);
    var cb_6 = new CommandButton(requestor);
    var cb_3 = new CommandButton(requestor);
    var dw_top = new DataWindow(requestor);
    var dw_kh = 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_Emportsp == null) cb_Emportsp = new CommandButton(requestor);
        cb_Emportsp.Attach(win.Control("cb_Emportsp"));
        if (cb_import == null) cb_import = new CommandButton(requestor);
        cb_import.Attach(win.Control("cb_import"));
        if (cb_6 == null) cb_6 = new CommandButton(requestor);
        cb_6.Attach(win.Control("cb_6"));
        if (cb_3 == null) cb_3 = new CommandButton(requestor);
        cb_3.Attach(win.Control("cb_3"));
        if (dw_top == null) dw_top = new DataWindow(requestor);
        dw_top.Attach(win.Control("dw_top"));
        if (dw_kh == null) dw_kh = new DataWindow(requestor);
        dw_kh.Attach(win.Control("dw_kh"));
        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.Emport_sp) == "function")
            win.AttachEvent("cb_Emportsp", "Clicked", self.Emport_sp);

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

        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.Js) == "function")
            win.AttachEvent("cb_6", "Clicked", self.Js);

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

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

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

        if (typeof (self.dw_kh_click) == "function")
            win.AttachEvent("dw_kh", "Clicked", self.dw_kh_click);

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

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

    }

    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_Emportsp.Detach(); cb_Emportsp = null;
        cb_import.Detach(); cb_import = null;
        cb_6.Detach(); cb_6 = null;
        cb_3.Detach(); cb_3 = null;
        dw_top.Detach(); dw_top = null;
        dw_kh.Detach(); dw_kh = null;
        dw_bm.Detach(); dw_bm = null;
        requestor.Detach(); requestor = null;

        setTimeout(CollectGarbage, 100);
    }

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

    var curW_OPEN_BMDA = new OpenWindowQueryBase();
    var curOpenWindowInsertRowCxfaSpkhBase = new OpenWindowInsertRowCxfaSpkhBase();
    var curW_CXFADR = new W_CXFSSPDR()
    var webReq = new WebRequestor();
    webReq.SetWebServiceUrl("/Service/ServiceCuXiaoFangAn.ashx");
    var curDw_list = "";
    var curDw_key = "";
    var curFocusDw = "dw_bm";
    this.SetDataWindow = function (dw__1, keyValue) {
        curDw_list = dw__1;
        curDw_key = keyValue;
    }
    //	 this.GoNextMonth = function () {
    //	     var today = new Date();
    //	     var curYear = today.getFullYear();
    //	     var curMonth = today.getMonth();
    //	     if (curMonth == 12) {
    //	         curYear = curYear + 1;
    //	         curMonth = 0;
    //	     }
    //	     else {
    //	         curMonth = curMonth + 1
    //	     }
    //	     var nextDate = new Date(curYear, curMonth, 1).getVarDate();
    //	     return nextDate;
    //	 }
    this.PostOpen = function () {
    

        if (curDw_key == "") {
            // 新增

            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.SetItem(dw_top.GetRow(), "qbsp", "0");
            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'");
        } else {
            //修改
            dw_top.RetrieveEx("pzh='" + curDw_key + "'");
            dw_kh.RetrieveEx("pzh='" + curDw_key + "'");
            dw_bm.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'");

            }
        }
    }

    //#region 选择部门
    this.Xzbm = function () {
        curW_OPEN_BMDA.SetDataWindow(dw_bm, "bmbh", "bmmc", "C")  // 这个用来给子窗口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&curQxlx=da&selectStyle=multiple"), curW_OPEN_BMDA, null);
    }
    //#endregion

    //#region 选择款号
    this.Xzkh = function () {
        //        if (dw_bm.RowCount() < 1) {
        //            alert("请先选择部门,再选款号！")
        //            return;
        //        }
        var curBmbh = "";
        var bmbhValue = "";
        var isYxfkc = "";
        var dataWindow = "";
        for (var i = 1; i <= dw_bm.RowCount(); i++) {
            curBmbh += "'" + dw_bm.GetItemString(i, "bmbh") + "'" + ","

        }
        if (dw_top.GetItemString(1, "qbsp") == "1") {
            isYxfkc = "YES";
            dataWindowSpkh = "dw_pub_sp_spda_open_list";

        } else {
            if (curBmbh.length != 0) {
                curBmbh = curBmbh.substring(0, curBmbh.length - 1);
                webReq.Invoke("Get_Sfyxfkc_By_Bmbh", "curBmbh=" + curBmbh)
                if (webReq.ResponseText() == "YES") {
                    isYxfkc = "YES";
                    dataWindowSpkh = "dw_pub_sp_spda_open_list";

                }
                else {
                    isYxfkc = "NO";
                    bmbhValue = webReq.ResponseText();
                    dataWindowSpkh = "dw_cxfa_spkh";
                }

            } else {

                isYxfkc = "YES";
                dataWindowSpkh = "dw_pub_sp_spda_open_list";
            } 
        }

        curOpenWindowInsertRowCxfaSpkhBase.SetDataWindow(dw_kh);   // 这个用来给子窗口Set当前数据窗口
        curOpenWindowInsertRowCxfaSpkhBase.SetDataWindowForBm(bmbhValue);
        requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.DangAn.Base.OpenWindowInsertRowCxfaSpkhBase&treeDw=dw_da_sp_lbda_tree&listDw=" + dataWindowSpkh + "&treeRootLabel=商品分类&treeData=lbbh&treeLabel=lbmc&treeParentData=flbbh&treeLeave=islast&selectStytle=multiple&spkhFlag=CxfaSpkh&isYxfkc=" + isYxfkc), curOpenWindowInsertRowCxfaSpkhBase, null);
        //var countRow = dw_kh.RowCount();
        //requestor.MessageBox("提示", "目前总共选择" + countRow + "款", ICON.Information);
    }
    //#endregion

    //#region 批量导入商品

    this.Import = function () {
        if (dw_bm.RowCount() < 1) {
            alert("请先选择部门,再导入商品！")
            return;
        }
        var curSpkh = "";
        var curBmbh = "";

//        for (var i = 1; i <= dw_bm.RowCount(); i++) {
//            curBmbh += "'" + dw_bm.GetItemString(i, "bmbh") + "'" + ","
//        }
//        curBmbh = curBmbh.substring(0, curBmbh.length - 1);
//        webReq.Invoke("Get_Ksysp_By_Bmbh", "curBmbh=" + curBmbh);
//        if (webReq.StatusCode == 200) {
//            curSpkh = webReq.ResponseText();
//        }
        curW_CXFADR.SetObj(dw_kh, cb_import);
        requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.import.W_CXFSSPDR&curcxfamc=ybcxfa&cur_dw_indwx=1"), curW_CXFADR, null);
        //requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.import.W_CXFADR&curcxfamc=ybcxsz&cur_dw_indwx=1&pzh=" + curDw_key), curW_CXFADR, null);

    }

    //#endregion
    this.Cancal = function () {
        requestor.Close();
    }
    this.dw_kh_click = function (x, y, row, dwcNme, sobj) {
        curFocusDw = "dw_kh";
    }
    this.dw_bm_click = function (x, y, row, dwcNme, sobj) {
        curFocusDw = "dw_bm";
    }
    this.DeleteRow = function () {
        if (curFocusDw.length > 0) {
            eval(curFocusDw + ".DeleteRow(" + curFocusDw + ".GetRow())");
        }
    }
    this.Js = function () {
        if (dw_top.GetItemString(1, "qbsp") == "1")
            return;
        dw_top.AcceptText();
        dw_top.AcceptText();
        dw_kh.AcceptText();
        dw_kh.AcceptText();
        var row = dw_top.GetRow();
        var bl = dw_top.GetItemNumber(row, "bjl");
        var zklb = dw_top.GetItemString(row, "zklb"); // 折扣类别
        var fdgz = dw_top.GetItemString(row, "fdgz"); //规则
        var msg = "";
        if (bl == 0) {
            msg += "变价量不能为0;\r\n";
        }
        if (dw_kh.RowCount() < 1) {
            msg += "请选择款号;\r\n";
        }
        if (msg.length > 0) {
            requestor.MessageBox("提示", msg.toString(), ICON.StopSign);
            return;
        }
        //按折扣%
        if (zklb == 1) {
            //划一
            if (fdgz == 1) {
                var a = bl / 100;
                for (var aa = 1; aa <= dw_kh.RowCount(); aa++) {
                    var dpj = dw_kh.GetItemNumber(aa, "dpj");
                    var curZkbfb = a;
                    var curCxj = dpj * a;
                    dw_kh.SetItem(aa, "zkbfb", curZkbfb);
                    dw_kh.SetItem(aa, "cxj", curCxj);
                }
            } else if (fdgz == 2) {
                //增加
                var b = bl / 100;
                for (var bb = 1; bb <= dw_kh.RowCount(); bb++) {
                    var dpj = dw_kh.GetItemNumber(bb, "dpj");
                    var yzkl = dw_kh.GetItemNumber(bb, "zkbfb");
                    var curZkbfb = b + yzkl;

                    var curCxj = dpj * curZkbfb;
                    dw_kh.SetItem(bb, "zkbfb", curZkbfb);
                    dw_kh.SetItem(bb, "cxj", curCxj);
                }

            } else if (fdgz == 3) {
                // 减少
                //增加
                var c = bl / 100;
                for (var cc = 1; cc <= dw_kh.RowCount(); cc++) {
                    var dpj = dw_kh.GetItemNumber(cc, "dpj");
                    var yzkl = dw_kh.GetItemNumber(cc, "zkbfb");
                    var curZkbfb = yzkl - c;

                    var curCxj = dpj * curZkbfb;
                    dw_kh.SetItem(cc, "zkbfb", curZkbfb);
                    dw_kh.SetItem(cc, "cxj", curCxj);
                }

            }
        } else if (zklb == 2) {

            //划一
            if (fdgz == 1) {
                for (var aa = 1; aa <= dw_kh.RowCount(); aa++) {
                    var dpj = dw_kh.GetItemNumber(aa, "dpj");
                    var curCxj = bl;
                    var curZkbfb = curCxj / dpj;
                    dw_kh.SetItem(aa, "zkbfb", curZkbfb);
                    dw_kh.SetItem(aa, "cxj", curCxj);
                }
            } else if (fdgz == 2) {
                //增加
                var b = bl / 100;
                for (var bb = 1; bb <= dw_kh.RowCount(); bb++) {
                    var dpj = dw_kh.GetItemNumber(bb, "dpj");
                    var curYCxj = dw_kh.GetItemNumber(bb, "cxj");
                    var addPart = curYCxj * b;
                    var curCxj = curYCxj + addPart;
                    var curZkbfb = curCxj / dpj;
                    dw_kh.SetItem(bb, "zkbfb", curZkbfb);
                    dw_kh.SetItem(bb, "cxj", curCxj);
                }

            } else if (fdgz == 3) {
                // 减少
                //增加
                var c = bl / 100;
                for (var cc = 1; cc <= dw_kh.RowCount(); cc++) {
                    var dpj = dw_kh.GetItemNumber(cc, "dpj");
                    var curYCxj = dw_kh.GetItemNumber(cc, "cxj");
                    var delPart = curYCxj * c;
                    var curCxj = curYCxj - delPart;
                    var curZkbfb = curCxj / dpj;
                    dw_kh.SetItem(cc, "zkbfb", curZkbfb);
                    dw_kh.SetItem(cc, "cxj", curCxj);
                }

            }
        }
    }
    this.SaveAll = function () {
        dw_top.AcceptText();
        dw_bm.AcceptText();
        dw_kh.AcceptText();
        var msg = "";
        if (dw_top.GetItemString(dw_top.GetRow(), "hdmc").length < 1) {
            msg += "活动名称不能为空\r\n";
        }
        if (dw_kh.RowCount() < 1 && dw_top.GetItemString(1, "qbsp") == "0") {
            msg += "请选择款号\r\n";
        }
        if (dw_bm.RowCount() < 1) {
            msg += "请选择部门\r\n";
        }
        if (msg.length > 0) {
            requestor.MessageBox("提示", msg.toString(), ICON.StopSign);
            return;
        }

        if (dw_top.GetItemNumber(1, "bjl") <= 0) {
            requestor.MessageBox("提示","变价量必须大于零", ICON.Information);
            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"));

        var isNull = "";
        if (dw_top.GetItemString(1, "qbsp") == "1")
            isNull = "YES";
        else {
            isNull = "NO";
            dw_kh.SetItem(1, "spkh", dw_kh.GetItemString(1, "spkh"));
        }

        var dwChange1 = dw_top.GetChanges();
        var dwChange2 = dw_kh.GetChanges();
        var dwChange3 = dw_bm.GetChanges();

        webReq.Invoke("W_YiBanCuXiaoSheZhi_Edit_Save", "dwObject1=" + dw_top.GetDataObject() + "&dwChange1=" + dwChange1 + "&dwObject2=" + dw_kh.GetDataObject() + "&dwChange2=" + dwChange2 + "&dwObject3=" + dw_bm.GetDataObject() + "&dwChange3=" + dwChange3 + "&isNull=" + isNull);
        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);
            }
            dw_top.SetItem(dw_top.GetRow(), "pzh", curPzh);
            dw_top.ResetUpdate();
            dw_kh.RetrieveEx("pzh='" + curPzh + "'");
            dw_bm.RetrieveEx("pzh='" + curPzh + "'");

            requestor.MessageBox("提示", "数据保存成功!", ICON.Information);
            return;
        }
        else {
            requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
        }
    }
    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.DwKhItemChanged = function (row, dwoName, data) {
     
        if (row < 1) {
            return;
        }
        if (dwoName == "dpj") {

            if (data == "") {
                dw_kh.SetItem(row, "dpj", "");
                dw_kh.SetItem(row, "zkbfb", "")
                dw_kh.SetItem(row, "cxj", "");
            } else {
                var dpj = data
                var cxbfb = dw_kh.GetItemNumber(row, "zkbfb");
                var curCxj = dpj * cxbfb;
                dw_kh.SetItem(row, "dpj", dpj);
                dw_kh.SetItem(row, "zkbfb", cxbfb);
                dw_kh.SetItem(row, "cxj", curCxj);
            }
        } else if (dwoName == "zkbfb") {
            var cxbfb = data
            var dpj = dw_kh.GetItemNumber(row, "dpj");
            var curCxj = dpj * cxbfb;
            dw_kh.SetItem(row, "dpj", dpj);
            dw_kh.SetItem(row, "zkbfb", cxbfb);
            dw_kh.SetItem(row, "cxj", curCxj);
        } else if (dwoName == "cxj") {
            var curCxj = data
            var dpj = dw_kh.GetItemNumber(row, "dpj");     
            var curCxbfb = curCxj / dpj;
            dw_kh.SetItem(row, "dpj", dpj);
            dw_kh.SetItem(row, "zkbfb", curCxbfb);
            dw_kh.SetItem(row, "cxj", curCxj);
        }
    }

    this.Emport_sp = function () {
        
       
        //获取当前的数据窗口

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

    this.top_clicked = function (x, y, row, downame, sender) {
        if (downame == "qbsp") {

            if (dw_top.GetItemString(1, "qbsp") == "0") {
                cb_3.SetEnabled(false);
            } 
           
            if (dw_top.GetItemString(1, "qbsp") == "1") {
                cb_3.SetEnabled(true);
            }
        }
    }
}