<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Contracts\Encryption\DecryptException;
//use Illuminate\Support\Facades\Crypt;
use App\Models\UserActivity;
use DB;
use Auth;

class MypostadController extends Controller
{
    
    public function index(Request $request)
    {
         $dataLog            = new UserActivity;
         $dataLog->subject   = "users post-ad page";
         $dataLog->current_url = url()->current();
         $dataLog->previous_url = url()->previous();
         $dataLog->from_ip   = $request->ip();
         $dataLog->user_detail_Id = Auth::user()->id;
         $dataLog->save();

        $categories=DB::table('listing_category')->where('cat_status',1)->where('del_status',1)->get();
        $subcategories=DB::table('listing_subcategory')->where('subcat_status',1)->where('del_subcat_status',1)->get();

        $countrys=DB::table('countrys')->where('del_status',1)->get();
        $states=DB::table('states')->where('del_status',1)->get();
        $citys=DB::table('citys')->where('del_status',1)->get();
        return view('users.post-ad', compact('citys','states','countrys','categories','subcategories'));
    }
    public function addpost(Request $request)
    {
       // dd($request->all());
         $dataLog            = new UserActivity;
         $dataLog->subject   = "users add new post-ad page";
         $dataLog->current_url = url()->current();
         $dataLog->previous_url = url()->previous();
         $dataLog->from_ip   = $request->ip();
         $dataLog->user_detail_Id = Auth::user()->id;
         $dataLog->save();

        $catid=$request->category;
        $subcatid = $request->subcategory;
        $contid=Auth::user()->country_name;
        $statid= Auth::user()->state_name; //$request->state;
        $cityid = Auth::user()->city_name; //$request->city;

        $cat=DB::table('listing_category')->where('cat_id',$catid)->first();
        $subcat=DB::table('listing_subcategory')->where('subcat_id', $subcatid)->first();
        $countrys=DB::table('countrys')->where('country_name',$contid)->first();
        $states=DB::table('states')->where('state_name', $statid)->first();
        $citys=DB::table('citys')->where('city_name',$cityid)->first();
        //dd($statid);



        //$add1=$request->address1;
        $add1 = str_replace(' ','_',Auth::user()->address);
        //$add2 = str_replace(' ','_',$request->address2);
        $seo1=$cat->seo_cat.'_'.$subcat->seo_subcat.'_'.$add1; //.'_'.$add2;
        $file = $request->file('upload');
        //Display File Name
        $filename=$file->getClientOriginalName();
        //Display File Extension
        //echo 'File Extension: '.$file->getClientOriginalExtension();
        //echo '<br>';
        //Display File Real Path
        //echo 'File Real Path: '.$file->getRealPath();
        //echo '<br>';
        //Display File Size
        //echo 'File Size: '.$file->getSize();
        //echo '<br>';
        //Display File Mime Type
        //echo 'File Mime Type: '.$file->getMimeType();
        //Move Uploaded File
        $destinationPath = 'public/uploads';
        $file->move($destinationPath,$file->getClientOriginalName());
        //$file->move(public_path('uploads'), $fileName);
        $filepath = 'uploads/'.$filename;

        DB::table('listing_main')->insert([
            'list_cat_id' => $catid,
            'list_cat_name' => $cat->cat_name,
            'list_subcat_id' => $subcatid,
            'list_subcat_name' => $subcat->subcat_name,
            'list_heading' => $request->title,
            'list_image' => $filepath,
            'list_price' => $request->price,
            'list_address' => Auth::user()->address,// $request->address1.' '.$request->address2,
            'list_description' => $request->description,
            'list_contact_name' => $request->fname.' '.$request->lname,
            'list_contact_no' => Auth::user()->phone, // $request->phone,
            'list_seo_url' => $seo1,
            'list_seo_url1' => $seo1,

            'country' => $countrys->countryid, //$request->country,
            'state' => $states->stateid,//$request->state,
            'city' => $citys->cityid,//$request->city,

            'country_name' => Auth::user()->country_name,// $countrys->country_name,
            'state_name' => Auth::user()->state_name,//$states->state_name,
            'city_name' => Auth::user()->city_name, //$citys->city_name,

            'user_email' => Auth::user()->email,
        ]);
        return redirect('/my-ads');
        //dd('save');
    }
    public function getSubcategories(Request $request)
    { 
       // dd($request->all());
        $category_id = $request->input('category_id');
        // Fetch subcategories where parent_id matches the selected category_id
        $subcategories = DB::table('listing_subcategory')->where('cat_id', $category_id)->get();
        
        return response()->json($subcategories); // Return data as JSON
    }



