<?php

namespace App\Services\NoticeService\Channels;

use App\Http\Controllers\Miniprogram\Core;

class TemplateChannel extends Channel
{


    public $uuid;
    public $public_id;
    protected $notice_type = '公众号模板消息';
    protected $url;
    protected $page_path;
    protected $data;

    /**
     * TemplateChannel constructor.
     * @param  int  $uid
     * @param  string  $templateId
     * @param  array  $data
     * @param  string  $public_id
     */
    public function __construct(int $uid, string $templateId, array $data, string $public_id = "")
    {
        $this->uid = $uid;
        $this->templateId = $templateId;
        $this->data = $data;
        $this->content = json_encode($data, JSON_UNESCAPED_UNICODE);
        $this->public_id = $public_id ?: config("wechat.fpdx.public_id");
    }


    public function setUuid($uuid)
    {
        $this->uuid = $uuid;

        return $this;
    }


    public function setUrl($url)
    {
        $this->url = $url;

        return $this;
    }


    public function setPath($path)
    {
        $path = urlencode("/" . $path);
        $page_path = "pages/starter/starter?log_type=notice&log_id={$this->uuid}&launch_type=free&url={$path}";
        $this->page_path = $page_path;

        return $this;
    }


    /**
     * @return bool
     */
    public function send(): bool
    {
        $publicId = $this->public_id;
        $page = $this->page_path;
        $data = $this->data;
        $core = new Core();

        return $core->template($this->uid, $this->templateId, $publicId, $page, $data);
    }
}
