<?php

namespace Custom;

use SilverStripe\Forms\DateField;
use SilverStripe\Forms\TextareaField;
use SilverStripe\Forms\TextField;
use SilverStripe\Assets\Image;
use SilverStripe\Assets\File;
use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\Forms\CheckboxSetField;
use SilverStripe\Forms\DropdownField;
use Page;

class ArticlePage extends Page
{
	private static $can_be_root = false;

	private static $db = [
		'Date' => 'Date',
		'AuthorDiscripe' => 'Text',
		'AuthorName' => 'Varchar',
        'Fblink' => 'Varchar',
        'Instagramlink' => 'Varchar',
        'Linkedinlink' => 'Varchar',
	];

    private static $has_one = [
        'CoverPhoto' => Image::class,
        'AuthorImage' => Image::class,

        'Region' => Region::class,
  	];

    private static $many_many = [
        'Categories' => ArticleCategory::class,
    ];

    private static $has_many = [
        'Comments' => ArticleComment::class,
    ];


  	private static $owns = [
  		'CoverPhoto',
          'AuthorImage',


  	];

	public function getCMSFields()
	{
		$fields = parent::getCMSFields();
		$fields->addFieldToTab('Root.Main', DateField::create('Date','Date of article'));
        $fields->addFieldToTab('Root.Main', TextareaField::create('AuthorDiscripe')
			->setDescription('Discripe about your self'));
        $fields->addFieldToTab('Root.Main', TextField::create('AuthorName','Author Name'));
        $fields->addFieldToTab('Root.Main', TextField::create('Fblink','Fb link'));
        $fields->addFieldToTab('Root.Main', TextField::create('Instagramlink','Instagram Link'));
        $fields->addFieldToTab('Root.Main', TextField::create('Linkedinlink','LinkedIn link'));
        $fields->addFieldToTab(
			'Root.Attachments',
			$cphoto = UploadField::create('CoverPhoto')
		);
        $fields->addFieldToTab(
			'Root.Attachments',
			$aphoto = UploadField::create('AuthorImage')
		);
        $cphoto->setFolderName('Blog-CoverPhotos');
        $aphoto->setFolderName('Author-Images');



        $fields->addFieldToTab('Root.Categories', CheckboxSetField::create(
            'Categories',
            'Selected categories',
            $this->Parent()->Categories()->map('ID','Title')
        ));

        $fields->addFieldToTab('Root.Main', DropdownField::create(
            'RegionID',
            'Region',
            Region::get()->map('ID','Title')
        )->setEmptyString('-- None --'), 'Content');

		return $fields;
	}

    public function CategoriesList()
    {
        if($this->Categories()->exists()) {
            return implode(', ', $this->Categories()->column('Title'));
        }

        return null;
    }

}
