Current File : //home/tradevaly/text.tradevaly.com.bd/app/Http/Controllers/User/UserController.php
<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\AdvertiserOrder;
use App\Models\Award;
use App\Models\BuyOffer;
use App\Models\Category;
use App\Models\Certificate;
use App\Models\Company;
use App\Models\Country;
use App\Models\FeaturedProduct;
use App\Models\FeaturedRequest;
use App\Models\Income;
use App\Models\Message;
use App\Models\Messsage;
use App\Models\Method;
use App\Models\Package;
use App\Models\Pdf;
use App\Models\Product;
use App\Models\QuoteMessage;
use App\Models\Subscriber;
use App\Models\Tag;
use App\Models\Thread;
use App\Models\Thumb;
use App\Models\User;
use Brian2694\Toastr\Facades\Toastr;
use Carbon\Carbon;
use DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;
use shurjopayv2\ShurjopayLaravelPackage8\Http\Controllers\ShurjopayController;


class UserController extends Controller
{
    public function feature_product_create(Request $request)
    {
        $product = Product::where('status', 1)->findorFail($request->product_id);
        if ($request->isMethod("post")) {
            $featured = new FeaturedRequest();
            $featured->product_id = $product->id;
            $featured->user_id = Auth::user()->id;
            if ($featured->save()) {
                return redirect()->route('user.featuredlist');
            }
        }

        return view('user.pages.submitfeatured', compact('product'));
    }

    public function dashboard()
    {
        //   $percent = $columns = Schema::getColumnListing('users');
        $columns = Schema::getColumnListing('users');
        $emptyClm = 0;
        $company_id = Company::where('user_id', Auth::id())->first()->id;
        foreach ($columns as $column) {
            $count = User::whereNull($column)->where('id', Auth::id())->first();
            if ($count) {
                $emptyClm++;
            }
        }
        $percentage = ($emptyClm / count($columns)) * 100;

        // company profile complete count
        $columns_c = Schema::getColumnListing('companies');
        $emptyClm_c = 0;

        foreach ($columns_c as $column_c) {
            $count_c = Company::whereNull($column_c)->where('id', $company_id)->first();
            if ($count_c) {
                $emptyClm_c++;
            }
        }
        $percentage_c = ($emptyClm_c / count($columns_c)) * 100;


        //   $data['auth_total_product'] = Product::where('supplier_id',Auth::id())->take(10)->count();
        $data['auth_total_product'] = Product::where('supplier_id', Auth::id())->take(10)->count();
        $data['auth_buy_offer'] = BuyOffer::where('buyer_id', Auth::id())->take(10)->count();
        $data['user_profile_complete'] = number_format($percentage);
        $data['user_profile_complete_c'] = number_format($percentage_c);
        $data['messages'] = Message::where('status', 0)->where(function ($query) {
            $query->where('sender_id', Auth::user()->id)
                ->orWhere('receiver_id', Auth::user()->id);
        })->count();

        $data['products'] = Product::where('supplier_id', Auth::user()->id)->count();
        $data['product_list'] = Product::where('supplier_id', Auth::user()->id)->latest()->get()->take(6);

        $data['buyoffer'] = BuyOffer::where('buyer_id', Auth::user()->id)->count();
        return view('user.pages.index')->with($data);
    }

    public function subs()
    {
        $package = Package::find(Auth::user()->package_id);
        return view('user.pages.subs', compact("package"));
    }

    public function changeplan()
    {
        $packages = Package::where('status', 1)->get();
        return view('user.pages.changeplan', compact("packages"));
    }

