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

    var self = this;
    this.Instance = null;
    var requestor = new PBWindow();
    var cbx_shr = new CheckBox(requestor);
    var cbx_zdr = new CheckBox(requestor);
    var cbx_fkbz = new CheckBox(requestor);
    var rb_wjs = new RadioButton(requestor);
    var cbx_bzxx = new CheckBox(requestor);
    var rb_cgd = new RadioButton(requestor);
    var rb_jsd = new RadioButton(requestor);
    var rb_1 = new RadioButton(requestor);
    var sle_cgdh = new SingleLineEdit(requestor);
    var sle_jsdh = new SingleLineEdit(requestor);
    var cb_2 = new CommandButton(requestor);
    var cb_1 = new CommandButton(requestor);
    var cbx_fkje = new CheckBox(requestor);
    var cbx_fkfs = new CheckBox(requestor);
    var cbx_cgdh = new CheckBox(requestor);
    var cbx_cgrq = new CheckBox(requestor);
    var cbx_bm = new CheckBox(requestor);
    var cbx_jdsslje = new CheckBox(requestor);
    var cbx_gys = new CheckBox(requestor);
    var cbx_rq = new CheckBox(requestor);
    var cbx_pzh = new CheckBox(requestor);
    var sle_bmbh = new SingleLineEdit(requestor);
    var sle_gysbh = new SingleLineEdit(requestor);
    var dp_endDate = new DatePicker(requestor);
    var dp_startDate = new DatePicker(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 (cbx_shr == null) cbx_shr = new CheckBox(requestor);
        cbx_shr.Attach(win.Control("cbx_shr"));
        if (cbx_zdr == null) cbx_zdr = new CheckBox(requestor);
        cbx_zdr.Attach(win.Control("cbx_zdr"));
        if (cbx_fkbz == null) cbx_fkbz = new CheckBox(requestor);
        cbx_fkbz.Attach(win.Control("cbx_fkbz"));
        if (rb_wjs == null) rb_wjs = new RadioButton(requestor);
        rb_wjs.Attach(win.Control("rb_wjs"));
        if (cbx_bzxx == null) cbx_bzxx = new CheckBox(requestor);
        cbx_bzxx.Attach(win.Control("cbx_bzxx"));
        if (rb_cgd == null) rb_cgd = new RadioButton(requestor);
        rb_cgd.Attach(win.Control("rb_cgd"));
        if (rb_jsd == null) rb_jsd = new RadioButton(requestor);
        rb_jsd.Attach(win.Control("rb_jsd"));
        if (rb_1 == null) rb_1 = new RadioButton(requestor);
        rb_1.Attach(win.Control("rb_1"));
        if (sle_cgdh == null) sle_cgdh = new SingleLineEdit(requestor);
        sle_cgdh.Attach(win.Control("sle_cgdh"));
        if (sle_jsdh == null) sle_jsdh = new SingleLineEdit(requestor);
        sle_jsdh.Attach(win.Control("sle_jsdh"));
        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 (cbx_fkje == null) cbx_fkje = new CheckBox(requestor);
        cbx_fkje.Attach(win.Control("cbx_fkje"));
        if (cbx_fkfs == null) cbx_fkfs = new CheckBox(requestor);
        cbx_fkfs.Attach(win.Control("cbx_fkfs"));
        if (cbx_cgdh == null) cbx_cgdh = new CheckBox(requestor);
        cbx_cgdh.Attach(win.Control("cbx_cgdh"));
        if (cbx_cgrq == null) cbx_cgrq = new CheckBox(requestor);
        cbx_cgrq.Attach(win.Control("cbx_cgrq"));
        if (cbx_bm == null) cbx_bm = new CheckBox(requestor);
        cbx_bm.Attach(win.Control("cbx_bm"));
        if (cbx_jdsslje == null) cbx_jdsslje = new CheckBox(requestor);
        cbx_jdsslje.Attach(win.Control("cbx_jdsslje"));
        if (cbx_gys == null) cbx_gys = new CheckBox(requestor);
        cbx_gys.Attach(win.Control("cbx_gys"));
        if (cbx_rq == null) cbx_rq = new CheckBox(requestor);
        cbx_rq.Attach(win.Control("cbx_rq"));
        if (cbx_pzh == null) cbx_pzh = new CheckBox(requestor);
        cbx_pzh.Attach(win.Control("cbx_pzh"));
        if (sle_bmbh == null) sle_bmbh = new SingleLineEdit(requestor);
        sle_bmbh.Attach(win.Control("sle_bmbh"));
        if (sle_gysbh == null) sle_gysbh = new SingleLineEdit(requestor);
        sle_gysbh.Attach(win.Control("sle_gysbh"));
        if (dp_endDate == null) dp_endDate = new DatePicker(requestor);
        dp_endDate.Attach(win.Control("dp_endDate"));
        if (dp_startDate == null) dp_startDate = new DatePicker(requestor);
        dp_startDate.Attach(win.Control("dp_startDate"));
        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.ChangeBmbh) == "function")
            win.AttachEvent("cb_2", "Clicked", self.ChangeBmbh);

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

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

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

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

        if (typeof (self.Print) == "function")
            win.AttachEvent("btn_3", "Clicked", self.Print);

        if (typeof (self.Exit) == "function")
            win.AttachEvent("btn_4", "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();

        cbx_shr.Detach(); cbx_shr = null;
        cbx_zdr.Detach(); cbx_zdr = null;
        cbx_fkbz.Detach(); cbx_fkbz = null;
        rb_wjs.Detach(); rb_wjs = null;
        cbx_bzxx.Detach(); cbx_bzxx = null;
        rb_cgd.Detach(); rb_cgd = null;
        rb_jsd.Detach(); rb_jsd = null;
        rb_1.Detach(); rb_1 = null;
        sle_cgdh.Detach(); sle_cgdh = null;
        sle_jsdh.Detach(); sle_jsdh = null;
        cb_2.Detach(); cb_2 = null;
        cb_1.Detach(); cb_1 = null;
        cbx_fkje.Detach(); cbx_fkje = null;
        cbx_fkfs.Detach(); cbx_fkfs = null;
        cbx_cgdh.Detach(); cbx_cgdh = null;
        cbx_cgrq.Detach(); cbx_cgrq = null;
        cbx_bm.Detach(); cbx_bm = null;
        cbx_jdsslje.Detach(); cbx_jdsslje = null;
        cbx_gys.Detach(); cbx_gys = null;
        cbx_rq.Detach(); cbx_rq = null;
        cbx_pzh.Detach(); cbx_pzh = null;
        sle_bmbh.Detach(); sle_bmbh = null;
        sle_gysbh.Detach(); sle_gysbh = null;
        dp_endDate.Detach(); dp_endDate = null;
        dp_startDate.Detach(); dp_startDate = 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 curW_OPEN_BMDA_NEW = new OpenWindowQueryNewBase();
    var curW_OPEN_BMDA_URL = "OpenWindowQueryNewBase";
  

    this.PostOpen = function () {

        rb_jsd.SetChecked(true);

        cbx_fkbz.SetChecked(true);
        cbx_pzh.SetChecked(true);
        cbx_rq.SetChecked(true);
        cbx_gys.SetChecked(true);
        cbx_fkfs.SetChecked(true);
        cbx_fkje.SetChecked(true);
        cbx_bzxx.SetChecked(true);


     

    }
    //#region this.Query
    this.Query = function () {
        var curAllColumnsStr = "pzh,kdrq,gysbh,gysmc,csmc,fksl,fkze,bmbh,bmmc,cgdh,cgrq,ygfje,yfje,wfje,zjsl,zdr,shr,shsj,fkbz,bzxx";
        var curSelectColumnsStr = "pzh,kdrq,gysbh,gysmc,csmc,max(fksl)fksl,max(fkze)fkze,bmbh,bmmc,cgdh,cgrq,sum(zjsl)zjsl,sum(zjje)ygfje,sum(CASE WHEN fkbz='wfk' THEN 0 ELSE  zjje END) yfje, sum(CASE WHEN fkbz<>'wfk' THEN 0 ELSE  zjje END) wfje,zdr,shr,shsj,[fkbz],bzxx";
        var curGroupByColumnStr = "pzh,kdrq,gysbh,gysmc,csmc,bmbh,bmmc,cgdh,cgrq,zdr,shr,fkbz,bzxx";
        var curOrderByColumnStr = " order by gysbh,bmbh,cgrq desc";
        var curVisibleColunsStr = new Array();
        if (!cbx_fkbz.GetChecked()) {

            curSelectColumnsStr = curSelectColumnsStr.replace("[fkbz]", " '' fkbz");

            curGroupByColumnStr = curGroupByColumnStr.replace("fkbz,", "");
        } else {
            curVisibleColunsStr.push("fkbz");
        }
        if (!cbx_pzh.GetChecked()) {
            curSelectColumnsStr = curSelectColumnsStr.replace("pzh", " '' pzh");
            curGroupByColumnStr = curGroupByColumnStr.replace("pzh,", "");
        } else {
            curVisibleColunsStr.push("pzh");
        }
        if (!cbx_rq.GetChecked()) {
            curSelectColumnsStr = curSelectColumnsStr.replace("kdrq", " '2001-01-01' kdrq");
            curGroupByColumnStr = curGroupByColumnStr.replace("kdrq,", "");
        } else {
            curVisibleColunsStr.push("kdrq");
        }
        if (!cbx_gys.GetChecked()) {
            curSelectColumnsStr = curSelectColumnsStr.replace("gysbh", " '' gysbh");
            curSelectColumnsStr = curSelectColumnsStr.replace("gysmc", " '' gysmc");

            curGroupByColumnStr = curGroupByColumnStr.replace("gysbh,", "");
            curGroupByColumnStr = curGroupByColumnStr.replace("gysmc,", "");
        } else {
            curVisibleColunsStr.push("gysbh");
            curVisibleColunsStr.push("gysmc");
        }
      
        if (!cbx_jdsslje.GetChecked()) {

        } else {
            curVisibleColunsStr.push("fksl");
            curVisibleColunsStr.push("fkze");
        }
        if (!cbx_bm.GetChecked()) {
            curSelectColumnsStr = curSelectColumnsStr.replace("bmbh", " '' bmbh");
            curSelectColumnsStr = curSelectColumnsStr.replace("bmmc", " '' bmmc");

            curGroupByColumnStr = curGroupByColumnStr.replace("bmbh,", "");
            curGroupByColumnStr = curGroupByColumnStr.replace("bmmc,", "");
        } else {
            curVisibleColunsStr.push("bmbh");
            curVisibleColunsStr.push("bmmc");
        }
        if (!cbx_cgdh.GetChecked()) {
            curSelectColumnsStr = curSelectColumnsStr.replace("cgdh", " '' cgdh");
            curGroupByColumnStr = curGroupByColumnStr.replace("cgdh,", "");
        } else {
            curVisibleColunsStr.push("cgdh");
        }

        if (!cbx_cgrq.GetChecked()) {
            curSelectColumnsStr = curSelectColumnsStr.replace("cgrq", " '2021-01-01' cgrq");
            curGroupByColumnStr = curGroupByColumnStr.replace("cgrq,", "");
        } else {
            curVisibleColunsStr.push("cgrq");
        }
        if (!cbx_fkfs.GetChecked()) {
            curSelectColumnsStr = curSelectColumnsStr.replace("csmc", " '' csmc");

            curGroupByColumnStr = curGroupByColumnStr.replace("csmc,", "");
        } else {
            curVisibleColunsStr.push("csmc");

        }
        if (!cbx_fkje.GetChecked()) {

        } else {
            curVisibleColunsStr.push("zjsl");
            curVisibleColunsStr.push("ygfje");
            curVisibleColunsStr.push("yfje");
            curVisibleColunsStr.push("wfje");
        }
        if (!cbx_zdr.GetChecked()) {
            curSelectColumnsStr = curSelectColumnsStr.replace("zdr", " '' zdr");
            curGroupByColumnStr = curGroupByColumnStr.replace("zdr,", "");
        } else {
            curVisibleColunsStr.push("zdr");

        }
        if (!cbx_shr.GetChecked()) {
            curSelectColumnsStr = curSelectColumnsStr.replace("shr", " '' shr");
            curSelectColumnsStr = curSelectColumnsStr.replace("shsj", " max(shsj) shsj");

            curGroupByColumnStr = curGroupByColumnStr.replace("shr,", "");
            curGroupByColumnStr = curGroupByColumnStr.replace("shsj,", "");
        } else {
            curSelectColumnsStr = curSelectColumnsStr.replace("shsj", " max(shsj) shsj");
            curVisibleColunsStr.push("shr");
            curVisibleColunsStr.push("shsj");
        }

        

        if (!cbx_bzxx.GetChecked()) {

            curSelectColumnsStr = curSelectColumnsStr.replace("bzxx", " '' bzxx");

            curGroupByColumnStr = curGroupByColumnStr.replace("bzxx", "");
        } else {
            curVisibleColunsStr.push("bzxx");
        }

       

        if (curGroupByColumnStr.length > 0) {
            if (curGroupByColumnStr.indexOf("bzxx") > -1) {
                curGroupByColumnStr = " GROUP BY " + curGroupByColumnStr;
            } else {
                curGroupByColumnStr = " GROUP BY " + curGroupByColumnStr.substring(0, curGroupByColumnStr.length - 1);

            }
        }
        var curSql = " SELECT  " + curSelectColumnsStr +
	                   " FROM (" +
                       " SELECT   CASE WHEN cgd.fkbz='1' THEN cgd.pzh ELSE   jsd.pzh END pzh," +
				       "          CASE WHEN jsd.pzh <> ''  THEN CONVERT(DATE, jsd.kdrq, 120)  ELSE (CONVERT(DATE, cgd.kdrq, 120) ) end kdrq,  cgd.gysbh," +
				       "          CASE WHEN cgd.fkbz='1' THEN 'xj'    ELSE  jsd.fkfs  END fkfs," +
				       "          CASE WHEN cgd.fkbz='1' THEN '现金'  ELSE  jsd.csmc  END csmc, " +
				       "          CASE WHEN cgd.fkbz='1' THEN cgd.zjsl ELSE   jsd.fksl END fksl," +
				       "          CASE WHEN cgd.fkbz='1' THEN cgd.zjje ELSE   jsd.fkze END fkze, " +
				       "          cgd.bmbh,bmda.bmmc," +
				       "          cgd.pzh cgdh,gysmc,CONVERT(DATE,cgd.kdrq,120) cgrq,"+
                       "          CASE WHEN cgd.djlx='1' THEN zjsl ELSE -zjsl END zjsl," +
                       "          CASE WHEN cgd.djlx='1' THEN zjje ELSE -zjje END zjje," +
				       "          CASE WHEN cgd.fkbz='1' THEN cgd.kdr ELSE   jsd.zdr END zdr," +
				       "          CASE WHEN cgd.fkbz='1' THEN cgd.shr ELSE   jsd.shr END shr," +
				       "          CASE WHEN cgd.fkbz='1' THEN cgd.shsj ELSE jsd.shsj END shsj," +
				       "          CASE WHEN cgd.fkbz='1' THEN 'cgdfk' ELSE (  CASE WHEN jsd.pzh<>'' THEN 'jsdfk' ELSE 'wfk' END     ) END  fkbz," +
				       "          jsd.bzxx" +
		               " FROM     yw_cg_rkdhz cgd LEFT JOIN ( SELECT hz.pzh,kdrq,hz.fkfs,csb.csmc csmc,fksl,hz.fkze,zdr,shr,shsj,bzxx,mx.lydh cgdh " +
					   " 					                 FROM   cw_new_gysjsd_hz hz LEFT JOIN ( select csbh,csmc FROM dbo.da_qj_csb WHERE csfl='skfs') csb ON hz.fkfs=csb.csbh,dbo.cw_new_gysjsd_mx mx " +
					   " 					                 WHERE  hz.pzh=mx.pzh" +
					   " 					                 AND    hz.shbz='1'	" +
					   " 					                )jsd ON cgd.pzh=jsd.cgdh" +
					   " 		                  LEFT JOIN dbo.da_bmda bmda ON cgd.bmbh=bmda.bmbh" +
					   " 		                  LEFT JOIN dbo.da_gysda gysda ON cgd.gysbh=gysda.gysbh" +
		               "   WHERE ISNULL(cgd.jzbz,'0')='1'" +
                       "   )AAA ";

        var curGsybh = sle_gysbh.GetText();
        var curBmbh = sle_bmbh.GetText();
        var curStartDate = UtcDateFormatToString(dp_startDate.GetValue());
        var curEndDate = UtcDateFormatToString(dp_endDate.GetValue());
        var curJsdh = sle_jsdh.GetText();
        var curCgdh = sle_cgdh.GetText();
        // 2021-07-30 张刚要求改为采购日期，必须打勾【采购日期】列哦
        var curWhere = " WHERE    CONVERT(DATE,cgrq,120) BETWEEN '" + curStartDate + "' AND '" + curEndDate + "'";

       // var curWhere = " WHERE    CONVERT(DATE,kdrq,120) BETWEEN '" + curStartDate + "' AND '" + curEndDate + "'";
        if (curGsybh.length > 0) {
            curWhere += " and gysbh in(" + curGsybh + ")"
        }
        if (curBmbh.length > 0) {
            curWhere += " and bmbh in(" + curBmbh + ")"
        }
        if (curJsdh.length > 0) {
            curWhere += " and pzh ='" + curJsdh + "'"
        }
        if (curCgdh.length > 0) {
            curWhere += " and cgdh ='" + curCgdh + "'"
        }
        if (rb_jsd.GetChecked()) {
            curWhere += " and fkbz ='jsdfk'"
        }
        if (rb_cgd.GetChecked()) {
            curWhere += " and fkbz ='cgdfk'"
        } 
        if (rb_wjs.GetChecked()) {
            curWhere += " and fkbz ='wfk'"
        }

        var curSqlStr = curSql + curWhere + curGroupByColumnStr + curOrderByColumnStr
       // requestor.ClipBoardNao(curSqlStr);
        dw_list.Retrieve(curSqlStr);


        for (var r = 0; r < curAllColumnsStr.split(',').length; r++) {
            dw_list.Modify(curAllColumnsStr.split(',')[r] + ".visible='0'");
        }

        for (var i = 0; i < curVisibleColunsStr.length; i++) {
            dw_list.Modify(curVisibleColunsStr[i] + ".visible='1'");
        }
    }
    //#endregion

    this.Print = function () {
        dw_list.PrintPreview();
    }
    this.Export = function () {
        dw_list.Export();

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

    this.ChangeBmbh = function (x, y, row, dwoName) {
        curW_OPEN_BMDA_NEW.SetDataWindow(W_GysJsdTj, "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=cg"), curW_OPEN_BMDA_NEW, null);

    }

    this.ChangeGongYingShang = function (x, y, row, dwoName) {

        curW_OPEN_BMDA_NEW.SetDataWindow(W_GysJsdTj, "gysbh", "gysmc", "M", self)  // 这个用来给子窗口Set当前数据窗口
        requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.DangAn.Base." + curW_OPEN_BMDA_URL + "&treeDw=dw_da_gysda_fl_tree&listDw=dw_pub_list_gysda&treeRootLabel=供应商分类&treeData=flbh&treeLabel=flmc&treeParentData=fflbh&treeLeave=islast&selectStyle=multiple"), curW_OPEN_BMDA_NEW, null);
    }
    this.SetSleValue = function (difBh, val) {
        if ("''" == val)
            val = "";
        if ("bmbh" == difBh)
            sle_bmbh.SetText(val);        
        else if ("gysbh" == difBh)
            sle_gysbh.SetText(val);
        else if ("lbbh" == difBh)
            sle_lbbh.SetText(val);
        else if ("ppbm" == difBh)
            sle_ppbm.SetText(val);
        else if ("jjbm" == difBh)
            sle_jjbm.SetText(val);
        else if ("csbh" == difBh)
            sle_cgfs.SetText(val);
        else if ("spkh" == difBh)
            sle_kh.SetText(val);
        else if ("mlbh" == difBh)
            sle_ml.SetText(val);
    }


    //#region  双击pzh
    this.DoubleClicked = function (x, y, row, dwoname) {
        if (dwoname.indexOf("_t") < 0) {
            
            if (dwoname == "cgdh") {

                //获取pzh
                var curpzh = "";
                var ywflag = "";
                var curRow = dw_list.GetRow();
                if (curRow > 0) {
                    curpzh = dw_list.GetItemString(curRow, "cgdh");
                    ywflag = curpzh.substring(0, 2)
                    //反调到原单据    
                    if (ywflag == "CR") {
                        self.OpenSheet("SJ006", "采购入库单", curpzh, "");

                    }
                    if (ywflag == "CT") {
                        self.OpenSheet("SJ090", "采购退货单", curpzh, "");
                    }

                }
            }
        }
    }

    this.OpenSheet = function (itemData, itemLabel, hyh, gysbh) {
        var tempitemData = itemData.substring(2, 5);
        var   curSjcd = GetYHMKQX();
        var s = curSjcd.indexOf(tempitemData);
        if (s > -1) {
            var ts_1 = window.external.Control("ts_1");
            ts_1.CloseItem(itemData);
            ts_1.Open(itemData, itemLabel, '', encodeURI("/Service/OpenSheet.aspx?FunID=" + itemData + "&curDate=" + new Date().toLocaleTimeString() + "&Reload=Y&curFormPzh=" + hyh + "&curParam1=" + gysbh), true);
        }
        else {
            alert("您无此权限，请联系管理员！！")
        }

    }
    //#endregion
}