<?php

namespace App\Http\Controllers\Manage\Channel;

use App\Http\Controllers\Manage\BaseController;
use App\Http\Controllers\Manage\Channel\Transformers\ChannelTransformer;
use App\Modules\Channel\Models\Channel;
use App\Modules\Channel\Services\ChannelService;
use Illuminate\Http\Request;

/**
 * Class ChannelsController 渠道
 * @package App\Http\Controllers\Manage\Channel
 */
class ChannelsController extends BaseController
{
    /**
     * @apiDefine channel 渠道
     */

    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取所有渠道
     * @api {GET} channel/getChannelList 获取所有渠道
     * @apiGroup channel
     * @apiName getChannelList
     * @apiParam {Number}  [channel_user_id] 账户ID
     * @apiSuccess {Number}  id 渠道ID.
     * @apiSuccess {String}  name 渠道名称.
     * @apiSuccess {String}  phone 手机号码.
     * @apiSuccess {String}  pay_merchant 支付商户.
     * @apiSuccess {String}  nickname 昵称.
     * @apiSuccess {String}  password 密码.
     * @apiSuccess {String}  latest_login_time  最后登陆时间.
     * @apiSuccess {String}  latest_login_ip 最后登陆IP.
     * @apiSuccess {String}  remark 备注.
     * @apiSuccess {String}  register_ip 注册IP.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": [
     *             {
     *                 "id": 1,
     *                 "name": "分销1",
     *                 "phone": "1586810210",
     *                 "pay_merchant": "ALLINPAY_NALAN",
     *                 "nickname": "昵称",
     *                 "password": "34sdfsdfdsf23",
     *                 "latest_login_time": "2017-12-12 12:12:12",
     *                 "latest_login_ip": "56.3.21.2",
     *                 "remark": "啦啦啦",
     *                 "register_ip": "123.25.21.21",
     *              }
     *          ]
     *     }
     */

    function getChannelList(Request $request)
    {
        $channel_user_id = $request->has('channel_user_id') ? $request->input('channel_user_id') : '';

        $params = [
            'channel_user_id' => $channel_user_id,
        ];

        $channelList = ChannelService::getAllChannels($params);
        return response()->collection(new ChannelTransformer(), $channelList);
    }
    
    /**
     * @apiVersion 1.0.0
     * @apiDescription 创建渠道
     * @api {POST} channel/createChannel 创建渠道
     * @apiGroup channel
     * @apiName createChannel
     * @apiSuccess {Number}  id 渠道ID.
     * @apiParam {String}  [name] 渠道名称.
     * @apiParam {String}  [pay_merchant] 支付商户.
     * @apiParam {String}  [nickname] 昵称.
     * @apiParam {String}  phone 手机号码.
     * @apiParam {String}  password 密码.
     * @apiParam {String}  [latest_login_time] 最后登陆时间.
     * @apiParam {String}  [latest_login_ip] 最后登陆IP.
     * @apiParam {String}  [remark] 备注.
     * @apiParam {String}  [register_ip] 注册IP.
     * @apiSuccess {String}  name 渠道名称.
     * @apiSuccess {String}  phone 手机号码.
     * @apiSuccess {String}  pay_merchant 支付商户.
     * @apiSuccess {String}  nickname 昵称.
     * @apiSuccess {String}  password 密码.
     * @apiSuccess {String}  latest_login_time  最后登陆时间.
     * @apiSuccess {String}  latest_login_ip 最后登陆IP.
     * @apiSuccess {String}  remark 备注.
     * @apiSuccess {String}  register_ip 注册IP.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": 
     *         {
     *            "id": 1,
     *            "phone": "1586810210",
     *            "name": "分销1",
     *            "pay_merchant": "ALLINPAY_NALAN",
     *            "nickname": "昵称",
     *            "password": "34sdfsdfdsf23",
     *            "latest_login_time": "2017-12-12 12:12:12",
     *            "latest_login_ip": "56.3.21.2",
     *            "remark": "啦啦啦",
     *            "register_ip": "123.25.21.21",
     *         }
     *        
     *     }
     */
    function create(Request $request)
    {
        $name = $request->has('name') ? trim($request->input('name')) : '';
        $pay_type = $request->has('pay_type') ? trim($request->input('pay_type')) : 'ALLINPAY_NALAN';
        $phone = $request->has('phone') ? trim($request->input('phone')) : '';
        $password = $request->has('password') ? trim($request->input('password')) : '';
        $nickname = $request->has('nickname') ? trim($request->input('nickname')) : '';
        $latest_login_time = date("Y-m-d H:i:s");
        $remark = $request->has('remark') ? trim($request->input('remark')) : '';
        $latest_login_ip = $register_ip = $request->has('register_ip') ? trim($request->input('register_ip')) : '';

        if(!$phone || !$password) return response()->error('PARAM_ERROR');

        $password = md5($password."^-^zhuishuyun^_^");

        $channel = Channel::firstOrCreate(compact('phone'),compact('password','name','pay_type','nickname','latest_login_ip','latest_login_time','remark','register_ip'));
        return response()->item(new ChannelTransformer(), $channel);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 通过手机号码获取渠道
     * @api {GET} channel/getByPhone 通过手机号码获取渠道
     * @apiGroup channel
     * @apiName getByPhone
     * @apiParam {Number} phone 手机号码
     *
     * @apiSuccess {Number}  id 渠道ID.
     * @apiSuccess {String}  phone 手机号码.
     * @apiSuccess {String}  name 渠道名称.
     * @apiSuccess {String}  pay_merchant 支付商户.
     * @apiSuccess {String}  nickname 昵称.
     * @apiSuccess {String}  password 密码.
     * @apiSuccess {String}  latest_login_time  最后登陆时间.
     * @apiSuccess {String}  latest_login_ip 最后登陆IP.
     * @apiSuccess {String}  remark 备注.
     * @apiSuccess {String}  register_ip 注册IP.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": 
     *         {
     *            "id": 1,
     *            "phone": "1586810210",
     *            "name": "分销1",
     *            "pay_merchant": "ALLINPAY_NALAN",
     *            "nickname": "昵称",
     *            "password": "34sdfsdfdsf23",
     *            "latest_login_time": "2017-12-12 12:12:12",
     *            "latest_login_ip": "56.3.21.2",
     *            "remark": "啦啦啦",
     *            "register_ip": "123.25.21.21",
     *         }
     *        
     *     }
     */
    function getByPhone(Request $request)
    {
        $phone = $request->has('phone') ? trim($request->input('phone')) : '';

        if(!$phone) return response()->error('PARAM_ERROR');
        $channel = Channel::getByPhone($phone);
        return response()->item(new ChannelTransformer(), $channel);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 校验密码
     * @api {POST} checkPassword 校验密码
     * @apiGroup channel
     * @apiName checkPassword
     * @apiParam {String}  phone 手机号码.
     * @apiParam {String}  password 密码.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": {}
     *     }
     */
    function checkPassword(Request $request)
    {
        $phone = $request->has('phone') ? trim($request->input('phone')) : '';
        $password = $request->has('password') ? trim($request->input('password')) : '';

        if(!$phone || !$password) return response()->error('PARAM_ERROR');

        $channel = Channel::getByPhone($phone);
        if($channel->password == md5($password."^-^zhuishuyun^_^"))
        {
            return response()->success();
        }else{
            return response()->error('LOGIN_VERIFY_ERROR');
        }
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 通过id获取渠道
     * @api {GET} channel/getById 通过id获取渠道
     * @apiGroup channel
     * @apiName getById
     * @apiParam {Number} id 渠道id
     *
     * @apiSuccess {Number}  id 渠道ID.
     * @apiSuccess {String}  phone 手机号码.
     * @apiSuccess {String}  name 渠道名称.
     * @apiSuccess {String}  pay_merchant 支付商户.
     * @apiSuccess {String}  nickname 昵称.
     * @apiSuccess {String}  password 密码.
     * @apiSuccess {String}  latest_login_time  最后登陆时间.
     * @apiSuccess {String}  latest_login_ip 最后登陆IP.
     * @apiSuccess {String}  remark 备注.
     * @apiSuccess {String}  register_ip 注册IP.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":
     *         {
     *            "id": 1,
     *            "phone": "1586810210",
     *            "name": "分销1",
     *            "pay_merchant": "ALLINPAY_NALAN",
     *            "nickname": "昵称",
     *            "password": "34sdfsdfdsf23",
     *            "latest_login_time": "2017-12-12 12:12:12",
     *            "latest_login_ip": "56.3.21.2",
     *            "remark": "啦啦啦",
     *            "register_ip": "123.25.21.21",
     *         }
     *
     *     }
     */
    function getById(Request $request)
    {
        $id = $request->has('id') ? trim($request->input('id')) : '';

        if(!$id) return response()->error('PARAM_ERROR');
        $channel = ChannelService::getById($id);
        return response()->item(new ChannelTransformer(), $channel);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 通过id获取渠道名称
     * @api {GET} channel/getChannelNameById 通过id获取渠道
     * @apiGroup channel
     * @apiName getChannelNameById
     * @apiParam {Number} id 渠道id
     *
     * @apiSuccess {Number}  id 渠道ID.
     * @apiSuccess {String}  phone 手机号码.
     * @apiSuccess {String}  name 渠道名称.
     * @apiSuccess {String}  pay_merchant 支付商户.
     * @apiSuccess {String}  nickname 昵称.
     * @apiSuccess {String}  password 密码.
     * @apiSuccess {String}  latest_login_time  最后登陆时间.
     * @apiSuccess {String}  latest_login_ip 最后登陆IP.
     * @apiSuccess {String}  remark 备注.
     * @apiSuccess {String}  register_ip 注册IP.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":
     *         {
     *            "name": "分销1",
     *         }
     *     }
     */
    function getChannelNameById(Request $request)
    {
        $id = $request->has('id') ? trim($request->input('id')) : '';

        if(!$id) return response()->error('PARAM_ERROR');
        $channelName = ChannelService::getChannelNameById($id);
        return response()->json($channelName);
    }




























    /**
     * @apiVersion 1.0.0
     * @apiDescription 更新渠道信息
     * @api {POST} channel/updateInfo 更新渠道信息
     * @apiGroup channel
     * @apiName channel/updateInfo
     * @apiParam {Number} distribution_channel_id 渠道id
     * @apiParam {String} [name] 渠道名称
     * @apiParam {String} [nickname] 渠道昵称
     * @apiParam {Number} [pay_merchant_id] 渠道支付通道
     * @apiParam {Number} [distribution_manages_id] 渠道管理员
     * @apiParam {String} [phone] 渠道电话
     * @apiParam {String} [person_in_charge_name] 渠道负责人
     * @apiParam {String} [remark] 备注
     * @apiParam {Number} [is_enabled] 是否开通 0:未审核; 1:审核通过
     *
     * @apiSuccess {Number}  id 渠道ID.
     * @apiSuccess {String}  phone 手机号码.
     * @apiSuccess {String}  name 渠道名称.
     * @apiSuccess {String}  pay_merchant 支付商户.
     * @apiSuccess {String}  nickname 昵称.
     * @apiSuccess {String}  password 密码.
     * @apiSuccess {String}  latest_login_time  最后登陆时间.
     * @apiSuccess {String}  latest_login_ip 最后登陆IP.
     * @apiSuccess {String}  remark 备注.
     * @apiSuccess {String}  register_ip 注册IP.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":
     *         {
     *            "id": 1,
     *            "phone": "1586810210",
     *            "name": "分销1",
     *            "pay_merchant": "ALLINPAY_NALAN",
     *            "nickname": "昵称",
     *            "password": "34sdfsdfdsf23",
     *            "latest_login_time": "2017-12-12 12:12:12",
     *            "latest_login_ip": "56.3.21.2",
     *            "remark": "啦啦啦",
     *            "register_ip": "123.25.21.21",
     *         }
     *
     *     }
     */
    function updateChannelData(Request $request) {
        $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';

        if(empty(ChannelService::getById($distribution_channel_id))) {
            return response()->error('PARAM_ERROR');
        }
        $distribution_channel_name = $request->has('distribution_channel_name') ? $request->input('distribution_channel_name') : '';
        $nickname = $request->has('nickname') ? $request->input('nickname') : '';
        $pay_merchant_id = $request->has('pay_merchant_id') ? $request->input('pay_merchant_id') : '';
        $phone = $request->has('phone') ? $request->input('phone') : '';
        $person_in_charge_name = $request->has('person_in_charge_name') ? $request->input('person_in_charge_name') : '';
        $remark = $request->has('remark') ? $request->input('remark') : '';

        $distribution_manages_id = $request->has('distribution_manages_id') ? $request->input('distribution_manages_id') : '';

        $isEnabled = $request->has('is_enabled') ? $request->input('is_enabled') : '';

        $params = [
            'name'=>$distribution_channel_name,
            'nickname'=>$nickname,
            'pay_merchant_id'=>$pay_merchant_id,
            'phone'=>$phone,
            'person_in_charge_name'=>$person_in_charge_name,
            'distribution_manages_id'=>$distribution_manages_id,
            'remark'=>$remark,
            'is_enabled'=>$isEnabled,
        ];

        $channel = ChannelService::updateChannelData($distribution_channel_id, $params);

        return response()->item(new ChannelTransformer(), $channel);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取渠道列表
     * @api {GET} channel/getList 获取渠道列表
     * @apiGroup channel
     * @apiName channel/getList
     * @apiParam   {Number}  [distribution_channel_id] 渠道id.(可不传,获取所有渠道)
     * @apiParam   {String}  [distribution_channel_name] 渠道名称.(可不传,获取所有渠道)
     * @apiParam   {String}  [search_name] 搜索名称
     * @apiParam   {String}  [start_time] 开始时间(可不传)
     * @apiParam   {String}  [end_time] 结束时间(可不传)
     * @apiParam   {Number}  [is_enabled] 0:待审核; 1:已审核.(可不传)
     * @apiParam   {Number}  [distribution_manages_id] 管理员ID.(可不传)
     * @apiSuccess {Number}  id 渠道ID.
     * @apiSuccess {String}  name 渠道名称.
     * @apiSuccess {String}  phone 手机号码.
     * @apiSuccess {String}  pay_merchant 支付商户.
     * @apiSuccess {String}  nickname 昵称.
     * @apiSuccess {String}  latest_login_time  最后登陆时间.
     * @apiSuccess {String}  latest_login_ip 最后登陆IP.
     * @apiSuccess {String}  remark 备注.
     * @apiSuccess {String}  register_ip 注册IP.
     * @apiSuccess {String}  person_in_charge_name 负责人.
     * @apiSuccess {String}  create_time 注册时间
     * @apiSuccess {Number}  distribution_manages_id 管理员ID
     * @apiSuccess {String}  distribution_manages_account 管理员
     * @apiSuccess {String}  distribution_manages_number 管理员
     * @apiSuccess {String}  distribution_manages_nickname 管理员昵称
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": [
     *             {
     *                  "id": 1,
     *                   "phone": "",
     *                   "name": "121",
     *                   "pay_merchant_id": 1,
     *                   "nickname": "是说",
     *                   "latest_login_time": "",
     *                   "latest_login_ip": "",
     *                   "is_enabled": 1,
     *                   "register_ip": "",
     *                   "remark": "",
     *                   "person_in_charge_name": "波哥帅",
     *                   "create_time": "2017-11-20 18:34:17",
     *                   "distribution_manages_id": 0,
     *                   "distribution_manages_account": null,
     *                   "distribution_manages_number": null,
     *                   "distribution_manages_nickname": null
     *              }
     *          ]
     *     }
     */
    function getList(Request $request) {
        $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';
        $distribution_channel_name = $request->has('distribution_channel_name') ? $request->input('distribution_channel_name') : '';

        $start_time = $request->has('start_time') && !empty($request->input('start_time')) ? date('Ymd',strtotime($request->input('start_time'))) : '';
        $end_time = $request->has('end_time') && !empty($request->input('end_time')) ? date('Ymd',strtotime($request->input('end_time'))) : '';

        $distribution_manages_id = $request->has('distribution_manages_id') ? $request->input('distribution_manages_id') : '';
        $isEnabled = $request->has('is_enabled') ? $request->input('is_enabled') : '';
        $search_name = $request->has('search_name') ? $request->input('search_name') : '';

        //商务,只获取到当前商务下的渠道列表
        if($this->getLoginUserRole() == "business") {
            $distribution_manages_id = $this->getLoginUserId();
        }
//        dd($this->getLoginUserId());

        $params = [
            'channel_id'=>$distribution_channel_id,
            'channel_name'=>$distribution_channel_name,
            'start_date'=>$start_time,
            'end_date'=>$end_time,
            'distribution_manages_id'=>$distribution_manages_id,
            'is_enabled'=>$isEnabled,
            'search_name'=>$search_name,
        ];
        $result = ChannelService::getChannelList($params);

        return response()->pagination(new ChannelTransformer(),  $result);
    }
}