<?php

namespace App\Http\Controllers;

use App\Models\Branch;
use App\Models\global_configuration;
use Auth, DB, Mail, Validator, File, DataTables, Exception;
use Illuminate\Http\Request;
class GlobalConfigController extends Controller
{
    /** construct */
        public function __construct(){            
            $this->middleware('permission:global-configuration-create', ['only' => ['create']]);
            $this->middleware('permission:global-configuration-delete', ['only' => ['delete']]);
        }
    /** construct */
 
    public function index(Request $request){
         if($request->ajax()){
        $data = global_configuration::select('global_configuration.*')->get();
        return Datatables::of($data)
                        ->addIndexColumn()
                        ->addColumn('action', function($data){
                            $return = '<div class="btn-group">';
                            
                                $return .= '<a href="javascript:;" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">
                                                    <i class="fa fa-bars"></i>
                                                </a> &nbsp;
                                                <ul class="dropdown-menu">
                                                <li><a class="dropdown-item" href="javascript:;" onclick="change_status(this);" data-status="active" data-id="' . base64_encode($data->id) . '">Active</a></li>
                                                    
                                                    <li><a class="dropdown-item" href="javascript:;" onclick="change_status(this);" data-status="delete" data-id="' . base64_encode($data->id) . '">Delete</a></li>
                                                </ul>';
                            

                            $return .= '</div>';

                            return $return;
                        })

                        ->editColumn('status', function ($data) {
                            if ($data->status == 'active') {
                                return '<span class="badge badge-pill badge-success">Active</span>';
                            }else if ($data->status == 'delete') {
                                return '<span class="badge badge-pill badge-danger">Deleted</span>';
                            }else{
                                return '-';
                            }
                        })
                        ->rawColumns(['action','status'])
                        ->make(true);
                    }
    return view('global_configuration.index');
    }

    public function create(){
        $branch = Branch::where('status', 'active')->get(['id', 'city']); 
        return view('global_configuration.create',compact('branch'));
    }

