<?php

namespace App\Http\Controllers\Channel\Channel;

use App\Modules\OfficialAccount\Models\OfficialAccount;
use App\Modules\Finance\Services\FinancialStatService;
use App\Http\Controllers\Channel\BaseController;
use App\Modules\Channel\Services\ChannelService;
use App\Http\Controllers\Channel\Channel\Transformers\ChannelTransformer;
use App\Modules\Channel\Services\ChannelUserService;
use App\Modules\Finance\Services\CommissionRateService;//结算比例
use App\Modules\OfficialAccount\Services\PaySubstituteSwitchService;
use http\Url;
use Illuminate\Http\Request;

class ChannelController extends BaseController
{

    /**
     * @apiDefine Channel 渠道
     */
    //设置当前操作渠道
    public function setCurrent(Request $request)
    {
        $channel_user_id = $this->getChannelUserId();
        $distribution_channel_id = $request->has('distribution_channel_id') ? (int)$request->input('distribution_channel_id') : '';
        //获取该账号下渠道
        if (!$channel_user_id || !$distribution_channel_id || !$channel = ChannelService::getUserChannel($distribution_channel_id, $channel_user_id)) return response()->error('PARAM_ERROR');

        $request->session()->put('ydychannel', serialize($channel));
        return response()->success();
    }

    //创建渠道
    public function createChannel(Request $request)
    {
        $channel_user_id = $this->getChannelUserId();
        $channel_user = ChannelUserService::getById($channel_user_id);
        $distribution_manages_id = $channel_user->distribution_manages_id;
        $phone = $channel_user->phone;
        $person_in_charge_name = $channel_user->person_in_charge_name;
        $nickname = $channel_user->nickname;
        if ($channel = ChannelService::createChannel(compact('channel_user_id', 'distribution_manages_id', 'phone', 'person_in_charge_name', 'nickname'))) {
            //创建结算比例
            CommissionRateService::addCommissionRate($channel->id, 0, 0, 0.9);
            return response()->success();
        }
    }

    //当前账号下渠道列表
    public function channelList(Request $request)
    {
        $channel_user_id = $this->getChannelUserId();
        $channels = ChannelService::getByChannelUserId($channel_user_id);
        foreach ($channels as $value) {
            $pay_substitute = PaySubstituteSwitchService::getChannelSwitch($value->id);
            $value->paySubstituteSwitch =(bool)((int)$pay_substitute);
        }
        return response()->collection(new ChannelTransformer(), $channels);
    }
    
    // 删除站点
    public function deleteChannel(Request $request)
    {
    	$channel_user_id = $this->getChannelUserId();
    	$distribution_channel_id = $request->has('distribution_channel_id') ? (int)$request->input('distribution_channel_id') : '';
    	if (empty($distribution_channel_id)) {
    		return response()->error('PARAM_EMPTY');
    	}
    	
    	\Log::info('deleteChannel_start:'.$distribution_channel_id);
    	
    	// 检测未解绑公众号
    	$exist_official_account = OfficialAccount::getOneOfficialAuthAccounts($distribution_channel_id,1,1);
    	if(!empty($exist_official_account)){
    		return response()->error('UNABLE_DEL_EXIST_GZH');
    	}
    	
    	// 检测账户余额
    	$finance_stat = FinancialStatService::getFinancialStatSingle($distribution_channel_id);
    	$enable_withdrawal_amount = $finance_stat ? (float)$finance_stat->enable_withdrawal_amount : 0;
    	\Log::info('deleteChannel_start:'.$distribution_channel_id.' $enable_withdrawal_amount:'.$enable_withdrawal_amount);
    	 
    	if($enable_withdrawal_amount > 0){
    		return response()->error('UNABLE_DEL_EXIST_WITHDRAWL_AMOUNT');
    	}
    	\Log::info('deleteChannel_after_check:'.$distribution_channel_id.' channel_user_id:'.$channel_user_id);
    	 
    	ChannelService::deleteChannel($distribution_channel_id, $channel_user_id);
    	return response()->success();
    }


    /**
     * @apiVersion 1.0.0
     * @apiDescription 设置站点别名
     * @api {POST} channels/setsitename 设置站点别名
     * @apiGroup Channel
     * @apiName setSiteNickName
     * @apiParam   {Number}  distribution_channel_id  渠道id
     * @apiParam   {String}  site_name  名称
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":
     *         {
     *         }
     *     }
     */
    public function setSiteNickName(Request $request)
    {
        $distribution_channel_id = $request->has('distribution_channel_id') ? (int)$request->input('distribution_channel_id') : '';
        $site_name = $request->has('site_name') ? $request->input('site_name') : '';
        if (empty($distribution_channel_id) || empty($site_name)) {
            return response()->error('PARAM_EMPTY');
        }

        ChannelService::setChannelSiteNickName($distribution_channel_id, $site_name);
        return response()->success();
    }

    public function updateDistributionOuterSiteSwitch(Request $request)
    {
        $status = $request->has('status') ? $request->input('status') : '0';
        $distribution_channel_id = $this->getChannelId();
        \Log::info('updateDistributionOuterSiteSwitch:$distribution_channel_id:' . $distribution_channel_id . ' $status:' . $status);
        ChannelService::updateDistributionOuterSiteSwitch($distribution_channel_id, $status);
        return response()->success();
    }

    public function updateDistributionOldUserSiteSwitch(Request $request)
    {
        $status = $request->has('status') ? $request->input('status') : '0';
        $distribution_channel_id = $this->getChannelId();
        \Log::info('updateDistributionOldUserSiteSwitch:$distribution_channel_id:' . $distribution_channel_id . ' $status:' . $status);
        ChannelService::updateDistributionOldUserSiteSwitch($distribution_channel_id, $status);
        return response()->success();
    }

    public function getDistributionChannel(Request $request)
    {
        $distribution_channel_id = $this->getChannelId();
        $channel = ChannelService::getDistributionChannel($distribution_channel_id);
        return response()->success($channel);
    }
    
    public function getDistributionChannelSwitchByCategory(Request $request)
    {
    	$category = $request->has('category') ? $request->input('category') : '';
    	if (empty($category)) {
    		return response()->error('PARAM_EMPTY');
    	}
    	$distribution_channel_id = $this->getChannelId();
    	$channel = ChannelService::getDistributionChannelSwitchByCategory($distribution_channel_id,$category);
    	$is_show = 0;
    	if(!empty($channel)){
    		$is_show = 1;
    	}
    	
    	// 开放全部：周报、图片素材上传
    	if(in_array($category,array('weekly_report','create_img_chapter'))){
    		$is_show = 1;
    	}
    	
    	return response()->success($is_show);
    }

}