<?php

namespace App\Http\Controllers\Vendor;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
use Session;
use Mail;
use Auth;
use Hash;

class ShopController extends Controller
{
    // Orders /////////////////////////////////////
    public function pendOrd()
    {
        $data['vend_data'] = DB::table('vendors')->where('id', Auth::guard('vendor')->user()->id)->first();
        $data['orders'] = DB::table('orders')->where('vend_id', $data['vend_data']->id) ->where('status', 0)->orderBy('created_at', 'asc')->get();

        return view('vendor.pendord', $data);
    }

    public function paidOrd()
    {

        $data['orders'] = DB::table('orders')->where('status', 1)->orderBy('created_at', 'desc')->get();

        return view('vendor.paidord', $data);
    }

    public function invoice($id)
    {
        $data['ord_data'] = DB::table('orders')->where('id', $id)->first();
        $data['user_data'] = DB::table('members')->where('username', $data['ord_data']->username)->first();
        $data['pro_data'] = DB::table('orders_products')->where('order_id', $data['ord_data']->id)->get();
        $data['shop'] = DB::table('shops')->where('id', $data['ord_data']->shop_id)->first();

        return view('vendor.invoice', $data);
    }

    

    public function ordMng($id)
    {

        $data['ord_data'] = DB::table('orders')->where('id', $id)->first();
        
        return view('vendor.ordmng', $data);
    }

    public function ordProcess(Request $request)
    {
        if ($request->isMethod('post')) {
            $data = $request->all();

            if (!empty($data['status'])) {
                $vendor = DB::table('vendors')->where('id', Auth::guard('vendor')->user()->id)->first();
                if (Hash::check($data['pin'], $vendor->pin)) {
                    $ord_data = DB::table('orders')->where('id', $data['ord_id'])->first();
                    $data1['status'] = 1;
                    DB::table('orders')->where('id', $ord_data->id)->update($data1);

                    toastr()->success('Order Completed Successfully!!');
                    return redirect('vendor/pendord');
                }else{
                    toastr()->error('Invalid Security PIN!');
                    return redirect()->back(); 
                }
            }else{
                toastr()->error('Select Order Status!');
                return redirect()->back();
            }
        }
    }
}
