<?php

namespace App\Http\Controllers\Admin;

use Carbon\Carbon;
use App\Models\CollectMedia;
use Illuminate\Http\Request;
use App\Models\ProductDescription;
use App\Models\Product;
use App\Models\ProductCate;

class CollectMediaController extends AdminController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where = $request->except('page');

        $query = CollectMedia::query();

        if ($word = $request->input('word')) {
            $query = $query->where('path', 'like', "%{$word}%");
        }

        if ($status = $request->input('status')) {
            $query = $query->where('status', $status == 1 ? 1 : 0);
        }

        $nests = ProductCate::getNestedList('cname', 'id', $this->seperator, '├ ', true);

        $data = $query->latest()->paginate(20);

        return view('admin.collect_media.index', compact('data', 'where', 'nests'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $result = CollectMedia::destroy(array_str($id));

        return response()->json(['status' => $result ? 'success' : 'error']);
    }

    public function import(Request $request)
    {
        $data = [];
        $pid = $request->input('pid');
        if ($pid > 0) {
            $cate = ProductCate::flag()->where('id', $pid)->first();
            $idArr    = $cate->getDescendantsAndSelf(['id'])->pluck('id')->toArray();
            $productIds = Product::flag()->whereIn('pid', $idArr)->lists('id')->toArray();
            $imgs = ProductDescription::whereIn('product_id', $productIds)->lists('imgs')->toArray();
        } else {
            $imgs = ProductDescription::lists('imgs')->toArray();
        }

        $imgs = array_unique(array_filter($imgs));

        if (!empty($imgs)) {
            foreach ($imgs as $img) {
                foreach (json_decode($img, true) as $val) {
                    if (CollectMedia::where('path', $val)->exists()) {
                        continue;
                    }
                    $data[] = [
                        'path' => $val,
                        'created_at' => Carbon::now(),
                        'updated_at' => Carbon::now()
                    ];
                }
            }
        }

        CollectMedia::insert($data);

        return response()->json(['status' => 'success']);
    }
}
