<?php

namespace App\Jobs;

use App\Procurement;
use App\Services\ProcurementWeiXinSendMessageService;
use App\User;
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\Carbon;

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

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $pro=$this->procurement->loadMissing(['supplier.user.userDetail']);
        $initiator='';
        if ($pro->initiator)$initiator=User::query()->where('id',$pro->initiator)->value('name');
            foreach ($pro->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.wait_procurement_inform'),
                       "url"=>"http://weixin.qq.com/download",
                       "miniprogram"=>[
                           "appid"=>config('weiXin.appId.appId'),
                           "pagepath"=>"pages/login/login"
                       ],
                       'data'=>[
                           'first' => [
                               "value"=>"采购申请等待处理。",
                           ],
                           'keyword1' => [
                               "value"=>$pro->code ?? '',
                               "color"=>"#173177"
                           ],
                           'keyword2' => [
                               "value"=> $initiator,
                               "color"=>"#173177"
                           ],
                           'keyword3' => [
                               "value"=>$pro->cost_price*$pro->quantity>0 ? $pro->cost_price*$pro->quantity.'元' :0 .'元',
                               "color"=>"#173177"
                           ],
                           'keyword4' => [
                               "value"=>Carbon::now()->toDateString(),
                               "color"=>"#173177"
                           ],
                           'remark' => [
                               "value"=>'请及时确认，谢谢。',
                               "color"=>"#173177"
                           ],
                       ]
                   ];
                   app(ProcurementWeiXinSendMessageService::class)->sendWenChantTemplate($param);
               }
            }

    }
}
