<?php

namespace App\Jobs;

use App\Services\ProcurementWeiXinSendMessageService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;

class ProcurementCheckConfirmInform implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $procurementTotalBills;
    /**
     * Create a new job instance.
     *
     * @param Collection $procurementTotalBills
     */
    public function __construct(Collection $procurementTotalBills)
    {
        $this->procurementTotalBills=$procurementTotalBills;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if (empty($this->procurementTotalBills)) return;
        foreach ($this->procurementTotalBills as $procurementTotalBill){
            foreach ($procurementTotalBill->supplier as $supplier) {
                foreach ($supplier->user as $user){
                   if($user->userDetail->procurement_wechat_open_id){
                       $param['touser'] =$user->userDetail->procurement_wechat_open_id;
                       $param['mp_template_msg'] =[
                           "appid"=>config('weiXin.fuWuHao.appId'),
                           "template_id"=>config('weiXin.fuWuHaoTemplate.check_sheet_inform'),
                           "url"=>"http://weixin.qq.com/download",
                           "miniprogram"=>[
                               "appid"=>config('weiXin.appId.appId'),
                               "pagepath"=>"pages/login/login"
                           ],
                           'data'=>[
                               'first' => [
                                   "value"=>"您好，您的对账单已生成。",
                               ],
                               'keyword1' => [
                                   "value"=>$procurementTotalBill->created_at ?? '',
                                   "color"=>"#173177"
                               ],
                               'keyword2' => [
                                   "value"=>$procurementTotalBill->procurement_count ?? '',
                                   "color"=>"#173177"
                               ],
                               'keyword3' => [
                                   "value"=>$procurementTotalBill->total_payable ?? '',
                                   "color"=>"#173177"
                               ],
                               'remark' => [
                                   "value"=>'点击查看详情。',
                               ],
                           ]
                       ];
                       app(ProcurementWeiXinSendMessageService::class)->sendWenChantTemplate($param);
                   }
                }
            }

        }

    }
}
