<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;
use Ixudra\Curl\Facades\Curl;

class Kfaccount extends Controller
{

    //

    /*-----------------------------------------------------------客服接口-发消息--------------------------------------------------------------*/

    #发送文本消息
    public static function text(array $array)
    {
        $post = json_encode([
            'touser' => (string)$array['touser'],
            'msgtype' => 'text',
            'text' => [
                'content' => (string)$array['content'],
            ],
            'customservice' => [
                'kf_account' => (string)isset($array['kf_account']) ? $array['kf_account'] : 'notice@admin'
            ]
        ], JSON_UNESCAPED_UNICODE);
        $http = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . Base::getAccessToken();
        $result = json_decode(\Curl::to($http)->withData($post)->post(), true);
        return ['code' => $result['errcode'], 'msg' => $result['errmsg']];
    }

    #发送图片消息
    public static function image(string $to_user, array $image, $kf_account = 'notice@admin')
    {
        $http = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . Base::getAccessToken();
        $result = \Curl::to($http)->withData([
            'touser' => $to_user,
            'msgtype' => 'image',
            'voice' => $image,
            'customservice' => [
                'kf_account' => $kf_account
            ]
        ])->asJson()->post();
        return ['code' => $result->errcode, 'msg' => $result->errmsg];
    }

    #发送语音消息
    public static function voice(string $to_user, array $voice, string $kf_account = 'notice@admin')
    {
        $http = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . Base::getAccessToken();
        $result = \Curl::to($http)->withData([
            'touser' => $to_user,
            'msgtype' => 'voice',
            'voice' => $voice,
            'customservice' => [
                'kf_account' => $kf_account
            ]
        ])->asJson()->post();
        return [
            'code' => $result->errcode,
            'msg' => $result->errmsg
        ];
    }

    #发送视频消息
    public static function video(string $to_user, array $video, string $kf_account = 'notice@admin')
    {
        $http = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . Base::getAccessToken();
        $result = \Curl::to($http)->withData([
            'touser' => $to_user,
            'msgtype' => 'video',
            'video' => $video,
            'customservice' => [
                'kf_account' => $kf_account
            ]
        ])->asJson()->post();
        return [
            'code' => $result->errcode,
            'msg' => $result->errmsg
        ];
    }

    #发送音乐消息
    public static function music(string $to_user, array $music, string $kf_account = "notice@admin")
    {
        $http = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . Base::getAccessToken();
        $result = \Curl::to($http)->withData([
            'touser' => $to_user,
            'msgtype' => 'music',
            'music' => $music,
            'customservice' => [
                'kf_account' => $kf_account
            ]
        ])->asJson()->post();
        return [
            'code' => $result->errcode,
            'msg' => $result->errmsg
        ];
    }

    public static function news(string $to_user, array $news, string $kf_account = "notice@admin")
    {
        $http = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . Base::getAccessToken();
        $result = \Curl::to($http)->withData([
            'touser' => $to_user,
            'msgtype' => 'news',
            'news' => [
                'articles' => $news
            ],
            'customservice' => [
                'kf_account' => $kf_account
            ]
        ])->asJson()->post();
        return [
            'code' => $result->errcode,
            'msg' => $result->errmsg
        ];
    }


    /*-----------------------------------------------------------客服帐号管理-----------------------------------------------------------------*/
    #添加客服帐号
    public static function add($kf_account, $nickname, $password)
    {
        $http = 'https://api.weixin.qq.com/customservice/kfaccount/add?access_token=' . Base::getAccessToken();
        $result = \Curl::to($http)->withData([
            'kf_account' => $kf_account,
            'nickname' => $nickname,
            'password' => $password,
        ])->asJson()->post();
        return [
            'code' => $result->errcode,
            'msg' => $result->errmsg
        ];
    }

    #修改客服帐号
    public static function update($kf_account, $nickname, $password)
    {
        $http = 'https://api.weixin.qq.com/customservice/kfaccount/update?access_token=' . Base::getAccessToken();
        $result = \Curl::to($http)->withData([
            'kf_account' => $kf_account,
            'nickname' => $nickname,
            'password' => $password,
        ])->asJson()->post();
        return [
            'code' => $result->errcode,
            'msg' => $result->errmsg
        ];
    }


    #删除客服帐号
    public static function del($kf_account, $nickname, $password)
    {
        $http = 'https://api.weixin.qq.com/customservice/kfaccount/del?access_token=' . Base::getAccessToken();
        $result = \Curl::to($http)->withData([
            'kf_account' => $kf_account,
            'nickname' => $nickname,
            'password' => $password,
        ])->asJson()->post();
        return [
            'code' => $result->errcode,
            'msg' => $result->errmsg
        ];
    }

    #设置客服帐号的头像
    public static function uploadHeadImg($kf_account, $imageUrl)
    {

        $filename = storage_path() . '/app/' . uniqid() . '.png';
        \File::put($filename, \Curl::to($imageUrl)->get());
        $http = 'http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=' . Base::getAccessToken() . '&kf_account=' . $kf_account;
        $data = [
            'media' => "@{$filename}"
        ];
        $response = Curl::to($http)
            ->withData([
                'media' => "@{$filename}"
            ])
            ->withFile('chat_' . uniqid(), $filename)
            ->post();
        \File::delete($filename);
        $result = json_decode($response, true);
        return ['code' => $result['errcode'], 'msg' => $result['errmsg']];
    }


    #获取所有客服账号
    public static function getKfList()
    {
        $http = 'https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=' . Base::getAccessToken();
        $result = \Curl::to($http)->asJson()->get();
        if (isset($result->errcode)) {
            return ['code' => $result->errcode, 'msg' => $result->errmsg];
        } else {
            return ['code' => 0, 'msg' => 'Success', 'data' => $result];
        }
    }
}