    public function myads(Request $request)
    {
         $dataLog            = new UserActivity;
         $dataLog->subject   = "users post-ad page";
         $dataLog->current_url = url()->current();
         $dataLog->previous_url = url()->previous();
         $dataLog->from_ip   = $request->ip();
         $dataLog->user_detail_Id = Auth::user()->id;
         $dataLog->save();

        $datas = DB::table('listing_main')->where('list_del',1)->where('user_email',Auth::user()->email)->get();
        $totalads = DB::table('listing_main')->where('list_del',1)->where('user_email',Auth::user()->email)->count();
        return view('users.my-ads', compact('datas','totalads'));
    }
    public function postdel(Request $request)
    {
        //dd($request->all());
         $dataLog            = new UserActivity;
         $dataLog->subject   = "users delete post-ad page";
         $dataLog->current_url = url()->current();
         $dataLog->previous_url = url()->previous();
         $dataLog->from_ip   = $request->ip();
         $dataLog->user_detail_Id = Auth::user()->id;
         $dataLog->save();

        $id=Crypt::decryptString($request->postid);
        DB::table('listing_main')->where('list_id',$id)->update([
            'list_del' => 0,
        ]);
        $request->session()->flash('message', 'Post Delete successful!');

        return redirect('/my-ads');
    }
    public function postedit(Request $request)
    {
         $dataLog            = new UserActivity;
         $dataLog->subject   = "users edit post-ad page";
         $dataLog->current_url = url()->current();
         $dataLog->previous_url = url()->previous();
         $dataLog->from_ip   = $request->ip();
         $dataLog->user_detail_Id = Auth::user()->id;
         $dataLog->save();

        $id=Crypt::decryptString($request->postid);
        $data = DB::table('listing_main')->where('list_id',$id)->where('user_email',Auth::user()->email)->first();

        $categories=DB::table('listing_category')->where('cat_status',1)->where('del_status',1)->get();
        $subcategories=DB::table('listing_subcategory')->where('subcat_status',1)->where('del_subcat_status',1)->get();

        $countrys=DB::table('countrys')->where('del_status',1)->get();
        $states=DB::table('states')->where('del_status',1)->get();
        $citys=DB::table('citys')->where('del_status',1)->get();
        return view('users.post-ad-edit', compact('citys','states','countrys','categories','subcategories','data'));
    }
    public function postupdate(Request $request)
    {
       // dd($request->all());
         $dataLog            = new UserActivity;
         $dataLog->subject   = "users post-ad update page";
         $dataLog->current_url = url()->current();
         $dataLog->previous_url = url()->previous();
         $dataLog->from_ip   = $request->ip();
         $dataLog->user_detail_Id = Auth::user()->id;
         $dataLog->save();

        $id=Crypt::decryptString($request->postid);
        $file = $request->file('upload');
        $catid=$request->category;
        $subcatid = $request->subcategory;
        $cat=DB::table('listing_category')->where('cat_id',$catid)->first();
        $subcat=DB::table('listing_subcategory')->where('subcat_id', $subcatid)->first();
        $contid=Auth::user()->country_name;
        $statid= Auth::user()->state_name; //$request->state;
        $cityid = Auth::user()->city_name; //$request->city;
        $countrys=DB::table('countrys')->where('country_name',$contid)->first();
        $states=DB::table('states')->where('state_name', $statid)->first();
        $citys=DB::table('citys')->where('city_name',$cityid)->first();

        $add1 = str_replace(' ','_',Auth::user()->address);
        //$add2 = str_replace(' ','_',$request->address2);
        $seo1=$cat->seo_cat.'_'.$subcat->seo_subcat.'_'.$add1; //.'_'.$add2;

        if(!empty($file))
        {
            $filename=$file->getClientOriginalName();
            $destinationPath = 'public/uploads';
            $file->move($destinationPath,$file->getClientOriginalName());
            //$file->move(public_path('uploads'), $fileName);
            $filepath = 'uploads/'.$filename;

             DB::table('listing_main')->where('list_id',$id)->update([
            //'list_del' => 0,
            'list_cat_id' => $catid,
            'list_cat_name' => $cat->cat_name,
            'list_subcat_id' => $subcatid,
            'list_subcat_name' => $subcat->subcat_name,
            'list_heading' => $request->title,
            'list_image' => $filepath,
            'list_price' => $request->price,
            'list_address' => Auth::user()->address,// $request->address1.' '.$request->address2,
            'list_description' => $request->description,
            'list_contact_name' => $request->fname.' '.$request->lname,
            'list_contact_no' => Auth::user()->phone, // $request->phone,
            'list_seo_url' => $seo1,
            'list_seo_url1' => $seo1,

            'country' => $countrys->countryid, //$request->country,
            'state' => $states->stateid,//$request->state,
            'city' => $citys->cityid,//$request->city,

            'country_name' => Auth::user()->country_name,// $countrys->country_name,
            'state_name' => Auth::user()->state_name,//$states->state_name,
            'city_name' => Auth::user()->city_name, //$citys->city_name,

            'user_email' => Auth::user()->email,
        ]);

        }else{
            DB::table('listing_main')->where('list_id',$id)->update([
            //'list_del' => 0,
            'list_cat_id' => $catid,
            'list_cat_name' => $cat->cat_name,
            'list_subcat_id' => $subcatid,
            'list_subcat_name' => $subcat->subcat_name,
            'list_heading' => $request->title,
            'list_price' => $request->price,
            'list_address' => Auth::user()->address,// $request->address1.' '.$request->address2,
            'list_description' => $request->description,
            'list_contact_name' => $request->fname.' '.$request->lname,
            'list_contact_no' => Auth::user()->phone, // $request->phone,
            'list_seo_url' => $seo1,
            'list_seo_url1' => $seo1,

            'country' => $countrys->countryid, //$request->country,
            'state' => $states->stateid,//$request->state,
            'city' => $citys->cityid,//$request->city,

            'country_name' => Auth::user()->country_name,// $countrys->country_name,
            'state_name' => Auth::user()->state_name,//$states->state_name,
            'city_name' => Auth::user()->city_name, //$citys->city_name,

            'user_email' => Auth::user()->email,
        ]);
        
        }
        $request->session()->flash('success', 'Post update successful!');

        return redirect('/my-ads');
    }
    public function offers(Request $request)
    {
        return view('users.my-ads');
    }
    public function payments(Request $request)
    {
        return view('users.payments');
    }
    public function favorites(Request $request)
    {
        return view('users.favorites');
    }
    public function privacy(Request $request)
    {
        return view('users.privacy');
    }
    public function productdetails(Request $request)
    {
        return view('users.product-details');
    }
    public function category(Request $request)
    {
        return view('users.category');
    }
    public function categorylist(Request $request)
    {
        return view('users.category-list');
    }
}
