<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/11/20
 * Time: 下午3:12
 */

namespace App\Http\Controllers\Manage\Finance;


use App\Http\Controllers\Manage\Finance\Transformers\FinancialConfigTransformer;
use App\Modules\Finance\Services\FinancialConfigService;
use Illuminate\Http\Request;

class FinancialConfigController extends BaseController
{

    /**
     * @apiDefine Finance 结算提现模块
     */

    /**
     * @apiVersion 1.0.0
     * @apiDescription 账号冻结，与解冻
     * @api {GET} getFinancialConfig 账号冻结，与解冻
     * @apiGroup Finance
     * @apiName getFinancialConfig
     * @apiParam   {Number}  distribution_channel_id 渠道id.
     * @apiSuccess   {Number}  frozen 0:解冻； -1:冻结状态.
     * @apiSuccess   {String}  remark 渠道备注.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":{
     *                 frozen : 0
     *                 remark : ''
     *              }
     *     }
     */
    function get_frozen(Request $request) {
        $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';
        if(empty($distribution_channel_id)) {
            return response()->error("PARAM_EMPTY");
        }

        $financialConfig = FinancialConfigService::getFinancialConfigSingle($distribution_channel_id);
        if(empty($financialConfig)) {
            $financialConfig = new FinancialConfig();
            $financialConfig['is_frozen'] = 0;
            return response()->item(new FinancialConfigTransformer(), $financialConfig);
        }
        return response()->item(new FinancialConfigTransformer(), $financialConfig);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 账号冻结，与解冻变更
     * @api {POST} changeFinancialConfig 账号冻结，与解冻变更
     * @apiGroup Finance
     * @apiName changeFinancialConfig
     * @apiParam   {Number}  distribution_channel_id 渠道id.
     * @apiParam   {Number}  frozen 0:解冻； -1:冻结状态.
     * @apiParam    {String}  remark 渠道备注.
     * @apiSuccess   {Number}  frozen 0:解冻； -1:冻结状态.
     * @apiSuccess   {String}  remark 渠道备注.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":{
     *                 frozen : 0
     *                 remark : ''
     *              }
     *     }
     */
    function change_frozen(Request $request) {
        $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';
        if(empty($distribution_channel_id)) {
            return response()->error("PARAM_EMPTY");
        }

        $remark = $request->has('remark') ? $request->input('remark') : '';
        $frozen = $request->has('frozen') ? $request->input('frozen') : '';

        if(empty($frozen)) {
            $frozen = 0;
        }
        if($frozen == -1 || $frozen == 0) {
        } else {
            $frozen = 0;
        }

//        dd($distribution_channel_id);

        FinancialConfigService::updateFrozenDistributionChannel($distribution_channel_id, $frozen, $remark);
        return response()->success(compact('frozen', 'remark'));
    }
}