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

    var self = this;
    this.Instance = null;
    var requestor = new PBWindow();
    var cb_bm = new CommandButton(requestor);
    var sle_bm = new SingleLineEdit(requestor);
    var dw_hz = new DataWindow(requestor);
    var dw_pf = new DataWindow(requestor);
    var dw_cg = new DataWindow(requestor);
    var dw_list = new DataWindow(requestor);
    var tb_1 = new ToolStrip(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_bm == null) cb_bm = new CommandButton(requestor);
        cb_bm.Attach(win.Control("cb_bm"));
        if (sle_bm == null) sle_bm = new SingleLineEdit(requestor);
        sle_bm.Attach(win.Control("sle_bm"));
        if (dw_hz == null) dw_hz = new DataWindow(requestor);
        dw_hz.Attach(win.Control("dw_hz"));
        if (dw_pf == null) dw_pf = new DataWindow(requestor);
        dw_pf.Attach(win.Control("dw_pf"));
        if (dw_cg == null) dw_cg = new DataWindow(requestor);
        dw_cg.Attach(win.Control("dw_cg"));
        if (dw_list == null) dw_list = new DataWindow(requestor);
        dw_list.Attach(win.Control("dw_list"));
        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.changeBm) == "function")
            win.AttachEvent("cb_bm", "Clicked", self.changeBm);

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

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

        if (typeof (self.Hj) == "function")
            win.AttachEvent("btn_hj", "Clicked", self.Hj);

        if (typeof (self.Save) == "function")
            win.AttachEvent("btn_Save", "Clicked", self.Save);

        if (typeof (self.QueRen) == "function")
            win.AttachEvent("btn_cwhj", "Clicked", self.QueRen);

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

    }

    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_bm.Detach(); cb_bm = null;
        sle_bm.Detach(); sle_bm = null;
        dw_hz.Detach(); dw_hz = null;
        dw_pf.Detach(); dw_pf = null;
        dw_cg.Detach(); dw_cg = null;
        dw_list.Detach(); dw_list = null;
        tb_1.Detach(); tb_1 = null;
        requestor.Detach(); requestor = null;

        setTimeout(CollectGarbage, 100);
    }

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

    var curParentSelf = null;
    var curW_OPEN_BMDA = new OpenWindowQueryBase();
    var curW_OPEN_SIMPLE_CHOSE = new OpenWindowSimpleChoseBase();

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

    var curPzh = "";
    var curHjbz = "";

    var curJjfs = "";       //计价方式（客户）
    var curQjfs = ""; //取基础价方式（系统）

    this.SetDataWindow = function (self) {
        curParentSelf = self;
    }


    this.PostOpen = function () {
        if (requestor.GetParm("QJFS") == "ERROR") {
            alert("系统基础价取价方式获取错误！\n请检查参数档案!");
            return;
        } else {
            curQjfs = requestor.GetParm("QJFS");
        }
        dw_hz.Retrieve();
    }


    this.RowFocusChanged = function (row) {
        if (row < 1)
            return;

        curHjbz = dw_hz.GetItemString(row, "hjbz");

        if (curHjbz == "2") {
            dw_list.Modify("DataWindow.Readonly=yes");
        } else {
            dw_list.Modify("DataWindow.Readonly=no");
        }
        var curPzh = dw_hz.GetItemString(row, "pzh");
        dw_list.Retrieve(curPzh);

        if (curHjbz < "1") {
            self.ForJjAndXsj();
        }
    }


    this.Exit = function () {

        requestor.Close();
    }


    this.Query = function () {
        var curPzh = dw_hz.GetItemString(dw_hz.GetRow(), "pzh");
        dw_list.Retrieve(curPzh);
    }


    this.Save = function () {

        if (dw_list.RowCount() < 1) return;
        if (curHjbz == "2") {
            requestor.MessageBox("提示", "单据已最终确认,请勿重复确认!", ICON.Information);
            return
        }

        dw_list.AcceptText();
        var dwObject = dw_list.GetDataObject();
        var dwChanges = dw_list.GetChanges();
        webReq.Invoke("FenJianSave", "dwObject=" + dwObject + "&dwChanges=" + dwChanges);
        if (webReq.StatusCode() == 200) {

            var curPzh = dw_hz.GetItemString(dw_hz.GetRow(), "pzh");
            dw_list.Retrieve(curPzh);

            var code = self.UpdateMark("8", curPzh)

            if (code == 200) {
                requestor.MessageBox("提示", "保存完成!", ICON.Information);

            } else {
                requestor.MessageBox("提示", "保存失败,请联系管理员!", ICON.StopSign);
            }

        } else {
            requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
        }
    }


    this.QueRen = function () {

        if (dw_list.RowCount() < 1) return;

        if (curHjbz == "2") {
            requestor.MessageBox("提示", "当前分拣单已核价,请左边汇总换行重新选择单据!", ICON.Information);
            return
        }
        if (sle_bm.GetText().length < 1) {
            requestor.MessageBox("提示", "请选择部门再确认!", ICON.Information);
            return;
        }
        if (confirm("确认后将再无法进行修改，是否继续？")) {
            var curBmbh = sle_bm.GetText();
            var curRow = dw_hz.GetRow();
            var curPzh = dw_hz.GetItemString(curRow, "pzh");
            var code = self.UpdateMark("7", curPzh, curBmbh)
            if (code == 200) {
                requestor.MessageBox("提示", "确认完成,已转单!", ICON.Information);
                dw_hz.SetItem(curRow, "hjbz", "2");
                curHjbz = dw_hz.GetItemString(curRow, "hjbz");
                dw_list.Modify("DataWindow.Readonly=yes");
                curParentSelf.Tab_1_Clicked(1);
            } else {
                requestor.MessageBox("提示", "确认失败,请联系管理员!", ICON.StopSign);
            }
        }
    }


    this.SetSleValue = function (difBh, val) {
        if ("''" == val) {
            val = "";
        }
        if ("bmbh" == difBh) {
            if (val.substring(0, 1) == "'") {
                val = val.replace(/(\'*)/g, '');
            }
            sle_bm.SetText(val);
        }
    }

    this.UpdateMark = function (val, pzh,bmbh) {
        webReq.Invoke("Update_Mark", "curVar=" + val + "&curPZH=" + pzh + "&curBmbh=" + bmbh);
        return webReq.StatusCode();
    }

    this.changeBm = function () {
        curW_OPEN_BMDA.SetDataWindow("", "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_gys&treeRootLabel=部门分类&treeData=flbh&treeLabel=flmc&treeParentData=fflbh&treeLeave=islast&curQxlx=cg"), curW_OPEN_BMDA, null);
    }


    this.ForJjAndXsj = function () {
        var bmbh = "";
        var khbh = "";
        var spkh = "";
        bmbh = dw_list.GetItemString(1, "bmbh");
        khbh = dw_list.GetItemString(1, "khbh");

        webReq.Invoke("GetJJFS", "CusId=" + bmbh);
        if (webReq.StatusCode() == 200) {
            curJjfs = webReq.ResponseText();
        } else {
            alert(webReq.ResponseText());
            return;
        }
        for (var i = 1; i <= dw_list.RowCount(); i++) {

            spkh = dw_list.GetItemString(i, "spkh");
            dw_cg.Retrieve(spkh, bmbh, "cg", curJjfs + "#" + khbh, curQjfs); //FLDA 
            dw_list.SetItem(i, "jj", dw_cg.GetItemNumber(1, "jhjg"));

            dw_pf.Retrieve(spkh, khbh, "ls", "JGZZ#" + bmbh, curQjfs);
            dw_list.SetItem(i, "xsj", dw_pf.GetItemNumber(1, "pfjg1"));

        }
    }
}