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

    var self = this;
    var requestor = new PBWindow();
    var mle_1 = new MultiLineEdit(requestor);
    var dw_rq = new DataWindow(requestor);
    var dw_gys = new DataWindow(requestor);
    var dw_3 = new DataWindow(requestor);
    var dw_2 = new DataWindow(requestor);
    var dw_1 = new DataWindow(requestor);
    var rb_rq = new RadioButton(requestor);
    var rb_gys = new RadioButton(requestor);
    var rb_bm = new RadioButton(requestor);
    var rb_sp = new RadioButton(requestor);
    var sle_cgfs = new SingleLineEdit(requestor);
    var sle_jjbm = new SingleLineEdit(requestor);
    var cbx_hbzxx = new CheckBox(requestor);
    var cbx_zdr = new CheckBox(requestor);
    var cbx_fs = new CheckBox(requestor);
    var cbx_gys = new CheckBox(requestor);
    var cbx_bm = new CheckBox(requestor);
    var cbx_pzh = new CheckBox(requestor);
    var cbx_rq = new CheckBox(requestor);
    var sle_gysbh = new SingleLineEdit(requestor);
    var sle_bmbh = new SingleLineEdit(requestor);
    var sle_ppbm = new SingleLineEdit(requestor);
    var sle_lbbh = new SingleLineEdit(requestor);
    var sle_pzh = new SingleLineEdit(requestor);
    var sle_spkh = new SingleLineEdit(requestor);
    var dp_2 = new DatePicker(requestor);
    var dp_1 = new DatePicker(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 (mle_1 == null) mle_1 = new MultiLineEdit(requestor);
        mle_1.Attach(win.Control("mle_1"));
        if (dw_rq == null) dw_rq = new DataWindow(requestor);
        dw_rq.Attach(win.Control("dw_rq"));
        if (dw_gys == null) dw_gys = new DataWindow(requestor);
        dw_gys.Attach(win.Control("dw_gys"));
        if (dw_3 == null) dw_3 = new DataWindow(requestor);
        dw_3.Attach(win.Control("dw_3"));
        if (dw_2 == null) dw_2 = new DataWindow(requestor);
        dw_2.Attach(win.Control("dw_2"));
        if (dw_1 == null) dw_1 = new DataWindow(requestor);
        dw_1.Attach(win.Control("dw_1"));
        if (rb_rq == null) rb_rq = new RadioButton(requestor);
        rb_rq.Attach(win.Control("rb_rq"));
        if (rb_gys == null) rb_gys = new RadioButton(requestor);
        rb_gys.Attach(win.Control("rb_gys"));
        if (rb_bm == null) rb_bm = new RadioButton(requestor);
        rb_bm.Attach(win.Control("rb_bm"));
        if (rb_sp == null) rb_sp = new RadioButton(requestor);
        rb_sp.Attach(win.Control("rb_sp"));
        if (sle_cgfs == null) sle_cgfs = new SingleLineEdit(requestor);
        sle_cgfs.Attach(win.Control("sle_cgfs"));
        if (sle_jjbm == null) sle_jjbm = new SingleLineEdit(requestor);
        sle_jjbm.Attach(win.Control("sle_jjbm"));
        if (cbx_hbzxx == null) cbx_hbzxx = new CheckBox(requestor);
        cbx_hbzxx.Attach(win.Control("cbx_hbzxx"));
        if (cbx_zdr == null) cbx_zdr = new CheckBox(requestor);
        cbx_zdr.Attach(win.Control("cbx_zdr"));
        if (cbx_fs == null) cbx_fs = new CheckBox(requestor);
        cbx_fs.Attach(win.Control("cbx_fs"));
        if (cbx_gys == null) cbx_gys = new CheckBox(requestor);
        cbx_gys.Attach(win.Control("cbx_gys"));
        if (cbx_bm == null) cbx_bm = new CheckBox(requestor);
        cbx_bm.Attach(win.Control("cbx_bm"));
        if (cbx_pzh == null) cbx_pzh = new CheckBox(requestor);
        cbx_pzh.Attach(win.Control("cbx_pzh"));
        if (cbx_rq == null) cbx_rq = new CheckBox(requestor);
        cbx_rq.Attach(win.Control("cbx_rq"));
        if (sle_gysbh == null) sle_gysbh = new SingleLineEdit(requestor);
        sle_gysbh.Attach(win.Control("sle_gysbh"));
        if (sle_bmbh == null) sle_bmbh = new SingleLineEdit(requestor);
        sle_bmbh.Attach(win.Control("sle_bmbh"));
        if (sle_ppbm == null) sle_ppbm = new SingleLineEdit(requestor);
        sle_ppbm.Attach(win.Control("sle_ppbm"));
        if (sle_lbbh == null) sle_lbbh = new SingleLineEdit(requestor);
        sle_lbbh.Attach(win.Control("sle_lbbh"));
        if (sle_pzh == null) sle_pzh = new SingleLineEdit(requestor);
        sle_pzh.Attach(win.Control("sle_pzh"));
        if (sle_spkh == null) sle_spkh = new SingleLineEdit(requestor);
        sle_spkh.Attach(win.Control("sle_spkh"));
        if (dp_2 == null) dp_2 = new DatePicker(requestor);
        dp_2.Attach(win.Control("dp_2"));
        if (dp_1 == null) dp_1 = new DatePicker(requestor);
        dp_1.Attach(win.Control("dp_1"));
        if (tb_1 == null) tb_1 = new ToolStrip(requestor);
        tb_1.Attach(win.Control("tb_1"));

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

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

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

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

        if (typeof (self.ChangeFangShi) == "function")
            win.AttachEvent("cb_6", "Clicked", self.ChangeFangShi);

        if (typeof (self.FilterChanges) == "function")
            win.AttachEvent("sle_cgfs", "EditChanged", self.FilterChanges);

        if (typeof (self.ChangeJiJie) == "function")
            win.AttachEvent("cb_5", "Clicked", self.ChangeJiJie);

        if (typeof (self.FilterChanges) == "function")
            win.AttachEvent("sle_jjbm", "EditChanged", self.FilterChanges);

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

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

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

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

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

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

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

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

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

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

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

        if (typeof (self.FilterChanges) == "function")
            win.AttachEvent("sle_gysbh", "EditChanged", self.FilterChanges);

        if (typeof (self.FilterChanges) == "function")
            win.AttachEvent("sle_bmbh", "EditChanged", self.FilterChanges);

        if (typeof (self.FilterChanges) == "function")
            win.AttachEvent("sle_ppbm", "EditChanged", self.FilterChanges);

        if (typeof (self.FilterChanges) == "function")
            win.AttachEvent("sle_lbbh", "EditChanged", self.FilterChanges);

        if (typeof (self.FilterChanges) == "function")
            win.AttachEvent("sle_pzh", "LoseFocus", self.FilterChanges);

        if (typeof (self.FilterChanges) == "function")
            win.AttachEvent("sle_spkh", "LoseFocus", self.FilterChanges);

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

    }

    if (typeof (this.unbind) == "function") var baseUnBind = this.unbind;
    this.unbind = function () {
        if (typeof (this.Release) == "function") this.Release();
        if (typeof (baseUnBind) == "function") baseUnBind();

        mle_1.Detach(); mle_1 = null;
        dw_rq.Detach(); dw_rq = null;
        dw_gys.Detach(); dw_gys = null;
        dw_3.Detach(); dw_3 = null;
        dw_2.Detach(); dw_2 = null;
        dw_1.Detach(); dw_1 = null;
        rb_rq.Detach(); rb_rq = null;
        rb_gys.Detach(); rb_gys = null;
        rb_bm.Detach(); rb_bm = null;
        rb_sp.Detach(); rb_sp = null;
        sle_cgfs.Detach(); sle_cgfs = null;
        sle_jjbm.Detach(); sle_jjbm = null;
        cbx_hbzxx.Detach(); cbx_hbzxx = null;
        cbx_zdr.Detach(); cbx_zdr = null;
        cbx_fs.Detach(); cbx_fs = null;
        cbx_gys.Detach(); cbx_gys = null;
        cbx_bm.Detach(); cbx_bm = null;
        cbx_pzh.Detach(); cbx_pzh = null;
        cbx_rq.Detach(); cbx_rq = null;
        sle_gysbh.Detach(); sle_gysbh = null;
        sle_bmbh.Detach(); sle_bmbh = null;
        sle_ppbm.Detach(); sle_ppbm = null;
        sle_lbbh.Detach(); sle_lbbh = null;
        sle_pzh.Detach(); sle_pzh = null;
        sle_spkh.Detach(); sle_spkh = null;
        dp_2.Detach(); dp_2 = null;
        dp_1.Detach(); dp_1 = null;
        tb_1.Detach(); tb_1 = null;
        requestor.Detach(); requestor = null;

        setTimeout(CollectGarbage, 100);
    }

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

    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 curOtherWidht = 1080;
    var curOtherHeight = 760;

    var curOpenWindowDoubleRowsChooseBase = new OpenWindowDoubleRowsChooseBase();

    webReq = new WebRequestor();
    webReq.SetWebServiceUrl("/Service/ServiceForDwSyndax.ashx");


    //#region Query
    this.Query = function () {

        if (rb_bm.GetChecked()) {

            self.Query_bm();
            return;

        } else if (rb_gys.GetChecked()) {

            self.Query_gys();
            return;

        } else if (rb_rq.GetChecked()) {

            self.Query_rq();
            return;
        }

        var dynamicShowStr = "";

        if (cbx_bm.GetChecked())
            dynamicShowStr += "bmbh,bmmc,";
        if (cbx_gys.GetChecked())
            dynamicShowStr += "gysbh,gysmc,";
        if (cbx_pzh.GetChecked())
            dynamicShowStr += "pzh,";
        if (cbx_rq.GetChecked())
            dynamicShowStr += "cgrq,";
        if (cbx_fs.GetChecked())
            dynamicShowStr += "cgfsbh,cgfsmc,";
        if (cbx_zdr.GetChecked())
            dynamicShowStr += "zdr,";
        if (cbx_hbzxx.GetChecked())
            dynamicShowStr += "hbzxx,";

        var doneWithDynamicShowStr = "";
        if (!(dynamicShowStr == ""))
            doneWithDynamicShowStr += "," + dynamicShowStr.substring(0, dynamicShowStr.length - 1);

        var finishSql = "select " + dynamicShowStr + " splbbh,splbmc," +
                        "SUM(ISNULL(sl,0)) as sl,max(ISNULL(lsj,0)) as lsj,max(ISNULL(jj,0)) as jj," +
                         "SUM(ISNULL(sl,0))*max(ISNULL(lsj,0)) as lsjze,SUM(ISNULL(sl,0))*max(ISNULL(jj,0)) as jjze  " +
                         "from ls_cg_cgdd_jqdlb_mx where (1 = 1)  " + self.GetSqlWhere(true) +
                         " group by splbbh,splbmc " + doneWithDynamicShowStr;

        self.DrawDataWindow(finishSql);
    }
    //#endregion

    //#region Query_bm
    this.Query_bm = function () {

        var execProcedure = "exec [dbo].[TEST_CROSSTAB] 'ls_cg_cgdd_jqdlb_mx','splbbh','splbmc','bmmc','sl','jj','lsj',1,0," + "'where " + self.GetSqlWhere(false).toString().substring(5) + "'";
        self.DrawDataWindow(execProcedure);

    }
    //#endregion

    //#region Query_gys
    this.Query_gys = function () {

        var execProcedure = "exec [dbo].[TEST_CROSSTAB] 'ls_cg_cgdd_jqdlb_mx','splbbh','splbmc','gysmc','sl','jj','lsj',1,0," + "'where " + self.GetSqlWhere(false).toString().substring(5) + "'";
        self.DrawDataWindow(execProcedure);

    }
    //#endregion

    //#region Query_rq
    this.Query_rq = function () {

        var execProcedure = "exec [dbo].[TEST_CROSSTAB] 'ls_cg_cgdd_jqdlb_mx','splbbh','splbmc','cgrq','sl','jj','lsj',1,0," + "'where " + self.GetSqlWhere(false).toString().substring(5) + "'";
        self.DrawDataWindow(execProcedure);

    }
    //#endregion

    //#region Where
    this.GetSqlWhere = function (flag) {
        var realSql = "";
        //#region 处理Where语句
        var beginDate = UtcDateFormatToString(dp_1.GetValue());
        var endDate = UtcDateFormatToString(dp_2.GetValue());
        var curPzh = sle_pzh.GetText();
        var curKh = sle_spkh.GetText();
        var curBm = sle_bmbh.GetText();
        var curPp = sle_ppbm.GetText();
        var curGys = sle_gysbh.GetText();
        var curLb = sle_lbbh.GetText();
        var curJj = sle_jjbm.GetText();
        var curFs = sle_cgfs.GetText();

        if (flag == true)
            realSql += " and (cgrq between '" + beginDate + "' and '" + endDate + "')";
        else
            realSql += " and (cgrq between ''" + beginDate + "'' and ''" + endDate + "'')";


        if (curPzh.length > 0) {

            if (flag == true)
                realSql += " and pzh ='" + curPzh + "'";
            else
                realSql += " and pzh =''" + curPzh + "''";

        }
        if (curKh.length > 0) {

            if (flag == true)
                realSql += " and spkh ='" + curKh + "'";
            else
                realSql += " and spkh =''" + curKh + "''";

        }
        if (curBm.length > 0) {

            curBm = self.DealWords(curBm);

            if (flag == true)
                realSql += " and bmbh  in(" + curBm + ")";
            else
                realSql += "and bmbh  in('" + curBm + "')";

        }
        if (curPp.length > 0) {

            curPp = self.DealWords(curPp);

            if (flag == true)
                realSql += " and spppbh in(" + curPp + ")";
            else
                realSql += " and spppbh in('" + curPp + "')";

        }
        if (curGys.length > 0) {

            curGys = self.DealWords(curGys);

            if (flag == true)
                realSql += " and gysbh in(" + curGys + ")";
            else
                realSql += " and gysbh in('" + curGys + "')";

        }
        if (curLb.length > 0) {

            curLb = self.DealWords(curLb);

            if (flag == true)
                realSql += " and splbbh in(" + curLb + ")";
            else
                realSql += " and splbbh in('" + curLb + "')";

        }
        if (curJj.length > 0) {

            curJi = self.DealWords(curJi);

            if (flag == true)
                realSql += " and spjjbh in(" + curJj + ")";
            else
                realSql += " and spjjbh in('" + curJj + "') ";

        }
        if (curFs.length > 0) {

            curFs = self.DealWords(curFs);

            if (flag == true)
                realSql += " and cgfsbh in(" + curFs + ")";
            else
                realSql += " and cgfsbh in('" + curFs + "')";
        }

        //#endregion
        return realSql;
    }
    //#endregion

    //#region checkBox关闭还是打开状态
    this.CbxEnable = function (bv) {
        cbx_pzh.SetEnabled(bv);
        cbx_bm.SetEnabled(bv);
        cbx_rq.SetEnabled(bv);
        cbx_gys.SetEnabled(bv);
        cbx_fs.SetEnabled(bv);
        cbx_zdr.SetEnabled(bv);
        cbx_hbzxx.SetEnabled(bv);
    }

    //#endregion
    this.rb_sp = function (b) {
        self.CbxEnable(b);
        self.Query();

    }
    this.rb_bm = function (b) {
        self.CbxEnable(!b);
        self.Query_bm();
    }
    this.rb_gys = function (b) {
        self.CbxEnable(!b);
        self.Query_gys();
    }
    this.rb_rq = function (b) {
        self.CbxEnable(!b);
        self.Query_rq();
    }

    //#region 弹出窗口
    this.ChangeBmbh = function (x, y, row, dwoName) {
        dwoName == "bmbh";
        curW_OPEN_BMDA_NEW.SetDataWindow(W_JingQueDaoLeiBie, "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"), curW_OPEN_BMDA_NEW, null);
    }
    this.ChangePinPai = function (x, y, row, dwoName) {
        dwoName == "ppbm";
        curW_OPEN_SIMPLE_CHOSE.SetDataWindow(W_JingQueDaoLeiBie, "ppbm", "ppmc", "M", self)  // 这个用来给子窗口Set当前数据窗口
        requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.DangAn.Base.OpenWindowSimpleChoseBase&listDw=dw_da_sp_ppda_list&selectStyle=multiple"), curW_OPEN_SIMPLE_CHOSE, null);
    }
    this.ChangeGongYingShang = function (x, y, row, dwoName) {
        dwoName == "gysbh";
        curW_OPEN_BMDA_NEW.SetDataWindow(W_JingQueDaoLeiBie, "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.ChangeLeiBie = function (x, y, row, dwoName) {
        dwoName == "lbbh";
        curW_OPEN_BMDA_NEW.SetDataWindow(W_JingQueDaoLeiBie, "lbbh", "lbmc", "M", self)  // 这个用来给子窗口Set当前数据窗口
        requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.DangAn.Base." + curW_OPEN_BMDA_URL + "&treeDw=dw_da_sp_lbda_tree&listDw=dw_da_sp_lbda_list&treeRootLabel=商品分类&treeData=lbbh&treeLabel=lbmc&treeParentData=flbbh&treeLeave=islast&selectStyle=multiple"), curW_OPEN_BMDA_NEW, null);
    }
    this.ChangeJiJie = function (x, y, row, dwoName) {
        dwoName == "jjbm";
        curW_OPEN_SIMPLE_CHOSE.SetDataWindow(W_JingQueDaoLeiBie, "jjbm", "jjmc", "M", self)  // 这个用来给子窗口Set当前数据窗口
        requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.DangAn.Base.OpenWindowSimpleChoseBase&listDw=dw_da_sp_jjda_list&selectStyle=multiple"), curW_OPEN_SIMPLE_CHOSE, null);
    }
    this.ChangeFangShi = function (x, y, row, dwoName) {
        dwoName == "csbh";
        curW_OPEN_SIMPLE_CHOSE.SetDataWindow(W_JingQueDaoLeiBie, "csbh", "csmc", "M", self)  // 这个用来给子窗口Set当前数据窗口
        requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.DangAn.Base.OpenWindowSimpleChoseBase&listDw=dw_da_qj_csb_cgfs_list&selectStyle=multiple"), curW_OPEN_SIMPLE_CHOSE, 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);
    }
    //#endregion


    //#region Function FilterChanges
    this.FilterChanges = function () {
        if (rb_sp.GetChecked())
            self.Query();
        else if (rb_bm.GetChecked())
            self.Query_bm();
        else if (rb_gys.GetChecked())
            self.Query_gys();
        else
            self.Query_rq();
    }
    //#endregion


    //#region Function DealWords
    this.DealWords = function (words) {

        var temChar = words.toString().charAt(0);
        if (!(temChar == "'"))
            words = "'" + words + "'";
        return words;
    }
    //#endregion


    //#region Function DrawDataWindow
    this.DrawDataWindow = function (parameter) {

        webReq.Invoke("dealDwSyndax", "sql=" + parameter);
        if (webReq.StatusCode() == 200) {

            dw_1.Create(webReq.ResponseText());
            dw_1.Refresh();

        } else {

            alert("error!");

        }
    }
    //#endregion 


}