<?php
defined('IN_IA') or exit('Access Denied');
global $_GPC, $_W;
load()->func('communication');
$op =$_GPC['op'];
$sid = $_GPC['sid'];
/**
 * 开启快查询
 * 
 * 
 * */
if($op=='openevzg'){
    $rootset = pdo_fetch("SELECT * FROM".tablename("evzg_express_rootuser")."WHERE uniacid='{$_W['uniacid']}'");
    if(empty($rootset)){
        $data['url'] =$_W['siteroot'];
        $data['uniacid'] = $_W['uniacid'];
        $sendapi = "http://www.qrkjcompany.com/api/adduser.html";
        $code = ihttp_post($sendapi,$data);
        $result = json_decode($code['content'],true);
        if($result['code']==200){
            $data['num'] =$result['data']['num'];
            $data['createtime'] = $result['data']['createtime'];
            $data['price'] = $result['data']['price'];
            $data['status'] = 1;
            $id = pdo_insert("evzg_express_rootuser",$data);
        }else {
            exit(json_encode(array('code'=>400,'msg'=>$code)));
        }
    }else{
        pdo_update("evzg_express_rootuser",array('status'=>1),array('uniacid'=>$_W['uniacid']));
    }
    exit(json_encode(array('code'=>200,'msg'=>'操作成功')));
}
if($op=='close'){
    $rootset = pdo_fetch("SELECT * FROM".tablename("evzg_express_rootuser")."WHERE uniacid='{$_W['uniacid']}'");
    $result = pdo_update("evzg_express_rootuser",array('status'=>0),array('uniacid'=>$_W['uniacid']));
    if($result){
        exit(json_encode(array('code'=>200,'msg'=>'操作成功')));
    }else{
        exit(json_encode(array('code'=>400,'msg'=>'操作失败')));
    }
}
if($op=='pay'){
    $rootset = pdo_fetch("SELECT * FROM".tablename("evzg_express_rootuser")."WHERE uniacid='{$_W['uniacid']}'");
    $data['url'] =$_W['siteroot'];
    $data['uniacid'] = $_W['uniacid'];
    $data['num'] = intval($_GPC['num']);
    $data['createtime'] = $rootset['createtime'];
    $sendapi = "http://www.qrkjcompany.com/evg/order.html";
    $code = ihttp_post($sendapi,$data);
    $result = json_decode($code['content'],true);
    if($result['code']==200){
        exit(json_encode(array('code'=>200,'msg'=>'请支付','redirectUrl'=>$result['redirectUrl']))); 
    }else{
        exit(json_encode(array('code'=>400,'msg'=>"创建订单失败")));;
    }
}
if ($op=='set') {
    $shopset = pdo_fetch("SELECT * FROM".tablename("evzg_express_shop_set")."WHERE uniacid='{$_W['uniacid']}' AND sid ='{$sid}'");
    if(!empty($shopset)){
        if(empty($shopset['isset'])){
            $isset =1;
        }else{
            $isset =0;
        }
        $result = pdo_update("evzg_express_shop_set",array('isset'=>$isset),array('uniacid'=>$_W['uniacid'],'sid'=>$sid));
    }else{
        $result = pdo_insert("evzg_express_shop_set",['uniacid'=>$_W['uniacid'],'sid'=>$sid,'isset'=>1]);
    }
    if($result){
        exit(json_encode(array('code'=>200,'msg'=>'操作成功')));
    }else{
        exit(json_encode(array('code'=>400,'msg'=>'操作失败')));
    }
}
if($op=='print'){
    $print= pdo_get("evzg_express_print",array('uniacid'=>$_W['uniacid'],'resultCode'=>'100','status'=>0));
    if (empty($print)) {
        exit(json_encode(array('code'=>4000,'msg'=>'暂时没有需要打印的快递单号')));
    }
    $tpl = str_replace('<!DOCTYPE html>',"",$print['printTemplate']);
    $tpl = str_replace('<html lang="zh-CN">',"",$tpl);
    $tpl = str_replace("</body></html>","",$tpl);
    $tpl = str_replace("<body>","",$tpl);
    $tpl = str_replace("<head>","<div>",$tpl);
    $tpl = str_replace("</head>","</div>",$tpl);
    exit(json_encode(array('code'=>200,'msg'=>$tpl,'id'=>$print['id'])));
    
}
//打印状态
if($op=='printstatus'){
    $print= pdo_get("evzg_express_print",array('uniacid'=>$_W['uniacid'],'id'=>$_GPC['id'],'status'=>0));
    if (empty($print)) {
        exit(json_encode(array('code'=>4005,'msg'=>'打印的快递单子不存在')));
    }
    $result = pdo_update('evzg_express_print',array('status'=>1),array('id'=>$_GPC['id']));
   if ($result) {
        exit(json_encode(array('code'=>200,'msg'=>'更新成功')));
    }else{
        exit(json_encode(array('code'=>4006,'msg'=>'更新失败')));
    }
}
