<?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)
    {
        foreach ($collection as $model) {
            $openid = User::where('id',$model->id)->value('openid');
            $content = request('content');
            $remark= request('remark');
//            $openid = 'oVxTzvgYlGktIDZXwfLMLQ01Tr5s'; 自己
            if(empty($openid)) continue;
            $this->send($openid,$content,$remark);
        }

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

    public function form()
    {
        $this->text('title','标题')->value('社区通知')->disable();
        $this->textarea('content','内容')->rows(4);
        $this->textarea('remark','备注')->rows(3);
    }

    public function send($open_id,$cotent, $remark)
    {
        if(empty($open_id)) return true;
        $config = [
                'app_id' => 'wx1c2357232cd25f65',
                'secret' => 'c8cab53e4e52234ed1bc2abbdeaba57d',
//            'app_id' => 'wx13bedfcc62e9bab0',
//            'secret' => '175e5518b6426dd12d3096f24ca68fb8',
                'response_type' => 'array'
            ];
        $app = Factory::officialAccount($config);
        if(!isset($user['openid'])) return true;
        $res = $app->template_message->send([
            'touser' => $open_id,
            'template_id' => 'GWIu1FBBA8SOdOwEbXRAmBL9LlYJP_H6EJUC7rv-YiQ',
            'url' => 'https://t5.9026.com',
//            'miniprogram' => [
//                'appid' => 'wx6131f74e623bf6bf',
//                'pagepath' => '/pages/index/index',
//            ],
            'data' => [
                'first'=>'社区通知',
                'cardNumber' => '测试服务服务包',
                'type' => '门诊',
                'address' => '华西医院',
                'VIPPhone' => 18719141830,
                'VIPName' => '测试服务包',
                'expDate' => '2021年12月31号',
                'remark' => '请咨询13912345678。',
            ],
        ]);
        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;
    }

}
