/**
 * Created by pc on 2015/9/18.
 * @author wxl
 * @date 2015-09-21
 * @des:选择车型页
 */
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');
var toolUtil=require("../../../lib/toolUtil.js");

var template = hbs.compile(fs.readFileSync('./scripts/finance/info/selectModels.html').toString());
exports.handle=function(req, rh, maxAge, transEnc, queryString, callback) {
    co (function *(){
        var param=QS.parse(queryString);
        var serialId=param.serialId;//车系ID
        if(!toolUtil.checkNumber(serialId)){
            toolUtil.get404Page(fs,hbs,req,rh,callback);
        }
        var x= r.coGet("http://jr.pcauto.com.cn/interface/outer/mobile/listModel.jsp?sgId="+serialId);
        var resultX=yield x;
        try {
            var jsonR = JSON.parse(iconv.decode(resultX.body, "utf-8"));
            if(toolUtil.isEmptyObject(jsonR)){
                toolUtil.get404Page(fs,hbs,req,rh,callback);
            }
            jsonR.param = param;
            var path  = require('../../../conf/config.js').config().path;
            jsonR.host=req.headers.host+path;
            jsonR.cookieHost=req.headers.host;
            jsonR.flow=param.flow;
            var html = template(jsonR);
            // 设置响应头信息
            rh.setHeader('Content-Type', 'text/html; charset=UTF-8');
            if (maxAge)rh.setHeader('Cache-Control', 'max-age=' + (maxAge));

            // 设置响应内容
            rh.setBody(html);

            // 回调通知
            callback(null, rh);
        }catch(e){
            console.log("--selectModels.js err---\n");
            console.log(e);
            toolUtil.get404Page(fs,hbs,req,rh,callback);
        }
    })();

}
