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

    var self = this;
    var requestor = new PBWindow();
    var dw_fhjb = new DataWindow(requestor);
    var dw_1 = new DataWindow(requestor);
    var cb_Xzkdr = new CommandButton(requestor);
    var sle_Hxje = new SingleLineEdit(requestor);
    var cb_XzKh = new CommandButton(requestor);
    var cb_Xzbm = new CommandButton(requestor);
    var dp_RqEnd = new DatePicker(requestor);
    var dp_RqBegin = new DatePicker(requestor);
    var sle_Khbh = new SingleLineEdit(requestor);
    var st_YsyfDx = new StaticText(requestor);
    var sle_Bmbh = new SingleLineEdit(requestor);
    var sle_Kdr = new SingleLineEdit(requestor);
    var sle_Pzh = new SingleLineEdit(requestor);
    var tb_1 = new ToolStrip(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_fhjb == null) dw_fhjb = new DataWindow(requestor);
        dw_fhjb.Attach(win.Control("dw_fhjb"));
        if (dw_1 == null) dw_1 = new DataWindow(requestor);
        dw_1.Attach(win.Control("dw_1"));
        if (cb_Xzkdr == null) cb_Xzkdr = new CommandButton(requestor);
        cb_Xzkdr.Attach(win.Control("cb_Xzkdr"));
        if (sle_Hxje == null) sle_Hxje = new SingleLineEdit(requestor);
        sle_Hxje.Attach(win.Control("sle_Hxje"));
        if (cb_XzKh == null) cb_XzKh = new CommandButton(requestor);
        cb_XzKh.Attach(win.Control("cb_XzKh"));
        if (cb_Xzbm == null) cb_Xzbm = new CommandButton(requestor);
        cb_Xzbm.Attach(win.Control("cb_Xzbm"));
        if (dp_RqEnd == null) dp_RqEnd = new DatePicker(requestor);
        dp_RqEnd.Attach(win.Control("dp_RqEnd"));
        if (dp_RqBegin == null) dp_RqBegin = new DatePicker(requestor);
        dp_RqBegin.Attach(win.Control("dp_RqBegin"));
        if (sle_Khbh == null) sle_Khbh = new SingleLineEdit(requestor);
        sle_Khbh.Attach(win.Control("sle_Khbh"));
        if (st_YsyfDx == null) st_YsyfDx = new StaticText(requestor);
        st_YsyfDx.Attach(win.Control("st_YsyfDx"));
        if (sle_Bmbh == null) sle_Bmbh = new SingleLineEdit(requestor);
        sle_Bmbh.Attach(win.Control("sle_Bmbh"));
        if (sle_Kdr == null) sle_Kdr = new SingleLineEdit(requestor);
        sle_Kdr.Attach(win.Control("sle_Kdr"));
        if (sle_Pzh == null) sle_Pzh = new SingleLineEdit(requestor);
        sle_Pzh.Attach(win.Control("sle_Pzh"));
        if (tb_1 == null) tb_1 = new ToolStrip(requestor);
        tb_1.Attach(win.Control("tb_1"));

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

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

        if (typeof (self.Xzkdr) == "function")
            win.AttachEvent("cb_Xzkdr", "Clicked", self.Xzkdr);

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

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

        if (typeof (self.Query) == "function")
            win.AttachEvent("btn_Query", "Clicked", self.Query);

        if (typeof (self.Reset) == "function")
            win.AttachEvent("btn_ReSet", "Clicked", self.Reset);

        if (typeof (self.Audit) == "function")
            win.AttachEvent("btn_Audit", "Clicked", self.Audit);

        if (typeof (self.UnAudit) == "function")
            win.AttachEvent("btn_UnAudit", "Clicked", self.UnAudit);

        if (typeof (self.Tally) == "function")
            win.AttachEvent("btn_Tally", "Clicked", self.Tally);

        if (typeof (self.print) == "function")
            win.AttachEvent("btn_Print", "Clicked", self.print);

        if (typeof (self.Export) == "function")
            win.AttachEvent("btn_Export", "Clicked", self.Export);

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

    }

    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_fhjb.Detach(); dw_fhjb = null;
        dw_1.Detach(); dw_1 = null;
        cb_Xzkdr.Detach(); cb_Xzkdr = null;
        sle_Hxje.Detach(); sle_Hxje = null;
        cb_XzKh.Detach(); cb_XzKh = null;
        cb_Xzbm.Detach(); cb_Xzbm = null;
        dp_RqEnd.Detach(); dp_RqEnd = null;
        dp_RqBegin.Detach(); dp_RqBegin = null;
        sle_Khbh.Detach(); sle_Khbh = null;
        st_YsyfDx.Detach(); st_YsyfDx = null;
        sle_Bmbh.Detach(); sle_Bmbh = null;
        sle_Kdr.Detach(); sle_Kdr = null;
        sle_Pzh.Detach(); sle_Pzh = null;
        tb_1.Detach(); tb_1 = null;
        requestor.Detach(); requestor = null;

        setTimeout(CollectGarbage, 100);
    }

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

    var CurHeXiao_Edit = new ZhangDanHeXiao_Edit();
    var CurOpenWindow = new OpenWindowQueryBase();

    var curW_OPEN_BMDA_NEW = new OpenWindowQueryNewBase();
    var curW_OPEN_BMDA_URL = "OpenWindowQueryNewBase";
    
    
    var FunctionID = "";
    var webReq = new WebRequestor();
    webReq.SetWebServiceUrl("/Service/ServiceCaiWu.ashx");

    //var curYsyfDx = 1;
    var bmFlag = 1;

