<?php

namespace App\Http\Controllers\Admin;

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

class ProductController extends Controller
{
    public function categories()
    {
        $data['categories'] = DB::table('categories')->get();
        return view('admin.categories', $data);
    }

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

            $rules = [
                'cat_name' => 'required',
                'icon' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            ];
                
            $customMessages = [
                'cat_name.required' => 'Category Name is Required',
                'icon.required' => 'Choose a Icon',
            ];

            $this->validate($request,$rules,$customMessages);

            $data1['cat_name'] = $data['cat_name'];
            $data1['items'] = 0;
            $data1['status'] = 0;
            $icon = $request->file('icon');
                          
            $icon_name = hexdec(uniqid());
            $ext = strtolower($icon->getClientOriginalExtension());
            $icon_full_name = $icon_name.'.'.$ext;
            $upload_path = 'web/uploads/icon/';
            $icon_url = $upload_path.$icon_full_name;
            $success = $icon->move($upload_path,$icon_full_name);
            $data1['icon'] = $icon_url;

            DB::table('categories')->insert($data1);

            Session::flash('success_msg','Categories Added Successfully!');
            return redirect()->back();
                
          


        }
    }

    public function catedit($id)
    {
        $data['cat_data'] = DB::table('categories')->where('id', $id)->first();
        return view('admin.catedit', $data);
    }

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

            $rules = [
                'cat_name' => 'required',
            ];
                
            $customMessages = [
                'cat_name.required' => 'Category Name is Required',
            ];

            $this->validate($request,$rules,$customMessages);

            $cat_data = DB::table('categories')->where('id', $data['cat_id'])->first();

            $data1['cat_name'] = $data['cat_name'];
            $data1['status'] = $data['status'];
            $icon = $request->file('icon');
            if ($icon) {
                if (!is_null($cat_data->icon)) {
                    unlink($cat_data->icon);
                }

                $icon_name = hexdec(uniqid());
                $ext = strtolower($icon->getClientOriginalExtension());
                $icon_full_name = $icon_name.'.'.$ext;
                $upload_path = 'web/uploads/icon/';
                $icon_url = $upload_path.$icon_full_name;
                $success = $icon->move($upload_path,$icon_full_name);
                $data1['icon'] = $icon_url;

                DB::table('categories')->where('id', $cat_data->id)->update($data1);
                
                Session::flash('success_msg','Categories Updated Successfully!');
                return redirect('admin/categories');
            }else{
                DB::table('categories')->where('id', $cat_data->id)->update($data1);
                
                Session::flash('success_msg','Categories Updated Successfully!');
                return redirect('admin/categories');
            }  

        }
    }

    public function addpro()
    {
        $data['categories'] = DB::table('categories')->get();
        return view('admin.addpro', $data);
    }

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

            $data1['title'] = $data['title'];
            $data1['cat_id'] = $data['cat_id'];
            $data1['ptype'] = $data['ptype'];
            $data1['buy_price'] = $data['buying_price'];
            $data1['price'] = $data['selling_price'];
            $data1['regu_price'] = $data['regular_price'];
            $data1['point'] = $data['pv'];
            $data1['summary'] = $data['summary'];
            $data1['details'] = $data['details'];
            $data1['stock'] = $data['stock'];
            $data1['status'] = $data['type'];
            $image = $request->file('image');
           
            $image_name = hexdec(uniqid());
            $extention = strtolower($image->getClientOriginalExtension());
            $image_full_name = $image_name.'.'.$extention;
            $upload_path = 'web/uploads/product_image/';
            $image_url = $upload_path.$image_full_name;
            $success = $image->move($upload_path,$image_full_name);
            $data1['image'] = $image_url;
            DB::table('products')->insert($data1);

            $cat_data = DB::table('categories')->where('id', $data['cat_id'])->first();
            $data2['items'] = $cat_data->items+1;
            DB::table('categories')->where('id', $cat_data->id)->update($data2);
                
            toastr()->success('Product Uploaded Successfully!');
            return redirect()->back();

        }
    }

    public function proList()
    {
        $data['products'] = DB::table('products')->orderBy('created_at', 'desc')->get();
        return view('admin.products', $data);
    }

    public function proMng($id)
    {
        $data['pro_data'] = DB::table('products')->where('id', $id)->first();
        $data['cat_data'] = DB::table('categories')->where('id', $data['pro_data']->cat_id)->first();
        $data['category'] = DB::table('categories')->where('status', 0)->where('id', '!=', $data['pro_data']->cat_id)->get();
        return view('admin.promng', $data);
    }

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

            $pro_data = DB::table('products')->where('id', $data['pro_id'])->first();

            if ($pro_data->cat_id != $data['cat_id']) {
                $cat_data = DB::table('categories')->where('id', $pro_data->cat_id)->first();
                $cdata1['items'] = $cat_data->items-1;
                DB::table('categories')->where('id', $cat_data->id)->update($cdata1);

                $new_cat = DB::table('categories')->where('id', $data['cat_id'])->first();
                $cdata2['items'] = $new_cat->items+1;
                DB::table('categories')->where('id', $new_cat->id)->update($cdata2);
            }

            $data1['title'] = $data['title'];
            $data1['cat_id'] = $data['cat_id'];
            $data1['buy_price'] = $data['buying_price'];
            $data1['price'] = $data['selling_price'];
            $data1['regu_price'] = $data['regular_price'];
            $data1['point'] = $data['pv'];
            $data1['summary'] = $data['summary'];
            $data1['details'] = $data['details'];
            $data1['stock'] = $data['stock'];
            $data1['status'] = $data['type'];
            $image = $request->file('image');
            if ($image) {
                unlink($pro_data->image);
                $image_name = hexdec(uniqid());
                $extention = strtolower($image->getClientOriginalExtension());
                $image_full_name = $image_name.'.'.$extention;
                $upload_path = 'web/uploads/product_image/';
                $image_url = $upload_path.$image_full_name;
                $success = $image->move($upload_path,$image_full_name);
                $data1['image'] = $image_url;
            }

            DB::table('products')->where('id', $pro_data->id)->update($data1);

            toastr()->success('Product Updated Successfully!');
            return redirect('admin/products');
        }
    }
}
