<?php

namespace App\Http\Controllers\Admin;

use App\Company;
use App\Customer;
use App\CustomerDocument;
use App\Document;
use App\EditorDoc;
use App\Services\AdminService;
use Carbon\Carbon;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class PostController extends Controller
{
    protected $AdminService;

    public function __construct(AdminService $AdminService)
    {
        $this->AdminService = $AdminService;
    }

    public function createCustomer(Request $request)
    {

        return view('customers.customer');
    }

    public function addCompany(Request $request)
    {

        $save_cusmpany = $this->AdminService->addCompany($request->all());
        if ($save_cusmpany['status'] == 0) {
            return response()->json(array('err_message' => $save_cusmpany['message']));
        } else {
            return response()->json(array('success_message' => $save_cusmpany['message'], 'set_company' => $save_cusmpany['set_company'], 'set_company_name' => $save_cusmpany['set_company_name']));
        }
    }

    public function addCustomer(Request $request)
    {
        $this->validate($request, [
            'customer_id' => 'required',
            'customer_name' => 'required',
            'address' => 'required',
            'mobile' => 'required|min:10',
            'email' => 'email|max:255|unique:users',
            'category' => 'required',

        ]);

        $save_customer = $this->AdminService->addCustomer($request->all());
        if ($save_customer['status'] == 0) {
            return response()->json(array('err_message' => $save_customer['message']));
        } else {
            return response()->json(array('success_message' => $save_customer['message']));
        }
    }

    public function showCustomer(Request $request)
    {
        $customer_views = Customer::with('companyList')->orderBy('id', 'DESC')->get();
        $customer_companies = Company::orderBy('id', 'DESC')->get();
        return view('customers.customer_views')->with(['customer_lists' => $customer_views, 'customer_companies' => $customer_companies]);
    }

    public function updateCustomer(Request $request)
    {
        $customer_table_id = $request->cus_id;
        $cus_name = $request->customer_name;
        $cus_company_name = $request->company;
        $customer_id = $request->customer_id;
        $customer_mobile = $request->mobile;
        $customer_email = $request->email;
        $customer_category = $request->category;
        $customer_refer_by = $request->referred_by;
        $customer_address = $request->address;
        $customer_gender = $request->gender;

        $check_mobile = Customer::where('mobile', $customer_mobile)->first();
        $check_email = Customer::where('email', $customer_email)->first();
//d
        $company_name = "0";
        if ($cus_company_name == '') {
            $company_name = "0";
        } else {
            $company_name = $cus_company_name;
        }
        $customer_update = Customer::find($customer_table_id);
        $customer_update->customer_id = $customer_id;
        $customer_update->customer_name = $cus_name;
        $customer_update->company = $company_name;
        $customer_update->address = $customer_address;
        $customer_update->mobile = $customer_mobile;
        $customer_update->email = $customer_email;
        $customer_update->gender = $customer_gender;
        $customer_update->category = $customer_category;
        $customer_update->referred_by = $customer_refer_by;
        $customer_update->created_by = Auth::user()->role;
        $customer_update->save();
        return response()->json(['success' => ucfirst($cus_name) . ' field is Updated Successfully']);


    }

//    public function addDocumentByCustomer(Request $request)
//    {
//        $doc_id = $request->doc_id;
//        $customer_id = $request->customer_id;
//        $customer_name = $request->cus_name;
//        $subject = $request->subject;
//        $refer_date = $request->refer_date;
//        $message = $request->message;
//        $attachment_photos = $request->attachment_photos;
//        $attachment_doc = $request->attachment_doc;
//        $attachment_files = $request->attachment_files;
//
//
//        $save_documents_table = new CustomerDocument();
//        $save_documents_table->user_id = Auth::user()->id;
//        $save_documents_table->document_id = $doc_id;
//        $save_documents_table->customer_id = $customer_id;
//        $save_documents_table->customer_name = $customer_name;
//        $save_documents_table->subject = $subject;
//        $save_documents_table->refer_date = Carbon::parse($refer_date)->format('Y-m-d');
//        $save_documents_table->message = $message;
//        $save_documents_table->save();
//
//
//        if (!empty($attachment_photos)) {
//            foreach ($attachment_photos as $image) {
//                $name = str_random(12) . '.' . $image->getClientOriginalExtension();
//                $extension = $image->getClientOriginalExtension();
//                //$image_path = $image->move('/public/gallery/' . $customer_id, $name);
//                $image->move(public_path('/gallery/' . $customer_id), $name);
//                $save_image = new Document();
//                $save_image->doc_id = $save_documents_table->id;
//                $save_image->documents = $name;
//                $save_image->extension = $extension;
//                $save_image->save();
//
//            }
//        }
//
//        if (!empty($attachment_doc)) {
//            foreach ($attachment_doc as $image) {
//                $name = str_random(12) . '.' . $image->getClientOriginalExtension();
//                $extension = $image->getClientOriginalExtension();
//                $image->move(public_path('/gallery/' . $customer_id), $name);
//                // $image_path = $image->storeAs('/public/gallery/' . $customer_id, $name);
//                $save_image = new Document();
//                $save_image->doc_id = $save_documents_table->id;
//                $save_image->documents = $name;
//                $save_image->extension = $extension;
//                $save_image->save();
//
//            }
//        }
//        if (!empty($attachment_files)) {
//            foreach ($attachment_files as $image) {
//                $name = str_random(12) . '.' . $image->getClientOriginalExtension();
//                $extension = $image->getClientOriginalExtension();
//                $image->move(public_path('/gallery/' . $customer_id), $name);
//                //$image_path = $image->storeAs('/public/gallery/' . $customer_id, $name);
//                $save_image = new Document();
//                $save_image->doc_id = $save_documents_table->id;
//                $save_image->documents = $name;
//                $save_image->extension = $extension;
//                $save_image->save();
//
//            }
//        }
//        return redirect()->back()->withSuccess("Successfully Inserted");
//
//    }


//    public function uplodePhotos(Request $request)
//    {
//        $get_images = $request->all();
//
//        dd($get_images);
//    }
    public function addDocumentByCustomer(Request $request)
    {

        $doc_id = $request['doc_id'];
        $customer_id = $request['customer_id'];
        $customer_name = $request['cus_name'];
        $subject = $request['subject'];
        $refer_date = $request['refer_date'];
        $message = $request['message'];
        $attachment_photos = $request['postImgs'];
        $attachment_doc = $request['attachment_doc'];
        $attachment_files = $request['attachment_file'];


        $save_documents_table = new CustomerDocument();
        $save_documents_table->user_id = Auth::user()->id;
        $save_documents_table->document_id = $doc_id;
        $save_documents_table->customer_id = $customer_id;
        $save_documents_table->customer_name = $customer_name;
        $save_documents_table->subject = $subject;
        $save_documents_table->refer_date = Carbon::parse($refer_date)->format('Y-m-d');
        $save_documents_table->message = $message;
        $save_documents_table->save();
        if ($attachment_photos[0] == null && $attachment_doc[0] == null && $attachment_files[0] == null) {
            return response()->json(['success' => "Successfully Inserted"]);
        } else {

            if (!empty($attachment_photos)) {
                foreach ($attachment_photos as $image) {
                    $name = str_random(12) . '.' . $image->getClientOriginalExtension();
                    $extension = $image->getClientOriginalExtension();
                    //$image_path = $image->move('/public/gallery/' . $customer_id, $name);
                    $image->move(public_path('/gallery/' . $customer_id), $name);
                    $save_image = new Document();
                    $save_image->doc_id = $save_documents_table->id;
                    $save_image->documents = $name;
                    $save_image->extension = $extension;
                    $save_image->save();

                }
            }

            if (!empty($attachment_doc)) {
                foreach ($attachment_doc as $image) {
                    $name = str_random(12) . '.' . $image->getClientOriginalExtension();
                    $extension = $image->getClientOriginalExtension();
                    $image->move(public_path('/gallery/' . $customer_id), $name);
                    // $image_path = $image->storeAs('/public/gallery/' . $customer_id, $name);
                    $save_image = new Document();
                    $save_image->doc_id = $save_documents_table->id;
                    $save_image->documents = $name;
                    $save_image->extension = $extension;
                    $save_image->save();

                }
            }
            if (!empty($attachment_files)) {
                foreach ($attachment_files as $image) {
                    $name = str_random(12) . '.' . $image->getClientOriginalExtension();
                    $extension = $image->getClientOriginalExtension();
                    $image->move(public_path('/gallery/' . $customer_id), $name);
                    //$image_path = $image->storeAs('/public/gallery/' . $customer_id, $name);
                    $save_image = new Document();
                    $save_image->doc_id = $save_documents_table->id;
                    $save_image->documents = $name;
                    $save_image->extension = $extension;
                    $save_image->save();

                }
            }
        }
        return response()->json(['success' => "Successfully Inserted"]);

    }

    public function updateDocumentList(Request $request)
    {

        $attachment_photos = $request['postImgs'];
        $attachment_doc = $request['attachment_doc'];
        $attachment_files = $request['attachment_file'];
        $doc_id = $request['doc_id'];

        $get_document_details = CustomerDocument::where('id', $doc_id)->first();

        if ($attachment_photos[0] == null && $attachment_doc[0] == null && $attachment_files[0] == null) {
            return response()->json(['error' => "Attachment Not Found!"]);
        } else {

            if (!empty($attachment_photos)) {
                foreach ($attachment_photos as $image) {
                    $name = str_random(12) . '.' . $image->getClientOriginalExtension();
                    $extension = $image->getClientOriginalExtension();
                    //$image_path = $image->move('/public/gallery/' . $customer_id, $name);
                    $image->move(public_path('/gallery/' . $get_document_details->customer_id), $name);
                    $save_image = new Document();
                    $save_image->doc_id = $doc_id;
                    $save_image->documents = $name;
                    $save_image->extension = $extension;
                    $save_image->save();

                }
            }

            if (!empty($attachment_doc)) {
                foreach ($attachment_doc as $image) {
                    $name = str_random(12) . '.' . $image->getClientOriginalExtension();
                    $extension = $image->getClientOriginalExtension();
                    $image->move(public_path('/gallery/' . $get_document_details->customer_id), $name);
                    // $image_path = $image->storeAs('/public/gallery/' . $customer_id, $name);
                    $save_image = new Document();
                    $save_image->doc_id = $doc_id;
                    $save_image->documents = $name;
                    $save_image->extension = $extension;
                    $save_image->save();

                }
            }
            if (!empty($attachment_files)) {
                foreach ($attachment_files as $image) {
                    $name = str_random(12) . '.' . $image->getClientOriginalExtension();
                    $extension = $image->getClientOriginalExtension();
                    $image->move(public_path('/gallery/' . $get_document_details->customer_id), $name);
                    //$image_path = $image->storeAs('/public/gallery/' . $customer_id, $name);
                    $save_image = new Document();
                    $save_image->doc_id = $doc_id;
                    $save_image->documents = $name;
                    $save_image->extension = $extension;
                    $save_image->save();

                }
            }
        }
        return response()->json(['success' => "Successfully Inserted"]);

    }

    public function updateDocs(Request $request)
    {
        $cus_id = $request->cus_id;
        $customer_name = $request->customer_name;
        $document_id = $request->document_id;
        $customer_id = $request->customer_id;
        $subject = $request->subject;
        $refer_date = $request->refer_date;
        $message = $request->message;

        $get_customet_id = CustomerDocument::where('document_id', $document_id)->first();
        $get_customet_id->subject = $subject;
        $get_customet_id->refer_date = Carbon::parse($refer_date)->format('Y-m-d');
        $get_customet_id->message = $message;
        $get_customet_id->save();

        return response()->json(['success' => $document_id . ' field is Updated Successfully']);


    }

    public function showDocuments(Request $request)
    {
        $show_documents = CustomerDocument::orderBy('id', 'DESC')->get();
        return view('customers.document_view')->with(['documents' => $show_documents, 'doc_lists' => $show_documents]);
    }

    public function showDocumentReport(Request $request)
    {
        $show_documents = CustomerDocument::orderBy('id', 'DESC')->get();
        return view('customers.reports')->with(['documents' => $show_documents, 'doc_lists' => $show_documents]);
    }

    public function getDocumentList(Request $request)
    {
        $doc_id = $request->select_doc_id;
        $show_documents = CustomerDocument::orderBy('id', 'DESC')->where('document_id', $doc_id)->orWhere('customer_id', $doc_id)
            ->orWhere('customer_name', $doc_id)->get();

        return view('customers.document_view')->with(['documents' => $show_documents,
            'selected_file' => $doc_id]);

    }

    public function customerHidden($id)
    {
        $customer_visiblity = Customer::where('id', $id)->first();

        if ($customer_visiblity->visibility == 0) {
            $customer_visiblity->visibility = 1;
            $customer_visiblity->save();
            return redirect()->back()->withSuccess($customer_visiblity->customer_name . ' Field Is Hidden');
        } else {
            $customer_visiblity->visibility = 0;
            $customer_visiblity->save();
            return redirect()->back()->withSuccess($customer_visiblity->customer_name . ' Field Is Visible');
        }
    }

    public function getDateFilter(Request $request)
    {
        $from_filter = Carbon::parse($request->select_from_date)->format('Y-m-d');
        $to_filter = Carbon::parse($request->select_to_date)->format('Y-m-d');

        $get_doc_lists = CustomerDocument::orderBy('id', 'DESC')->whereBetween('refer_date', [$from_filter, $to_filter])->get();


        return view('customers.reports')->with(['documents' => $get_doc_lists, 'from_dates' => $from_filter, 'to_dates' => $to_filter]);
    }

    public function customerDelete($id)
    {
        $cus_delete = Customer::find($id)->delete();
        return redirect()->back()->withSuccess('Successfully Deleted ');
    }

    public function DocumentDelete($id)
    {
        $cus_delete = CustomerDocument::find($id)->delete();
        $cus_doc_delete = Document::where('doc_id', $id)->delete();
        return redirect()->back()->withSuccess('Successfully Deleted ');
    }

    public function docViewDelete($id)
    {
        $cus_delete = Document::find($id)->delete();
        return redirect()->back()->withSuccess('Successfully Deleted ');
    }


//    text ediotr
    public function saveTextEditor(Request $request)
    {

        $content = $request['content'];
        $title = $request['title'];
        $set_id = $request['set_id'];
        if ($set_id == 0) {
            $save_content = new EditorDoc();
            $save_content->user_id = Auth::user()->id;
            $save_content->created_by = Auth::user()->id;
            $save_content->content = $content;
            $save_content->title = $title;
            $save_content->save();
            return response()->json(['success' => "Your File Save Successfully"]);
        } else {
            $get_filter_editor = EditorDoc::find($set_id);
            $get_filter_editor->user_id = Auth::user()->id;
            $get_filter_editor->created_by = Auth::user()->id;
            $get_filter_editor->content = $content;
            $get_filter_editor->title = $title;
            $get_filter_editor->save();
            return response()->json(['success' => "Your File Update Successfully"]);
        }

    }


}