//    this.Add = function () {
//        FunctionID = requestor.GetParm("FunctionID");
//        CurHeXiao_Edit.SetDataWindow(dw_1);
//        requestor.Open("/Service/LoadWindow.ashx?WinName=A8ERP.CaiWu.YingShouKuanZhangGuanLi.ZhangDanHeXiao_Edit&curParm1=ADD&curParm2=" + FunctionID, CurHeXiao_Edit, null);
//    }

//    this.Edit = function () {
//        var row = dw_1.GetRow();
//        if (row < 1) {
//            requestor.MessageBox("提示", "请选择要编辑的单据行！", ICON.Information);
//            return;
//        }
//        var CurPzh = dw_1.GetItemString(row, "pzh");
//        var CurDqzt = dw_1.GetItemString(row, "dqzt");
//        CurHeXiao_Edit.SetDataWindow(dw_1);
//        requestor.Open("/Service/LoadWindow.ashx?WinName=A8ERP.CaiWu.YingShouKuanZhangGuanLi.ZhangDanHeXiao_Edit&curParm1=EDIT&curParm2=" + CurPzh + "&CurDqzt=" + CurDqzt + "&CURGNQXSTR=" + requestor.GetParm("CURGNQXSTR"), CurHeXiao_Edit, null);
//    }

    this.DoubleClicked = function () {
        var row = dw_1.GetRow();
        if (row < 1) {
            requestor.MessageBox("提示", "请选择要编辑的单据行！", ICON.Information);
            return;
        }
        var CurPzh = dw_1.GetItemString(row, "pzh");
        //var CurDqzt = dw_1.GetItemString(row, "dqzt");
        var khxje = dw_1.GetItemNumber(row, "khxje");
        CurHeXiao_Edit.SetDataWindow(dw_1,self);
        //        requestor.Open("/Service/LoadWindow.ashx?WinName=A8ERP.CaiWu.YingShouKuanZhangGuanLi.ZhangDanHeXiao_Edit&curParm1=EDIT&curParm2=" + CurPzh + "&CurDqzt=" + CurDqzt + "&CURGNQXSTR=" + requestor.GetParm("CURGNQXSTR"), CurHeXiao_Edit, null);
        requestor.Open("/Service/LoadWindow.ashx?WinName=A8ERP.CaiWu.YingShouKuanZhangGuanLi.ZhangDanHeXiao_Edit&curParm2=" + CurPzh + "&CurKhxje=" + khxje + "&hxjz=" + dw_1.GetItemString(row, "hxjz"), CurHeXiao_Edit, null);
    }

    this.Xzbm = function () {
        bmFlag = 1;
        curW_OPEN_BMDA_NEW.SetDataWindow(dw_1, "bmbh", "bmmc", "M", self)  // 这个用来给子窗口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&selectStyle=multiple&curQxlx=cwcx"), curW_OPEN_BMDA_NEW, null);
    }

    this.Xzkh = function () {
        //if (curYsyfDx == 1) {
            curW_OPEN_BMDA_NEW.SetDataWindow(dw_1, "khbh", "khmc", "M", self)  // 这个用来给子窗口Set当前数据窗口
            requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.DangAn.Base." + curW_OPEN_BMDA_URL + "&treeDw=dw_da_khda_fl_tree&listDw=dw_pub_khda_list_all&treeRootLabel=客户分类&treeData=flbh&treeLabel=flmc&treeParentData=fflbh&treeLeave=islast&selectStyle=multiple"), curW_OPEN_BMDA_NEW, null);
//        } else if (curYsyfDx == 2) {
//            CurOpenWindow.SetDataWindow(dw_1, "gysbh", "gysmc", "M", self)  // 这个用来给子窗口Set当前数据窗口
//            requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.DangAn.Base.OpenWindowQueryBase&treeDw=dw_da_gysda_fl_tree&listDw=dw_pub_list_gysda&treeRootLabel=供应商分类&treeData=flbh&treeLabel=flmc&treeParentData=fflbh&treeLeave=islast&selectStyle=multiple"), CurOpenWindow, null);
//        } else if (curYsyfDx == 3) {
//            bmFlag = 0;
//            CurOpenWindow.SetDataWindow(dw_1, "bmbh", "bmmc", "M", self)  // 这个用来给子窗口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&selectStyle=multiple&curQxlx=cwcx"), CurOpenWindow, null);
//        }
    }

    this.SetSleValue = function (difBh, val) {
        if ("''" == val) {
            val = "";
        }
        if ("bmbh" == difBh) {
            if (bmFlag == 1) {
                sle_Bmbh.SetText(val);
            } else {
                sle_Khbh.SetText(val);
            }
        } else if ("khbh" == difBh) {
            sle_Khbh.SetText(val);
        }
    }

