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

    var self = this;
    this.Instance = null;
    var requestor = new PBWindow();
    var st_2 = new StaticText(requestor);
    var sle_tmh = new SingleLineEdit(requestor);
    var dw_ddmx_top_tmlr = new DataWindow(requestor);
    var dw_list = new DataWindow(requestor);
    var dw_pf = new DataWindow(requestor);
    var dw_cg = 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 (st_2 == null) st_2 = new StaticText(requestor);
        st_2.Attach(win.Control("st_2"));
        if (sle_tmh == null) sle_tmh = new SingleLineEdit(requestor);
        sle_tmh.Attach(win.Control("sle_tmh"));
        if (dw_ddmx_top_tmlr == null) dw_ddmx_top_tmlr = new DataWindow(requestor);
        dw_ddmx_top_tmlr.Attach(win.Control("dw_ddmx_top_tmlr"));
        if (dw_list == null) dw_list = new DataWindow(requestor);
        dw_list.Attach(win.Control("dw_list"));
        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 (tb_1 == null) tb_1 = new ToolStrip(requestor);
        tb_1.Attach(win.Control("tb_1"));

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

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

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

        if (typeof (self.sle_tmh_Modified) == "function")
            win.AttachEvent("sle_tmh", "KeyDown", self.sle_tmh_Modified);

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

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

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

        if (typeof (self.YiJianPiPei) == "function")
            win.AttachEvent("btn_yjpp", "Clicked", self.YiJianPiPei);

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

        st_2.Detach(); st_2 = null;
        sle_tmh.Detach(); sle_tmh = null;
        dw_ddmx_top_tmlr.Detach(); dw_ddmx_top_tmlr = null;
        dw_list.Detach(); dw_list = null;
        dw_pf.Detach(); dw_pf = null;
        dw_cg.Detach(); dw_cg = 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 curJjfs = "";       //计价方式（客户）
