<?php

namespace App\Http\Controllers;

use Request;
use Validator;
use App\Models\PromotionUser;
use App\Models\PromotionAdmin;
use App\Models\PromotionAdminProduct;
use App\Repositories\AdminPromotionRepository;



class AdminPromotionController extends Controller
{
    protected $promoRepo;

    public function __construct(AdminPromotionRepository $promoRepo)
    {
        $this->middleware('admin');
        $this->promoRepo = $promoRepo;

    }

    public function index()
    {
        $data = [
            'values' =>  PromotionAdmin::all(),
        ];
        return view('admin.promotion.index', $data);
    }

    public function detail($id){
        if($id == 0 || $id === 'new'){
            $promotion = new PromotionAdmin();
            $promotion->id = 0;

            $promotion->active = true;
        }else{
            $promotion = PromotionAdmin::findOrFail($id);
        }
      
        $data = [
             'promotion' => $promotion,

        ];
        return view('admin.promotion.detail', $data);
    }


    public function store($id)
    {
        $data = Request::all();
        if(isset($data['action']) && $data['action'] === 'save-promotion_product'){
            $model = $this->promoRepo->updateProduct($id, Request::all());
        }

        if(isset($data['action']) && $data['action'] === 'save-promotion'){
            $rules = array(
                'name' => 'required',
                'type' => 'required',
            );
            $validator = Validator::make(Request::all(), $rules);
            if ($validator->fails()) {
                return redirect(route('admin_promotion_detail', [$id]))->withErrors($validator)->withInput(Request::all());
            }
            $model = $this->promoRepo->update($id, Request::all());
        }
       
    
        \Session()->flash('alert-save', true);
        return redirect(route('admin_promotion_detail', [$model->id]));
    }

    public function delete($id, $del = false)
    {
       if($del === 'promotion_admin_product'){
            $PromotionAdminProduct = PromotionAdminProduct::findOrFail($id);
            $PromotionAdmin = $PromotionAdminProduct->promotion_admin;
            if($PromotionAdminProduct->canDelete()){
                \Session()->flash('alert-success', "Promotion Produkt gelöscht");
                $PromotionAdminProduct->delete();
            }else{
                \Session()->flash('alert-error', "Promotion Produkt kann nicht gelöscht werden, schon in Verwendung.");
            }
           return redirect(route('admin_promotion_detail', [$PromotionAdmin->id]));
       }

       if($del === 'admin_promotion'){
            $PromotionAdmin = PromotionAdmin::findOrFail($id);
            if($PromotionAdmin->canDelete()){
                foreach($PromotionAdmin->promotion_admin_products as $promotion_admin_product){
                    $promotion_admin_product->delete();
                }
                $PromotionAdmin->delete();
                \Session()->flash('alert-success', "Promotion gelöscht");
            }else{
                \Session()->flash('alert-error', "Promotion kann nicht gelöscht werden, schon in Verwendung.");
            }
            return redirect(route('admin_promotions'));

       }
    
    }

    public function show($by, $id = null){

        $data = [
            'id' => $id,
            'by' => $by,
        ];

        if($by === 'promotion'){
            $data['promotion'] = PromotionAdmin::findOrFail($id);
        }
        if($by === 'all'){
            $data['promotion'] = null;

        }
        return view('admin.promotion.show', $data);
    }

    public function datatable($by, $id = null){

        $query = PromotionUser::with('promotion_user_products', 'user')->select('promotion_users.*');
        if($by === 'promotion'){
           // $query->where('promotion_admin_id', $id);
        }

        return \DataTables::eloquent($query)
            ->addColumn('id', function (PromotionUser $PromotionUser) {
                return $PromotionUser->id; //'<a href="" class="btn icon-btn btn-sm btn-primary">'.$PromotionUser->id.'</a>';
            })
            ->addColumn('user', function (PromotionUser $PromotionUser) {
                if($PromotionUser->user){
                    return $PromotionUser->user->getFullName();
                }
                return "";
            })
            ->addColumn('name', function (PromotionUser $PromotionUser) {
                if(strlen($PromotionUser->name) > 30){
                    return substr($PromotionUser->name, 0, 30)."...";
                }
                return $PromotionUser->name;
            })
            ->addColumn('products_active', function (PromotionUser $PromotionUser) {
                return $PromotionUser->promotion_user_products_active->count();
            })
            ->addColumn('count_open_items', function (PromotionUser $PromotionUser) {
                return $PromotionUser->getCountOpenItems();
            })
            ->addColumn('count_sell_items', function (PromotionUser $PromotionUser) {
                return $PromotionUser->getCountSellItems();
            })
            ->addColumn('user_promotion_cart_price', function (PromotionUser $PromotionUser) {
                $back = $PromotionUser->calculateCart();
                return formatNumber($back['price'])." €";
            })
            ->addColumn('user_promotion_sell_price', function (PromotionUser $PromotionUser) {
                $back = $PromotionUser->calculateSell();
                return formatNumber($back['price'])." €";
            })
            ->addColumn('user_credit', function (PromotionUser $PromotionUser) {
                return formatNumber($PromotionUser->user->payment_credit)." €";;
            })
            ->addColumn('active', function (PromotionUser $PromotionUser) {
                return  get_active_badge($PromotionUser->active);
            })
            ->addColumn('pick_up', function (PromotionUser $PromotionUser) {
                return  get_active_badge($PromotionUser->pick_up);
            })
            ->addColumn('user_delete', function (PromotionUser $PromotionUser) {
                return  get_active_badge(($PromotionUser->user_deleted_at != null));
            })


           
            ->orderColumn('id', 'id $1')
            ->orderColumn('name', 'name $1')
            //->orderColumn('status', 'status $1')
            ->orderColumn('active', 'active $1')
            ->orderColumn('pick_up', 'pick_up $1')
           // ->orderColumn('user_delete', 'user_delete $1')


            ->rawColumns(['id', 'status', 'active', 'pick_up', 'user_delete'])
            ->make(true);
    }
}