//    this.YsyfDxZh = function (index) {
//        curYsyfDx = index;
//        if (curYsyfDx == 1) {
//            st_YsyfDx.SetText("客户编号：");
//        } else if (curYsyfDx == 2) {
//            st_YsyfDx.SetText("供应商编号：");
//        } else if (curYsyfDx == 3) {
//            st_YsyfDx.SetText("部门编号：");
//        }
//        self.ReSet();
    //    }

    //#region Query
    this.Query = function () {
        var sqlWhere = self.GetSqlWhere();
        dw_1.RetrieveEx(sqlWhere);
    }

    this.GetSqlWhere = function () {
        var sqlWhere = " (1=1) ";
        var hxje = sle_Hxje.GetText();
        if (isNaN(hxje)) {
            requestor.MessageBox("提示", "核销金额输入错误！", ICON.Information);
            return;
        }
        if (hxje.length > 0) {
            sqlWhere += " and (hxzdje <= " + hxje + ") ";
        }
        var pzh = sle_Pzh.GetText();
        if (pzh.length > 0) {
            sqlWhere += " and a.pzh like '%" + pzh + "%' ";
        }
        var rqBegin = UtcDateFormatToString(dp_RqBegin.GetValue());
        var rqEnd = UtcDateFormatToString(dp_RqEnd.GetValue());
        sqlWhere += " and (convert(date,rq) between CONVERT(date, '" + rqBegin + "') and CONVERT(date, '" + rqEnd + "')) "
        var bmbh = sle_Bmbh.GetText();
        if (bmbh.length > 0) {
            sqlWhere += " and  (a.bmbh in (" + bmbh + ")) ";
        }
        var khbh = sle_Khbh.GetText();
        if (khbh.length > 0) {
            sqlWhere += " and (a.khbh in (" + khbh + ")) ";
        }
        var kdr = sle_Kdr.GetText();
        if (kdr.length > 0) {
            sqlWhere += " and a.zdr in (" + kdr + ") ";
        }
        return sqlWhere;
    }
    //#endregion

//    this.Delete = function () {
//        //判断审核标志-------------


