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

    var self = this;
    var requestor = new PBWindow();
    var dw_2 = new DataWindow(requestor);
    var cbx_XsSydj = new CheckBox(requestor);
    var cb_Exit = new CommandButton(requestor);
    var cb_Ok = new CommandButton(requestor);
    var st_Ye = new StaticText(requestor);
    var dw_1 = new DataWindow(requestor);
    var dw_khye = new DataWindow(requestor);
    var dw_3 = new DataWindow(requestor);

    if (typeof (this.bind) == "function") var baseBind = this.bind;
    this.bind = function (win) {
        if (typeof (baseBind) == "function") baseBind(win);

        if (requestor == null) requestor = new PBWindow();
        requestor.Attach(win);
        if (dw_2 == null) dw_2 = new DataWindow(requestor);
        dw_2.Attach(win.Control("dw_2"));
        if (cbx_XsSydj == null) cbx_XsSydj = new CheckBox(requestor);
        cbx_XsSydj.Attach(win.Control("cbx_XsSydj"));
        if (cb_Exit == null) cb_Exit = new CommandButton(requestor);
        cb_Exit.Attach(win.Control("cb_Exit"));
        if (cb_Ok == null) cb_Ok = new CommandButton(requestor);
        cb_Ok.Attach(win.Control("cb_Ok"));
        if (st_Ye == null) st_Ye = new StaticText(requestor);
        st_Ye.Attach(win.Control("st_Ye"));
        if (dw_1 == null) dw_1 = new DataWindow(requestor);
        dw_1.Attach(win.Control("dw_1"));
        if (dw_khye == null) dw_khye = new DataWindow(requestor);
        dw_khye.Attach(win.Control("dw_khye"));
        if (dw_3 == null) dw_3 = new DataWindow(requestor);
        dw_3.Attach(win.Control("dw_3"));

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

        if (typeof (self.ClickedMx) == "function")
            win.AttachEvent("dw_2", "Clicked", self.ClickedMx);

        if (typeof (self.GetFocusDw) == "function")
            win.AttachEvent("dw_2", "GetFocus", self.GetFocusDw);

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

        if (typeof (self.XsSydj) == "function")
            win.AttachEvent("cbx_XsSydj", "Clicked", self.XsSydj);

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

        if (typeof (self.Ok) == "function")
            win.AttachEvent("cb_Ok", "Clicked", self.Ok);

        if (typeof (self.GetFocusDw) == "function")
            win.AttachEvent("dw_1", "GetFocus", self.GetFocusDw);

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

    }

    if (typeof (this.unbind) == "function") var baseUnBind = this.unbind;
    this.unbind = function () {
        if (typeof (this.Release) == "function") this.Release();
        if (typeof (baseUnBind) == "function") baseUnBind();

        dw_2.Detach(); dw_2 = null;
        cbx_XsSydj.Detach(); cbx_XsSydj = null;
        cb_Exit.Detach(); cb_Exit = null;
        cb_Ok.Detach(); cb_Ok = null;
        st_Ye.Detach(); st_Ye = null;
        dw_1.Detach(); dw_1 = null;
        dw_khye.Detach(); dw_khye = null;
        dw_3.Detach(); dw_3 = null;
        requestor.Detach(); requestor = null;

        setTimeout(CollectGarbage, 100);
    }

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

    var curParDw = "";
    var curSelfs = "";
    var curEditModel = "";
    var curPzh = "";
    var FunctionID = "";
    var CurUserId = "";
    var CurKhxje = 0;
    var CurXydj = "";
    var CurHxjz = "";
    var dwc = new DataWindowChild();

    var webReq = new WebRequestor();
    webReq.SetWebServiceUrl("/Service/ServiceCaiWu.ashx");

    this.PostOpen = function () {
        //curEditModel = requestor.GetParm("EditModel");CurHxjz
        CurUserId = requestor.GetParm("CurUserId");
        CurHxjz = requestor.GetParm("CurHxjz");
        if (CurHxjz == "1") {
            dw_2.Modify(" DataWindow.Readonly=yes");
            cbx_XsSydj.SetEnabled(false);
        }
        self.Edit();
        //        if (curEditModel == "EDIT") {
        //            self.Edit();
        //        } else {
        //            FunctionID = requestor.GetParm("FunctionID");
        //            self.Add();
        //        }
    }

    this.SetDataWindow = function (dw,selfs) {
        curParDw = dw;
        curSelfs = selfs;
    }

