<?php

namespace App\Http\Controllers\Channel\Activity;

use App\Http\Controllers\Channel\BaseController;
use App\Modules\Activity\Models\ActivitySwitch;
use App\Modules\Activity\Services\ActivitySwitchService;
use DB;
use Hashids;
use Illuminate\Http\Request;
use Log;
use Storage;

/**
 * 活动
 * Class ActivityController
 * @package App\Http\Controllers\Channel\Activity
 */
class ActivitySwitchController extends BaseController
{
    /**
     * @apiVersion 1.0.0
     * @api {GET} activity/updateActivitySwitchInfo 设置活动开关
     * @apiGroup Activity
     * @apiName updateActivitySwitchInfo
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": [
     *            {
     *
     *            }
     *        ]
     *     }
     */
    function updateActivitySwitchInfo(Request $request)
    {
        $distribution_channel_id = $this->getChannelId();;
        $activity_id = $request->input('activity_id') ? $request->input('activity_id') : '';
        if (empty($activity_id)) {
            return response()->error("PARAM_EMPTY");
        }

        $activitySwitchInfo = ActivitySwitch::getInfo($activity_id, $distribution_channel_id);
        $is_reader_page_show = $request->has('is_reader_page_show') ? $request->input('is_reader_page_show') : '';
        $is_sign_message_show = $request->has('is_sign_message_show') ? $request->input('is_sign_message_show') : '';
        $data = '';
        if ($activitySwitchInfo && isset($activitySwitchInfo->activity_id)) {
            Log::info('满足要更新的条件');
            $data = ActivitySwitchService::updateShowSwitch($activity_id, $distribution_channel_id, $is_reader_page_show, $is_sign_message_show);
        } else {
            $data = ActivitySwitchService::add(compact('activity_id', 'distribution_channel_id', 'is_reader_page_show', 'is_sign_message_show'));
        }

        if ($data) {
            return response()->success();
        } else {
            return response()->error('HANDLE_FAILED');
        }
    }
}
