<?php

namespace App\Http\Controllers\Manage\Manage;

use App\Http\Controllers\Manage\BaseController;
use App\Http\Controllers\Manage\Manage\Transformers\ManageTransformer1;
use App\Modules\Channel\Services\ChannelService;
use App\Modules\Channel\Services\ChannelUserService;
use App\Modules\Manage\Services\ManageService;
use Illuminate\Http\Request;
use Cache;
use Redis;

class ManageController extends BaseController
{

    /**
     * @apiVersion 1.0.0
     * @apiDescription 修改密码
     * @api {POST} auth/modifyPassword 修改密码
     * @apiGroup Manage
     * @apiName modifyPassword
     * @apiParam {String}  account 账号.
     * @apiParam {String}  password 密码.
     * @apiParam {String}  new_password 新密码.
     * @apiParam {String}  new_password_repeat 重复新密码.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": {}
     *     }
     */
    function modifyPassword(Request $request)
    {
    	$account = $request->has('account') ? trim($request->input('account')) : '';
        $password = $request->has('password') ? trim($request->input('password')) : '';
        $new_password = $request->has('new_password') ? trim($request->input('new_password')) : '';
        $new_password_repeat = $request->has('new_password_repeat') ? trim($request->input('new_password_repeat')) : '';

        if(!$account || !$password || !$new_password || !$new_password_repeat) return response()->error('PARAM_ERROR');

        if($new_password != $new_password_repeat) return response()->error('PASSWORD_NOT_SAME');
        
        
        $manage = ManageService::getByAccount($account);

        if($manage->password != md5($password."^-^zhuishuyun^_^")) return response()->error('PASSWORD_WRONG');

        if($manage->account)
        {
            $password = md5($new_password."^-^zhuishuyun^_^");
            if(ManageService::modifyPassword($manage->account, $password))
            {
                return response()->success();
            }
        }
    }



    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取商务列表
     * @api {GET} auth/business 获取商务列表
     * @apiGroup Manage
     * @apiName auth/business
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": {}
     *     }
     */
    function getBusiness(Request $request) {
        $params = [];
        $data = ManageService::getBusinessManageList($params, true);

        return response()->collection(new ManageTransformer1(), $data);

    }


    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取渠道修改账号信息短信验证码
     * @api {GET} channel/getChannelAccountSmsCode 获取渠道修改账号信息短信验证码
     * @apiGroup Manage
     * @apiName channel/getChannelAccountSmsCode
     *
     * @apiParam   {channelId}  channelId 渠道ID.
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": {}
     *     }
     */
    function getChannelAccountSms(Request $request) {
        $channelId = $request->has('channelId') ? $request->input('channelId') : '';
        if(!$channelId) {
            //return response("渠道ID输入错误");
            return response()->error('PARAM_ERROR');
        }
        $channel = ChannelService::getById($channelId);
        if(!$channel) {
            return response()->error('PARAM_ERROR');
        }
        $channelUser = ChannelUserService::getById($channel->channel_user_id);
        if(!$channelUser) {
            //return response("渠道ID输入错误");
            return response()->error('PARAM_ERROR');
        }
        $phone = $channelUser->phone;
        $phoneCode = Redis::get('sms_'.$channelId."_".$phone);
        if(!$phoneCode) {
            $phoneCode = Cache::get('sms_'.$channelId."_".$phone);
        }

        if(empty($phoneCode)) {
            //return response("没有找到验证码！");
            return response()->error('PHONE_CODE_ERROR');
        } else {
            return response()->success($phoneCode);
            //return response("渠道【".$channelId."】的验证码为【".$phoneCode."】");
        }
    }
}