<?php

namespace App\Imports;

use App\Http\Models\SocialPublish;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\ToCollection;

class SocialImport implements ToCollection
{
    use Importable;
    protected $siteId;

    public function __construct($siteId)
    {
        $this->siteId = $siteId;
    }


    public function collection(Collection $collections)
    {
        $list = [];
        $delList = [];

        foreach ($collections as $key => $collection) {
            if ($key < 1) {
                continue;
            }

            if (trim($collection[1]) == 1) {
                $delList[] = trim($collection[0]);
            } else {
                $update = [
                    'site_id' => $this->siteId,
                    'label' => trim($collection[0]),
                ];
                $list[] = $update;
            }
        }
        if (!empty($list)) {
            DB::table('social_label')->insert($list);
        }
        if (!empty($delList)) {
            DB::table('social_label')
                ->where('site_id', $this->siteId)
                ->whereIn('label', $delList)
                ->delete();
        }
    }

}