<?php
namespace app\allwin\widget;
use app\common\facade\WechatMp;

class Card{
    //新增会员卡
    public static function create(array $param){
        $wechat = WechatMp::isTypes($param['member_miniapp_id']);
        $attributes = [
            'prerogative'    => $param['prerogative'],
            'auto_activate'  => true,
            'supply_bonus'   => false,
            'supply_balance' => false,
            'custom_cell1' => [
                'name' => '买单',
                'tips' => '立即进入',
                'app_brand_user_name' =>  $param['gh_id'].'@app',
                'app_brand_pass' => '/pages/cardbao/index',
            ],
            'custom_cell2' => [
                'name' => '会员中心',
                'tips' => '立即进入',
                'app_brand_user_name'  =>  $param['gh_id'].'@app',
                'app_brand_pass'  => '/pages/cardbao/user',
            ],
            'base_info'      => [
                'logo_url'     => $param['picture'],
                'brand_name'   => $param['brand_name'],
                'title'        => $param['title'],
                'color'        => $param['color'],
                'notice'       => $param['notice'],
                'description'       => $param['description'],
                'sku'          => ['quantity' => $param['quantity']],
                'date_info'    => empty($param['end_time']) ? ['type' => 'DATE_TYPE_PERMANENT'] : ['type' => 'DATE_TYPE_FIX_TIME_RANGE', 'begin_timestamp' => $param['start_time'], 'end_timestamp' => $param['end_time']],
                'code_type'    => 'CODE_TYPE_TEXT',
                'custom_url_name' => '账单',
                'custom_url_sub_title' => '立即进入',
                'custom_app_brand_user_name' =>  $param['gh_id'].'@app',
                'custom_app_brand_pass' => '/pages/cardbao/index',
            ],
        ];
        $result = $wechat->card->create($cardType = 'MEMBER_CARD',$attributes);
        return $result;
    }
    //修改会员卡
    public static function update(array $param,$cardId,$state){
        $wechat = WechatMp::isTypes($param['member_miniapp_id']);
        $attributes = [
            'prerogative' => $param['prerogative'],
            'base_info'   => [
                'logo_url'    => $param['picture'],
                'title'       => $param['title'],
                'color'       => $param['color'],
                'notice'      => $param['notice'],
                'description' => $param['description'],
            ],
        ];
        if( $state == 1){
            $attributes['base_info']['date_info'] = ['type' => 'DATE_TYPE_FIX_TIME_RANGE', 'begin_timestamp' => $param['start_time'], 'end_timestamp' => $param['end_time']];
        }
        $rel = $wechat->card->update($cardId,$cardType = 'MEMBER_CARD',$attributes);
        return $rel;
    }

    //删除会员卡
    public static function delete($cardId,$member_miniapp_id){
        $wechat = WechatMp::isTypes($member_miniapp_id);
        return $wechat->card->delete($cardId);
    }

    //详情
    public static function info($cardId,$member_miniapp_id){
        $wechat = WechatMp::isTypes($member_miniapp_id);
        return $wechat->card->get($cardId);
    }

    //增加库存
    public static function increaseStock($cardId,$amount,$member_miniapp_id){
        $wechat = WechatMp::isTypes($member_miniapp_id);
        return $wechat->card->increaseStock($cardId,$amount);
    }

    //减少库存
    public static function reductStock($cardId,$amount,$member_miniapp_id){
        $wechat = WechatMp::isTypes($member_miniapp_id);
        return $wechat->card->reductStock($cardId,$amount);
    }

    //设置卡券失效
    public static function disable($cardId,$code,$member_miniapp_id){
        $wechat = WechatMp::isTypes($member_miniapp_id);
        return $wechat->card->disable($code,$cardId);
    }

    //获取用户已领卡券
    public static function getUserCards($cardId,$openid,$member_miniapp_id){
        $wechat = WechatMp::isTypes($member_miniapp_id);
        return $wechat->card->getUserCards($openid,$cardId);
    }
}