<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
use Maatwebsite\Excel\Facades\Excel;
use App\Partner;
use App\Schedule;
use App\User;
use App\Invoice;
use Illuminate\Support\Facades\Validator;
use DataTables;
use Str;
use DB;
use Illuminate\Support\Facades\Auth;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
use Illuminate\Support\Facades\URL;
use App\Imports\ImportSchedule;
use DateTime;
use DatePeriod;
use DateInterval;
use Log;
use PDF;
use App\For_signature;
use App\Complaint;



class MitraDashboardController extends Controller
{
    public function __construct()
    {
        // $this->middleware('auth');
        $this->middleware('mitra');
        // $this->pageTitle = 'Partner';
        // $this->masterActive = 'active';
        // $this->partnerActive = 'active';
    }
    public function index()
    {
        $this->pageTitle = 'Home';
        $this->homeActive = 'active';
        $data = (array)$this;
        $data['form'] = [
            [ 'name' => 'name', 'label' => 'Nama', 'type' => 'text'],
            [ 'name' => 'description', 'label' => 'Deskripsi', 'type' => 'text'],
            // ['name' => 'ket', 'label' => 'Keterangan', 'type'=>'select', 'value' => ['sa', 'tu', 'dua']]  
        ];
        $data['form_action'] = "partner.store";
        $data['form_update'] = "partner.update";
        // return json_encode($data);
        return View::make('dashboard.mitradashboard.index', $data);
    }
    public function indexData(Request $request){
        $data = Invoice::where('partner_id', Auth::guard('mitra')->user()->id);
         return Datatables::of($data)
               ->editColumn('action', function($data){ return view('dashboard.mitradashboard.index-action', compact('data'));})
               ->rawColumns(['action'])
               ->make(true); 

   }
   public function tagihan()
    {
        $this->pageTitle = 'Tagihan';
        $this->tagihanActive = 'active';
        $data = (array)$this;
        $data['form'] = [
            [ 'name' => 'file', 'label' => 'Berkas', 'type' => 'file'],
            // [ 'name' => 'description', 'label' => 'Deskripsi', 'type' => 'text'],
            // [ 'name' => 'description', 'label' => 'Deskripsi', 'type' => 'text'],
            // ['name' => 'ket', 'label' => 'Keterangan', 'type'=>'select', 'value' => ['sa', 'tu', 'dua']]  
        ];
        // $data['form_action'] = "partner.store";
        // $data['form_update'] = "partner.update";
        $data['form_update_upload'] = "dashboard.upload";
        // return json_encode($data);
        return View::make('dashboard.mitradashboard.tagihan', $data);
    }
    public function look(Request $request)
    {
       
        $data = Invoice::findOrFail($request->data);
        return json_encode($data);

    }
    public function upload(Request $request)
    {
        // return json_encode($request->all());
        if($request->hasFile('file')) {
            //    return json_encode('masuk');
                        // Upload path
                        $destinationPath = 'invoice_file/';
                 
                        // Get file extension
                        $extension = $request->file('file')->getClientOriginalExtension();
                 
                        // Valid extensions
                        $validextensions = array("jpeg","jpg","png");
                 
                        // Check extension
                        if(in_array(strtolower($extension), $validextensions)){
              
                          do{
                              $dokumen = Str::random(60);
                              $cekuniq =  Invoice::where('file', $dokumen.'.'.$extension);
                          }while(empty($cekuniq)); 
                 
                          // Rename file 
                          // $fileName = $request->file('file')->getClientOriginalName().time() .'.' . $extension;
                          $fileName = $request->file('file')->getClientOriginalName();
                          $fileReal = $dokumen . '.' . $extension;
                          // Uploading file to given path
                          $request->file('file')->move($destinationPath, $fileReal); 
                          
              
            
                          $data = Invoice::find($request->id);
                            $data->file = $fileReal;
                            $data->status = 1;
                            $data->save();
                            session()->put('success','Berhasi upload!');
                            return back(); 
            
                        }
                 
                      }
            
            
            
            
                        session()->put('error','Input gagal.');
                        return back();
    }
    public function cetak_pdf($id){

        $data['invoice'] = Invoice::with('partner.company')->findOrFail($id);
        // return json_encode(Auth::guard('mitra')->user()->company->name);
        $data['company'] = strtoupper($data['invoice']->partner->company->name);
        $data['description'] = strtoupper(Auth::guard('mitra')->user()->company->description);
        $data['admin'] = 'Irawan Pramudhita, SE';
        $data['signature'] = For_signature::with('signature')->where('name', 'Invoice')->first();
        $data['rekening'] = env('REKENING');
    
    
        $pdf = PDF::loadView('dashboard.invoice.cetak-prev', $data)->setPaper('a4', 'potrait');
            return $pdf->stream('invoice.pdf');
            // return $pdf->download('invoice.pdf');
    
       }
       public function pegawai()
    {
        $this->pageTitle = 'Pegawai';
        $this->pegawaiActive = 'active';
        $data = (array)$this;
        // $data['form'] = [
        //     [ 'name' => 'file', 'label' => 'Berkas', 'type' => 'file'],
            // [ 'name' => 'description', 'label' => 'Deskripsi', 'type' => 'text'],
            // [ 'name' => 'description', 'label' => 'Deskripsi', 'type' => 'text'],
            // ['name' => 'ket', 'label' => 'Keterangan', 'type'=>'select', 'value' => ['sa', 'tu', 'dua']]  
        // ];
        // $data['form_action'] = "partner.store";
        // $data['form_update'] = "partner.update";
        // $data['form_update_upload'] = "dashboard.upload";
        // return json_encode($data);
        return View::make('dashboard.mitradashboard.pegawai', $data);
    }
    public function dataPegawai(Request $request){
   

        // $data = User::with('role','company','partner')->where('role_id', $us[0]->id)->where('company_id', Auth::user()->company->id);
        $data = User::with('company','partner')->where('partner_id', Auth::guard('mitra')->user()->id);
        
         return Datatables::of($data)
               ->editColumn('action', function($data){ return view('dashboard.member.index-action', compact('data'));})
               ->rawColumns(['action'])
               ->make(true); 

   }

