/*
*Created by xmy_zjl on 2015/9/30
*/

var co = require('co');
var fs = require('fs');
var hbs = require('handlebars');
var qS = require('querystring');
var iconv = require('iconv-lite');
var r = require('../../../lib/r.js');
var toolUtil = require('../../../lib/toolUtil.js');
var path = require('../../../conf/config.js').config().path;

function getPhoneInCookie(req){
      reg = /.*phone%22:%22(\d+).*/;
      var phoneInCookie = req.headers.cookie;
      phoneInCookie=phoneInCookie;

      if(!phoneInCookie){
        phoneInCookie =undefined;
      }else{
        phoneInCookie = phoneInCookie.replace(reg, "$1");
      }

      return phoneInCookie;
};

exports.handle = function(req,rh,maxAge,transEnc,queryString,callback) {
	co(function *() {
	try{
            var param = qS.parse(queryString);
            var jsonX = {};//定义结果json

            //存在微信标识
            if(param.browser === 'wx') {

                  jsonX.browser = 'wx';

            }

            jsonX.host = req.headers.host +path;//域名加模块名
            jsonX.path = path;//模块名

            //存在qr参数
            if(param.qr) {
                  var domain = 'http://jr.pcauto.com.cn';//定义后端接口域名

                  //三级订单查询接口
                  var x = yield r.coGet(domain +'/interface/outer/mobile/checkOrder.jsp?orderId=' +param.qr);

                  var jx = JSON.parse(iconv.decode(x.body, 'UTF-8'));
                  //获取cookie中的phone
                  var phone=getPhoneInCookie(req);

                  if(jx.isL3order && jx.phone === phone) {

                        //合法参数，将值送人订单ID中
                        jsonX.orderId = param.qr;

                  }else {
                        //存在非法qr参数,返回404页
                        var template = hbs.compile(fs.readFileSync('./static/404.html').toString());

                        rh.setHeader('Content-Type','text/html;charset=UTF-8');
                        var html = template({});//设置响应数据
                        rh.setHeader('Cache-Control','max-age='+(maxAge));
                        rh.setBody(html);

                        callback(null, rh);//回调显示                       
                  }
            }
            // 微信访问标识
            jsonX.fromWechat=req.headers['user-agent'].indexOf('MicroMessenger') >0 ? true:undefined;
            var template = hbs.compile(fs.readFileSync('./scripts/finance/order/success.html').toString());

            if(param.fmt === 'json') {//返回json
                  rh.setHeader('Content-Type','application/json;charset=UTF-8');
                  rh.setBody(jsonX);
            }else {//返回html
                  rh.setHeader('Content-Type','text/html;charset=UTF-8');
                  var html = template(jsonX);//设置响应数据
                  rh.setHeader('Cache-Control','max-age='+(maxAge));
                  rh.setBody(html);
            }

            callback(null, rh);//回调显示

	}catch(e) {
            toolUtil.get404Page(fs,hbs,req,rh,callback);
		console.error('order/success.js:'+ e);
		callback(e);
	}
	})();
}