    public function choose($id, Request $request)
    {
        $package = Package::findOrFail($id);
        $subscriber = new Subscriber();
        $subscriber->user_id  = Auth::guard('web')->user()->id;
        $subscriber->order_id = rand(000000,666666);
        $subscriber->package_id = $package->id;

        $subscriber->date = date('Y-m-d');
        $subscriber->amount = $package->price;
        $subscriber->payment_status = 'unpaid';
        $subscriber->status = 'pending';

        if ($subscriber->save()) {
            $invoice = Subscriber::find($subscriber->id);
            $user = Auth::guard('web')->user();
            $info = array(
                'currency' => 'BDT',
                'amount' => intval($invoice->amount),
                'order_id' => $invoice->order_id,
                'discsount_amount' => 0,
                'disc_percent' => 0,
                'client_ip' => 'N\L',
                'customer_name' => $user->name,
                'customer_phone' => $user->phone ? $user->phone : Auth::user()->company->phone,
                'email' => $user->email,
                'customer_address' => "N\L",
                'customer_city' => "N/L",
                'customer_state' => "N/L",
                'customer_postcode' => "N/L",
                'customer_country' => Country::find($user->country_id)->name
            );
            $order = [
                'order_id' => $invoice->order_id,
                'order_for' => 'subscription',
                'package_id' => $package->id,
            ];
            session(['orderInfo' => $order]);
            $shurjopay_service = new ShurjopayController();
            return $shurjopay_service->checkout($info);
        }
    }

    public function subscriptionInvoice()
    {
        $invoice = Subscriber::where('user_id',Auth::user()->id)->first();
        return view('user.subscription_invoice.invoice', compact('invoice'));
    }
    public function subscriptionMoneyReceipt()
    {
        $money_receipt = Subscriber::where('user_id',Auth::user()->id)->first();
        return view('user.subscription_invoice.money_receipt', compact('money_receipt'));
    }

    public function renew(Request $request)
    {
        $package = Package::find(Auth::user()->package_id);
        $income = new Income();
        $income->user_id = Auth::guard('web')->user()->id;
        $income->package_id = $package->id;
        $income->date = date('Y-m-d');
        $income->amount = $package->price;
        if ($income->save()) {
            $income = Income::where('id', $income->id)->first();
            return view('stripe', compact('income'));
        }
    }

    public function products()
    {


        $data['products'] = Product::where('supplier_id', Auth::user()->id)->where('status', 1)->paginate(50);

        return view('user.pages.products')->with($data);
    }


    public function pending_product()
    {


        $data['products'] = Product::where('supplier_id', Auth::user()->id)->where('status', 0)->paginate(50);

        return view('user.pages.products')->with($data);
    }


    public function rej_product()
    {


        $data['products'] = Product::where('supplier_id', Auth::user()->id)->where('status', 5)->paginate(50);

        return view('user.pages.products')->with($data);
    }

    public function trashed_product()
    {


        $data['products'] = Product::onlyTrashed()->where('supplier_id', Auth::user()->id)->paginate(50);

        return view('user.pages.products')->with($data);
    }


    public function pdf()
    {


        $data['pdfs'] = Pdf::where('company_id', Auth::user()->company->id)->get();

        return view('user.pages.pdf')->with($data);
    }

    // awrd functionality here
    public function award()
    {


        $data['pdfs'] = Award::where('company_id', Auth::user()->company->id)->get();

        return view('user.pages.award.index')->with($data);
    }

    public function addaward(Request $request)
    {

        if ($request->isMethod("post")) {
            $product = new Award();
            if ($request->hasFile('image')) {

                $image = $request->file('image');
                $thefile = \File::get($image);
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/user/pdf')) {
                    Storage::disk('public')->makeDirectory('images/user/pdf');
                }
                Storage::disk('public')->put('images/user/pdf/' . $imageName, $thefile);
                $product->image = $imageName;
            } else {
                Toastr::error('Failed!', 'Pdf File Is Required!');
                return redirect()->back();
            }
            $product->company_id = Auth::user()->company->id;
            $product->name = $request->name;
            if ($product->save()) {

                Toastr::success('Success!', 'Setting Updated!');
                return redirect()->back();
            }
        }