   public function contact()
    {
        $this->pageTitle = 'Contact Service';
        $this->contactActive = 'active';
        $data = (array)$this;
        // $data['form'] = [
        //     [ 'name' => 'file', 'label' => 'Berkas', 'type' => 'file'],
            // [ 'name' => 'description', 'label' => 'Deskripsi', 'type' => 'text'],
            // [ 'name' => 'description', 'label' => 'Deskripsi', 'type' => 'text'],
            // ['name' => 'ket', 'label' => 'Keterangan', 'type'=>'select', 'value' => ['sa', 'tu', 'dua']]  
        // ];
        // $data['form_action'] = "partner.store";
        // $data['form_update'] = "partner.update";
        // $data['form_update_upload'] = "dashboard.upload";
        // return json_encode($data);
        return View::make('dashboard.mitradashboard.contact', $data);
    }

    public function contact_store(Request $request)
    {
        // return json_encode(Auth::guard('mitra')->user());
        
        $validator = Validator::make($request->all(), [
            'description' => 'required'
        ]);

        if ($validator->fails()) {
            session()->put('error','Input gagal.');
            return back();
        }
        $researche = Complaint::create([
            'partner_id' => Auth::guard('mitra')->user()->id,
            'company_id' => Auth::guard('mitra')->user()->company->id,
            'description' => $request->description
            ]);

        session()->put('success','Berhasil Input.');   
        return back();
    }

    public function jadwal()
    {
        $this->pageTitle = 'Jadwal';
        $this->jadwalActive = 'active';
        $data = (array)$this;
        $data['member'] = User::where('partner_id', Auth::guard('mitra')->user()->id)->where('is_active', 1)->get();
        
        return View::make('dashboard.mitradashboard.jadwal', $data);
    }

    public function indexAjax(Request $request)
    {
  
        // $data = Schedule::get(['id', 'title as mboh', 'start', 'end']);
        // return json_encode($data);
        // Log::info('masuk'. json_encode($request->all()));
        if($request->ajax()) {
       
             $data = Schedule::where('partner_id', Auth::guard('mitra')->user()->id)
                    //    ->get(['id', 'title', 'start', 'end', DB::raw('CONCAT(start_plot,"-",end_plot) as description')]);
                       ->get(['id','user_id','title', 'start', 'end', 'start_plot as description','end_plot as end_time']);
                       if (!empty ($request->user_id) ){
                        $data = $data->where('user_id', $request->user_id);
                    };
  
             return response()->json($data);
        }
    }

}