<?php

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

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

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

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

        $entity_id = isset( $_POST['entity_id'] ) ? intval( $_POST['entity_id'] ) : '';
        $investment_type_id = isset( $_POST['investment_type_id'] ) ? intval( $_POST['investment_type_id'] ) : '';
        $amount = isset( $_POST['amount'] ) ? sanitize_text_field( $_POST['amount'] ) : '';
        $investment_date = isset( $_POST['investment_date'] ) ? sanitize_text_field( $_POST['investment_date'] ) : '';
        $investor = isset( $_POST['investor'] ) ? $_POST['investor'] : array();

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

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

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

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

        if ( ! $investor ) {
            $errors[] = __( 'Error: Investor 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(
            'entity_id' => $entity_id,
            'investment_type_id' => $investment_type_id,
            'amount' => $amount,
            'investment_date' => $investment_date,
            'investor' => $investor,
        );


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

            $insert_id = esi_insert_investment( $fields );
            $fields['id'] = $insert_id;

        } else {

            $fields['id'] = $field_id;
            $insert_id = esi_insert_investment( $fields );
        }

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