        return view('user.pages.award.create');
    }

    public function delaward($id)
    {
        $award = Award::find($id);
        if (Storage::disk('public')->exists('images/user/pdf/' . $award->image)) {
            Storage::disk('public')->delete('images/user/pdf/' . $award->image);
        }
        $award->delete();
        Toastr::success('Success!', 'Award  deleted successfully!!');
        return redirect()->back();
    }

    public function certificate()
    {


        $certificates = Certificate::where('company_id', Auth::user()->company->id)->get();
        return view('user.pages.certificate', compact('certificates'));
    }


    public function addpdf(Request $request)
    {

        if ($request->isMethod("post")) {
            $product = new Pdf();
            if ($request->hasFile('image')) {

                $image = $request->file('image');
                $thefile = \File::get($image);
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/user/pdf')) {
                    Storage::disk('public')->makeDirectory('images/user/pdf');
                }
                Storage::disk('public')->put('images/user/pdf/' . $imageName, $thefile);
                $product->image = $imageName;
            } else {
                Toastr::error('Failed!', 'Pdf File Is Required!');
                return redirect()->back();
            }
            $product->company_id = Auth::user()->company->id;
            $product->name = $request->name;
            if ($product->save()) {

                Toastr::success('Success!', 'Setting Updated!');
                return redirect()->back();
            }
        }

        return view('user.pages.addpdf');
    }


    public function addcertificate(Request $request)
    {

        if ($request->isMethod("post")) {
            $product = new Certificate();
            if ($request->hasFile('image')) {

                $image = $request->file('image');
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/user/certificate/' . $product->image)) {
                    Storage::disk('public')->makeDirectory('images/user/certificate/' . $product->image);
                }
                $bannerImage = Image::make($image)->resize(500, 500)->stream();
                Storage::disk('public')->put('images/user/certificate/' . $imageName, $bannerImage);
                $product->image = $imageName;
            } else {
                Toastr::error('Failed!', 'Image File Is Required!');
                return redirect()->back();
            }
            $product->company_id = Auth::user()->company->id;
            $product->name = $request->name;
            if ($product->save()) {

                Toastr::success('Success!', 'Setting Updated!');
                return redirect()->back();
            }
        }

        return view('user.pages.addcertificate');
    }

    public function offers()
    {


        $data['offers'] = BuyOffer::where('buyer_id', Auth::user()->id)->paginate(50);

        return view('user.pages.offers')->with($data);
    }


    public function recommand_offerglobal()
    {
        if (Auth::user()->category->parent_id == 0) {

            $data['offers'] = BuyOffer::where('seller_id', null)->where('category_id', Auth::user()->category->parent_id)->latest()->get();
        } else {
            $data['offers'] = BuyOffer::where('seller_id', null)->where('category_id', Auth::user()->category_id)->latest()->get();

        }


        //  $data['offers'] = $offers->where('title', 'like', '%' . Auth::guard('web')->user()->products . '%')->orWhere('details', 'like', '%' . Auth::guard('web')->user()->products . '%')->orWhere('tags', 'like', '%' . Auth::guard('web')->user()->products . '%')->get();
        return view('user.pages.buy_offers')->with($data);
    }

    public function recommand_offerforyou()
    {

        $data['offers'] = BuyOffer::where('seller_id', Auth::id())->latest()->get();


        //  $data['offers'] = $offers->where('title', 'like', '%' . Auth::guard('web')->user()->products . '%')->orWhere('details', 'like', '%' . Auth::guard('web')->user()->products . '%')->orWhere('tags', 'like', '%' . Auth::guard('web')->user()->products . '%')->get();
        return view('user.pages.buy_offers')->with($data);
    }

    public function recommand_offerforyou_details($id)
    {

        $data['messages'] = QuoteMessage::where('buy_offer_id', $id)->where('sender_id', Auth::id())->orWhere('receiver_id', Auth::id())->latest()->get();
        $data['offer'] = BuyOffer::where('id', $id)->where('seller_id', Auth::id())->firstOrFail();

        return view('user.pages.buy_offer.buy_offer_details')->with($data);
    }

    public function inbox()
    {


        $data['offers'] = Thread::where('sender_id', Auth::user()->id)->orWhere('receiver_id', Auth::user()->id)->paginate(50);

        return view('user.pages.inbox')->with($data);
    }


    public function inbox_view(Request $request, $id)
    {


        if ($request->isMethod("post")) {
            $msg = new Message();
            $msg->thread_id = $id;
            $msg->sender_id = Auth::user()->id;
            $msg->description = $request->des;
            $msg->save();

            Toastr::success('Success!', 'Replied Sent!');
            return redirect()->back();
        }

        $messages = Message::where('thread_id', $id)->where('sender_id', '!=', Auth::user()->id)->orderBy('id', 'desc')->first();
        if ($messages != null) {
            $messages->seen_at = date('Y-m-d h:i:s');
            $messages->save();
        }
        $data['thread'] = Thread::findorFail($id);
        $data['offers'] = Message::where('thread_id', $id)->get();
        return view('user.pages.inbox_view')->with($data);
    }

    public function delpdf($id)
    {
        $work = Pdf::find($id);
        if (Storage::disk('public')->exists('images/user/pdf/' . $work->image)) {
            Storage::disk('public')->delete('images/user/pdf/' . $work->image);
        }
        $work->delete();
        Toastr::success('Success!', 'Pdf deleted!');
        return redirect()->back();
    }

    public function delcertificate($id)
    {
        $work = Certificate::find($id);
        if (Storage::disk('public')->exists('images/user/certificate/' . $work->image)) {
            Storage::disk('public')->delete('images/user/certificate/' . $work->image);
        }
        $work->delete();
        Toastr::success('Success!', 'Certififcate deleted!');
        return redirect()->back();
    }

    public function delproduct($id)
    {
        $product = Product::findOrFail($id);
        $this->feature_delete_by_product($product->id);
        $this->buy_offer_delete_by_product($product->id);
        $product->delete();

        Toastr::success('Success!', 'Product deleted!');
        return redirect()->back();
    }

    public function trashed_product_destroy($id)
    {
        $product = Product::onlyTrashed()->where('id', $id)->where('supplier_id', Auth::id())->firstOrFail();
        $this->delete_trash_product($product);
        Toastr::success('Success!', 'Product Permanent Deleted!');
        return redirect()->back();
    }

    public function trash_product_rerurn($id)
    {
        $product = Product::onlyTrashed()->where('id', $id)->where('supplier_id', Auth::id())->firstOrFail();
        if (!Category::find($product->child_id)) {
            Toastr::error('Error!', "Product Category Has Been Deleted You can't Restore The Product ");
            return back();
        }
        $this->restore_trash_product($product);
        Toastr::success('Success!', 'Product Restore Successfully!');
        return back();
    }


    // Trashed Product Restore Methods

    public function restore_trash_product($product)
    {
        $product->restore();
        return true;

    }

    // Trashed Product Permanent Methods
    public function delete_trash_product($product)
    {

        $this->feature_delete_by_product($product->id);
        $this->buy_offer_delete_by_product($product->id);
        if ($product->image !== 'image.jpg') {
            if (Storage::disk('public')->exists('images/admin/product/' . $product->image)) {
                Storage::disk('public')->delete('images/admin/product/' . $product->image);
            }
        }
        if (Storage::disk('public')->exists('images/admin/product/' . $product->gallary1)) {
            Storage::disk('public')->delete('images/admin/product/' . $product->gallary1);
        }
        if (Storage::disk('public')->exists('images/admin/product/' . $product->gallary2)) {
            Storage::disk('public')->delete('images/admin/product/' . $product->gallary2);
        }
        if (Storage::disk('public')->exists('images/admin/product/' . $product->gallary3)) {
            Storage::disk('public')->delete('images/admin/product/' . $product->gallary3);
        }
        if (Storage::disk('public')->exists('images/admin/product/' . $product->gallary4)) {
            Storage::disk('public')->delete('images/admin/product/' . $product->gallary4);
        }
        $product->forceDelete();
        return true;
    }

    public function deloffer($id)
    {
        $work = BuyOffer::find($id)->delete();
        Toastr::success('Success!', 'Offer deleted!');
        return redirect()->back();
    }


    public function editproduct(Request $request, $id)
    {
        $product = Product::find($id);
        if ($request->isMethod("post")) {


            if ($request->hasFile('image')) {

                $image = $request->file('image');
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/admin/product/' . $product->image)) {
                    Storage::disk('public')->makeDirectory('images/admin/product/' . $product->image);
                }
                $bannerImage = Image::make($image)->resize(500, 500)->stream();
                Storage::disk('public')->put('images/admin/product/' . $imageName, $bannerImage);
                $product->image = $imageName;
            }


            if ($request->hasFile('gallary1')) {

                $image = $request->file('gallary1');
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/admin/product/' . $product->gallary1)) {
                    Storage::disk('public')->makeDirectory('images/admin/product/' . $product->gallary1);
                }
                $bannerImage = Image::make($image)->resize(500, 500)->stream();
                Storage::disk('public')->put('images/admin/product/' . $imageName, $bannerImage);
                $product->gallary1 = $imageName;
            }


            if ($request->hasFile('gallary2')) {

                $image = $request->file('gallary2');
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/admin/product/' . $product->gallary2)) {
                    Storage::disk('public')->makeDirectory('images/admin/product/' . $product->gallary2);
                }
                $bannerImage = Image::make($image)->resize(500, 500)->stream();
                Storage::disk('public')->put('images/admin/product/' . $imageName, $bannerImage);
                $product->gallary2 = $imageName;
            }


            if ($request->hasFile('gallary3')) {

                $image = $request->file('gallary3');
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/admin/product/' . $product->gallary3)) {
                    Storage::disk('public')->makeDirectory('images/admin/product/' . $product->gallary3);
                }
                $bannerImage = Image::make($image)->resize(500, 500)->stream();
                Storage::disk('public')->put('images/admin/product/' . $imageName, $bannerImage);
                $product->gallary3 = $imageName;
            }


            if ($request->hasFile('gallary4')) {

                $image = $request->file('gallary4');
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/admin/product/' . $product->gallary4)) {
                    Storage::disk('public')->makeDirectory('images/admin/product/' . $product->gallary4);
                }
                $bannerImage = Image::make($image)->resize(500, 500)->stream();
                Storage::disk('public')->put('images/admin/product/' . $imageName, $bannerImage);
                $product->gallary4 = $imageName;
            }


            $product->ip = visitorip();
            $product->title = $request->title;
            $product->date = date('Y-m-d');
            $product->slug = Str::slug($request->title);
            $product->details = $request->des;
            $product->country_id = Auth::user()->country_id;
            if ($request->filled('child_id')) {
                $product->category_id = $request->category_id;
                $product->sub_id = $request->sub_id;
                $product->child_id = $request->child_id;
            } else {
                $product->category_id = $request->category_id;
                $product->sub_id = $request->sub_id;
            }
            $product->status = 0;
            $product->video = $request->video;
            $product->tags = json_encode($request->tags);
            $product->processing_time = $request->processing_time;
            $product->port = $request->port;
            $product->unit = $request->unit;
            $product->supply_ability = $request->supply_ability;
            $product->payment_method = $request->payment_method;
            $product->origin = $request->origin;
            $product->location = $request->location;
            $product->currency = $request->currency;
            $product->stock = $request->stock;
            $product->location = $request->location;
            $product->supplier_id = Auth::user()->id;

            if ($product->save()) {
                Toastr::success('Success!', 'Setting Updated!');
                return redirect()->back();


            }
        }
        $data['product'] = $product;
        $data['gallaries'] = Thumb::where('product_id', $product->id)->get();
        $data['tags'] = Tag::all();
        $data['caegories'] = Category::all();
        $data['method'] = Method::all();
        return view('user.pages.editproduct')->with($data);
    }


    public function editoffer(Request $request, $id)
    {
        $product = BuyOffer::find($id);
        if ($request->isMethod("post")) {


            $product->title = $request->title;
            $product->details = $request->des;
            $product->tags = json_encode($request->tags);
            $product->status = 0;

            if ($product->save()) {
                Toastr::success('Success!', 'Setting Updated!');
                return redirect()->back();
            }
        }
        $data['product'] = $product;
        $data['tags'] = Tag::all();
        $data['caegories'] = Category::all();
        return view('user.pages.editoffer')->with($data);
    }


    public function addoffer(Request $request)
    {

        if ($request->isMethod("post")) {

            $product = new BuyOffer();
            $product->status = 0;
            $product->ip = visitorip();
            $product->title = $request->title;
            $product->date = date('Y-m-d');
            $product->slug = Str::slug($request->title);
            $product->details = $request->des;
            $product->country_id = Auth::user()->country_id;
            if ($request->filled('child_id')) {
                $product->category_id = $request->child_id;
                $product->sub_id = $request->sub_id;
                $product->child_id = $request->category_id;
            } else {
                $product->category_id = $request->sub_id;
                $product->sub_id = $request->category_id;
            }
            $product->tags = json_encode($request->tags);
            $product->buyer_id = Auth::user()->id;

            if ($product->save()) {
                Toastr::success('Success!', 'Setting Updated!');
                return redirect()->back();
            }
        }

        $data['tags'] = Tag::all();
        $data['caegories'] = Category::all();
        return view('user.pages.addoffer')->with($data);
    }


    public function addproduct(Request $request)
    {

        if ($request->isMethod("post")) {
            //return json_encode($request->all());
            $request->validate([
                'qty' => ['nullable']
            ]);
            $product = new Product();


            if ($request->hasFile('brochure')) {

                $image = $request->file('brochure');
                $thefile = \File::get($image);
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/admin/product')) {
                    Storage::disk('public')->makeDirectory('images/admin/product');
                }
                Storage::disk('public')->put('images/admin/product/' . $imageName, $thefile);
                $product->brochure = $imageName;
            }


            if ($request->hasFile('image')) {

                $image = $request->file('image');
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/admin/product/' . $product->image)) {
                    Storage::disk('public')->makeDirectory('images/admin/product/' . $product->image);
                }
                $bannerImage = Image::make($image)->resize(500, 500)->stream();
                Storage::disk('public')->put('images/admin/product/' . $imageName, $bannerImage);
                $product->image = $imageName;
            } else {
                $imageName = "product.jpg";
            }


            if ($request->hasFile('gallary1')) {

                $image = $request->file('gallary1');
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/admin/product/' . $product->gallary1)) {
                    Storage::disk('public')->makeDirectory('images/admin/product/' . $product->gallary1);
                }
                $bannerImage = Image::make($image)->resize(500, 500)->stream();
                Storage::disk('public')->put('images/admin/product/' . $imageName, $bannerImage);
                $product->gallary1 = $imageName;
            } else {
                $imageName = "product.jpg";
            }


            if ($request->hasFile('gallary2')) {

                $image = $request->file('gallary2');
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/admin/product/' . $product->gallary2)) {
                    Storage::disk('public')->makeDirectory('images/admin/product/' . $product->gallary2);
                }
                $bannerImage = Image::make($image)->resize(500, 500)->stream();
                Storage::disk('public')->put('images/admin/product/' . $imageName, $bannerImage);
                $product->gallary2 = $imageName;
            } else {
                $imageName = "product.jpg";
            }


            if ($request->hasFile('gallary3')) {

                $image = $request->file('gallary3');
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/admin/product/' . $product->gallary3)) {
                    Storage::disk('public')->makeDirectory('images/admin/product/' . $product->gallary3);
                }
                $bannerImage = Image::make($image)->resize(500, 500)->stream();
                Storage::disk('public')->put('images/admin/product/' . $imageName, $bannerImage);
                $product->gallary3 = $imageName;
            } else {
                $imageName = "product.jpg";
            }


            if ($request->hasFile('gallary4')) {

                $image = $request->file('gallary4');
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/admin/product/' . $product->gallary4)) {
                    Storage::disk('public')->makeDirectory('images/admin/product/' . $product->gallary4);
                }
                $bannerImage = Image::make($image)->resize(500, 500)->stream();
                Storage::disk('public')->put('images/admin/product/' . $imageName, $bannerImage);
                $product->gallary4 = $imageName;
            } else {
                $imageName = "product.jpg";
            }
            if ($request->filled('child_id')) {
                $product->category_id = $request->child_id;
                $product->sub_id = $request->sub_id;
                $product->child_id = $request->category_id;
            } else {
                $product->category_id = $request->sub_id;
                $product->sub_id = $request->category_id;
            }

            $product->ip = visitorip();
            $product->title = $request->title;
            $product->date = date('Y-m-d');
            $product->slug = Str::slug($request->title);
            $product->tags = json_encode($request->tags);
            $product->model_number = $request->model_number;
            $product->brand = $request->brand;
            $product->material = $request->material;
            $product->origin = $request->origin;
            $product->price_type = $request->price_type;


            if (isset($request['outer-group'][0]['inner-group']) && is_array($request['outer-group'][0]['inner-group'])) {
                $product->attr = json_encode($request['outer-group'][0]['inner-group']);
            }

            if ($request->price_type == 'fob') {
                $product->currency = $request->currency;
                $product->min = $request->min;
                $product->max = $request->max;
                $product->unit = $request->unit;
                $product->min_qty = $request->min_qty;
            } else {

            }
            if ($request->filled('other_payment_methods')) {
                $arr = array($requet->other_payment_methods);
                $product->payment_method = json_encode(array_push($arr, $request->payment_ids));

            } else {
                $product->payment_method = json_encode($request->payment_ids);
            }
            $product->supply_type = $request->supply_type;
            $product->port = $request->port;
            $product->processing_time = $request->processing_time;
            $product->qty = $request->qty;
            $product->details = $request->des;
            $product->country_id = Auth::user()->country_id;

            $product->unit = $request->units;
            $product->shipping_time = $request->shipping_time;
            $product->details = $request->des;
            $product->supplier_id = Auth::user()->id;

            if ($product->save()) {

                Toastr::success('Success!', 'Setting Updated!');

                if (Auth::user()->verified == 1) {
                    return redirect()->back();
                    //   return redirect()->route('user.featured.submit', $product->id);
                }
                return redirect()->back();
            }
        }

        $data['tags'] = Tag::all();
        $data['caegories'] = Category::where('parent_id', 0)->get();
        $data['method'] = Method::all();
        return view('user.pages.addproducts')->with($data);
    }

    public function setting(Request $request)
    {
        if ($request->isMethod("post")) {
            // dd($request->type);
            $company = Company::where('id', Auth::user()->company->id)->first();
            $company->des = $request->des;
            $company->overview = $request->overview;
            $company->name = $request->name;
            if ($request->filled('type')) {
                $company->type = implode(',', $request->type);
            }
            $company->main_products = json_encode($request->main_products);
            $company->address = $request->address;
            $company->phone = $request->phone;
            $company->total_staff = $request->total_staff;
            $company->employee = $request->employee;
            $company->revenue = $request->revenue;
            $company->location = $request->location;
            $company->web = $request->web;
            $company->year = $request->year;
            $company->main_market = $request->main_market;
            $company->youtube = $request->youtube;
            //   $company->conduct = $request->conduct;
            //   $company->patent = $request->patent;
            //   $company->awards = $request->awards;
            //   $company->equipment = $request->equipment;
            $company->story = $request->story;
            $company->meta_author = $request->meta_author;
            $company->meta_title = $request->meta_title;
            $company->meta_description = $request->meta_description;
            $company->meta_tage = $request->meta_tage;


            if ($request->hasFile('image')) {
                $image = $request->file('image');
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (Storage::disk('public')->exists('images/user/avatar/' . $request->image)) {
                    Storage::disk('public')->delete('images/user/avatar/' . $request->image);
                }
                $bannerImage = Image::make($image)->resize(170, 170)->stream();
                Storage::disk('public')->put('images/user/avatar/' . $imageName, $bannerImage);
                $company->image = $imageName;
            }
            $company->save();
            Toastr::success('Success!', 'Setting Updated!');
            return redirect()->back();
        }
        $data['company'] = Company::where('id', Auth::user()->company->id)->first();

        return view('user.pages.setting')->with($data);

    }


    public function featuredsubmit($id, Request $request)
    {
        $product = Product::findorFail($id);
        if ($request->isMethod("post")) {
            $featured = new FeaturedRequest();
            $featured->product_id = $product->id;
            $featured->user_id = Auth::user()->id;
            $featured->notes = $request->notes;
            if ($featured->save()) {
                return redirect()->route('user.featuredlist');
            }
        }

        return view('user.pages.submitfeatured', compact('product'));
    }

    public function featured_product_add(Request $request)
    {
        dd($request);
    }


    public function featuredlist(Request $request)
    {
        $products = FeaturedRequest::where('user_id', Auth::user()->id)->latest();
        if ($request->filled('status')) {
            $products = $product->where('status', $request->status);
        }
        $products = $products->paginate(50);
        return view('user.pages.featuredlist', compact('products'));
    }

    public function activefeatureProfuctlist(Request $request)
    {
        $products = FeaturedProduct::where('user_id', Auth::user()->id)->latest();
        $products = $products->paginate(20);
        return view('user.pages.feature.active_product', compact('products'));
    }

    public function featuredlistdelete($id)
    {
        $product = FeaturedProduct::where('id', $id)->where('user_id', Auth::user()->id)->firstOrFail();
        $product->delete();
        Toastr::success('Delete!', 'Feature Product Deleted Successfully !!');
        return back();
    }


    public function profile_view()
    {
        return view('user.pages.profile_view');
    }


    public function basic_infirmation(Request $request)
    {

        $user = User::find(Auth::guard('web')->user()->id);
        $user->name = $request->name;
        $user->industry = $request->industry;
        $user->category_id = $request->industry;
        $user->address = $request->address;
        $user->city = $request->city;
        $user->products = $request->products;
        $user->gender = $request->gender;
        $user->postal = $request->postal;
        $user->state = $request->state;
        $user->country_id = $request->country_id;
        $user->type = $request->type;
        $user->qq = $request->qq;
        $user->skype = $request->skype;
        $user->wechat = $request->wechat;
        $user->whatsapp = $request->whatsapp;
        if ($request->hasFile('image')) {
            $image = $request->file('image');
            $currentDate = Carbon::now()->toDateString();
            $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
            if (Storage::disk('public')->exists('images/user/avatar/' . $request->image)) {
                Storage::disk('public')->delete('images/user/avatar/' . $request->image);
            }
            $bannerImage = Image::make($image)->resize(900, 800)->stream();
            Storage::disk('public')->put('images/user/avatar/' . $imageName, $bannerImage);
            $user->image = $imageName;
        }
        if ($user->save()) {
            Toastr::success('Success!', 'Setting Updated!');
            return redirect()->route('user.profile.view');
        }
        return print_r($request->all());
    }


    public function feature_delete_by_product($id)
    {

        $feature_requests = FeaturedRequest::where('user_id', $id)->get();
        if ($feature_requests) {
            foreach ($feature_requests as $feature_request) {
                $feature_request->delete();
            }
        }


        $feature_products = FeaturedProduct::where('user_id', $id)->latest()->get();
        foreach ($feature_products as $feature_product) {
            $feature_product->delete();
        }
    }

    public function buy_offer_delete_by_product($id)
    {
        $buy_offers = BuyOffer::where('buyer_id', $id)->orWhere('seller_id', $id)->get();
        if ($buy_offers) {
            foreach ($buy_offers as $buy_offer) {
                $buy_offer->delete();
            }
        }
    }

    public function activebuy_advertiser()
    {
        $data['advertisers'] = AdvertiserOrder::where('user_id', Auth::id())->where('status', 'active')->whereNotNull('start_date')->latest()->get();
        return view('user.pages.advertiser.active')->with($data);
    }

    public function my_pending_buy_advertiser()
    {
        $data['advertisers'] = AdvertiserOrder::where('user_id', Auth::id())->where('status', 'pending')->latest()->get();
        return view('user.pages.advertiser.pending')->with($data);
    }

    public function my_all_buy_advertiser()
    {
        $data['advertisers'] = AdvertiserOrder::where('user_id', Auth::id())->latest()->get();
        return view('user.pages.advertiser.index')->with($data);
    }

    public function advertiserInvoiceDetails($id)
    {
        $invoice = AdvertiserOrder::with('user:id,name,industry,email,package_id,type,address,products,country_id')->where('id', $id)->firstOrFail();
        return view('user.pages.advertiser.invoice', compact('invoice'));
    }

    public function advertiserMoneyReceipt($id)
    {
        $money_receipt = AdvertiserOrder::where('id', $id)->firstOrFail();
        return view('user.pages.advertiser.money_receipt', compact('money_receipt'));
    }

    public function advertiserPostpaidPayment($id)
    {
        $invoice = AdvertiserOrder::with('user:id,name,phone,industry,email,package_id,type,address,products,country_id')->where('id', $id)->firstOrFail();
        $user = $invoice->user;
        $info = array(
            'currency' => 'BDT',
            'amount' => intval($invoice->amount),
            'order_id' => $invoice->order_id,
            'discsount_amount' => 0,
            'disc_percent' => 0,
            'client_ip' => 'N\L',
            'customer_name' => $user->name,
            'customer_phone' => $user->phone,
            'email' => $user->email,
            'customer_address' => "N\L",
            'customer_city' => "N/L",
            'customer_state' => "N/L",
            'customer_postcode' => "N/L",
            'customer_country' => Country::find($user->country_id)->name
        );
        $order = [
            'order_id' => $invoice->order_id,
            'order_for' => 'advertiser'
        ];
        session(['orderInfo' => $order]);
        $shurjopay_service = new ShurjopayController();
        return $shurjopay_service->checkout($info);
    }

}