<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Media as mMedia;
use App\General as mGeneral;
use App\Category as mCategory;
use App\Product as mProduct;
use App\ProductDetail as mProductDetail;
use App\MediaLocale as mMediaLocale;
use App\GeneralLocale as mGeneralLocale;
use App\CategoryLocale as mCategoryLocale;
use App\ProductLocale as mProductLocale;
use App\Shoppingcart as mCartLocale;
use App\Checkout as mCheckout;
use App\Shoppingcart as mCart;
use Storage;
use Image;
use Carbon\Carbon;

class BackendOrder extends Controller
{
  public function __construct()
  {
      $this->middleware('auth');
  }

  function index(Request $req){
    $data['order_list'] = mCheckout::latest()->get();
    $data['email_text'] = mMedia::where(['media_page'=>'order','media_section'=>'email-text'])->first();
    $data['page'] = 'order';
    return view('backend.order')->with($data);
  }

  function checkOrder(Request $req, $order_id = ""){
    if($order_id != ""):
      $data['checkout'] = mCheckout::where(['order_id'=>$order_id])->first();
      $data['cart'] = unserialize(mCart::where(['identifier'=>$order_id])->first()->content);
    endif;

    $data['order_id'] = $order_id;
    $data['page'] = "order";
    return view('backend.check_order')->with($data);
  }

  function changeStatus(Request $req, $order_id){
    $id = mCheckout::where(['order_id'=>$order_id])->first()->id;
    $db = mCheckout::find($id);
    $db->checkout_status = $req->status;
    $db->save();

    $req->session()->flash('message-status', 'Status Updated!');
    $req->session()->flash('alert-class', 'alert-success');
		$req->session()->flash('anchor', '#message-status');
    return back();
  }

  function updateEmail(Request $req){
    $text =  $req->email_text;
    if(empty($req->email_text_id)){
      # INSERT
      $db = new mMedia;
      $db->media_content = $text[config('app.locale')];
      $db->media_page = "order";
      $db->media_section = "email-text";
      $db->media_desc = "custom text for email to customer";
      $db->save();

      $last = mMedia::where(['media_page'=>'order','media_section'=>'email-text'])->first();
      foreach (config('translatable.locales') as $lang => $language) {
        $db = new mMediaLocale;
        $db->locale = $lang;
        $db->media_id = $last->id;
        $db->media_content = $text[$lang];
        $db->save();
      }
    }else{
      # UPDATE
      $db = mMedia::find($req->email_text_id);
      $db->media_content = $text[config('app.locale')];
      $db->save();

      foreach (config('translatable.locales') as $lang => $language) {
        $get = mMedia::find($req->email_text_id)->locales()->where('locale',$lang)->first();
        if(empty($get)){
          #create
          $db = new mMediaLocale;
          $db->locale = $lang;
          $db->media_id = $req->email_text_id;
          $db->media_content = $text[$lang];
          $db->save();
        }else{
          #update
          $db = mMediaLocale::find($get->id);
          $db->media_content = $text[$lang];
          $db->save();
        }

      }
    }

    $req->session()->flash('message-email', 'Updated Successfully!');
    $req->session()->flash('alert-class', 'alert-success');
		$req->session()->flash('anchor', '#message-email');
    return back();
  }
}