    public function insert(Request $request){

            if($request->ajax()){ 
                return true ;
            }

            //login logo1
            if($request->hasFile('login_logo1')){
            $login_logo1 = $request->login_logo1;            
            $file_name = $login_logo1->getClientOriginalName();
            $folder = public_path('/uploads/global_configuration'.'/' . 'login_logo_one');
            if (!File::exists($folder)) {
                File::makeDirectory($folder, 0775, true, true);
            }
            //$dir = public_path().'\uploads\donar_master_kyc';
            
            $file = $login_logo1->move($folder, $file_name);            
            //dd($file);
            }

            //login logo2
            if($request->hasFile('login_logo2')){
            $login_logo2 = $request->login_logo2;            
            $login_file_name = $login_logo2->getClientOriginalName();
            $folder = public_path('/uploads/global_configuration'.'/' . 'login_logo_two');
            if (!File::exists($folder)) {
                File::makeDirectory($folder, 0775, true, true);
            }
            //$dir = public_path().'\uploads\donar_master_kyc';
            
            $file = $login_logo2->move($folder, $login_file_name);            
            //dd($file);
            }

            //cash_receipt_logo1
            if($request->hasFile('cash_receipt_logo1')){
            $cash_receipt_logo1 = $request->cash_receipt_logo1;            
            $cash_receipt_file_name = $cash_receipt_logo1->getClientOriginalName();
            $folder = public_path('/uploads/global_configuration'.'/' . 'cash_receipt_logo_one');
            if (!File::exists($folder)) {
                File::makeDirectory($folder, 0775, true, true);
            }
            //$dir = public_path().'\uploads\donar_master_kyc';
            
            $file = $cash_receipt_logo1->move($folder, $cash_receipt_file_name);            
            //dd($file);
            }

            //cash_receipt_logo2
            if($request->hasFile('cash_receipt_logo2')){
            $cash_receipt_logo2 = $request->cash_receipt_logo2;            
            $cash_receiptfile_name = $cash_receipt_logo2->getClientOriginalName();
            $folder = public_path('/uploads/global_configuration'.'/' . 'cash_receipt_logo_two');
            if (!File::exists($folder)) {
                File::makeDirectory($folder, 0775, true, true);
            }
            //$dir = public_path().'\uploads\donar_master_kyc';
            
            $file = $cash_receipt_logo2->move($folder, $cash_receiptfile_name);            
            //dd($file);
            }

            //obf_print1
            if($request->hasFile('obf_print1')){
            $obf_print1 = $request->obf_print1;            
            $obf_print_file_name = $obf_print1->getClientOriginalName();
            $folder = public_path('/uploads/global_configuration'.'/' . 'obf_print_one');
            if (!File::exists($folder)) {
                File::makeDirectory($folder, 0775, true, true);
            }
            //$dir = public_path().'\uploads\donar_master_kyc';
            
            $file = $obf_print1->move($folder, $obf_print_file_name);            
            //dd($file);
            }

            //obf_print2
            if($request->hasFile('obf_print2')){
            $obf_print2 = $request->obf_print2;            
            $obf_printfile_name = $obf_print2->getClientOriginalName();
            $folder = public_path('/uploads/global_configuration'.'/' . 'obf_print_two');
            if (!File::exists($folder)) {
                File::makeDirectory($folder, 0775, true, true);
            }
            //$dir = public_path().'\uploads\donar_master_kyc';
            
            $file = $obf_print2->move($folder, $obf_printfile_name);            
            //dd($file);
            }

            //invoice_logo1
            if($request->hasFile('invoice_logo1')){
            $invoice_logo1 = $request->invoice_logo1;            
            $invoice_logo_file_name = $invoice_logo1->getClientOriginalName();
            $folder = public_path('/uploads/global_configuration'.'/' . 'invoice_logo_one');
            if (!File::exists($folder)) {
                File::makeDirectory($folder, 0775, true, true);
            }
            //$dir = public_path().'\uploads\donar_master_kyc';
            
            $file = $invoice_logo1->move($folder, $invoice_logo_file_name);            
            //dd($file);
            }

            //invoice_logo2
            if($request->hasFile('invoice_logo2')){
            $invoice_logo2 = $request->invoice_logo2;            
            $invoice_logofile_name = $invoice_logo2->getClientOriginalName();
            $folder = public_path('/uploads/global_configuration'.'/' . 'invoice_logo_two');
            if (!File::exists($folder)) {
                File::makeDirectory($folder, 0775, true, true);
            }
            //$dir = public_path().'\uploads\donar_master_kyc';
            
            $file = $invoice_logo2->move($folder, $invoice_logofile_name);            
            //dd($file);
            }

            //gatepass_logo1
            if($request->hasFile('gatepass_logo1')){
            $gatepass_logo1 = $request->gatepass_logo1;            
            $gatepass_logo_file_name = $gatepass_logo1->getClientOriginalName();
            $folder = public_path('/uploads/global_configuration'.'/' . 'gatepass_logo_one');
            if (!File::exists($folder)) {
                File::makeDirectory($folder, 0775, true, true);
            }
            //$dir = public_path().'\uploads\donar_master_kyc';
            
            $file = $gatepass_logo1->move($folder, $gatepass_logo_file_name);            
            //dd($file);
            }

            //gatepass_logo2
            if($request->hasFile('gatepass_logo2')){
            $gatepass_logo2 = $request->gatepass_logo2;            
            $gatepass_logofile_name = $gatepass_logo2->getClientOriginalName();
            $folder = public_path('/uploads/global_configuration'.'/' . 'gatepass_logo_two');
            if (!File::exists($folder)) {
                File::makeDirectory($folder, 0775, true, true);
            }
            //$dir = public_path().'\uploads\donar_master_kyc';
            
            $file = $gatepass_logo2->move($folder, $gatepass_logofile_name);            
            //dd($file);
            }

            $crud=[
                'branch_id'=>$request->branch_id,
                'login_logo1'=>$file_name ?? null,
                'login_logo2'=>$login_file_name ?? null,
                'cash_receipt_logo1'=>$cash_receipt_file_name ?? null,
                'cash_receipt_logo2'=>$cash_receiptfile_name ?? null,
                'obf_print1'=>$obf_print_file_name ?? null,
                'obf_print2'=>$obf_printfile_name ?? null,
                'invoice_logo1'=>$invoice_logo_file_name ?? null,
                'invoice_logo2'=>$invoice_logofile_name ?? null,
                'gatepass_logo1'=>$gatepass_logo_file_name ?? null,
                'gatepass_logo2'=>$gatepass_logofile_name ?? null,
                'status'=>'active',
                'created_at' => date('Y-m-d H:i:s'),
                'created_by' => auth()->user()->id,
                'updated_at' => date('Y-m-d H:i:s'),
                'updated_by' => auth()->user()->id
            ];
            
           
            DB::beginTransaction();
            try {              
                DB::enableQueryLog();
                $last_id = global_configuration::insertGetId($crud);
                if ($last_id) {                    
                    DB::commit();
                    return redirect()->route('global-configuration')->with('success', 'Record inserted successfully');
                } else {
                    DB::rollback();
                    return redirect()->back()->with('error', 'Failed to insert record')->withInput();
                }
            } catch (\Throwable $th) {
                DB::rollback();
                return redirect()->back()->with('error', 'Something went wrong, please try again later')->withInput();
            }    
    }

     /** change-status */
        public function change_status(Request $request){
            if (!$request->ajax()) { exit('No direct script access allowed'); }

            $id = base64_decode($request->id);
            $data = global_configuration::where(['id' => $id])->first();

            if(!empty($data)){
                $update = global_configuration::where(['id' => $id])->update(['status' => $request->status, 'updated_at' => date('Y-m-d H:i:s'), 'updated_by' => auth('sanctum')->user()->id]);
                if($update){
                    return response()->json(['code' => 200]);
                }else{
                    return response()->json(['code' => 201]);
                }
            }else{
                return response()->json(['code' => 201]);
            }
        }
    /** change-status */



}