//        if (requestor.MessageBox("提示", "确定删除所选记录行吗？", ICON.Question, BUTTON.YesNo) == 2) {
//            return;
//        }
//        var curRow = dw_1.GetRow();
//        if (curRow < 1) {
//            requestor.MessageBox("提示", "请选择要删除的记录行!", ICON.Information);
//            return;
//        }
//        var curPzh = dw_1.GetItemString(curRow, "pzh");
//        webReq.Invoke("YsyfDjDelete", "CurPzh=" + curPzh);
//        if (webReq.StatusCode() == 200) {
//            requestor.MessageBox("提示", "记录删除成功！", ICON.Information);
//            dw_1.DeleteRow(curRow);
//            curRow = 0;
//        } else {
//            requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
//        }
//    }

    this.Reset = function () {
        sle_Bmbh.SetText("");
        sle_Khbh.SetText("");
        sle_Pzh.SetText("");
        sle_Kdr.SetText("");
        sle_Hxje.SetText("");
    }

    this.Audit = function () {
        var curRow = dw_1.GetRow();
        if (curRow > 0) {
            var curPzh = dw_1.GetItemString(curRow, "pzh");
            if (requestor.MessageBox("提示", "请选择要审核单据【" + curPzh + "】吗？", ICON.Question, BUTTON.YesNo) == 2) {
                return;
            }
            var nextZt = self.GetNextZt(dw_1.GetItemString(curRow, "dqzt"));
            webReq.Invoke("SetDjShzt", "PZH=" + curPzh + "&DJZT=" + nextZt);
            if (webReq.StatusCode() == 200) {
                requestor.MessageBox("提示", "审核成功!", ICON.Information);
                dw_1.SetItem(curRow, "dqzt", nextZt);
                self.RowFocusChanged(curRow);
            } else {
                requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
            }
        } else {
            requestor.MessageBox("提示", "请选择要审核的记录行!", ICON.Information);
        }
    }

    this.UnAudit = function () {
        var curRow = dw_1.GetRow();
        if (curRow > 0) {
            var curPzh = dw_1.GetItemString(curRow, "pzh");
            if (requestor.MessageBox("提示", "请选择要退审单据【" + curPzh + "】吗？", ICON.Question, BUTTON.YesNo) == 2) {
                return;
            }
            webReq.Invoke("DjUnAudit", "PZH=" + curPzh);
            if (webReq.StatusCode() == 200) {
                requestor.MessageBox("提示", "退审成功!", ICON.Information);
                dw_1.SetItem(curRow, "dqzt", "F");
                self.RowFocusChanged(curRow);
            } else {
                requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
            }
        } else {
            requestor.MessageBox("提示", "请选择要退审的记录行!", ICON.Information);
        }
    }

    this.GetNextZt = function (curZt) {
        if ("F" == curZt) {
            var nextRow = dw_fhjb.Find(" fhjb='1' ", 1, dw_fhjb.RowCount());
            if (nextRow > 0) {
                return "1";
            } else {
                return "T";
            }
        } else {
            var nextZt = parseInt(curZt) + 1;
            var nextRow = dw_fhjb.Find(" fhjb='" + nextZt + "' ", 1, dw_fhjb.RowCount());
            if (nextRow > 0) {
                return nextZt + "";
            } else {
                return "T";
            }
        }
    }

    this.RowFocusChanged = function (row) {
        if (row > 0) {
            var khxje = dw_1.GetItemNumber(row, "khxje");

            if (khxje != 0) {
                tb_1.SetEnabled("btn_Audit", false);
                tb_1.SetEnabled("btn_UnAudit", false);
                tb_1.SetEnabled("btn_Tally", false);
            } else {
                var curShzt = dw_1.GetItemString(row, "dqzt");
                var curJzbz = dw_1.GetItemString(row, "hxjz");
                if (curShzt == "F") {
                    tb_1.SetEnabled("btn_Audit", true);
                    tb_1.SetEnabled("btn_UnAudit", false);
                    tb_1.SetEnabled("btn_Tally", false);
                } else if (curShzt == "T") {
                    tb_1.SetEnabled("btn_Audit", false);
                    if (curJzbz == "1") {
                        tb_1.SetEnabled("btn_UnAudit", false);
                        tb_1.SetEnabled("btn_Tally", false);
                    } else {
                        tb_1.SetEnabled("btn_UnAudit", true);
                        tb_1.SetEnabled("btn_Tally", true);
                    }
                } else {
                    tb_1.SetEnabled("btn_Audit", true);
                    tb_1.SetEnabled("btn_UnAudit", true);
                    tb_1.SetEnabled("btn_Tally", false);
                }
                var fhjbRow = dw_fhjb.Find(" fhjb='" + curShzt + "' ", 1, dw_fhjb.RowCount());
                tb_1.SetText("btn_Audit", dw_fhjb.GetItemString(fhjbRow, "dygn"));
            }
        }
    }

    this.Tally = function () {
        var row = dw_1.GetRow();
        var pzh = dw_1.GetItemString(row, "pzh");
        if (row < 1) {
            alert("请先选择一笔数据进行审核");
            return;
        }

        if (pzh.length < 1) {
            alert("凭证号异常");
            return;
        }
        if (!confirm("1您确定要记账凭证号为[" + pzh + "]的单据吗？")) {
            return;
        }

        webReq.Invoke("HeXiaoTally", "curPzh=" + pzh);
        if (webReq.StatusCode() == 200) {
            requestor.MessageBox("提示", webReq.ResponseText(), ICON.Information);
            //#region 刷新汇总表

            //刷新
            self.Query();
            //找到相应的记录
            var findRow = dw_1.Find("pzh='" + pzh + "' ", 1, dw_1.RowCount());
            if (findRow > 0) {
                dw_1.ScrollToRow(findRow);
            }
            //#endregion 
            self.RowFocusChanged(findRow);
        } else {
            requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
        }
    }

    this.Exit = function () {
        setTimeout("window.close()", 100);
    }
}