<?php
defined('IN_IA') or exit('Access Denied');
global $_GPC, $_W;
$settings = $this->module['config'];
$sql = "SELECT * FROM" . tablename("evzg_express_shop") . "WHERE openid='{$_W['openid']}' AND 	uniacid='{$_W['uniacid']}'";
$shop = pdo_fetch($sql);
$op = ! empty($_GPC['op']) ? $_GPC['op'] : 'display';
$shop_set = shop_get_set();
if (empty($shop)) {
    message("你不是门店管理者,请先注册", $this->createMobileUrl('login', array(
        'op' => 'reg'
    )), "error");
}
if(empty($shop['enabled'])){
    message("亲的店铺已被封停，请联系管理员开启",$this->createMobileUrl('shop'), "error");
}

if ($op == 'add') {
    mload()->classs('wxpay');
    $money = floatval($_GPC['money']);
    $putprice = $shop_set['putprice']<1?1:$shop_set['putprice'];
    if($money<$putprice){
        message("提现金额不能小于{$putprice}",$this->createMobileUrl('putforward'),"error");
    }
    if($money>$shop['credit2']){
        message("提现金额不能大于现有余额",$this->createMobileUrl('putforward'),"error");
    }
    $data = array();
    $data['uniacid'] = $_W['uniacid'];
    $data['sid'] = $shop['id'];
    $data['title'] = "提现";
    $data['ordersn'] = order_no();
    $data['openid'] = $_W['openid'];
    $data['money'] = $money;
    $data['createtime'] = time();
    $result = pdo_insert("evzg_express_putforward",$data);
    $sms_order_id = pdo_insertid();
    if(!$result){
        message("提现出错",$this->createMobileUrl('putforward'),'error');
    }
    shop_update_credit2($shop['id'],-$money);
    $put_type = $shop_set['putpaytype'];
    if($put_type==2||$put_type==0){
        $result= true;
        $msg = "提现申请提交成功,等待审核";
        message($msg,referer(),'success');
    }elseif ($put_type==1){
        $wx = new WxPay();
        $params = array();
        $params['openid'] = $data['openid'];
        $params['amount'] = $data['money']*100;
        $params['partner_trade_no'] = $data['ordersn'];
        $params['desc'] ="提现打款";
        $result = $wx->mktTransfers($params,'NO_CHECK');
        $msg ="提现成功";
        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($msg,referer(),'success');
        }
    }
    message("提现出错",$this->createMobileUrl('putforward'),'error');
}
$default='default/shop/';
include $this->template($default.'putforward');