<?php
/**
 * CardController.php
 * User: ZhouBenxu
 * Date: 2024/6/27
 * Time: 上午9:35
 * Notes: 储值卡管理
 */

namespace app\admin\controller\card;

use app\admin\service\card\CardService;
use app\admin\validate\card\CardValidate;
use support\Request;
use Tinywan\Jwt\JwtToken;

class CardController
{

    /**
     * Notes: 储值卡列表
     * User: ZhouBenXu
     * DateTime: 2024/6/28 上午10:42
     */
    public function list(Request $request)
    {
        $params = $request->get();
        $params['page'] = $params['page'] ?? 1;
        $params['pageSize'] = $params['pageSize'] ?? 20;
        $params['join_card_batch_user_id'] = JwtToken::getCurrentId();
        // 验证器
        $validate = new CardValidate();
        if (!$validate->scene('list')->check($params)) {
            return json_fail($validate->getError());
        }
        return CardService::getList($params);
    }

    /**
     * Notes: 储值卡详情
     * User: ZhouBenXu
     * DateTime: 2024/6/28 上午10:43
     */
    public function info(Request $request)
    {
        $params = $request->get();
        // 验证器
        $validate = new CardValidate();
        if (!$validate->scene('info')->check($params)) {
            return json_fail($validate->getError());
        }
        return CardService::getInfo($params['card_id']);
    }


    /**
     * Notes: 修改储值卡信息
     * User: ZhouBenXu
     * DateTime: 2024/6/28 上午10:43
     */
    public function save(Request $request)
    {
        $params = $request->post();
        $params['join_card_batch_user_id'] = JwtToken::getCurrentId();
        // 验证器
        $params['validtimes_status'] = $params['validtimes_status'] ?? 3;
        $validate = new CardValidate();
        if (!$validate->scene('save')->check($params)) {
            return json_fail($validate->getError());
        }
        // 校验有效期 0 永久 1增加 2 减少 3不修改
        return CardService::save($params);
    }


    /**
     * Notes: 删除储值卡信息
     * User: ZhouBenXu
     * DateTime: 2024/6/28 上午10:43
     */
    public function delete(Request $request)
    {
        // 验证器
        $validate = new CardValidate();
        if (!$validate->scene('delete')->check($request->post())) {
            return json_fail($validate->getError());
        }
        $card_ids = $request->post('card_ids');
        return CardService::delete($card_ids);
    }


    /**
     * Notes: 储值卡导出
     * User: ZhouBenXu
     * DateTime: 2024/6/28 下午2:44
     * @param Request $request
     * @return \support\Response
     */
    public function exportCard(Request $request)
    {
        return CardService::exportCard($request);
    }

    /**
     * Notes: 卡状态下拉列表
     * User: ZhouBenXu
     * DateTime: 2024/7/1 上午9:27
     * @return Response
     */
    public function getStatusList()
    {
        return CardService::getStatusList();
    }


    /**
     * Notes: 储值卡绑定
     * User: ZhouBenXu
     * DateTime: 2024/7/2 下午6:08
     * @param Request $request
     * @return \support\Response
     */
    public function binding(Request $request)
    {
        $params = $request->post();
        var_dump($params);
        $validate = new CardValidate();
        if (!$validate->scene('binding')->check($params)) {
            return json_fail($validate->getError());
        }
        return CardService::binding($params);
    }

}