<?php

/**
* Provide a admin area view for the plugin
*
* This file is used to markup the admin-facing aspects of the plugin.
*
* @link       http://www.exapartners.com/
* @since      1.0.0
*
* @package    Esi_Management
* @subpackage Esi_Management/admin/partials
*/
global $wpdb;

$item = esi_get_investment( $id );
$companies = esi_get_all_entity(array('number' => 10000));
$investmentTypeList = esi_get_all_investment_type(array('number' => 10000));
$investorArray = esi_get_investor_array($id);

add_thickbox();
?>

<div class="wrap">
    <h1><?php _e( 'Add Investment', 'esi' ); ?></h1>

    <form action="" method="post">

        <table class="form-table">
            <tbody>
                <tr class="row-entity-id">
                    <th scope="row">
                        <label for="entity_id"><?php _e( 'entity', 'esi' ); ?></label>
                    </th>
                    <td>
                        <select id="entity_id" name="entity_id">
                            <option value="0">Select a entity</option>
                            <?php foreach ($companies as $entity) : ?>
                                <option value="<?= $entity->id ?>" <?= ($item->entity_id == $entity->id)?'selected="selected"':'' ?>><?= $entity->name ?></option>
                            <?php endforeach; ?>
                        </select>
                    </td>
                </tr>
                <tr class="row-investment-type-id">
                    <th scope="row">
                        <label for="investment_type_id"><?php _e( 'Investment Type', 'esi' ); ?></label>
                    </th>
                    <td>
                        <select id="investment_type_id" name="investment_type_id">
                            <option value="0">Select an investment type</option>
                            <?php foreach ($investmentTypeList as $investmentType) : ?>
                                <option value="<?= $investmentType->id ?>" <?= ($item->investment_type_id == $investmentType->id)?'selected="selected"':'' ?>><?= $investmentType->name ?></option>
                            <?php endforeach; ?>
                        </select>
                    </td>
                </tr>
                <tr class="row-investor">
                    <th scope="row">
                        <label for="investor"><?php _e( 'Investor', 'esi' ); ?></label>
                    </th>
                    <td>
                        <select id="investor" name="investor[]" data-placeholder="Choose a entity..." multiple="" class="chosen-select">
                            <?php foreach ($companies as $entity) : ?>
                                <option value="<?= $entity->id ?>" <?= (in_array($entity->id, $investorArray))?'selected="selected"':'' ?>><?= $entity->name ?></option>
                            <?php endforeach; ?>
                        </select>
                    </td>
                </tr>
                <tr class="row-amount">
                    <th scope="row">
                        <label for="amount"><?php _e( 'Amount', 'esi' ); ?></label>
                    </th>
                    <td>
                        <input type="text" name="amount" id="amount" class="regular-text" placeholder="<?php echo esc_attr( 'Add investment amount', 'esi' ); ?>" value="<?php echo ($item)?esc_attr( $item->amount ):''; ?>" required="required" />
                    </td>
                </tr>
                <tr class="row-investment-date">
                    <th scope="row">
                        <label for="investment_date"><?php _e( 'Investment Date', 'esi' ); ?></label>
                    </th>
                    <td>
                        <input type="date" name="investment_date" id="investment_date" class="regular-text" placeholder="<?php echo esc_attr( '', 'esi' ); ?>" value="<?php echo ($item)?esc_attr( explode(' ', $item->investment_date)[0] ):''; ?>" required="required" />
                    </td>
                </tr>
            </tbody>
        </table>

        <input type="hidden" name="field_id" value="<?php echo ($item)?$item->id:0; ?>">

        <?php wp_nonce_field( '' ); ?>
        <?php submit_button( __( 'Add investment', 'esi' ), 'primary', 'submit investment' ); ?>

    </form>
</div>
