<?php
namespace Indeed\Ulp\PostType;
if (!defined('ABSPATH')) exit();

class AnnouncementsUlp extends \CustomPostTypeUlp
{
    protected $post_type_slug = 'ulp_announcement';
	  protected $supports = [
                            'title',
                            'editor',
                            'thumbnail',
                            'author',
                            'comments',
    ];

    public function __construct(){
  		/// post type
  		$this->labels = array(
  								    'name'               => __('Announcements', 'ulp'),
  								    'singular_name'      => __('Announcement', 'ulp'),
  								    'add_new'            => __('Add new Announcement', 'ulp'),
  								    'add_new_item'       => __('Add new Announcement', 'ulp'),
  								    'edit_item'          => __('Edit Announcement', 'ulp'),
  								    'new_item'           => __('New Announcement', 'ulp'),
  								    'all_items'          => __('All Announcements', 'ulp'),
  								    'view_item'          => __('View Announcement', 'ulp'),
  								    'search_items'       => __('Search Announcement', 'ulp'),
  								    'not_found'          => '',
  								    'not_found_in_trash' => '',
  								    'parent_item_colon'  => '',
  								    'menu_name'          => __('Announcements', 'ulp'),
  		);
  		/// taxonomy
  		$this->taxonomy_labels = array(
  									'name'              => __('Announcements Categories', 'ulp'),
  									'singular_name'     => __('Announcement Category', 'ulp'),
  									'search_items'      => __('Search Announcement Category', 'ulp'),
  									'all_items'         => __('Announcements Categories', 'ulp'),
  									'parent_item'       => '',
  									'parent_item_colon' => '',
  									'edit_item'         => __('Edit', 'ulp'),
  									'update_item'       => __('Update', 'ulp'),
  									'add_new_item'      => __('Add new announcement category', 'ulp'),
  									'new_item_name'     => __('Add new announcement category', 'ulp'),
  									'menu_name'         => __('Announcements Categories', 'ulp'),
  		);
  		$this->taxonomy_slug = 'ulp_announcement_categories';
  		///meta boxes
  		$this->metaBoxes[] = array(
  												'slug' => 'courseMetaBox',
  												'title' => __('Course', 'ulp'),
  												'callback' => [$this, 'selectCourseMetaBox'],
  												'context' => 'normal', // normal || side || advanced
  												'priority' => 'high', /// high || low
  		);
  		$this->run(); /// from parent class
  	}

    public function selectCourseMetaBox($post=null)
    {
      $data = [
          'post_id' => isset($post->ID) ? $post->ID : 0,
          'courses' => \DbUlp::getAllCourses(),
      ];
      $data['ulp_course_id'] = get_post_meta($data['post_id'], 'ulp_course_id', true);
      if (empty($data['ulp_course_id']) && isset($_GET['course_id'])){
          $data['ulp_course_id'] = esc_sql($_GET['course_id']);
      }
      $view = new \ViewUlp();
      $view->setTemplate(ULP_PATH . 'views/admin/meta_boxes/announcement_select_course.php');
      $view->setContentData($data);
      echo $view->getOutput();
    }

    public function afterSavePost($post_id=0)
    {
        $postType = \DbUlp::getPostTypeById($post_id);
        if ($postType!=$this->post_type_slug){
            return;
        }
        $postStatus = \DbUlp::postStatus($post_id);
        if ($postStatus!=null && $postStatus!='auto-draft' && \DbUlp::get_post_meta($post_id, 'ulp_course_id')===null && \DbUlp::getPostTypeById($post_id)=='ulp_announcement' ){
            do_action('ulp_create_new_announcement', $post_id, @$_POST['ulp_course_id']);
        }
        if (isset($_POST['ulp_course_id'])){
            update_post_meta( $post_id, 'ulp_course_id', $_POST['ulp_course_id'] );
        }
    }

    public function getPostMetas($post_id=0, $keys=[])
    {

    }



}
