<?php

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

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



    /**
    * Handle the new entity and edit form
    *
    * @return void
    */
    public function handle_form($is_ajax = false) {

        if ( !isset( $_POST['submit_entity'] ) && !$is_ajax ) {
            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-entity' );
        $field_id = isset( $_POST['field_id'] ) ? intval( $_POST['field_id'] ) : 0;

        $name = isset( $_POST['name'] ) ? sanitize_text_field( $_POST['name'] ) : '';
        $description = isset( $_POST['description'] ) ? sanitize_text_field( $_POST['description'] ) : '';
        $url = isset( $_POST['url'] ) ? sanitize_text_field( $_POST['url'] ) : '';
        $country = isset( $_POST['country'] ) ? sanitize_text_field( $_POST['country'] ) : '';
        $city = isset( $_POST['city'] ) ? sanitize_text_field( $_POST['city'] ) : '';
        $is_investor = isset( $_POST['is_investor'] ) ? sanitize_text_field( $_POST['is_investor'] ) : '';
        $entity_type_id = isset( $_POST['entity_type_id'] ) ? sanitize_text_field( $_POST['entity_type_id'] ) : '';
        $employees = isset( $_POST['employees'] ) ? (int)sanitize_text_field( $_POST['employees'] ) : '';
        $founded_date = isset( $_POST['founded-date'] ) ? sanitize_text_field( $_POST['founded-date'] ) : '';
        $acquired_by = isset( $_POST['acquired'] ) ? intval( $_POST['acquired'] ) : '';
        $acquired_date = isset( $_POST['acquired-date'] ) ? sanitize_text_field( $_POST['acquired-date'] ) : '';
        $acquired_amount = isset( $_POST['acquired-amount'] ) ? (int)sanitize_text_field( $_POST['acquired-amount'] ) : '';
        $total_raised = isset( $_POST['total-raised'] ) ? (int)sanitize_text_field( $_POST['total-raised'] ) : '';
        $latest_round_type = isset( $_POST['latest-round-type'] ) ? (int)sanitize_text_field( $_POST['latest-round-type'] ) : '';
        $technology_ids = isset( $_POST['technology_id'] ) ? ( $_POST['technology_id'] ) : '';
        $tag_ids = isset( $_POST['tag_id'] ) ? ( $_POST['tag_id'] ) : '';
        $industry_ids = isset( $_POST['industry_id'] ) ? ( $_POST['industry_id'] ) : '';
        $industry_sub_ids = isset( $_POST['industry_sub_id'] ) ? ( $_POST['industry_sub_id'] ) : '';
        $facebook = isset( $_POST['facebook'] ) ? ( $_POST['facebook'] ) : '';
        $twitter = isset( $_POST['twitter'] ) ? ( $_POST['twitter'] ) : '';
        $linkedin = isset( $_POST['linkedin'] ) ? ( $_POST['linkedin'] ) : '';

        if ($is_investor == "on") {
            $is_investor = 1;
        }

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

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

        if ( ! $url ) {
            $errors[] = __( 'Error: Description 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;
        }

        $fields = array(
            'name' => $name,
            'description' => $description,
            'url' => $url,
            'is_investor' => $is_investor,
            'country' => $country,
            'city' => $city,
            'entity_type_id' => $entity_type_id,
            'employees' => $employees,
            'founded_in' => $founded_date,
            'acquired_by' => $acquired_by,
            'acquired_at' => $acquired_date,
            'acquired_amount' => $acquired_amount,
            'total_raised' => $total_raised,
            'latest_round_type' => $latest_round_type,
            'facebook_link' => $facebook,
            'twitter_link' => $twitter,
            'linkedin_link' => $linkedin
        );

        // New or edit?
        if ( ! $field_id ) {

            $insert_id = esi_insert_entity( $fields );

            $fields['id'] = $insert_id;

        } else {

            $fields['id'] = $field_id;

            $insert_id = esi_insert_entity( $fields );
        }

        $industry_ids = array_merge($industry_ids, $industry_sub_ids);

        esi_save_list_entity_technology($fields['id'], $technology_ids);
        esi_save_list_entity_tag($fields['id'], $tag_ids);
        esi_save_list_entity_industry($fields['id'], $industry_ids);

        if($is_ajax){
            return $fields;
        }

        if ( is_wp_error( $insert_id ) ) {
            $redirect_to = add_query_arg( array( 'error' => $insert_id->get_error_messages() ), $page_url );
        } else {
            $redirect_to = add_query_arg( array( 'success' => 'Update Entity Success' ), $page_url );
        }
        
        wp_safe_redirect( $redirect_to );
        exit;
    }
}

new Esi_Management_Entity_Form();
