<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\OtherPageItem;
use App\Models\CustomPage;
use Illuminate\Validation\Rule;

class AdminOtherPageItemController extends Controller
{
    public function index()
    {
        $other_page_items = OtherPageItem::where('id',1)->first();
        $custom_pages = CustomPage::orderBy('id','asc')->get();
        return view('admin.other_page.index', compact('other_page_items','custom_pages'));
    }

    public function about_update(Request $request)
    {
        if(env('PROJECT_MODE') == 0) {
            return redirect()->back()->with('info', env('PROJECT_NOTIFICATION'));
        }
        
        $request->validate([
            'page_about_service_how_many' => ['numeric'],
            'page_about_team_members_how_many' => ['numeric'],
        ],[
            'page_about_service_how_many.numeric' => __('The service how many must be a number'),
            'page_about_team_members_how_many.numeric' => __('The team member how many must be a number'),
        ]);

        $obj = OtherPageItem::where('id', 1)->first();

        if($request->page_about_welcome_photo1 != null) {
            $request->validate([
                'page_about_welcome_photo1' => 'mimes:jpg,jpeg,png',
            ],[
                'page_about_welcome_photo1.mimes' => __('Photo must be jpeg, png, jpg or gif'),
            ]);

            if($obj->page_about_welcome_photo1!=null) {
                unlink(public_path('uploads/'.$obj->page_about_welcome_photo1));
            }
            
            $final_name1 = 'page_about_welcome_photo1_'.time().'.'.$request->page_about_welcome_photo1->extension();
            $request->page_about_welcome_photo1->move(public_path('uploads'), $final_name1);
            $obj->page_about_welcome_photo1 = $final_name1;
        }

        if($request->page_about_welcome_photo2 != null) {
            $request->validate([
                'page_about_welcome_photo2' => 'mimes:jpg,jpeg,png',
            ],[
                'page_about_welcome_photo2.mimes' => __('Photo must be jpeg, png, jpg or gif'),
            ]);

            if($obj->page_about_welcome_photo2!=null) {
                unlink(public_path('uploads/'.$obj->page_about_welcome_photo2));
            }
            
            $final_name2 = 'page_about_welcome_photo2_'.time().'.'.$request->page_about_welcome_photo2->extension();
            $request->page_about_welcome_photo2->move(public_path('uploads'), $final_name2);
            $obj->page_about_welcome_photo2 = $final_name2;
        }

        if($request->page_about_portfolio_background != null) {
            $request->validate([
                'page_about_portfolio_background' => 'mimes:jpg,jpeg,png',
            ],[
                'page_about_portfolio_background.mimes' => __('Photo must be jpeg, png, jpg or gif'),
            ]);

            if($obj->page_about_portfolio_background!=null) {
                unlink(public_path('uploads/'.$obj->page_about_portfolio_background));
            }
            
            $final_name3 = 'page_about_portfolio_background_'.time().'.'.$request->page_about_portfolio_background->extension();
            $request->page_about_portfolio_background->move(public_path('uploads'), $final_name3);
            $obj->page_about_portfolio_background = $final_name3;
        }

        $obj->page_about_title = strip_tags($request->page_about_title);
        $obj->page_about_seo_title = strip_tags($request->page_about_seo_title);
        $obj->page_about_seo_meta_description = sanitize_textarea($request->page_about_seo_meta_description);
        $obj->page_about_welcome_subheading = strip_tags($request->page_about_welcome_subheading);
        $obj->page_about_welcome_heading = sanitize_textarea($request->page_about_welcome_heading);
        $obj->page_about_welcome_text = sanitize_textarea($request->page_about_welcome_text);
        $obj->page_about_welcome_list1 = strip_tags($request->page_about_welcome_list1);
        $obj->page_about_welcome_list2 = strip_tags($request->page_about_welcome_list2);
        $obj->page_about_welcome_list3 = strip_tags($request->page_about_welcome_list3);
        $obj->page_about_welcome_button_text = strip_tags($request->page_about_welcome_button_text);
        $obj->page_about_welcome_button_url = strip_tags($request->page_about_welcome_button_url);
        $obj->page_about_welcome_experience_years = strip_tags($request->page_about_welcome_experience_years);
        $obj->page_about_welcome_phone = strip_tags($request->page_about_welcome_phone);
        $obj->page_about_welcome_status = strip_tags($request->page_about_welcome_status);
        $obj->page_about_service_heading = sanitize_textarea($request->page_about_service_heading);
        $obj->page_about_service_subheading = strip_tags($request->page_about_service_subheading);
        $obj->page_about_service_how_many = strip_tags($request->page_about_service_how_many);
        $obj->page_about_service_status = strip_tags($request->page_about_service_status);
        $obj->page_about_portfolio_work_heading = strip_tags($request->page_about_portfolio_work_heading);
        $obj->page_about_portfolio_work_number = strip_tags($request->page_about_portfolio_work_number);
        $obj->page_about_portfolio_work_text = sanitize_textarea($request->page_about_portfolio_work_text);
        $obj->page_about_portfolio_heading = strip_tags($request->page_about_portfolio_heading);
        $obj->page_about_portfolio_subheading = strip_tags($request->page_about_portfolio_subheading);
        $obj->page_about_portfolio_how_many = strip_tags($request->page_about_portfolio_how_many);
        $obj->page_about_portfolio_status = strip_tags($request->page_about_portfolio_status);
        $obj->page_about_client_status = strip_tags($request->page_about_client_status);
        
        $obj->update();

        return redirect()->back()->with('success', __('Data is updated successfully'));
    }