//    var curQjfs = ""; //取基础价方式（系统）
//    var curFjbz = "";    




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


    this.PostOpen = function () {

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

        dw_list.RetrieveEx("a.pzh = '" + curPzh + "'");
        sle_tmh.SetFocus();
        //        self.RowFocusChanged(dw_list.RowCount());
        if (curFjbz == "2") {
            dw_list.Modify("DataWindow.Readonly=yes");
        } else {
            dw_list.Modify("DataWindow.Readonly=no");
        }
    }


    this.Exit = function () {

        requestor.Close();
    }


    this.Query = function () {
        if (curFjbz == "2") {
            requestor.MessageBox("提示", "单据已最终确认,无法再重置数据!", ICON.Information);
            return
        }
        dw_list.RetrieveEx("a.pzh = '" + curPzh + "'");
    }


    this.Save = function () {
        if (curFjbz == "2") {
            requestor.MessageBox("提示", "单据已最终确认,请勿重复保存!", ICON.Information);
            return
        }
        if (dw_list.RowCount() < 1) return;
//        self.ForJjAndXsj();
        dw_list.AcceptText();
        var dwObject = dw_list.GetDataObject();
        var dwChanges = dw_list.GetChanges();
        webReq.Invoke("FenJianSave", "dwObject=" + dwObject + "&dwChanges=" + dwChanges);
        if (webReq.StatusCode() == 200) {

            dw_list.RetrieveEx("a.pzh = '" + curPzh + "'");

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

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

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

        } else {
            requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
        }
    }
    
    //#region this.sle_tmh_Modified  条码录入
    this.sle_tmh_Modified = function (key) {
        if (curFjbz == "2") {
            requestor.MessageBox("提示", "单据已最终确认,无法再录入条码!", ICON.Information);
            return
        }
        if (dw_list.RowCount() < 1) {
            requestor.MessageBox("提示", "单据无明细,请检查!", ICON.Information);
            return;
        }
        var curSl = 0;
        var curTzsl = 0;
        var curTmh = "";
        if (key == "13") {
            curTmh = sle_tmh.GetText();
            //alert(["tmbh='" + curTmh + "' and sl<1", 1, dw_list.RowCount()])
            var row = dw_list.Find("tmbh='" + curTmh + "'", 1, dw_list.RowCount());
            if (row > 0) {
                var findRow = dw_list.Find("tmbh='" + curTmh + "' and sl <> tzsl ", 1, dw_list.RowCount());
                if (findRow > 0) {
                    if (findRow == 1) {
                        dw_list.SelectRow(1, true);
                        curSl = dw_list.GetItemNumber(dw_list.GetRow(), "sl");
                        dw_list.SetItem(dw_list.GetRow(), "sl", curSl + 1);
                    }
                    else {
                        dw_list.ScrollToRow(findRow);
                        curSl = dw_list.GetItemNumber(dw_list.GetRow(), "sl");
                        dw_list.SetItem(dw_list.GetRow(), "sl", curSl + 1);
                    }

                } else {
                    var findRow = dw_list.Find("tmbh='" + curTmh + "'", 1, dw_list.RowCount());
                    if (findRow > 0) {
                        if (findRow == 1) {
                            dw_list.SelectRow(1, true);
                            curSl = dw_list.GetItemNumber(dw_list.GetRow(), "sl");
                            dw_list.SetItem(dw_list.GetRow(), "sl", curSl + 1);
                        }
                        else {
                            dw_list.ScrollToRow(findRow);
                            curSl = dw_list.GetItemNumber(dw_list.GetRow(), "sl");
                            dw_list.SetItem(dw_list.GetRow(), "sl", curSl + 1);
                        }

                    }
                }
                self.RowFocusChanged(findRow);
            } else {

                dw_ddmx_top_tmlr.Retrieve(curTmh);
                if (dw_ddmx_top_tmlr.RowCount() > 0) {
                    dw_list.ScrollToRow(dw_list.InsertRow(0));
                    var newRow = dw_list.RowCount();
                    dw_list.SetItem(newRow, "spkh", dw_ddmx_top_tmlr.GetItemString(1, "spkh"));
                    //dw_list.SetItem(newRow, "ksmc", dw_ddmx_top_tmlr.GetItemString(1, "ksmc"));
                    dw_list.SetItem(newRow, "yslsh", dw_ddmx_top_tmlr.GetItemString(1, "yslsh"));
                    dw_list.SetItem(newRow, "ysbh", dw_ddmx_top_tmlr.GetItemString(1, "ysbh"));
                    //dw_list.SetItem(newRow, "ysmc", dw_ddmx_top_tmlr.GetItemString(1, "ysmc"));
                    dw_list.SetItem(newRow, "bxbh", dw_ddmx_top_tmlr.GetItemString(1, "bxbh"));
                    dw_list.SetItem(newRow, "spbh", dw_ddmx_top_tmlr.GetItemString(1, "spbh"));
                    dw_list.SetItem(newRow, "tmbh", curTmh);
                    dw_list.SetItem(newRow, "spbh", dw_ddmx_top_tmlr.GetItemString(1, "spbh"));
                    dw_list.SetItem(newRow, "jj", 0);
                    dw_list.SetItem(newRow, "xsj", 0);
                    dw_list.SetItem(newRow, "pzh", dw_list.GetItemString(1, "pzh"));
                    dw_list.SetItem(newRow, "mxdh", dw_list.GetItemString(1, "pzh") + ("_000000" + newRow).substr(-7));
                    dw_list.SetItem(newRow, "spbh", dw_ddmx_top_tmlr.GetItemString(1, "spbh"));
                    dw_list.SetItem(newRow, "bmbh", dw_list.GetItemString(1, "bmbh"));
                    dw_list.SetItem(newRow, "khbh", dw_list.GetItemString(1, "khbh"));
                    dw_list.SetItem(newRow, "cmzbh", dw_ddmx_top_tmlr.GetItemString(1, "cmzbm"));
                    dw_list.SetItem(newRow, "cmbh", dw_ddmx_top_tmlr.GetItemString(1, "cmbh"));
                    dw_list.SetItem(newRow, "cmbt", dw_ddmx_top_tmlr.GetItemString(1, "cmbt"));
                    dw_list.SetItem(newRow, "kdrq", dw_list.GetItemDate(1, "kdrq"));
                    dw_list.SetItem(newRow, "kdr", dw_list.GetItemString(1, "kdr"));
                    dw_list.SetItem(newRow, "sl", 1);
                }
                else {
                    requestor.MessageBox("提示", "条码编号不存在请检查!", ICON.Information);
                    return;
                }
            }


            sle_tmh.SetText("");
            sle_tmh.SetFocus();
            return 1;
        }
    }
    //#endregion


    this.RowFocusChanged = function (row) {
        if (row < 1) return;
        curFjbz = dw_list.GetItemString(row, "fjbz");
        st_2.SetText(dw_list.GetItemNumber(1, "sum_sl"));
    }


    this.CaiWuHeJia = function () {

        webReq.Invoke("CG_FenJian_Initialize", "curPzhStr=" + curPzhStr);
        if (webReq.StatusCode() == 200) {

            if (confirm("确认后将再无法进行修改，是否继续？")) {
                var rPzh = webReq.ResponseText();
                curW_PH_FenJian_List.SetDataWindow(self, rPzh);
                requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.CaiGou.Base.W_PH_FenJian_List"), curW_PH_FenJian_List, null);
            }

        } else {
            requestor.MessageBox("提示", "数据保存失败!\r\n" + webReq.ResponseText(), ICON.StopSign);
        }
    }

    this.QueRen = function () {
        var curFjsl = dw_list.GetItemNumber(1, "sum_sl")
        if (curFjbz == "2") {
            requestor.MessageBox("提示", "单据已最终确认,请勿重复确认!", ICON.Information);
            return
        }

        if (curFjsl < 1) {
            requestor.MessageBox("提示", "单据尚未分拣,无法确认单据!", ICON.Information);
            return
        }
        if (confirm("确认后将再无法进行修改，是否继续？")) {


            var code = self.UpdateMark(4, curPzh)
            if (code == 200) {
                requestor.MessageBox("提示", "确认完成", ICON.Information);
                curParentSelf.Tab_1_Clicked(1);
                dw_list.RetrieveEx("a.pzh = '" + curPzh + "'");
                dw_list.Modify("DataWindow.Readonly=yes");
               
            } else {
                requestor.MessageBox("提示", "确认失败,请联系管理员!", ICON.StopSign);
            }
        }
    }

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

    this.YiJianPiPei = function () {
        if (curFjbz == "2") {
            requestor.MessageBox("提示", "单据已最终确认,请勿重复操作!", ICON.Information);
            return;
        }
        dw_list.ScrollFirstRow();
        var curCount = dw_list.RowCount();

        for (var i = 1; i <= curCount; i++) {
            dw_list.SetItem(i, "sl", dw_list.GetItemNumber(i, "tzsl"));
            self.RowFocusChanged(i);
        }
    }

    this.ItemChanged = function (row, downame, data) {

    }


//    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"));

//        }
//    }
}