<?php

namespace App\Http\Controllers\Backend;

use App\Components\Functions;
use App\Models\Popup;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Yajra\Datatables\Datatables;

class PopupController extends Controller {
	//
	public function index() {
		return view( 'admin.popup.index' );
	}

	public function store( Request $request ) {
		$data = $request->all();

		if ( $request->file( 'image' ) && $request->file( 'image' )->isValid() ) {
			$data['image'] = Functions::uploadImage( $request->file( 'image' ) );
		}

		$startTimeObj = null;
		$endTimeObj   = null;

		if ( ! empty( $data['start_time'] ) ) {
			$startTimeObj       = Carbon::createFromFormat( 'd/m/Y', $data['start_time'] );
			$data['start_time'] = Carbon::createFromFormat( 'd/m/Y', $data['start_time'] )->toDateString();


		}

		if ( ! empty( $data['end_time'] ) ) {
			$endTimeObj       = Carbon::createFromFormat( 'd/m/Y', $data['end_time'] );
			$data['end_time'] = Carbon::createFromFormat( 'd/m/Y', $data['end_time'] )->toDateString();

		}

		if ( $startTimeObj->greaterThan( $endTimeObj ) ) {
			return redirect()->back()->with( 'error', 'Thời gian bắt đầu không thể lớn hơn thời gian kết thúc' );
		}

		Popup::create( $data );

		return response( [
			'status'  => 1,
			'message' => 'Thành công'
		] );
	}

	public function delete( $id ) {
		$item = Popup::find( $id );
		if ( ! $item ) {
			return redirect()->back()->with( 'error', 'Dữ liệu không hợp lệ' );
		}

		$item->delete();

		return redirect()->back()->with( 'success', 'Thành công' );
	}

	public function edit( $id ) {
		$item = Popup::find( $id );
		if ( ! $item ) {
			return redirect()->back()->with( 'error', 'Dữ liệu không hợp lệ' );
		}

		return view( 'admin.popup.edit', compact( 'item' ) );
	}

	public function update( $id, Request $request ) {
		$item = Popup::find( $id );
		if ( ! $item ) {
			return redirect()->back()->with( 'error', 'Dữ liệu không hợp lệ' );
		}

		$data = $request->except('type');

		if ( $request->file( 'image' ) && $request->file( 'image' )->isValid() ) {
			$data['image'] = Functions::uploadImage( $request->file( 'image' ) );
		}

		$startTimeObj = null;
		$endTimeObj   = null;

		if ( ! empty( $data['start_time'] ) ) {
			$data['start_time'] = Carbon::createFromFormat( 'd/m/Y', $data['start_time'] )->toDateString();
			$startTimeObj       = Carbon::createFromFormat( 'Y-m-d', $data['start_time'] );

		}

		if ( ! empty( $data['end_time'] ) ) {
			$data['end_time'] = Carbon::createFromFormat( 'd/m/Y', $data['end_time'] )->toDateString();
			$endTimeObj       = Carbon::createFromFormat( 'Y-m-d', $data['end_time'] );
		}

		if ( $startTimeObj->greaterThan( $endTimeObj ) ) {
			return redirect()->back()->with( 'error', 'Thời gian bắt đầu không thể lớn hơn thời gian kết thúc' );
		}


		$item->update( $data );

		return redirect()->back()->with( 'success', 'Cập nhật thành công' );
	}

	public function getPopupByAttribute( Request $request ) {
		$type  = $request->input( 'type' );
		$items = Popup::query()->where( 'type', $type );

		return Datatables::of( $items )->editColumn( 'image', function ( $item ) {

			if ( ! empty( $item->image ) ) {
				return '<img style="max-width: 150px" src="/files/' . $item->image . '" />';
			}

		} )->editColumn( 'start_time', function ( $item ) {

			if ( ! empty( $item->start_time ) ) {
				return $item->start_time->format( 'd/m/Y' );
			}

		} )->editColumn( 'end_time', function ( $item ) {

			if ( ! empty( $item->end_time ) ) {
				return $item->end_time->format( 'd/m/Y' );
			}

		} )->editColumn( 'status', function ( $item ) {

			return $item->status_text;

		} )->addColumn( 'action', function ( $item ) {

			$activeText = 'Kích hoạt';

			if ( $item->status == Popup::ACTIVE ) {
				$activeText = 'Hủy Kích hoạt';
			}

			return '<a href="#" class="btn btn-sm red btn-outline btn-active" data-id="' . $item->id . '" data-type="course">' . $activeText . '</a>' .
			       '<a href="' . url( 'admin/popup/edit', [ 'id' => $item->id ] ) . '" class="btn btn-sm green btn-outline "> Sửa</a>' .
			       '<a href="' . url( 'admin/popup/delete', [ 'id' => $item->id ] ) . '" class="btn btn-sm red btn-outline delete-btn"> Xóa</a><br><br>';

		} )->make( true );
	}
}
