<?php

namespace App\Http\Controllers;

use App\Models\CmmsDemo;

use File;
use ZipArchive;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Hekmatinasser\Verta\Verta;
use Illuminate\Support\Collection; 
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;

class CmmsDemoController extends Controller
{
    public function index(){
        $start_time = microtime(true); 
        // $projectsArray = CmmsDemo::all();

        $projectsArray = CmmsDemo::select(['id as 0', 'car_name as 1', 'unit_group_fa as 2','car_group_fa as 3', 
                'car_type_fa as 4', 'machine_code as 5', 'estate_no as 6','build_date as 7','car_color as 8',
                'plak_number as 9','body_number as 10','dam_number as 11','motor_number as 12','vin_number as 13',
                'section_fa as 14','gps_code as 15','driver_name as 16','last_work_value as 17','unit_name as 18',
                'status_fa as 19', 'repair_hourly_price as 20','res1 as 21','mobile as 22','category_fa as 23' 
                ])->get();
        // ])->take(10)->get();
        // $projectsArray = $projectsArray->offset(0)->limit(10);
        // $projectsArray = CmmsDemo::all();
        $data = [
            'status' => 'succeed',
            'message' => '',
            'data' => $projectsArray
        ];
        // Storage::disk('public')->put('cmms.json', json_encode($data));
        // $data = \Storage::disk('public')->get('cmms.json');
        // $url = Storage::disk('public')->url('cmms.json');
        
        // $path = Storage::disk('public')->path('cmms.json');
        $zip = new ZipArchive;
        $zip->open('/var/www/rms/storage/app/public/cmms.zip',ZipArchive::CREATE); 
        // $zip->addFile('cmms.json');
        $zip->addFromString('cmms.json', json_encode($data));
        $zip->close();
         // End clock time in seconds 
         $end_time = microtime(true); 
         // Calculate script execution time 
         $execution_time = ($end_time - $start_time); 
         $time = "Execution time of script = ".$execution_time." sec\n";
        return response()->json([
            'time' => $time,
            'link' => 'https://rms.rmto.ir/storage/cmms.zip'
        ]);
        // $headers = array(
        //     'Content-Type' => 'application/octet-stream',
        // );
        // return Storage::download('cmms.zip', 'cmms.zip', $headers);
        // return response()->download('/var/www/rms/storage/app/public/cmms.zip',$headers);
        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => 'https://rms.rmto.ir/storage/cmms.zip'
        ], 200);
        // return response()->json([
        //     'status' => 'succeed',
        //     'message' => '',
        //     'data' => $projectsArray
        // ], 200);
    }
    public function index_view(){
        return view('rmto-preview.pages.cmms');
    }
    public function download(){
        $headers = array(
            'Content-Type: application/zip',
          );
        return response()->download(Storage::disk('public')->path('cmms.zip'),'cmms.zip',$headers);
    }
}