    public function team_member_update(Request $request)
    {
        if(env('PROJECT_MODE') == 0) {
            return redirect()->back()->with('info', env('PROJECT_NOTIFICATION'));
        }
        
        $obj = OtherPageItem::where('id', 1)->first();
        $obj->page_team_members_title = strip_tags($request->page_team_members_title);
        $obj->page_team_members_seo_title = strip_tags($request->page_team_members_seo_title);
        $obj->page_team_members_seo_meta_description = sanitize_textarea($request->page_team_members_seo_meta_description);
        $obj->update();

        return redirect()->back()->with('success', __('Data is updated successfully'));
    }

    public function team_member_single_update(Request $request) 
    {
        if(env('PROJECT_MODE') == 0) {
            return redirect()->back()->with('info', env('PROJECT_NOTIFICATION'));
        }
        
        $obj = OtherPageItem::where('id', 1)->first();
        $obj->page_team_member_experience_heading = strip_tags($request->page_team_member_experience_heading);
        $obj->page_team_member_experience_status = strip_tags($request->page_team_member_experience_status);
        $obj->page_team_member_contact_form_subheading = strip_tags($request->page_team_member_contact_form_subheading);
        $obj->page_team_member_contact_form_heading = sanitize_textarea($request->page_team_member_contact_form_heading);
        $obj->page_team_member_contact_form_status = strip_tags($request->page_team_member_contact_form_status);
        $obj->update();

        return redirect()->back()->with('success', __('Data is updated successfully'));
    }
    

    public function testimonial_update(Request $request)
    {
        if(env('PROJECT_MODE') == 0) {
            return redirect()->back()->with('info', env('PROJECT_NOTIFICATION'));
        }
        
        $obj = OtherPageItem::where('id', 1)->first();
        $obj->page_testimonials_title = strip_tags($request->page_testimonials_title);
        $obj->page_testimonials_seo_title = strip_tags($request->page_testimonials_seo_title);
        $obj->page_testimonials_seo_meta_description = sanitize_textarea($request->page_testimonials_seo_meta_description);
        $obj->update();

        return redirect()->back()->with('success', __('Data is updated successfully'));
    }

    public function pricing_update(Request $request)
    {
        if(env('PROJECT_MODE') == 0) {
            return redirect()->back()->with('info', env('PROJECT_NOTIFICATION'));
        }
        
        $obj = OtherPageItem::where('id', 1)->first();
        $obj->page_pricing_title = strip_tags($request->page_pricing_title);
        $obj->page_pricing_seo_title = strip_tags($request->page_pricing_seo_title);
        $obj->page_pricing_seo_meta_description = sanitize_textarea($request->page_pricing_seo_meta_description);
        $obj->update();

        return redirect()->back()->with('success', __('Data is updated successfully'));
    }

    public function faq_update(Request $request)
    {
        if(env('PROJECT_MODE') == 0) {
            return redirect()->back()->with('info', env('PROJECT_NOTIFICATION'));
        }
        
        $obj = OtherPageItem::where('id', 1)->first();
        $obj->page_faq_title = strip_tags($request->page_faq_title);
        $obj->page_faq_seo_title = strip_tags($request->page_faq_seo_title);
        $obj->page_faq_seo_meta_description = sanitize_textarea($request->page_faq_seo_meta_description);
        $obj->update();

        return redirect()->back()->with('success', __('Data is updated successfully'));
    }

    public function services_update(Request $request)
    {
        if(env('PROJECT_MODE') == 0) {
            return redirect()->back()->with('info', env('PROJECT_NOTIFICATION'));
        }
        
        $obj = OtherPageItem::where('id', 1)->first();
        $obj->page_services_title = strip_tags($request->page_services_title);
        $obj->page_services_seo_title = strip_tags($request->page_services_seo_title);
        $obj->page_services_seo_meta_description = sanitize_textarea($request->page_services_seo_meta_description);
        $obj->update();

        return redirect()->back()->with('success', __('Data is updated successfully'));
    }

