<?php

namespace App\Http\Controllers\Channel\Channel;

use App\Http\Controllers\Channel\BaseController;
use App\Modules\Channel\Models\ChannelQuestionNaire;
use App\Modules\Channel\Services\ChannelQuestionNaireService;
use App\Modules\Channel\Services\ChannelUserService;
use Illuminate\Http\Request;

//结算比例

/**
 * 渠道问卷调查
 * Class ChannelSwitchController
 * @package App\Http\Controllers\Channel\Channel
 */
class ChannelQuestionNaireController extends BaseController
{
    /**
     * 添加问卷调查
     * @param Request $request
     * @return mixed
     */
    public function add_question_naire(Request $request)
    {
        $id = $request->input('id', '');
        $reply_grade = $request->input('reply_grade', '');
        $business_name = ChannelQuestionNaireService::getNickName($this->getChannelUserId());

        if (!$id || !$reply_grade) {
            return response()->error("PARAM_EMPTY");
        }

        $result = ChannelQuestionNaireService::updateInfo($id, $reply_grade, $business_name);
        if ($result) {
            return response()->success();
        } else {
            return response()->error("HANDLE_FAILED");
        }
    }

    /**
     * 获取渠道的问卷调查是否显示或者提交过
     * @param Request $request
     */
    function checkQuestionNaireShow(Request $request)
    {
        $isShow = 0;
        $isReply = 0;
        $channel_user_id = $this->getChannelUserId();
        $channel_user = ChannelUserService::getById($channel_user_id);
        if ($channel_user) {
            $isShow = ChannelQuestionNaire::checkQuestionNaire($channel_user->company_id, false);
            $isReply = ChannelQuestionNaire::checkQuestionNaire($channel_user->company_id, true);
        }

        $isShow = ($isShow > 0) ? true : false;
        $isReply = ($isReply > 0) ? true : false;
        $business_name = ChannelQuestionNaireService::getNickName($channel_user_id);
        return response()->success(['isShow' => $isShow, 'nickName' => $business_name, 'isReply' => $isReply]);
    }

    /**
     * 设置问卷调查的显示状态
     * @param Request $request
     * @return mixed
     */
    function setQuestionNaireShowed(Request $request)
    {
        $channel_user_id = $this->getChannelUserId();
        $business_name = ChannelQuestionNaireService::getNickName($channel_user_id);
        //查询这个channel_user_id本月是否已经显示过

        $channel_user = ChannelUserService::getById($channel_user_id);
        $company_id = $channel_user->company_id;
        $distribution_manages_id = $channel_user->distribution_manages_id;

        $result = ChannelQuestionNaire::geFirstQuestionNaire($company_id);
        if (!$result) {
            $result = ChannelQuestionNaireService::addQuestionNaire(compact('channel_user_id', 'business_name', 'distribution_manages_id', 'company_id'));
        }

        if ($result) {
            return response()->success(['id' => $result->id]);
        } else {
            return response()->error("HANDLE_FAILED");
        }
    }
}