<?php

namespace App\Http\Controllers;

use App\Http\Controllers\IssueController;
use App\Models\Orders;
use App\User;
use DB;
use Auth;
use File;
use Mpdf\Mpdf;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Redirect;



class OrdersController extends BenthamController
{

   
   public function Order($action="",Request $request)
   {     
        $editPermission   = false;
        $deletePermission = false;

        if (checkPermission('orders', 'edit')) {
            $editPermission = true;
        }if (checkPermission('orders', 'delete')) {
            $deletePermission = true;
        }


        switch($action)
        {
           case 'list':

             $order=$this->GetOrderList($request);  

            return View::make('admin.orders.orders')
            ->with('orders', $order['orders'])
            ->with('ord_status', $order['ord_status'])
            ->with('order_counts', $order['order_counts'])
            ->with('editPermission', $editPermission)
            ->with('deletePermission', $deletePermission);
            break;

           case 'detail':

              if(isset($request->add_comments))
              {
                 $this->UpdateOrderDetails($request);  
              }

             $details=$this->GetOrderDetails($request);  

            return View::make('admin.orders.order_detail')
            ->with('order', $details['order'])
            ->with('user', $details['user'])
            ->with('payments', $details['payments'])
            ->with('balance', $details['balance'])
            ->with('products', $details['products'])
            ->with('comments', $details['comments'])
            ->with('admin_comments', $details['admin_comments'])
            ->with('order_logs', $details['order_logs'])
            ->with('ord_status', $details['ord_status'])
            ->with('editPermission', $editPermission)
            ->with('deletePermission', $deletePermission);
            break;

           case 'invoice':

             $details=$this->GetOrderDetails($request);  

             $mpdf = new Mpdf(['mode' => 'utf-8', 'format' => 'A4', 'margin_left' => '5', 'margin_right' => '5','tempDir' => __DIR__ . '/../../tmp']);

            $html= View::make('admin.orders.template.invoice')
            ->with('order', $details['order'])
            ->with('user', $details['user'])
            ->with('payments', $details['payments'])
            ->with('balance', $details['balance'])
            ->with('products', $details['products'])
            ->with('comments', $details['comments'])
            ->with('admin_comments', $details['admin_comments'])
            ->with('order_logs', $details['order_logs'])
            ->with('ord_status', $details['ord_status'])
            ->with('editPermission', $editPermission)
            ->with('deletePermission', $deletePermission);
            $mpdf->WriteHTML($html);
            $mpdf->Output();
            exit();

            break;
			
        }
    } 

     /*
     ----------------------------frontend order listing-----------------------------------
    */
     public function GetOrderList($request)
     {  
        $order_counts = Orders::GetOrderList($request);
        $orders = Orders::GetOrderList($request,20);
        $ord_status = Orders::GetOrderStatus();
        return with(['orders' => $orders, 'ord_status' => $ord_status, 'order_counts' => $order_counts]);
     }
     /*
     ----------------------------frontend order Details-----------------------------------
    */
     public function GetOrderDetails($request)
     {
        $balance=0;
        $paid_amounts=0;
        $order = Orders::GetOrder($request->id);
        $user = User::getUserById($order->uid);
        $products = Orders::GetOrderProducts($request->id);
        $payments = Orders::GetOrderPaymentReceipts($request->id);
        if($payments)
        {
          foreach($payments as $paid)
          {
            $paid_amounts=$paid_amounts+$paid->amount;
          }
          $balance=$order->order_total-$paid_amounts;
        }
        $comments = Orders::GetOrderComments($request->id);
        $admin_comments = Orders::GetOrderAdminComments($request->id);
        $order_logs = Orders::GetOrderLogs($request->id);
        $ord_status = Orders::GetOrderStatus();

        return with(['order' => $order,
                    'user' => $user,
                    'payments' => $payments,
                    'balance' => $balance,
                    'products' => $products,
                    'comments' => $comments,
                    'admin_comments' => $admin_comments,
                    'order_logs' => $order_logs,
                    'ord_status' => $ord_status]);
     }
     /*
     ----------------------------Update order Details-----------------------------------
    */
     public function UpdateOrderDetails($request)
     {  
        $user     = Auth::user();
        $notification=0;
        if($request->order_status != "" || $request->order_comment != "")
        { 
          if(isset($request->notified)){$notification=$request->notified;}
          
          $add_comments  = array();
          $updateDetails = array();
          $orderlog = array();

          $add_comments = array('uid'    => $user->id,
              'order_id'                 => $request->id,
              'order_status'             => $request->order_status,
              'notified'                 => $notification,
              'message'                  => $request->order_comment,
              'created'                  => time());

          Orders::AddOrderComments($add_comments);

          $updateDetails=array(
          'order_status' => $request->order_status,
          'modified'       => time());

          Orders::UpdateOrder($request->id,$updateDetails);

          if($request->order_status != $request->old_order_status)
          { 
            $orderlog=array('uid'    => $user->id,
              'order_id'             => $request->id,
              'changes'              => 'Order status changed from <em>'.$request->old_order_status.'</em> to <em>'.$request->order_status.'</em>.',
              'created'              => time());

            Orders::AddOrderLog($orderlog);
          }

        }

        if($request->admin_comment != "")
        {
          $add_admin_comments  = array();
          $add_admin_comments = array('uid'    => $user->id,
              'order_id'                 => $request->id,
              'message'                  => $request->admin_comment,
              'created'                  => time());

          Orders::AddOrderAdminComments($add_admin_comments);
        }

     }

}
