<?php

/**
* Handle the form submissions
*
* @package Package
* @subpackage Sub Package
*/
class Esi_Management_News_Form {

    /**
    * Hook 'em all
    */
    public function __construct() {
        add_action( 'admin_init', array( $this, 'handle_form' ) );
    }

    /**
    * Handle the new new and edit form
    *
    * @return void
    */
    public function handle_form() {

        if ( ! isset( $_POST['submit_new'] ) ) {
            return;
        }

        if ( ! wp_verify_nonce( $_POST['_wpnonce'], '' ) ) {
            die( __( 'Are you cheating?', 'esi' ) );
        }

        if ( ! current_user_can( 'read' ) ) {
            wp_die( __( 'Permission Denied!', 'esi' ) );
        }

        $errors   = array();
        $page_url = admin_url( 'admin.php?page=esi-management-news' );
        $field_id = isset( $_POST['field_id'] ) ? intval( $_POST['field_id'] ) : 0;

        $title = isset( $_POST['title'] ) ? sanitize_text_field( $_POST['title'] ) : '';
        $description = isset( $_POST['description'] ) ? sanitize_text_field( $_POST['description'] ) : '';
        $url = isset( $_POST['url'] ) ? sanitize_text_field( $_POST['url'] ) : '';

        $publish_date = isset( $_POST['publish_date'] ) ? sanitize_text_field( $_POST['publish_date'] ) : '';
        $entity_id = isset( $_POST['entity_id'] ) ? intval( $_POST['entity_id'] ) : '';
        $media_id = isset( $_POST['media_id'] ) ? intval( $_POST['media_id'] ) : '';
        $type_id = isset( $_POST['type_id'] ) ? intval( $_POST['type_id'] ) : '';
        $region_id = isset( $_POST['region_id'] ) ? intval( $_POST['region_id'] ) : '';
        $investment_id = isset( $_POST['investment_id'] ) ? intval( $_POST['investment_id'] ) : '';

        $industry_id = isset( $_POST['radar_industry'] ) ? $_POST['radar_industry'] : array();
        $function_id = isset( $_POST['radar_function'] ) ? $_POST['radar_function'] : array();
        $technology_ids = isset( $_POST['technology_sub_id'] ) ? $_POST['technology_sub_id'] : array();

        $post_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : '';

        // some basic validation
        if ( ! $title ) {
            $errors[] = __( 'Error: Title is required', 'esi' );
        }

        if ( ! $description ) {
            $errors[] = __( 'Error: Description is required', 'esi' );
        }

        if ( ! $publish_date ) {
            $errors[] = __( 'Error: Publish Date is required', 'esi' );
        }

        if ( ! $entity_id ) {
            $errors[] = __( 'Error: entity is required', 'esi' );
        }

        if ( ! $media_id ) {
            $errors[] = __( 'Error: Media is required', 'esi' );
        }

        if ( ! $type_id ) {
            $errors[] = __( 'Error: Type is required', 'esi' );
        }

        // if ( ! $investment_id ) {
        //     $errors[] = __( 'Error: Investment is required', 'esi' );
        // }

        if ( ! $region_id ) {
            $errors[] = __( 'Error: Region is required', 'esi' );
        }

        if ( empty($industry_id) ) {
            $errors[] = __( 'Error: Radar industry is required', 'esi' );
        }

        if ( empty($function_id) ) {
            $errors[] = __( 'Error: Radar function is required', 'esi' );
        }

        // bail out if error found
        if ( $errors ) {
            $first_error = reset( $errors );
            $redirect_to = add_query_arg( array( 'error' => $first_error ), $page_url );
            //wp_safe_redirect( $redirect_to );
            //exit;

            return;

        }

        $fields = array(
            'title' => $title,
            'description' => $description,
            'url' => $url,
            'publish_date' => $publish_date,
            'entity_id' => $entity_id,
            'media_id' => $media_id,
            'type_id' => $type_id,
            'investment_id' => $investment_id,
            'region_id' => $region_id,
            'industry_id' => $industry_id,
            'function_id' => $function_id,
            'technology_ids' => $technology_ids,
            'post_id' => $post_id
        );

        // New or edit?
        if ( ! $field_id ) {
            $insert_id = esi_insert_new( $fields );
        } else {
            $fields['id'] = $field_id;
            $insert_id = esi_insert_new( $fields );
        }
        
        

        if ( is_wp_error( $insert_id ) ) {
            $redirect_to = add_query_arg( array( 'message' => 'error' ), $page_url );
        } else {
            $redirect_to = add_query_arg( array( 'message' => 'success' ), $page_url );
        }

        wp_safe_redirect( $redirect_to );
        exit;
    }
}

new Esi_Management_News_Form();
