<?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'] ) : '';
        $ability_ids = isset( $_POST['ability_id'] ) ? ( $_POST['ability_id'] ) : '';

        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
        );

        // 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 );
        }

        esi_save_list_entity_technology($fields['id'], $technology_ids);
        esi_save_list_entity_ability($fields['id'], $ability_ids);

        if($is_ajax){
            return $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_Entity_Form();
