<?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_entity( $id );
$countries = esi_get_all_countries();
$entity_types = esi_get_all_entity_type_id();
$industries = array();
$industries = array_merge(esi_get_list_industry(), $industries);
$technologies = esi_get_all_list_technology();
$technologies_selected = esi_get_list_selected_entity_technology($id, 'array');
$investment_type = esi_get_all_investment_type(array('number' => 10000));
$entities = esi_get_all_entity(array('number' => 20000));
$tags = esi_get_all_tag(array('number' => 10000));
$tags_selected = esi_get_list_selected_entity_tag($id, 'array');

$industries_selected = esi_get_list_selected_entity_industry($id, 'array');


add_thickbox();
?>

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

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

        <table class="form-table">
            <tbody>
                <tr class="row-name">
                    <th scope="row">
                        <label for="name"><?php _e( 'Name', 'esi' ); ?></label>
                    </th>
                    <td>
                        <input type="text" name="name" id="name" class="regular-text" placeholder="<?php echo esc_attr( 'Add entity name', 'esi' ); ?>" value="<?php echo ($item)?esc_attr( $item->name ):''; ?>" required="required" />
                    </td>
                </tr>
                <tr class="row-description">
                    <th scope="row">
                        <label for="description"><?php _e( 'Description', 'esi' ); ?></label>
                    </th>
                    <td>
                        <textarea name="description" id="description"placeholder="<?php echo esc_attr( '', 'esi' ); ?>" rows="5" cols="30" required="required"><?php echo ($item)?esc_attr( $item->description ):''; ?></textarea>
                    </td>
                </tr>
                <tr class="row-country">
                    <th scope="row">
                        <label for="entity_type_id"><?php _e( 'Entity Type', 'esi' ); ?></label>
                    </th>
                    <td>
                        <select id="entity_type_id" name="entity_type_id" required="required">
                            <option value="0">Select an Entity Type</option>
                            <?php foreach ($entity_types as $entity_type) : ?>
                                <option value="<?= $entity_type->id ?>" <?= ($entity_type->id == $item->entity_type_id)?'selected="selected"':'' ?>><?= $entity_type->name ?></option>
                            <?php endforeach; ?>
                        </select>
                    </td>
                </tr>
                <tr class="row-url">
                    <th scope="row">
                        <label for="url"><?php _e( 'URL', 'esi' ); ?></label>
                    </th>
                    <td>
                        <input type="text" name="url" id="url" class="regular-text" placeholder="<?php echo esc_attr( 'Add entity URL', 'esi' ); ?>" value="<?php echo ($item)?esc_attr( $item->url ):''; ?>" required="required" />
                    </td>
                </tr>
                <tr class="row-country">
                    <th scope="row">
                        <label for="country"><?php _e( 'Country', 'esi' ); ?></label>
                    </th>
                    <td>
                        <select id="country" name="country">
                            <option value="0">Select a country</option>
                            <?php foreach ($countries as $country) : ?>
                                <option value="<?= $country->country_code ?>" <?= ($country->country_code == $item->country)?'selected="selected"':'' ?>><?= $country->country_name ?></option>
                            <?php endforeach; ?>
                        </select>
                    </td>
                </tr>
                <tr class="row-city">
                    <th scope="row">
                        <label for="url"><?php _e( 'City', 'esi' ); ?></label>
                    </th>
                    <td>
                        <input type="text" name="city" id="city" class="regular-text" placeholder="<?php echo esc_attr( 'Add city', 'esi' ); ?>" value="<?php echo ($item)?esc_attr( $item->city ):''; ?>" required="required" />
                    </td>
                </tr>
                <tr class="row-is-investor">
                    <th scope="row">
                        <?php _e( 'Is Investor', 'esi' ); ?>
                    </th>
                    <td>
                        <label for="is_investor"><input type="checkbox" name="is_investor" id="is_investor" value="on" <?php ($item)?checked( $item->is_investor, '1' ):''; ?> /> <?php _e( '', 'esi' ); ?></label>
                    </td>
                </tr>
                <tr class="row-founded-date">
                    <th scope="row">
                        <label for="founded-date"><?php _e( 'Founded In', 'esi' ); ?></label>
                    </th>
                    <td>
                        <input type="date" name="founded-date" id="founded-date" class="regular-text" placeholder="<?php echo esc_attr( '', 'esi' ); ?>" value="<?php echo ($item)?esc_attr($item->founded_in):''; ?>" />
                    </td>
                </tr>
                <tr class="row-employees">
                    <th scope="row">
                        <label for="employees"><?php _e( 'Number of Employees', 'esi' ); ?></label>
                    </th>
                    <td>
                        <input type="text" name="employees" id="employees" class="regular-text" placeholder="<?php echo esc_attr( 'Enter number employees', 'esi' ); ?>" value="<?php echo ($item)?esc_attr( $item->employees ):''; ?>" />
                    </td>
                </tr>
                <tr class="row-acquired-by">
                    <th scope="row">
                        <label for="acquired-by"><?php _e( 'Acquired by', 'esi' ); ?></label>
                    </th>
                    <td>
                        <select id="acquired" name="acquired" class="chosen-select">

                            <option value="0">Select an entity</option>
                            <?php foreach ($entities as $entity) : ?>
                                <option value="<?= $entity->id ?>" <?= ($item->acquired_by == $entity->id)?'selected="selected"':'' ?>><?= $entity->name ?></option>
                            <?php endforeach; ?>
                        </select>
                    </td>
                </tr>
                <tr class="row-acquired-date">
                    <th scope="row">
                        <label for="acquired-date"><?php _e( 'Acquired Date', 'esi' ); ?></label>
                    </th>
                    <td>
                        <input type="date" name="acquired-date" id="acquired-date" class="regular-text" placeholder="<?php echo esc_attr( '', 'esi' ); ?>" value="<?php echo ($item)?esc_attr($item->acquired_at):''; ?>" />
                    </td>
                </tr>
                <tr class="row-acquired-amount">
                    <th scope="row">
                        <label for="acquired-amount"><?php _e( 'Acquired Amount', 'esi' ); ?></label>
                    </th>
                    <td>
                        <input type="text" name="acquired-amount" id="acquired-amount" class="regular-text" placeholder="<?php echo esc_attr( 'Acquired Amount', 'esi' ); ?>" value="<?php echo ($item)?esc_attr( $item->acquired_amount ):''; ?>" />
                    </td>
                </tr>
                <tr class="row-total-raised">
                    <th scope="row">
                        <label for="total-raised"><?php _e( 'Total Raised', 'esi' ); ?></label>
                    </th>
                    <td>
                        <input type="text" name="total-raised" id="total-raised" class="regular-text" placeholder="<?php echo esc_attr( 'Total Raised', 'esi' ); ?>" value="<?php echo ($item)?esc_attr( $item->total_raised ):''; ?>" />
                    </td>
                </tr>
                <tr class="row-latest-round-type">
                    <th scope="row">
                        <label for="latest-round-type"><?php _e( 'Latest Round Type', 'esi' ); ?></label>
                    </th>
                    <td>
                        <select id="latest-round-type" name="latest-round-type" class="chosen-select" style="float:left">
                            <option value="0">Select an investment type</option>
                            <?php foreach ($investment_type as $type) : ?>
                                <option value="<?= $type->id ?>" <?= ($item->latest_round_type == $type->id)?'selected="selected"':'' ?>><?= $type->name ?></option>
                            <?php endforeach; ?>
                        </select>
                    </td>
                </tr>
                <hr />
                <tr class="row-entity-industry">
                    <th scope="row">
                        <label for="industry_id"><?php _e( 'Industry', 'esi' ); ?></label>
                    </th>
                    <td>
                        <select id="industry_id" name="industry_id[]">
                            <option value="0">Select an industry</option>
                            <?php foreach ($industries as $industry) : ?>
                                <option value="<?= $industry->id ?>" <?= (in_array($industry->id, $industries_selected)? 'selected="selected"':'') ?> ><?= $industry->name ?></option>
                            <?php endforeach; ?>
                        </select><br />
                    </td>
                </tr>
                <tr class="row-entity-sub-industry">
                    <th scope="row">
                    </th>
                    <td>
                        <select id="industry_sub_id" name="industry_sub_id[]" multiple="multiple">
                            <?php foreach ($industries_selected as $industry_selected) : ?>
                                <?php $current_industry = esi_get_industry($industry_selected);?>
                                <option value="<?= $industry_selected ?>" selected="selected" > <?= $current_industry[0]->name; ?></option>
                            <?php endforeach; ?>esi_get_industry
                        </select><br />
                    </td>
                </tr>
                <tr class="row-technology-id">
                    <th scope="row">
                        <label for="technology_id"><?php _e( 'Technology', 'esi' ); ?></label>
                    </th>
                    <td>
                        <select id="technology_id" class="chosen-select" name="technology_id[]" multiple="multiple" required="required">
                            <?php foreach ($technologies as $technology) : ?>
                                <option value="<?= $technology->id ?>" <?= (in_array($technology->id, $technologies_selected)) ? "selected='selected'" : ""?>><?= $technology->name ?></option>
                            <?php endforeach; ?>
                        </select><br />
                    </td>
                </tr>
                <tr class="row-tag-id">
                    <th scope="row">
                        <label for="tag_id"><?php _e( 'Tag', 'esi' ); ?></label>
                    </th>
                    <td>
                        <select id="tag_id" class="chosen-select" name="tag_id[]" multiple="multiple">
                            <?php foreach ($tags as $tag) : ?>
                                <option value="<?= $tag->id ?>" <?= (in_array($tag->id, $tags_selected)) ? "selected='selected'" : ""?>><?= $tag->name ?></option>
                            <?php endforeach; ?>
                        </select><br />
                    </td>
                </tr>
                <tr class="row-facebook">
                    <th scope="row">
                        <label for="facebook"><?php _e( 'Facebook', 'esi' ); ?></label>
                    </th>
                    <td>
                        <input type="text" name="facebook" id="facebook" class="regular-text" placeholder="<?php echo esc_attr( 'Facebook URL', 'esi' ); ?>" value="<?php echo ($item)?esc_attr( $item->facebook_link ):''; ?>" />
                    </td>
                </tr>
                <tr class="row-twitter">
                    <th scope="row">
                        <label for="twitter"><?php _e( 'Twitter', 'esi' ); ?></label>
                    </th>
                    <td>
                        <input type="text" name="twitter" id="twitter" class="regular-text" placeholder="<?php echo esc_attr( 'Twitter URL', 'esi' ); ?>" value="<?php echo ($item)?esc_attr( $item->twitter_link ):''; ?>" />
                    </td>
                </tr>
                <tr class="row-linkedin">
                    <th scope="row">
                        <label for="employees"><?php _e( 'LinkedIn', 'esi' ); ?></label>
                    </th>
                    <td>
                        <input type="text" name="linkedin" id="linkedin" class="regular-text" placeholder="<?php echo esc_attr( 'LinkedIn URL', 'esi' ); ?>" value="<?php echo ($item)?esc_attr( $item->linkedin_link ):''; ?>" />
                    </td>
                </tr>
            </tbody>
        </table>

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

        <?php wp_nonce_field( '' ); ?>
        <?php if ($_GET['action'] == 'view'): ?>
            <?php submit_button( __( 'Update entity', 'esi' ), 'primary', 'submit entity' ); ?>
        <?php else: ?>
            <?php submit_button( __( 'Add entity', 'esi' ), 'primary', 'submit entity' ); ?>
        <?php endif; ?>
    </form>
</div>
