<?php
defined('IN_IA') or exit('Access Denied');
global $_GPC, $_W;
$settings = $this->module['config'];
$op = ! empty($_GPC['op']) ? $_GPC['op'] : 'list';

if ($op == 'list') {
    $sid = $member['id'];
    $condition = ' WHERE uniacid = :aid AND openid = :openid';
    $params[':aid'] = $_W['uniacid'];
    $params[':openid'] = $_W['openid'];
    $list = pdo_fetchall('SELECT * FROM ' . tablename('evzg_express_trends') . $condition . ' ORDER BY id DESC ', $params);
    include $this->template('trends');
}
if($op == 'pay'){

    $trendOrder = pdo_get("evzg_express_trends",array('id'=>$_GPC['id']));
    if(empty($trendOrder)){
        message("订阅不存在",referer(),'error');
    }
    $setting = pdo_get("evzg_express_setting",array('uniacid'=>$_W['uniacid']));
    if(!empty($setting['trendsprice'])){
        $fee = floatval($setting['trendsprice']);
        pdo_update("evzg_express_trends",array('price'=>$fee),array('id'=>$oid));
        if($fee <= 0) {
            message('支付错误, 金额小于0');
        }
        //构造支付请求中的参数
        $params = array(
            'tid' => "trend_".$trendOrder['id'],      //充值模块中的订单号，此号码用于业务模块中区分订单，交易的识别码
            'ordersn' => $trendOrder['ordersn'],  //收银台中显示的订单号
            'title' => '订阅快递动态',          //收银台中显示的标题
            'fee' => $fee,      //收银台中显示需要支付的金额,只能大于 0
            'user' => $_W['member']['uid'],     //付款用户, 付款的用户名(选填项)
        );
        //调用pay方法
        $this->pay($params);die();
   }else{
       pdo_update("evzg_express_trends",array('status'=>1),array('id'=>$_GPC['id']));
       message("支付成功",referer(),'success');
   }
}
if($op == 'del'){

    $trendOrder = pdo_get("evzg_express_trends",array('id'=>$_GPC['id']));
    if(empty($trendOrder)){
        message("订阅不存在",referer(),'error');
    }
    pdo_delete("evzg_express_trends",array('id'=>$_GPC['id']));
    message("删除订阅成功",referer(),'success');
}
if($op == 'close'){

    $trendOrder = pdo_get("evzg_express_trends",array('id'=>$_GPC['id']));
    if(empty($trendOrder)){
        message("订阅不存在",referer(),'error');
    }
    pdo_update("evzg_express_trends",array('status'=>'-1'),array('id'=>$_GPC['id']));
    message("取消订阅成功",referer(),'success');
}
if ($op == 'add') {
    if(checksubmit('submit')){
        $express = $_GPC['express'];
       if(empty($express)){
           message("快递单号不能为空",referer(),'error');
       }
       $data = array();
       $evgShop = getEvgShop($express);
       if($evgShop){
           $data['expresscode'] = $evgShop['shippercode'];
           $data['expressshop'] = $evgShop['shippername'];
       }
       $data['uniacid'] = $_W['uniacid'];
       $data['openid'] = $_W['openid'];
       $data['expresssn'] =$express;
       $data['createtime'] =time();
       $data['ordersn'] =order_no();
       $data['note'] = $_GPC['description'];
       $result = pdo_insert("evzg_express_trends",$data);
       $oid = pdo_insertid();
       if(empty($result)){
           message("添加订阅失败",referer(),'error');
       }else{
            $setting = pdo_get("evzg_express_setting",array('uniacid'=>$_W['uniacid']));
            if(!empty($setting['trendsprice'])){
                $fee = floatval($setting['trendsprice']);
                pdo_update("evzg_express_trends",array('price'=>$fee),array('id'=>$oid));
                if($fee <= 0) {
                    message('支付错误, 金额小于0');
                }
                //构造支付请求中的参数
                $params = array(
                    'tid' => "trend_".$oid,      //充值模块中的订单号，此号码用于业务模块中区分订单，交易的识别码
                    'ordersn' => $data['ordersn'],  //收银台中显示的订单号
                    'title' => '订阅快递动态',          //收银台中显示的标题
                    'fee' => $fee,      //收银台中显示需要支付的金额,只能大于 0
                    'user' => $_W['member']['uid'],     //付款用户, 付款的用户名(选填项)
                );
                //调用pay方法
                $this->pay($params);die();
            }else{
                pdo_update("evzg_express_trends",array('status'=>1),array('id'=>$oid));
                message("添加订阅成功",referer(),'success');
            }


       }
    }
    include $this->template('trendsadd');
}
