<?php

namespace App\Http\Controllers;

use App\Models\Shipment;
use App\Models\ShipmentHistory;
use Illuminate\Http\Request;

class CourierController extends Controller
{
    //
    function index(){
        $shipment = Shipment::all();
        return view('admin.shipment.index', compact('shipment'));
    }
    function create(){
        return view('admin.shipment.add');
    }
    function store(Request $request){
        // Validate???
        $code = shipCode();
        $input = $request->all();
        $input['code'] = $code;
        $shipment = Shipment::create($input);
        return redirect()->route('admin.shipment.history', $code)->withSuccess("Shipment Added Successfully.");

    }
    function edit($code){
        $shipment = Shipment::whereCode($code)->first();
        return view('admin.shipment.edit', compact('shipment'));
    }
    function delete($code){
        $shipment = Shipment::whereCode($code)->delete();
        return back()->withSuccess('Shipment Deleted Successfully');
    }
    
    function update($code, Request $request){
        $shipment = Shipment::whereCode($code)->first();
        $request->all();
        //Send EMail
        $email = $request->receiver_email;
        $subj = "Shipment Tracking Information";
        $mesg = "
            <p>Dear {$shipment->receiver_name}, </p>
            <p>We are pleased to inform you that your shipment is registered with us at <strong> ". get_setting('title')."</strong> and is now in ". shipment_status($request->status). ".</p>
        
            <h3>TRACKING INFORMATION</h3>
            <p><strong>Tracking number:</strong> [{$shipment->code}]</p>
        
            <p>We hope this meets with your approval. Please do not hesitate to get in touch if we can be of any further assistance.</p>
        
            <p>Yours sincerely,</p>
            
            <p>If you wish to track this shipment, please visit the Company website below:</p>
            <p>Website: ".route('index')." </p>

        ";
        $shipment->update($request->all());

        general_email($email, $mesg, $subj);
        
        return back()->withSuccess('Shipment Updated Successfully');
    }
    // shipment History
    function history($code){
        $shipment = Shipment::whereCode($code)->first();
        return view('admin.shipment.history', compact('shipment'));
    }
    function store_history($code, Request $request){
        //return $request->all();
        $shipment = Shipment::whereCode($code)->first();
        $shipment->history()->create($request->all());
        //Send EMail
        $email = $shipment->receiver_email;
        $subj = "Shipment Tracking Information";
        $mesg = "
            <p>Dear {$shipment->receiver_name}, </p>
            <p>We are pleased to inform you that your shipment is registered with us at <strong> ". get_setting('title')."</strong> and is now in ". shipment_status($shipment->status). ".</p>
        
            <h3>TRACKING INFORMATION</h3>
            <p><strong>Remark :</strong> [{$request->remark}]</p>
            <p><strong>Tracking number:</strong> [{$shipment->code}]</p>
        
            <p>We hope this meets with your approval. Please do not hesitate to get in touch if we can be of any further assistance.</p>
        
            <p>Yours sincerely,</p>
            
            <p>If you wish to track this shipment, please visit the Company website below:</p>
            <p>Website:  ".route('index')."</p>

        ";
        general_email($email, $mesg, $subj);
        return back()->withSuccess('Track History Added Successfully');
    }
    function update_history($code, $id, Request $request){
        $shipment = Shipment::whereCode($code)->first();
        $history = ShipmentHistory::find($id)->update($request->all());
        //Send EMail
        $email = $shipment->receiver_email;
        $subj = "Shipment Tracking Information";
        $mesg = "
            <p>Dear {$shipment->receiver_name}, </p>
            <p>We are pleased to inform you that your shipment is registered with us at <strong> ". get_setting('title')."</strong> and is now in ". shipment_status($shipment->status). ".</p>
        
            <h3>TRACKING INFORMATION</h3>
            <p><strong>Remark :</strong> [{$request->remark}]</p>
            <p><strong>Tracking number:</strong> [{$shipment->code}]</p>
        
            <p>We hope this meets with your approval. Please do not hesitate to get in touch if we can be of any further assistance.</p>
        
            <p>Yours sincerely,</p>
            
            <p>If you wish to track this shipment, please visit the Company website below:</p>
            <p>Website: ".route('index')."</p>

        ";
        general_email($email, $mesg, $subj);
        return back()->withSuccess('Track History Updated Successfully');
    }

    function delete_history($id){
        $history = ShipmentHistory::findorFail($id);
        $history ->delete();
        return back()->withSuccess('Tracking History Deleted Successfully');
    }


    // Settings
    function setting(){
        return view('admin.shipment.setting');
    }
}
