<?php

namespace App\Admin\Actions\Community\Notice;

use App\User;
use EasyWeChat\Factory;
use Encore\Admin\Actions\BatchAction;
use Illuminate\Database\Eloquent\Collection;

class sendNotice extends BatchAction
{
    public $name = '群发通知';

    public function handle(Collection $collection)
    {
        $info = $collection->toArray();
        $ids = array_unique(array_column($info,'id'));
        foreach ($ids as $id) {
            $openid = User::where('id',$id)->value('openid');
            $content = request('content');
            $remark= request('remark');
            $stime= request('stime');
            $type= request('type',1);
//            $openid = 'oYmUA5A1OIqtpA1XSrw35tbjtv1w';
            $service_arr = [1=>'儿保服务',2=>'疫苗接种服务'];
            $service_name = $service_arr[$type];

            //没有openid 直接不发送
            if(empty($openid)) continue;
            $template_arr = [
                1=>'CP3AxrgS-cbW1da8QlIDFcxd-H0RStMEuXdqNRePLoc',
                2=>'xpcaMFXI0Kc9U12o3D6CGPa7ASTpOZJwXJm2mlip6Zo'
            ];
            $tempId =  $template_arr[$type];

            $msg['content'] = $content;
            $msg['service_date'] = $stime;
            $msg['remark'] = $remark;
            $msg['service_name'] = $service_name;

            $this->send($openid,$tempId,$msg);
        }

        return $this->response()->success('发送成功')->refresh();
    }

    public function form()
    {

//        xpcaMFXI0Kc9U12o3D6CGPa7ASTpOZJwXJm2mlip6Zo 接种服务提醒
//        first,keyword1,keword2,remark;
//        CP3AxrgS-cbW1da8QlIDFcxd-H0RStMEuXdqNRePLoc 儿保服务提醒

//        服务项目：XX疫苗接种
//        服务日期：后台设置的服务时间

        $this->textarea('content','内容')->rows(4);
        $this->select('type','服务类型')->options([1=>'儿保',2=>'计免']);
        $this->date('stime','服务时间');
        $this->textarea('remark','备注')->rows(3);
    }

    public function send($open_id,$tempId,$msg)
    {
        $template = [
            'touser' => $open_id,
            'mp_template_msg' => [
                'appid' => env('OFFICIAL_WECHAT_APPID'),
                'template_id' => $tempId,
                'url' => '',
                'miniprogram' => [
                    'appid' => env('WECHAT_APPID', 'wxd41dd232837996c4'),
                    'pagepath' => '',
                ],
                'data' => [
                    'first' => [
                        'value' => $msg['content'],
                    ],
                    'keyword1' => [
                        'value' => $msg['service_name'],
                    ],
                    'keyword2' => [
                        'value' => $msg['service_date'],
                    ],
                    'remark' => [
                        'value' => $msg['remark'],
                    ],
                ],
            ],
        ];
        $app = Factory::miniProgram(config('config.wechat_small_program'));
        $app->uniform_message->send($template);
        return true;

    }


    public function send2($user_ids,$number,$content){

        if(!is_array($user_ids)) {
            $user_ids = [$user_ids];
        }


        $app = app('wechat.official_account');

        foreach($user_ids as $user_id) {

            $user = (new User())->find($user_id);

            if(empty($user) || empty($user['open_id'])) continue;

            $official_open_id = $user->getOfficialOpenId($user['union_id']);

            if(empty($official_open_id)) continue;

            $result=$app->template_message->send([
                'touser' => $official_open_id,
                'template_id' => '4x80cGpV2boFdY7fm4QpsPBNo-FLytKjQGJKtZq_nr4',
                'miniprogram' => [
                    'appid' => env('WECHAT_MINI_PROGRAM_APPID'),
                    'pagepath' => 'pages/index/index',
                ],
                'data' => [
                    'first' => '作业许可状态改变',
                    'keyword1' => $number,
                    'keyword2' => $content,
                    'keyword3' => $user->name,
                    'remark' => '备注'
                ],
            ]);
            var_dump($result);
        }
        return true;
    }

}
