<?php

namespace App\Jobs;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

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

    public $externalSkuId;
    public $cost;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($externalSkuId, $cost)
    {
        $this->externalSkuId = $externalSkuId;
        $this->cost = $cost;
    }

    /**
     * Execute the job.
     *
     * @return void
     * @throws GuzzleException
     */
    public function handle()
    {
        $url = 'http://shop.chutang66.com/miaoxuan/cost';
        $method = 'PUT';
        $headers = [
            'headers' => ['Content-type' => 'application/x-www-form-urlencoded;charset=UTF-8'],
            'form_params' => [
                'external_sku_id' => $this->externalSkuId,
                'cost' => $this->cost,
            ]
        ];
        (new Client())->request($method, $url, $headers);
    }
}
