<?php

namespace App\Http\Controllers\Admin;

use App\Company;
use App\Customer;
use App\CustomerDocument;
use App\Document;
use App\EditorDoc;
use App\Helpers\Utility;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ViewController extends Controller
{

    public function getCompany(Request $request)
    {
        $company = Company::orderBy('id', 'DESC')->get();
        return response()->json(['companies' => $company]);
    }

    public function filterCustomer(Request $request)
    {
        $filter_option = $request['search_id'];
        echo $filter_option;
        $customer_companies_LISTS = Company::orderBy('id', 'DESC')->get();
        $check_name = Customer::where('customer_name', $filter_option)->orWhere('customer_id', $filter_option)->orWhere('mobile', $filter_option)->
        orWhere('email', $filter_option)->get();
        if ($check_name) {
            return view('customers.customer_views')->with(['customer_lists' => $check_name, 'customer_companies' => $customer_companies_LISTS, 'set_name' => $filter_option]);
        } else {
            return view('customers.customer_views')->with(['customer_lists' => "No Record Found"]);
        }
    }


    public function filterCustomerCategory(Request $request)
    {
        $category_id = $request['category_name'];
        $filter_category_all = Customer::orderBy('id', 'DESC')->get();
        $filter_category = Customer::where('category', $category_id)->get();
        $customer_companies_LISTS = Company::orderBy('id', 'DESC')->get();
        if ($category_id) {
            return view('customers.customer_views')->with(['customer_lists' => $filter_category, 'customer_companies' => $customer_companies_LISTS, 'set_category' => $category_id]);
        } elseif ($category_id == 0) {
            return view('customers.customer_views')->with(['customer_lists' => $filter_category_all, 'customer_companies' => $customer_companies_LISTS, 'set_category' => $category_id]);
        } else {
            return view('customers.customer_views')->with(['customer_lists' => "No Record Found"]);
        }

    }

    public function filterCustomerCompany(Request $request)
    {
        $company_id = $request['company'];
        $customer_companies_LISTS = Company::orderBy('id', 'DESC')->get();
        $filter_category = Customer::with('companyList')->where('company', $company_id)->get();
        $customer_companies = Customer::with('companyList')->orderBy('id', 'DESC')->get();
        if ($company_id) {
            return view('customers.customer_views')->with(['customer_lists' => $filter_category, 'customer_companies' => $customer_companies_LISTS, 'set_company' => $company_id]);
        } elseif ($company_id == 0) {
            return view('customers.customer_views')->with(['customer_lists' => $customer_companies, 'customer_companies' => $customer_companies_LISTS, 'set_company' => $company_id]);
        } else {
            return view('customers.customer_views')->with(['customer_lists' => "No Record Found", 'customer_companies' => $customer_companies_LISTS,]);
        }

    }


    public function getCustomer($id)
    {
        $get_customers = Customer::where('id', $id)->get();
        return response()->json(['customer_datas' => $get_customers]);

    }

    public function getDocuments($doc_id)
    {

        $get_documents = CustomerDocument::where('id', $doc_id)->get();
        return response()->json(['documents_datas' => $get_documents]);

    }


    public function createDocument(Request $request)
    {

//        $gen_doc_id = (new Utility())->genDocId();
        $set_doc_id = CustomerDocument::all()->count();
        if ($set_doc_id == 0 || $set_doc_id != 0) {
            $set_id = $set_doc_id + 1;
            $get_customer_details = Customer::orderBy('id', 'DESC')->where('visibility', 0)->get();
            return view('customers.create_document')->with(['customers' => $get_customer_details, 'doc_id' => $set_id]);
        }
    }

    public function getCustomerId($id)
    {
        $getCustomerId = Customer::where('customer_id', $id)->Where('visibility', 0)->first();
        return response()->json(['customer_id' => ucfirst($getCustomerId->customer_name)]);
    }

    public function getImage($id)
    {
        $get_image = Document::where('doc_id', $id)->get();
        $get_docs = CustomerDocument::where('id', $id)->first();
        if ($get_image == '') {
            return response()->json(['err_images' => "No Record Found"]);
        } else {
            return response()->json(['images' => $get_image, 'customer_id' => $get_docs->customer_id]);
        }

    }

    public function getDocumentGallery($doc_id, $customer_id, $category)
    {
        if ($category == 1) {
            $get_documents = Document::where('doc_id', $doc_id)->whereIn('extension', ['jpg', 'png', 'jpeg', 'gif', 'tiff'])->get();
            return view('customers.show_gallery')->with(['images' => $get_documents, 'doc_ID' => $doc_id, 'cartegory' => $customer_id]);
        } else if ($category == 2) {
            $get_documents_docs = Document::where('doc_id', $doc_id)->whereIn('extension', ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'csv', 'txt'])->get();
            return view('customers.show_gallery')->with(['docs' => $get_documents_docs, 'doc_ID' => $doc_id, 'cartegory' => $customer_id]);
        } else if ($category == 3) {
            $get_documents_files = Document::where('doc_id', $doc_id)->whereIn('extension', ['rar', 'zip', 'psd'])->get();
            return view('customers.show_gallery')->with(['files' => $get_documents_files, 'doc_ID' => $doc_id, 'cartegory' => $customer_id]);
        } else {
            return redirect()->back()->withErrors('No Record Found!');
        }

    }

    public function editorView(Request $request)
    {
        $get_editor = EditorDoc::orderBy('id', 'DESC')->get();

        return view('editor')->with(['editors' => $get_editor]);

    }

    public function getFilterEditor($id)
    {
        $get_filter_editor = EditorDoc::find($id);
        return response()->json(['title' => $get_filter_editor->title, 'id' => $get_filter_editor->id, 'content' => $get_filter_editor->content]);
    }


}