    public function portfolios_update(Request $request)
    {
        if(env('PROJECT_MODE') == 0) {
            return redirect()->back()->with('info', env('PROJECT_NOTIFICATION'));
        }
        
        $obj = OtherPageItem::where('id', 1)->first();
        $obj->page_portfolios_title = strip_tags($request->page_portfolios_title);
        $obj->page_portfolios_seo_title = strip_tags($request->page_portfolios_seo_title);
        $obj->page_portfolios_seo_meta_description = sanitize_textarea($request->page_portfolios_seo_meta_description);
        $obj->update();

        return redirect()->back()->with('success', __('Data is updated successfully'));
    }

    public function portfolio_single_update(Request $request)
    {
        if(env('PROJECT_MODE') == 0) {
            return redirect()->back()->with('info', env('PROJECT_NOTIFICATION'));
        }
        
        $obj = OtherPageItem::where('id', 1)->first();
        $obj->page_portfolio_single_bottom_subheading = strip_tags($request->page_portfolio_single_bottom_subheading);
        $obj->page_portfolio_single_bottom_heading = strip_tags($request->page_portfolio_single_bottom_heading);
        $obj->page_portfolio_single_bottom_how_many = strip_tags($request->page_portfolio_single_bottom_how_many);
        $obj->page_portfolio_single_bottom_status = strip_tags($request->page_portfolio_single_bottom_status);
        $obj->update();

        return redirect()->back()->with('success', __('Data is updated successfully'));
    }

    public function blog_update(Request $request)
    {
        if(env('PROJECT_MODE') == 0) {
            return redirect()->back()->with('info', env('PROJECT_NOTIFICATION'));
        }
        
        $obj = OtherPageItem::where('id', 1)->first();
        $obj->page_blog_title = strip_tags($request->page_blog_title);
        $obj->page_blog_seo_title = strip_tags($request->page_blog_seo_title);
        $obj->page_blog_seo_meta_description = sanitize_textarea($request->page_blog_seo_meta_description);
        $obj->update();

        return redirect()->back()->with('success', __('Data is updated successfully'));
    }

    public function contact_update(Request $request)
    {
        if(env('PROJECT_MODE') == 0) {
            return redirect()->back()->with('info', env('PROJECT_NOTIFICATION'));
        }
        
        $obj = OtherPageItem::where('id', 1)->first();
        $obj->page_contact_title = strip_tags($request->page_contact_title);
        $obj->page_contact_send_mail_subheading = strip_tags($request->page_contact_send_mail_subheading);
        $obj->page_contact_send_mail_heading = sanitize_textarea($request->page_contact_send_mail_heading);
        $obj->page_contact_info_heading = strip_tags($request->page_contact_info_heading);
        $obj->page_contact_info_subheading = strip_tags($request->page_contact_info_subheading);
        $obj->page_contact_info_text = sanitize_textarea($request->page_contact_info_text);
        $obj->page_contact_info_phone_title = strip_tags($request->page_contact_info_phone_title);
        $obj->page_contact_info_phone_value = strip_tags($request->page_contact_info_phone_value);
        $obj->page_contact_info_email_title = strip_tags($request->page_contact_info_email_title);
        $obj->page_contact_info_email_value = strip_tags($request->page_contact_info_email_value);
        $obj->page_contact_info_address_title = strip_tags($request->page_contact_info_address_title);
        $obj->page_contact_info_address_value = strip_tags($request->page_contact_info_address_value);
        $obj->page_contact_map = sanitize_iframe($request->page_contact_map);
        $obj->page_contact_seo_title = strip_tags($request->page_contact_seo_title);
        $obj->page_contact_seo_meta_description = sanitize_textarea($request->page_contact_seo_meta_description);
        $obj->update();

        return redirect()->back()->with('success', __('Data is updated successfully'));
    }

    public function terms_update(Request $request)
    {
        if(env('PROJECT_MODE') == 0) {
            return redirect()->back()->with('info', env('PROJECT_NOTIFICATION'));
        }
        
        $obj = OtherPageItem::where('id', 1)->first();
        $obj->page_terms_title = strip_tags($request->page_terms_title);
        $obj->page_terms_content = $request->page_terms_content;
        $obj->page_terms_seo_title = strip_tags($request->page_terms_seo_title);
        $obj->page_terms_seo_meta_description = sanitize_textarea($request->page_terms_seo_meta_description);
        $obj->update();

        return redirect()->back()->with('success', __('Data is updated successfully'));
    }

