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

    var self = this;
    this.Instance = null;
    var requestor = new PBWindow();
    var cb_4 = new CommandButton(requestor);
    var cb_3 = new CommandButton(requestor);
    var cb_2 = new CommandButton(requestor);
    var cb_1 = new CommandButton(requestor);
    var dw_top = new DataWindow(requestor);
    var dw_mx = new DataWindow(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_4 == null) cb_4 = new CommandButton(requestor);
        cb_4.Attach(win.Control("cb_4"));
        if (cb_3 == null) cb_3 = new CommandButton(requestor);
        cb_3.Attach(win.Control("cb_3"));
        if (cb_2 == null) cb_2 = new CommandButton(requestor);
        cb_2.Attach(win.Control("cb_2"));
        if (cb_1 == null) cb_1 = new CommandButton(requestor);
        cb_1.Attach(win.Control("cb_1"));
        if (dw_top == null) dw_top = new DataWindow(requestor);
        dw_top.Attach(win.Control("dw_top"));
        if (dw_mx == null) dw_mx = new DataWindow(requestor);
        dw_mx.Attach(win.Control("dw_mx"));

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

        if (typeof (self.Fx) == "function")
            win.AttachEvent("cb_4", "Clicked", self.Fx);

        if (typeof (self.Qx) == "function")
            win.AttachEvent("cb_3", "Clicked", self.Qx);

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

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

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

        if (typeof (self.dw_top_doubleClicked) == "function")
            win.AttachEvent("dw_top", "Clicked", self.dw_top_doubleClicked);

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

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

    }

    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_4.Detach(); cb_4 = null;
        cb_3.Detach(); cb_3 = null;
        cb_2.Detach(); cb_2 = null;
        cb_1.Detach(); cb_1 = null;
        dw_top.Detach(); dw_top = null;
        dw_mx.Detach(); dw_mx = null;
        requestor.Detach(); requestor = null;

        setTimeout(CollectGarbage, 100);
    }

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

    var curW_OPEN_BMDA = new OpenWindowQueryBase();
    var curW_OPEN_BMDA_NEW = new OpenWindowQueryNewBase();
    var curW_OPEN_BMDA_URL = "OpenWindowQueryNewBase";
    var dwc = new DataWindowChild();
    var webReq = new WebRequestor();
    webReq.SetWebServiceUrl("/Service/ServiceCaiWu.ashx");
    var curParentSelf = "";
    var curDwList = "";
    var curEditModel = "";
    var isAdd = false;
    var curOp = "";
    var curPzh = "";
    var curBmbh = "";

    var curGsybh = ""
    var curStartDate = "";
    var curEndDate = "";


    this.PostOpen = function () {

        if (curOp == "Edit") {

            dw_top.Retrieve(curPzh);
            curGsybh = dw_top.GetItemString(dw_top.GetRow(), "gysbh");
            dw_mx.Retrieve(curPzh, "Edit", "2000-01-01", "3000-01-01", curGsybh, "");

            var curBmbh = dw_top.GetItemString(dw_top.GetRow(), "bmbh");
            var curZh = dw_top.GetItemString(dw_top.GetRow(), "zh");
            if (curZh.length > 0) {
                dw_top.GetChild("zh", dwc);
                if (curZh.substring(0, 2) == "XJ") {
                    dwc.Retrieve(curBmbh, "0");
                } else {
                    dwc.Retrieve(curBmbh, "1");
                }
            }

            //#region 外边反调
            if (curDwList == null) {
                cb_2.SetEnabled(false);
            }
            //#endregion

        } else {

            dw_top.ScrollToRow(dw_top.InsertRow(0));
        }
    }
    this.SetDataWindow = function (op, selfs, dw, pzh) {
        curPzh = pzh;
        curOp = op;
        curParentSelf = selfs;
        curDwList = dw;
    }

    this.Add = function () {
 
        dw_top.ScrollToRow(dw_top.InsertRow(0));
    }
    this.Edit = function () {

    }


    this.Query = function () {
          dw_top.AcceptText();
          curGsybh = dw_top.GetItemString(dw_top.GetRow(), "gysbh");
          curBmbh = dw_top.GetItemString(dw_top.GetRow(), "bmbh");
          curStartDate = UtcDateFormatToString(dw_top.GetItemDate(dw_top.GetRow(), "startdate"));
          curEndDate = UtcDateFormatToString(dw_top.GetItemDate(dw_top.GetRow(), "enddate"));

          if (curBmbh.length < 1) {
              alert("请选择部门。");
              return
          }
        if (curGsybh.length < 1) {
            alert("请选择供应商。");
            return
        }

        if (curOp == "Edit") {
            // alert(["edit", curPzh, "EditQuery"]);
            dw_mx.Retrieve(curPzh, "EditQuery", curStartDate, curEndDate, curGsybh,curBmbh);
        } else {
            // alert(["Add", "", "Add"]);
            dw_mx.Retrieve("", "Add", curStartDate, curEndDate, curGsybh, curBmbh);
        }
    }


    this.MxItemChanged = function (row, dwcName, data) {

        if (row < 1) {
            return;
        }
    }

    this.Qx = function () {

        for (var i = 1; i <= dw_mx.RowCount(); i++) {
            dw_mx.SetItem(i, "flag", "1");

        }
    }

    this.Fx = function () {

        for (var i = 1; i <= dw_mx.RowCount(); i++) {
            if (dw_mx.GetItemString(i, "flag") == "1") {
                dw_mx.SetItem(i, "flag", "0");

            } else {
                dw_mx.SetItem(i, "flag", "1");


            }
        }
    }

    this.Save = function () {

        dw_top.AcceptText();
        dw_mx.AcceptText();
        var curFkfs = dw_top.GetItemString(dw_top.GetRow(), "fkfs");
        if (curFkfs.length < 1) {
            requestor.MessageBox("提示", "付款方式不能为空，请检查。", ICON.StopSign);
            return;
        }

        var curHasChoose = "N";
        for (var i = 1; i <= dw_mx.RowCount(); i++) {
            if (dw_mx.GetItemString(i, "flag") == "1") {
                curHasChoose = "Y";
                break;
            }
        }
        if (curHasChoose == "N"){
            requestor.MessageBox("提示", "您还没有勾选采购单，请检查。", ICON.StopSign);
            return;
        }
     
        dw_mx.AcceptText();
        dw_top.SetItem(dw_top.GetRow(), "fksl", dw_mx.GetItemNumber(1, "sum_sfsl"));
        dw_top.SetItem(dw_top.GetRow(), "fkze", dw_mx.GetItemNumber(1, "sum_sfje"));

        dw_top.SetItem(dw_top.GetRow(), "ml", dw_mx.GetItemNumber(1, "sum_ml"));
        dw_top.AcceptText();


        var dwHzChanges = dw_top.GetChanges();
        var dwMxChanges = dw_mx.GetFullState();
        if (dwHzChanges == "" || dwMxChanges == "") {
            requestor.MessageBox("提示", "没有数据修改，请检查。", ICON.StopSign);
            return;
        }
        //#endregion
        webReq.Invoke("SaveGysJsd", "dwHzObject=da_cw_gysjsd_hz_top&dwMxObject=da_cw_gysjsd_mx&dwHzChanges=" + dwHzChanges + "&dwMxChanges=" + dwMxChanges);

        if (webReq.StatusCode() == 200) {
            var curMsg = webReq.ResponseText();
            if (curMsg.split('#')[0] == "Y") {
                curPzh = curMsg.split('#')[1];
                curOp = "Edit";
                requestor.MessageBox("提示", "数据保存成功!", ICON.Information);

                dw_top.AcceptText();
                curBmbh = dw_top.GetItemString(dw_top.GetRow(), "bmbh");
                curStartDate = UtcDateFormatToString(dw_top.GetItemDate(dw_top.GetRow(), "startdate"));
                curEndDate = UtcDateFormatToString(dw_top.GetItemDate(dw_top.GetRow(), "enddate"));

                dw_top.Retrieve(curPzh);


                dw_top.SetItem(dw_top.GetRow(), "startdate", curStartDate);
                dw_top.SetItem(dw_top.GetRow(), "enddate", curEndDate);

                dw_mx.Retrieve(curPzh, "Edit", curStartDate, curEndDate, curGsybh, curBmbh);
                // dw_mx.Retrieve(curPzh, "Edit", "2000-01-01", "3000-01-01", curGsybh, "");
                curDwList.Retrieve();
                var r = dw_mx.Find(" pzh='" + curPzh + "'", 1, dw_mx.RowCount());
                if (r > 0) {
                    dw_mx.ScrollToRow(r);
                }
            } else {
                requestor.MessageBox("提示", curMsg.split('#')[1], ICON.StopSign);
                return;
            }
        }
        else {
            requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
        }
    }
    this.Exit = function () {
        requestor.Close();
    }

    //#region this.dw_mx_top_doubleClicked  双击弹出窗口

    this.dw_top_doubleClicked = function (x, y, row, dwoName) {
        if (curOp == "Add") {
            if (dwoName == "btn_bm") { 
                curW_OPEN_BMDA_NEW.SetDataWindow(W_GysJsd_Edit, "bmbh", "bmmc", "MM", 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=kccx"), curW_OPEN_BMDA_NEW, null);
     
            }
            if (dwoName == "btn_gys") {
                curW_OPEN_BMDA.SetDataWindow(dw_top, "gysbh", "gysmc", "GYSJDS_GYS", 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&curQxlx=cg"), curW_OPEN_BMDA, null);
            }
        } 
    }


    //#endregion
    this.SetGysValue = function (code, value) {
 
        dw_top.SetItem(dw_top.GetRow(), "gysbh", code);

        dw_top.SetItem(dw_top.GetRow(), "gysmc", value);
    }

    this.SetSleValue = function (bmbhStr) {
        bmbhStr = bmbhStr.replace(/'/g, "");
        dw_top.SetItem(dw_top.GetRow(), "bmbh", bmbhStr);
    }


    //#region Top_itemChanged
    this.Top_itemChanged = function (row, dwoName, data) {
        if (dwoName == "fkfs") {
            var curBmbh = dw_top.GetItemString(dw_top.GetRow(), "bmbh");
            dw_top.SetItem(dw_top.GetRow(), "zh", "");
            dw_top.Modify("zh.background.color='536870912'");
            dw_top.Modify("zh.protect='0'");
            dw_top.GetChild("zh", dwc);
            skfsbz = GetXiTongCanShu(data, "skfs", "bzxx");
            //                if (data == "xj") {
            //                    dwc.Retrieve(bmbh, "0");
            //                }
            //                else {
            //                    dwc.Retrieve(bmbh, "1");
            //                }
            if (skfsbz == "0") {
                dw_top.Modify("zh.protect='1'");
                dw_top.Modify("zh.background.color='268435456'");
                return;
            }
            if (skfsbz == "1") {
                dwc.Retrieve(curBmbh, "0");
                var findRow = dwc.Find("  zhbh like 'XJ%' ", 1, dwc.RowCount());
                if (findRow <= 0) {
                    requestor.MessageBox("提示", "本部门现金账户提取失败或尚未开通！", ICON.StopSign);
                    dw_top.SetItem(dw_top.GetRow(), "fkfs", "");
                } else {
                    findRow = dwc.Find("  bmmc <>'' ", 1, dwc.RowCount());
                    if (findRow > 0) {
                        dw_top.SetItem(dw_top.GetRow(), "zh", dwc.GetItemString(findRow, "zhbh"))
                    }
                }
            } else if (skfsbz == "2") {
                dwc.Retrieve(curBmbh, "1");
                var findRow = dwc.Find("  zhbh like 'YH%' ", 1, dwc.RowCount());
                if (findRow <= 0) {
                    requestor.MessageBox("提示", "本部门银行账户提取失败或尚未开通！", ICON.StopSign);
                    dw_top.SetItem(dw_top.GetRow(), "fkfs", "");
                } else {
                    findRow = dwc.Find("  bmmc <>'' ", 1, dwc.RowCount());
                    if (findRow > 0) {
                        dw_top.SetItem(dw_top.GetRow(), "zh", dwc.GetItemString(findRow, "zhbh"))
                    }
                }
            }

        }
    }
    //#endregion
}