/* jshint esnext: true */

var co    = require('co');
var fs    = require('fs');
var hbs   = require('handlebars');
var iconv = require('iconv-lite');
var r     = require('../../../lib/r.js');
var QS    = require('querystring');
exports.handle = function(req, rh, maxAge, transEnc, queryString, callback) {
    co(function *() {
        try {
			var param = QS.parse(queryString);
			var rId = param.rId;     //
			var level = param.level;                //级别
			var bid = param.bid;                    //品牌ID
			var prePayRange = param.prePayRange;    //首付范围
			var paymentRange = param.paymentRange;  //月供范围
			var sort = param.sort;                  //排序
			var pageSize = param.pageSize;          //每页大小
			var pageNo = param.pageNo;  
			//按预算选车列表
			var url = "http://jr.pcauto.com.cn/interface/outer/mobile/listModelByBudget.jsp?rId="+rId;
			if(level){
				url = url + '&level='+level;
			}
			if(bid){
               url = url + '&bid='+bid;
			}
			if(prePayRange){
               url = url + '&prePayRange='+prePayRange;
			}
			if(paymentRange){
               url = url + '&paymentRange='+paymentRange;
			}
			if(sort){
               url = url + '&sort='+sort;
			}
			if(pageSize){
               url = url + '&pageSize='+pageSize;
			}
			if(pageNo){
               url = url + '&pageNo='+pageNo;
			}
            var x = r.coGet(url);
			var RESULT = yield [x];
			var jsonX = JSON.parse(iconv.decode(RESULT[0].body, 'UTF-8'));
		    rh.setHeader('Content-Type', 'application/json; charset=UTF-8');
            rh.setBody(JSON.stringify(jsonX))
            callback(null, rh);
        } catch (e) {
            console.error(e);
            callback(e);
        }
    })();

};
