///<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" />
///<reference  path="/CaiWu/CaiWuTongJiFenXi/CW_TJFX_Base.win.js" /> 

//注意,需要在对象定义后面，手工增加以下代码实现对象继承
// W_GYSYFKMXTH_Main.prototype = new CW_TJFX_Base( ); 

function W_GYSYFKMXTH_Main() {

    var self = this;
    this.Instance = null;
    var requestor = new PBWindow();
    var sle_gys = new SingleLineEdit(requestor);
    var sle_bm = new SingleLineEdit(requestor);
    var dp_2 = new DatePicker(requestor);
    var dp_1 = new DatePicker(requestor);
    var mle_1 = new MultiLineEdit(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 (sle_gys == null) sle_gys = new SingleLineEdit(requestor);
        sle_gys.Attach(win.Control("sle_gys"));
        if (sle_bm == null) sle_bm = new SingleLineEdit(requestor);
        sle_bm.Attach(win.Control("sle_bm"));
        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 (mle_1 == null) mle_1 = new MultiLineEdit(requestor);
        mle_1.Attach(win.Control("mle_1"));
        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.Changegys) == "function")
            win.AttachEvent("cb_7", "Clicked", self.Changegys);

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

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

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

    }

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

        sle_gys.Detach(); sle_gys = null;
        sle_bm.Detach(); sle_bm = null;
        dp_2.Detach(); dp_2 = null;
        dp_1.Detach(); dp_1 = null;
        mle_1.Detach(); mle_1 = 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 OpenWindowQueryBase();

    var curW_OPEN_BMDA_NEW = new OpenWindowQueryNewBase();
    var curW_OPEN_BMDA_URL = "OpenWindowQueryNewBase";
    
    
    //#region PostOpen
    this.PostOpen = function () {
        sle_bm.SetEnabled(false);
        sle_gys.SetEnabled(false);
        var myDate = new Date();
        var nian = myDate.getFullYear().toString();
        var yue = myDate.getMonth();
        var ri = "01";
        var objDate = new Date(nian, yue, ri);
        dp_1.SetValue(objDate.getVarDate())
        dp_2.SetValue(UtcDateFormatToString(new Date().getVarDate()))
    }
    //#endregion

    //#region Function 呼出数据窗口的函数群 

    this.Changebmbh = function (x, y, row, dwoName) {
        dwoName == "bmbh";
        curW_OPEN_BMDA_NEW.SetDataWindow(W_GYSYFKMXTH_Main, "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=all"), curW_OPEN_BMDA_NEW, null);
    }

 
    this.Changegys = function (x, y, row, dwoName) {
        dwoName == "gysbh";
        curW_OPEN_BMDA_NEW.SetDataWindow(W_GYSYFKMXTH_Main, "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);
    }

   
    //#endregion

    //#region 选中后赋值

    this.SetSleValue = function (difBh, val) {
        if ("''" == val)
            val = "";

        if ("bmbh" == difBh)
            sle_bm.SetText(val);
        else if ("gysbh" == difBh)
            sle_gys.SetText(val);
      
    }

    //#endregion

    //#region 执行查询
    this.Query = function () {
        mle_1.SetText("1=1 "+self.GetSqlWhere(true))
        dw_list.RetrieveEx("1=1 " + self.GetSqlWhere(true))
    }
    //#endregion

    //#region 获取where 条件
    this.GetSqlWhere = function (flag) {
        var realSql = "";
        var curYhbh = requestor.GetParm("curYhbh")
        var beginDate = UtcDateFormatToString(dp_1.GetValue());
        var endDate = UtcDateFormatToString(dp_2.GetValue());
        var curBm = sle_bm.GetText();
        var curGys = sle_gys.GetText();
        if (flag == true)
            realSql += " and (CONVERT(date, kdrq) between '" + beginDate + "' and '" + endDate + "')";
        else
            realSql += " and (CONVERT(date,kdrq) between '" + beginDate + "' and '" + endDate + "')";
    
        if (curBm.length > 0) {
            curBm = self.DealWords(curBm);
            if (flag == true)
                realSql += " and bmbh  in(" + curBm + ")";
            else
                realSql += "and  bmbh  in('" + curBm + "')";
        } else {
            realSql += "and bmbh in(select bmbh  from da_qx_bmqxsz where yhbh = '" + curYhbh + "'and tjfx = '1')"
        }

      

        if (curGys.length > 0) {

            curGys = self.DealWords(curGys);
            if (flag == true)
                realSql += " and cw_yfksj_tz.gysbh in(" + curGys + ")";
            else
                realSql += " and cw_yfksj_tz.gysbh in('" + curGys + "')";
        } else {
            realSql += "and cw_yfksj_tz.gysbh in( select gysbh from da_qx_gysda where yhbh='" + curYhbh + "')"
        }
        realSql += " and  CAST(CONVERT(VARCHAR(10),DATEADD(DAY,  CAST([DBO].[F_GET_DJ_CXTS]('" + curYhbh + "','CWGYSYFLMXTJFX') AS INT) ,kdrq),120) AS DATETIME) >= GETDATE()";
      
        return realSql;
      

    }
    //#endregion

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

        if (!(words.charAt(0) == "'"))
            words = "'" + words + "'";

        return words;

    }
    //#endregion

    this.Print = function () {
        dw_list.PrintPreview();
    }
}
//CW_TJFX_Base
W_GYSYFKMXTH_Main.prototype = new CW_TJFX_Base();