//    this.Add = function () {
//        var CurRow = dw_1.InsertRow(0);
//        webReq.Invoke("GetHXSJPzh", "FunctionID=" + FunctionID);
//        if (webReq.StatusCode() == 200) {
//            curPzh = webReq.ResponseText();
//            dw_1.SetItem(CurRow, "pzh", curPzh);
//            dw_1.SetItem(CurRow, "kdr", CurUserId);
//            dw_1.Modify(" kdrxm.text='" + CurUserId + "' ");
//            st_Ye.SetText("0");
//            cbx_XsSydj.SetVisible(false);
//            dw_1.SetItem(CurRow, "hxzdje", 0);
//            dw_1.SetItem(CurRow, "skje", 0);
//            dw_1.SetItem(CurRow, "sjye", 0);
//            dw_3.Reset();
//        } else {
//            requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
//            return;
//        }
//    }

    this.Edit = function () {
        //        dw_1.Modify(" bmbh.protect='1' khbh.protect='1' ");
        dw_2.SetFilter(" flag='1' ");
        dw_2.Filter();
        curPzh = requestor.GetParm("CurPzh");
        CurKhxje = parseFloat(requestor.GetParm("CurKhxje"));
        st_Ye.SetText(CurKhxje + "");
        if (CurKhxje <= 0) {
         dw_2.Modify("bchxje.Protect='1' zrje.Protect='1'");
        }
        //dw_3.RetrieveEx("cw_ysksj_hxmx.pzh='" + curPzh + "'");
    }

    this.GetFocusDw = function () {
        dw_1.AcceptText();
        dw_2.AcceptText();
       
    }

    //#region ItemChangedTop
    /***
    this.ItemChangedTop = function (row, dwoName, data) {
        if (dwoName == "bmbh") {
            dw_1.GetChild("bmbh", dwc);
            var curRow = dwc.Find(" bmbh='" + data + "' ", 1, dwc.RowCount());
            dw_1.SetItem(dw_1.GetRow(), "bmmc", dwc.GetItemString(curRow, "bmmc"));
            var khbh = dw_1.GetItemString(1, "khbh");
            if (khbh != "") {
                dw_khye.Retrieve(khbh, data);
                CurKhye = dw_khye.GetItemNumber(1, "kyye");
                CurXydj = dw_khye.GetItemString(1, "xydj");
                if (CurXydj == "0") {
                    if (UtcDateFormatToString(dw_khye.GetItemDate(row, "xyqx")) < UtcDateFormatToString(dw_khye.GetItemDate(row, "dqrq"))) {
                        dw_1.SetItem(row, "khbh", "");
                        dw_1.SetItem(row, "khmc", "");
                        dw_1.AcceptText();
                        return 1;
                    }
                } else if (CurXydj == "1") {
                    if (requestor.MessageBox("提示", "客户信用期限已超期，是否仍要核销？", ICON.Question, BUTTON.YesNo) == 2) {
                        dw_1.SetItem(row, "khbh", "");
                        dw_1.SetItem(row, "khmc", "");
                        dw_1.AcceptText();
                        return 1;
                    }
                }
                st_Ye.SetText(CurKhye + "");
                dw_2.RetrieveEx(" cw_yskzd.khbh='" + khbh + "' and  cw_yskzd.bmbh='" + data + "'");
            }
        } else if (dwoName == "khbh") {
            dw_1.GetChild("khbh", dwc);
            var curRow = dwc.Find(" khbh='" + data + "' ", 1, dwc.RowCount());
            dw_1.SetItem(dw_1.GetRow(), "khmc", dwc.GetItemString(curRow, "khmc"));
            var bmbh = dw_1.GetItemString(1, "bmbh");

            if (bmbh != "") {
                dw_khye.Retrieve(data, bmbh);        //更新客户信用、余额信息
                CurKhye = dw_khye.GetItemNumber(1, "kyye");
                CurXydj = dw_khye.GetItemString(1, "xydj");
                st_Ye.SetText(CurKhye + "");
                if (UtcDateFormatToString(dw_khye.GetItemDate(row, "xyqx")) < UtcDateFormatToString(dw_khye.GetItemDate(row, "dqrq"))) {
                    if (CurXydj == "0") {
                        dw_1.SetItem(row, dwoName, "");
                        dw_1.SetItem(row, "khmc", "");
                        dw_1.AcceptText();
                        return 1;
                    } else if (CurXydj == "1") {
                        if (requestor.MessageBox("提示", "客户信用期限已超期，是否仍要核销？", ICON.Question, BUTTON.YesNo) == 2) {
                            dw_1.SetItem(row, dwoName, "");
                            dw_1.SetItem(row, "khmc", "");
                            dw_1.AcceptText();
                            return 1;
                        }
                    }
                }
                st_Ye.SetText(CurKhye + "");
                dw_2.RetrieveEx(" cw_yskzd.khbh='" + data + "' and  cw_yskzd.bmbh='" + bmbh + "'");
            }
        }
    }
    ***/
    //#endregion

    this.ItemChangedMx = function (row, dwoName, data) {
        if (dwoName == "bchxje") {
            var NumData = parseFloat(data);
            //var NumData = dw_2.GetItemNumber(row, "zdje") - dw_2.GetItemNumber(row, "zrje") - dw_2.GetItemNumber(row, "yhxje");
            var curFzje = dw_2.GetItemNumber(row, "zdje") - dw_2.GetItemNumber(row, "zrje") - dw_2.GetItemNumber(row, "yhxje");

            if (CurKhxje <= 0) {
                requestor.MessageBox("提示", "可用余额为0，不能继续核销！", ICON.Information);
                dw_2.SetItem(row, "bchxje", 0);
           
                return 1;
            } else if (NumData >= CurKhxje && curFzje >= NumData) {

                requestor.MessageBox("提示", "客户余额不足，本次最多只能核销" + CurKhxje + "元！", ICON.Information);
                dw_2.SetItem(row, "sqje", dw_2.GetItemNumber(row, "sqje") - CurKhxje);
                dw_2.SetItem(row, "bchxje", CurKhxje);
                CurKhxje = 0;
                st_Ye.SetText("0");
                return 1;
            } else if (NumData > curFzje && curFzje > 0) {

                requestor.MessageBox("提示", "本单尚欠" + curFzje + "，本次最多只能核销" + curFzje + "元！", ICON.Information);
                dw_2.SetItem(row, "sqje", 0);
                dw_2.SetItem(row, "bchxje", curFzje);
              
                CurKhxje = 0;
                st_Ye.SetText("0");
                return 1;
          
            } else {

                CurKhxje = CurKhxje - NumData;
                st_Ye.SetText(CurKhxje + "");
                dw_2.SetItem(row, "sqje", dw_2.GetItemNumber(row, "sqje") - NumData);
                dw_2.SetItem(row, "bchxje", NumData);
            }
            if (dw_2.GetItemNumber(row, "bchxje") + dw_2.GetItemNumber(row, "zrje") != 0) {
                dw_2.SetItem(row, "flag", "1");
            } else {
                dw_2.SetItem(row, "flag", "0");
            }
        } else if (dwoName == "zrje") {
            //alert(dw_2.GetItemNumber(row, "sqje"));
            if (dw_2.GetItemNumber(row, "sqje") == 0) {
                requestor.MessageBox("提示", "本单已核销完成！", ICON.Information);
                return;
            }
            var NumData = parseFloat(data);
            var curFzje = dw_2.GetItemNumber(row, dwoName);
            dw_2.SetItem(row, "sqje", dw_2.GetItemNumber(row, "sqje") - NumData);
            if (dw_2.GetItemNumber(row, "bchxje") + dw_2.GetItemNumber(row, "zrje") != 0) {
                dw_2.SetItem(row, "flag", "1");
            } else {
                dw_2.SetItem(row, "flag", "0");
            }
        }
    }

    this.ClickedMx = function (x, y, row, dwoName) {
        if (dwoName == "flag") {
            var flag = dw_2.GetItemString(row, dwoName);
            var curFzje = dw_2.GetItemNumber(row, "bchxje");
            if (flag == "1") {
                return;
            } else {
                var NumData = dw_2.GetItemNumber(row, "zdje") - dw_2.GetItemNumber(row, "zrje") - dw_2.GetItemNumber(row, "yhxje");
            }
            if (CurKhxje <= 0) {
                requestor.MessageBox("提示", "可用余额为0，不能继续核销！", ICON.Information);
                return;
            } else if (CurKhxje - (NumData - curFzje) < 0) {

                requestor.MessageBox("提示", "客户余额不足，本次只能核销" + CurKhxje + "元！", ICON.Information);
                dw_2.SetItem(row, "sqje", dw_2.GetItemNumber(row, "sqje") - CurKhxje);
                dw_2.SetItem(row, "bchxje", CurKhxje);
                CurKhxje = 0;
                st_Ye.SetText("0");
               
            } else {
                dw_2.SetItem(row, "bchxje", NumData);
                CurKhxje = CurKhxje - (NumData - curFzje);
                st_Ye.SetText(CurKhxje + "");
                dw_2.SetItem(row, "sqje", dw_2.GetItemNumber(row, "sqje") - (NumData - curFzje));
            }
            if (flag == "0") {
                dw_2.SetItem(row, dwoName, "1");
            }
            dw_2.AcceptText();
            return 1;
        }
    }

    this.Ok = function () {

        dw_2.AcceptText();
        var curHxzdje = dw_2.GetItemNumber(1, "sumbchxje");


        var dw2Change = dw_2.GetChanges();
        if ((dw2Change) == "") {
            requestor.MessageBox("提示", "没有数据修改！", ICON.Information);
            return;
        }
        dw_3.ResetUpdate();
        var rowCount = dw_2.RowCount();
        var curHxje = 0;
        var curZrje = 0;
        var curRow = 0;
        var hxmxdh = "";
        var mxdh = dw_3.RowCount();
        for (var i = 1; i <= rowCount; i++) {
            curHxje = dw_2.GetItemNumber(i, "bchxje");
            curZrje = dw_2.GetItemNumber(i, "zrje");
            hxmxdh = dw_2.GetItemString(i, "mxdh");
            if (curHxje + curZrje != 0 && hxmxdh == "") {
                curRow = dw_3.InsertRow(0);
                dw_3.SetItem(curRow, "pzh", curPzh);
                mxdh += 1;
                dw_3.SetItem(curRow, "mxdh", mxdh + "");
                dw_3.SetItem(curRow, "zdbh", dw_2.GetItemString(i, "zdbh"));
                dw_3.SetItem(curRow, "hxzdje", curHxje);
                dw_3.SetItem(curRow, "zrje", curZrje);
                dw_3.SetItem(curRow, "hxr", CurUserId);
                //dw_3.SetItem(curRow, "hxrq", hxrq);
            }
        }

        dw_3.AcceptText();
        var dw3Object = dw_3.GetDataObject();
        var dw3Change = dw_3.GetChanges();
        if (dw_3.RowCount() < 1) {
            requestor.MessageBox("提示", "没有数据需要保存！", ICON.Information);
            return;
        }
        webReq.Invoke("HeXiaoSave1", "dw2Object=" + dw3Object + "&dw2Change=" + dw3Change + "&curPzh=" + curPzh);
        if (webReq.StatusCode() == 200) {
            requestor.MessageBox("提示", "保存成功！", ICON.Information);
            dw_1.ResetUpdate();
            dw_2.ResetUpdate();
            dw_3.ResetUpdate();
            curSelfs.Query();
            requestor.Close();
        } else {
            requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
        }
        return 1;
    }

    this.XsSydj = function (ch) {
        if (ch) {
            dw_2.SetFilter("");
        } else {
            dw_2.SetFilter(" flag='1' ");
        }
        dw_2.Filter();
    }

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