<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\CIQCompany;
use App\Quote;
use App\QuoteProduct;
use App\product_category;
use DB;
use PDF;
use Validator;

class QuoteController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $quotes = Quote::get();
        return view('Admin.company_quote.index', compact('quotes'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $qoute_no = Quote::count();
        $qoute_no = date('Y').($qoute_no + 1);

        $companies = CIQCompany::where('status', 1)->get();
        $products = DB::table('product_productinfo')->where('status', 1)->get();
        $categories = product_category::orderBy('category_name')->get();
        return view('Admin.company_quote.create', compact('companies', 'products', 'qoute_no', 'categories'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
          'company_id' => 'required',
          'quote_no' => 'required|unique:quotes',
          'date' => 'required',
          'total' => 'required',
          'unit_price.*' => 'required',
          'product_id.*' => 'required',
          'qty.*' => 'required',
        ]);

        if ($validator->fails()) {
          return redirect()->back()
                   ->withErrors($validator)
                   ->withInput();
        }

        $data = new Quote();
        $data->company_id = $request->company_id;
        $data->quote_no = $request->quote_no;
        $data->date = $request->date;
        $data->total = $request->total;
        $data->status = 1;
        $data->save();

        $category_id= $request->category_id;
        $product_id= $request->product_id;
        $qty= $request->qty;
        $unit_price= $request->unit_price;
        if ($product_id) {
            foreach ($category_id as $key => $value ){
                if(!empty($value)){
                    $product = DB::table('product_productinfo')->where('id', $product_id[$key])->first();

                    $com_inv_pro= new QuoteProduct();
                    $com_inv_pro->quote_id=$data->id;
                    $com_inv_pro->category_id=$value;
                    $com_inv_pro->product_id=$product_id[$key];
                    $com_inv_pro->qty =$qty[$key];
                    $com_inv_pro->description =$product->product_name;
                    $com_inv_pro->unit_price =$unit_price[$key];
                    $com_inv_pro->save(); 
                }
            }   
        }

        $notification=array(
            'messege'   =>'Quote Added Successfull',
            'alert-type'=>'success'
        );

        return redirect()->back()->with($notification); 
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $data = Quote::find($id);
        $data_items = QuoteProduct::where('quote_id', $id)->get();
        $company = CIQCompany::where('id', $data->company_id)->first();
        $pdf = PDF::loadView('Admin.company_quote.quote_pdf',  $data , compact('data', 'company', 'data_items'));
        return $pdf->stream($company->name.'-Quotation-'.$data->date.'.pdf', compact('data'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $companies = CIQCompany::where('status', 1)->get();
        $products = DB::table('product_productinfo')->orderBy('product_name')->where('status', 1)->get();
        $data = Quote::find($id);
        $data_items = QuoteProduct::where('quote_id', $id)->get();
        $company = CIQCompany::where('id', $data->company_id)->first();
        $categories = product_category::orderBy('category_name')->get();
        return view('Admin.company_quote.edit', compact('companies', 'products', 'data', 'data_items', 'company', 'categories'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = Quote::findOrFail($id);
        $data->company_id = $request->company_id;
        $data->quote_no = $request->quote_no;
        $data->date = $request->date;
        $data->total = $request->total;
        $data->status = 1;
        $data->save();

        QuoteProduct::where('quote_id', $id)->delete();
        $category_id= $request->category_id;
        $product_id= $request->product_id;
        $qty= $request->qty;
        $unit_price= $request->unit_price;
        if ($product_id) {
            foreach ($category_id as $key => $value ){
                if(!empty($value)){
                    $product = DB::table('product_productinfo')->where('id', $product_id[$key])->first();

                    $com_inv_pro= new QuoteProduct();
                    $com_inv_pro->quote_id=$data->id;
                    $com_inv_pro->category_id=$value;
                    $com_inv_pro->product_id=$product_id[$key];
                    $com_inv_pro->qty =$qty[$key];
                    $com_inv_pro->description =$product->product_name;
                    $com_inv_pro->unit_price =$unit_price[$key];
                    $com_inv_pro->save(); 
                }
            }   
        }

        $notification=array(
            'messege'   =>'Quote Update Successfull',
            'alert-type'=>'success'
        );

        return redirect()->back()->with($notification); 
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = Quote::findOrFail($id);
        $data->delete();
        QuoteProduct::where('quote_id', $id)->delete();
        $notification = array(
            'messege'   => 'Delete Successfull',
            'alert-type' => 'error'
        );
        return redirect()->back()->with($notification);
    }
}
