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

namespace App\Http\Controllers\Channel\Finance;


use App\Http\Controllers\Channel\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
     * @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 = $this->getChannelId();
        $distribution_channel_name = $this->getChannelName();
        $distribution_channel_name = '';

        $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);
    }

    /**
     * @apiignore
     * @apiVersion 1.0.0
     * @apiDescription 变更账号冻结，与解冻
     * @api {POST} changeFinancialConfig 变更账号冻结，与解冻
     * @apiGroup Finance
     * @apiName changeFinancialConfig
     * @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 = $this->getChannelId();
        $distribution_channel_name = $this->getChannelName();
        $distribution_channel_name = '';

        $remark = $request->has('remark') ? $request->input('remark') : '';
        if(empty($distribution_channel_id)) {
            return response()->error("PARAM_EMPTY");
        }
        $frozen = $request->input('frozen');
        if(empty($frozen)) {
            $frozen = 0;
        }
        if($frozen == -1 || $frozen == 0) {
        } else {
            $frozen = 0;
        }

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