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

    var self = this;
    var requestor = new PBWindow();
    var tab_1 = new TabControl(requestor);
    var dw_1 = new DataWindow(requestor);
    var dw_2 = new DataWindow(requestor);
    var tv_1 = new TreeView(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 (tab_1 == null) tab_1 = new TabControl(requestor);
        tab_1.Attach(win.Control("tab_1"));
        if (dw_1 == null) dw_1 = new DataWindow(requestor);
        dw_1.Attach(win.Control("dw_1"));
        if (dw_2 == null) dw_2 = new DataWindow(requestor);
        dw_2.Attach(win.Control("dw_2"));
        if (tv_1 == null) tv_1 = new TreeView(requestor);
        tv_1.Attach(win.Control("tv_1"));
        if (tb_1 == null) tb_1 = new ToolStrip(requestor);
        tb_1.Attach(win.Control("tb_1"));

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

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

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

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

        if (typeof (self.SelectionChangedTree) == "function")
            win.AttachEvent("tv_1", "SelectionChanged", self.SelectionChangedTree);

        if (typeof (self.BtnPriorRow) == "function")
            win.AttachEvent("btn_PriorRow", "Clicked", self.BtnPriorRow);

        if (typeof (self.BtnNextRow) == "function")
            win.AttachEvent("btn_NextRow", "Clicked", self.BtnNextRow);

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

        tab_1.Detach(); tab_1 = null;
        dw_1.Detach(); dw_1 = null;
        dw_2.Detach(); dw_2 = null;
        tv_1.Detach(); tv_1 = null;
        tb_1.Detach(); tb_1 = null;
        requestor.Detach(); requestor = null;

        setTimeout(CollectGarbage, 100);
    }

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

    var CurYhbh = "";
    var tvItem = new TreeViewItem();

    this.PostOpen = function () {
        CurYhbh = requestor.GetParm("CurYhbh");
    }

    this.RowFocusChangedHz = function (row) {
        var rowCount = dw_1.RowCount();
        if (rowCount <= 1) {
            tb_1.SetEnabled("btn_PriorRow", false);
            tb_1.SetEnabled("btn_NextRow", false);
        } else if (row == 1) {
            tb_1.SetEnabled("btn_PriorRow", false);
            tb_1.SetEnabled("btn_NextRow", true);
        } else if (row == rowCount) {
            tb_1.SetEnabled("btn_PriorRow", true);
            tb_1.SetEnabled("btn_NextRow", false);
        } else {
            tb_1.SetEnabled("btn_PriorRow", true);
            tb_1.SetEnabled("btn_NextRow", true);
        }
        self.RefleshMx(row);
    }

    this.DoubleClickedHz = function (x, y, row, dwoName) {
        self.RefleshMx(row);
        tab_1.SelectTab(2);
    }

    this.DoubleClickedMx = function () {
        tab_1.SelectTab(1);
    }

    this.RefleshMx = function (row) {
        if (row < 1) {
            return;
        }
        var curBmbh = dw_1.GetItemString(row, "bmbh");
        var curKhbh = dw_1.GetItemString(row, "gysbh");
        var curDzrq = UtcDateTimeFormatToString(dw_1.GetItemDate(row, "dzrq"));
        dw_2.RetrieveEx(" bmbh='" + curBmbh + "' and gysbh = '" + curKhbh + "' and CONVERT(DATE, dzrq) = CONVERT(DATE, '" + curDzrq + "' ) ");
    }

    this.SelectionChangedTree = function (oldHandle, itemHandle) {
        tv_1.GetItem(itemHandle, tvItem);
        if (tvItem.GetLevel() == 3) {
            var curDzrq = tvItem.GetData();
            dw_1.RetrieveEx(" bmbh in (select bmbh from da_qx_bmqxsz where yhbh = '" + CurYhbh + "' and cwcx = '1') and CONVERT(DATE, dzrq) = CONVERT(DATE, '" + curDzrq + "' ) ");
        } else if (tvItem.GetLevel() == 4) {
            var curDzrq = tvItem.GetData().split("#")[1];
            var curBmbh = tvItem.GetData().split("#")[0];
            dw_1.RetrieveEx(" bmbh='" + curBmbh + "' and CONVERT(DATE, dzrq) = CONVERT(DATE, '" + curDzrq + "' ) ");
        }
        return 1;
    }

    this.BtnPriorRow = function () {
        var curRow = dw_1.GetRow();
        dw_1.ScrollToRow(curRow - 1);
        self.RowFocusChangedHz(curRow - 1);
    }

    this.BtnNextRow = function () {
        var curRow = dw_1.GetRow();
        dw_1.ScrollToRow(curRow + 1);
        self.RowFocusChangedHz(curRow + 1);
    }

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