<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\ProductTag;
use Auth;

class ProductTagController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }
    
	public function store(Request $request)
	{
		$product_tag = ProductTag::where('product_id','=',$request->get('pid'))->where('tag_id','=',$request->get('product_tag'))->count();
		if($product_tag > 0) {
            return 0;
        }
        else {
        	$product_tag = new ProductTag();
        	$product_tag->product_id = $request->get('pid');
        	$product_tag->tag_id = $request->get('product_tag');
        	$product_tag->save();
        	return $product_tag->id;
        }
	}

	public function destroy(Request $request)
	{
		$id = $request->get('option');
		$product_tag = ProductTag::find($id);
		ProductTag::where('id','=',$id)->delete();
		return redirect()->route('products.edit',$product_tag->product_id)->with('success','Tag Deleted!');
	}
}