    public function privacy_update(Request $request)
    {
        if(env('PROJECT_MODE') == 0) {
            return redirect()->back()->with('info', env('PROJECT_NOTIFICATION'));
        }
        
        $obj = OtherPageItem::where('id', 1)->first();
        $obj->page_privacy_title = strip_tags($request->page_privacy_title);
        $obj->page_privacy_content = $request->page_privacy_content;
        $obj->page_privacy_seo_title = strip_tags($request->page_privacy_seo_title);
        $obj->page_privacy_seo_meta_description = sanitize_textarea($request->page_privacy_seo_meta_description);
        $obj->update();

        return redirect()->back()->with('success', __('Data is updated successfully'));
    }

    public function search_update(Request $request)
    {
        if(env('PROJECT_MODE') == 0) {
            return redirect()->back()->with('info', env('PROJECT_NOTIFICATION'));
        }
        
        $obj = OtherPageItem::where('id', 1)->first();
        $obj->page_search_seo_title = strip_tags($request->page_search_seo_title);
        $obj->page_search_seo_meta_description = sanitize_textarea($request->page_search_seo_meta_description);
        $obj->update();

        return redirect()->back()->with('success', __('Data is updated successfully'));
    }

    public function tag_update(Request $request)
    {
        if(env('PROJECT_MODE') == 0) {
            return redirect()->back()->with('info', env('PROJECT_NOTIFICATION'));
        }
        
        $obj = OtherPageItem::where('id', 1)->first();
        $obj->page_tag_seo_title = strip_tags($request->page_tag_seo_title);
        $obj->page_tag_seo_meta_description = sanitize_textarea($request->page_tag_seo_meta_description);
        $obj->update();

        return redirect()->back()->with('success', __('Data is updated successfully'));
    }

    public function category_update(Request $request)
    {
        if(env('PROJECT_MODE') == 0) {
            return redirect()->back()->with('info', env('PROJECT_NOTIFICATION'));
        }
        
        $obj = OtherPageItem::where('id', 1)->first();
        $obj->page_category_seo_title = strip_tags($request->page_category_seo_title);
        $obj->page_category_seo_meta_description = sanitize_textarea($request->page_category_seo_meta_description);
        $obj->update();

        return redirect()->back()->with('success', __('Data is updated successfully'));
    }

    public function custom_page_store(Request $request)
    {
        if(env('PROJECT_MODE') == 0) {
            return redirect()->back()->with('info', env('PROJECT_NOTIFICATION'));
        }
        
        $request->validate([
            'name' => ['required', 'unique:custom_pages'],
            'slug' => ['required', 'alpha_dash', 'unique:custom_pages'],
            'content' => ['required']
        ],[
            'name.required' => __('Name is required'),
            'name.unique' => __('Name already exists'),
            'slug.required' => __('Slug is required'),
            'slug.alpha_dash' =>  __('Slug can contain only letters, numbers, hyphens, and underscores'),
            'slug.unique' => __('Slug already exists'),
            'content.required' => __('Content is required')
        ]);

        $obj = new CustomPage();
        $obj->name = strip_tags($request->name);
        $obj->slug = strtolower(strip_tags($request->slug));
        $obj->content = $request->content;
        $obj->seo_title = strip_tags($request->seo_title);
        $obj->seo_meta_description = sanitize_textarea($request->seo_meta_description);
        $obj->save();

        return redirect()->back()->with('success', __('Data is added successfully'));
    }

    public function custom_page_update(Request $request, $id)
    {
        if(env('PROJECT_MODE') == 0) {
            return redirect()->back()->with('info', env('PROJECT_NOTIFICATION'));
        }

        $obj = CustomPage::find($id);
        $request->validate([
            'name' => ['required', Rule::unique('custom_pages')->ignore($id)],
            'slug' => ['required', 'alpha_dash', Rule::unique('custom_pages')->ignore($id)],
            'content' => ['required']
        ],[
            'name.required' => __('Name is required'),
            'name.unique' => __('Name already exists'),
            'slug.required' => __('Slug is required'),
            'slug.alpha_dash' =>  __('Slug can contain only letters, numbers, hyphens, and underscores'),
            'slug.unique' => __('Slug already exists'),
            'content.required' => __('Content is required')
        ]);
        $obj->name = strip_tags($request->name);
        $obj->slug = strtolower(strip_tags($request->slug));
        $obj->content = $request->content;
        $obj->seo_title = strip_tags($request->seo_title);
        $obj->seo_meta_description = sanitize_textarea($request->seo_meta_description);
        $obj->update();

        return redirect()->back()->with('success', __('Data is updated successfully'));
    }

    public function custom_page_destroy($id)
    {
        if(env('PROJECT_MODE') == 0) {
            return redirect()->back()->with('info', env('PROJECT_NOTIFICATION'));
        }

        $obj = CustomPage::find($id);
        $obj->delete();

        return redirect()->back()->with('success', __('Data is deleted successfully'));
    }
}
