<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;

use App\Packages;

use Illuminate\Support\Facades\Mail;
use App\Mail\MyEmail;

use Auth;

class PackagesController extends Controller
{

     public function __construct()
    {
        $this->middleware('auth', ['except' => ['insertPackage']]);
    }

    // Get all Packages and their items and groups
    public function PackageData(Request $request){


    }

    // Test email
    public function sendEmail(Request $request) {

        $Package = new Packages;

        Mail::to('mekgwele@gmail.com')->send(new MyEmail($Package));

        return "Something";
    }


    // List all Packages
    public function listPackages(Request $request){
        $packages = Packages::all();

        return response()->json($packages);

    }



    // Delete a Package
    public function deletePackage($id, Request $request){
        $Package = Packages::where('id', $id)->delete();

        if ($Package) {
            return "Package delete";
        }else{
            return "Something went wrong, Please try again";
        }
    }

    // Create new Package
    public function insertPackage(Request $request){

        $Package = new Packages;

        $Package->parent_id = $request->parent_id;
        $Package->waybill = $request->waybill;
        $Package->package_type = $request->package_type;
        $Package->package_desc = $request->package_desc;
        // $Package->package_value = $request->package_value;
        // $Package->quantity = $request->quantity;
        $Package->length = $request->length;
        $Package->height = $request->height;
        $Package->width = $request->width;
        $Package->weight = $request->weight;
        // $Package->package_price = $request->package_price;

        $input = $request->all();


        if (empty($request->package_type)) {
            return "empty";
        }

        if ($Package->save($input)) {
            return "success";
        }else{
            return "error";
        }

    }

    // Update existing Package
    public function updatePackage($id, Request $request){

        $Package = Packages::where('id', $id)->update(array('title' => $request->title));


        if (empty($request->title)) {
            return "Please fill in the title";
        }

        if ($Package) {
            return "Package Updated";
        }else{
            return "Something went wrong, Please try again";
        }

    }


}
