<?php
defined('IN_IA') or exit('Access Denied');
global $_GPC, $_W;
mload()->classs('wxpay');
mload()->model("shop");
$op = ! empty($_GPC['op']) ? $_GPC['op'] : 'list';

if ($op == 'list') {
    $ids = $_GPC['id'];
    $id = intval($ids);
    $status = intval($_GPC['status']);
    if (!is_array($ids)) {
        $ids = array(
            $ids
        );
    }
    foreach ($ids as $id) {
        if($status==5){
            $result= pdo_delete('evzg_express_putforward', array(
                'uniacid' => $_W['uniacid'],
                'id' => $id
            ));
        }else{
            if ($id <= 0)
                continue;
            if($status==2){
                $status=0;
            }
            $result= pdo_update('evzg_express_putforward', array(
                'status' => $status
            ), array(
                'uniacid' => $_W['uniacid'],
                'id' => $id
            ));
        }
    }
    if($result){
        message('操作成功！', referer(), 'success');
    }
    $sid = $member['id'];
    $condition = ' WHERE t.uniacid = :aid AND t.sid = m.id';
    $params[':aid'] = $_W['uniacid'];
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('evzg_express_putforward') . " WHERE uniacid = '{$_W['uniacid']}' ORDER BY id  DESC");
    $list = pdo_fetchall('SELECT t.*,m.name as name FROM ' . tablename('evzg_express_putforward').'as t,'.tablename("evzg_express_shop").'as m' . $condition . ' ORDER BY id DESC ', $params);
    $pager = pagination($total, $pindex, $psize);
    include $this->template('putforward');
}
if($op == 'pay'){
    $id = $_GPC['id'];
    $shop_set = shop_get_set();
    $put_type = $shop_set['putpaytype'];
    $putOrder = pdo_get("evzg_express_putforward",array('id'=>$_GPC['id']));
    if(empty($putOrder)){
        message("提现不存在",referer(),'error');
    }
    if($put_type==0){
        $result= true;
    }elseif ($put_type==2||$put_type==1){
        $wx = new WxPay();
        $params = array();
        $params['openid'] = $putOrder['openid'];
        $params['amount'] = $putOrder['money']*100;
        $params['partner_trade_no'] = $putOrder['ordersn'];
        $params['desc'] ="提现打款";
        $result = $wx->mktTransfers($params,'NO_CHECK');
    }

    if($result['errno']=='-1'){
        pdo_update("evzg_express_putforward",array('status'=>2,'paytime'=>time(),'note'=>$result['message'],'type'=>2),array('id'=>$_GPC['id']));
        message("提现失败:{$result['message']}",referer(),'error');
    }
    if($result){
        pdo_update("evzg_express_putforward",array('status'=>1,'paytime'=>time(),'type'=>2),array('id'=>$_GPC['id']));
        message("提现成功",referer(),'success');
    }

}
if($op == 'del'){
    $putOrder = pdo_get("evzg_express_putforward",array('id'=>$_GPC['id']));
    if(empty($putOrder)){
        message("提现不存在",referer(),'error');
    }
    pdo_delete("evzg_express_putforward",array('id'=>$_GPC['id']));
    message("删除提现成功",referer(),'success');
}
if($op == 'close'){

    $putOrder = pdo_get("evzg_express_putforward",array('id'=>$_GPC['id']));
    if(empty($putOrder)){
        message("提现不存在",referer(),'error');
    }
    pdo_update("evzg_express_putforward",array('status'=>'-1'),array('id'=>$_GPC['id']));
    message("取消提现成功",referer(),'success');
}

