<?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;
$title = $content = '';
$newsUrl = '';

if($_GET['post'] > 0){
    $queried_post = get_post($_GET['post']);
    $title = $queried_post->post_title;
    preg_match('@((https?://)?([-\\w]+\\.[-\\w\\.]+)+\\w(:\\d+)?(/([-\\w/_\\.]*(\\?\\S+)?)?)*)@',$queried_post->post_content,$a);
    if(!empty($a[0])){
        $newsUrl = $a[0];
        $mediaUrl = parse_url($newsUrl);
        $media_id = esi_insert_media(array('name' => $mediaUrl['host'], 'url' => $mediaUrl['scheme'].'://'.$mediaUrl['host']));
    }
    $content = strip_tags($queried_post->post_content);
}

$item = esi_get_new( $id );
$radars = esi_get_all_radar(array('number' => 10000));
$medias = esi_get_all_media(array('number' => 10000));
$companies = esi_get_all_entity(array('number' => 10000));
$types = esi_get_all_type(array('number' => 10000));
$regions = esi_get_all_region(array('number' => 10000));
$countries = esi_get_all_countries();

$investment = esi_get_investment( $item->investment_id );
$investmentTypeList = esi_get_all_investment_type(array('number' => 10000));
$investorArray = esi_get_investor_array($item->investment_id);


$industries = array();
$functions = array();
$radars_news = array();
$selectedTechnologies = array();

$radars_news = esi_get_radar_by_news($id);
if ($radars_news) {
    foreach ($radars_news as $key => $value) {
        $industries = array_merge(esi_get_radar_industry($value), $industries);
        $functions = array_merge(esi_get_radar_function($value), $functions);
    }
}
$industry_news = esi_get_industry_by_news($id);
$function_news = esi_get_function_by_news($id);

$technologies = esi_get_list_technology();
$selectedTechnologies = esi_get_list_technology_news($id);
$technologies_selected = esi_get_list_selected_news_technology($id, 'array');
$abilities = esi_get_all_ability(array('number' => 10000));
$abilities_selected = esi_get_list_selected_news_ability($id, 'array');

add_thickbox();
?>

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

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

        <table class="form-table">
            <tbody>
                <tr class="row-title">
                    <th scope="row">
                        <label for="title"><?php _e( 'Title', 'esi' ); ?></label>
                    </th>
                    <td>
                        <input type="text" name="title" id="title" class="regular-text" placeholder="<?php echo esc_attr( '', 'esi' ); ?>" value="<?php echo ($item)?esc_attr( $item->title ):$title; ?>" 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="15" cols="90" required="required"><?php echo ($item)?esc_textarea( $item->description ):$content; ?></textarea>
                    </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( '', 'esi' ); ?>" value="<?php echo ($item)?esc_attr( $item->url ):$newsUrl; ?>" />
                    </td>
                </tr>
                <tr class="row-radar-id">
                    <th scope="row">
                        <label for="radar_id"><?php _e( 'Radar', 'esi' ); ?></label>
                    </th>
                    <td>
                        <select id="radar_id" name="radar_id">
                            <option value="0">Select Radar</option>
                            <?php foreach ($radars as $radar) : ?>
                                <option value="<?= $radar->id ?>" <?= (in_array($radar->id, $radars_news))?'selected="selected"':'' ?>><?= $radar->name ?></option>
                            <?php endforeach; ?>
                        </select><br /><br />
                        <label for="radar_industry">Select industry</label>
                        <select id="radar_industry" name="radar_industry[]" multiple="multiple">
                            <?php foreach ($industries as $industry) : ?>
                                <option value="<?= $industry->id ?>" <?= (in_array($industry->id, $industry_news))?'selected="selected"':'' ?>><?= $industry->name ?></option>
                            <?php endforeach; ?>
                        </select><br />
                        <label for="radar_function">Select function</label>
                        <select id="radar_function" name="radar_function[]" multiple="multiple">
                            <?php foreach ($functions as $function) : ?>
                                <option value="<?= $function->id ?>" <?= (in_array($function->id, $function_news))?'selected="selected"':'' ?>><?= $function->name ?></option>
                            <?php endforeach; ?>
                        </select>
                    </td>
                </tr>
                <tr class="row-media-id">
                    <th scope="row">
                        <label for="media_id"><?php _e( 'Media', 'esi' ); ?></label>
                    </th>
                    <td>
                        <select id="media_id" name="media_id" style="float:left">
                            <option value="0">Select a media</option>
                            <?php foreach ($medias as $media) : ?>
                                <option value="<?= $media->id ?>" <?= ($item->media_id == $media->id || $media_id == $media->id )?'selected="selected"':'' ?>><?= $media->name ?></option>
                            <?php endforeach; ?>
                        </select>
                        <a href="#TB_inline?width=600&height=450&inlineId=esi-add-media"
                        class="page-title-action thickbox" id="esi_add_media" style="float:left;margin-top:5px;"  onclick="self.parent.tb_remove();">
                        Add media
                    </a>
                </td>
            </tr>
            <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" class="chosen-select" style="float:left">
                        <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>
                    <a href="/wp-admin/admin.php?page=esi-management-entity&action=new" target="_blank"
                    class="page-title-action" style="margin-top:5px;" >
                    Add entity
                </a>
                </td>
            </tr>
            <tr class="row-investment-id">
                <th scope="row">
                    <label for="investment_id"><?php _e( 'Investment', 'esi' ); ?></label>
                </th>
                <td>
                    <input type="hidden" name="investment_id" id="investment_id" value="<?= (isset($investment->id))?$investment->id:""; ?>" />

                    <p class="investment-details">
                        <?php if(!isset($investment)): ?>
                            <a href="#TB_inline?width=600&height=450&inlineId=esi-add-investment"
                            class="page-title-action thickbox" id="esi_add_investment" style="float:left">
                            Add investment
                        </a>
                        <?php else:
                            $entity = esi_get_entity($investment->entity_id);
                            $investmentType = esi_get_investment_type($investment->investment_type_id);
                            ?>
                            <b>entity :</b> <?= $entity->name; ?><br />
                            <b>Investment type :</b> <?= $investmentType->name; ?><br />
                            <b>Investor :</b>
                            <?php $investors = array();
                            foreach ($companies as $entity) : ?>
                                <?php if(isset($investorArray) && in_array($entity->id, $investorArray)){
                                    $investors[] = $entity->name;
                                } ?>
                            <?php endforeach; ?>
                            <?= implode(', ', $investors); ?>
                            <br />
                            <b>Amount :</b> <?= $investment->amount; ?><br />
                            <b>Date :</b> <?= $investment->investment_date; ?><br /><br />
                            </p>
                            <a href="#TB_inline?width=600&height=450&inlineId=esi-add-investment"
                            class="page-title-action thickbox" id="esi_industry">
                            Edit investment
                            </a>
                        <?php endif; ?>
                    </p>

                </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_sub_id[]" multiple="multiple">
                            <?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-type-id">
                <th scope="row">
                    <label for="type_id"><?php _e( 'Type', 'esi' ); ?></label>
                </th>
                <td>
                    <select id="type_id" name="type_id">
                        <option value="0">Select a type</option>
                        <?php foreach ($types as $type) : ?>
                            <option value="<?= $type->id ?>" <?= ($item->type_id == $type->id)?'selected="selected"':'' ?>><?= $type->name ?></option>
                        <?php endforeach; ?>
                    </select>
                </td>
            </tr>
            <tr class="row-type-id">
                <th scope="row">
                    <label for="region_id"><?php _e( 'Region', 'esi' ); ?></label>
                </th>
                <td>
                    <select id="region_id" name="region_id">
                        <option value="0">Select a region</option>
                        <?php foreach ($regions as $region) : ?>
                            <option value="<?= $region->id ?>" <?= ($item->region_id == $region->id)?'selected="selected"':'' ?>><?= $region->name ?></option>
                        <?php endforeach; ?>
                    </select>
                </td>
            </tr>
            <tr class="row-publish-date">
                <th scope="row">
                    <label for="publish_date"><?php _e( 'Publish Date', 'esi' ); ?></label>
                </th>
                <td>
                    <input type="date" name="publish_date" id="publish_date" class="regular-text" placeholder="<?php echo esc_attr( '', 'esi' ); ?>" value="<?php echo ($item)?esc_attr( explode(' ', $item->publish_date)[0] ):''; ?>" required="required" />
                </td>
            </tr>
            <tr class="row-ability-id">
                <th scope="row"><dsw
                    <label for="ability_id"><?php _e( 'Ability', 'esi' ); ?></label>
                </th>
                <td>
                    <select id="ability_id" class="chosen-select" name="ability_id[]" multiple="multiple">
                        <?php foreach ($abilities as $ability) : ?>
                            <option value="<?= $ability->id ?>" <?= (in_array($ability->id, $abilities_selected)) ? "selected='selected'" : ""?>><?= $ability->name ?></option>
                        <?php endforeach; ?>
                    </select><br />
                </td>
            </tr>
        </tbody>
    </table>

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

<?php wp_nonce_field( '' ); ?>
        <?php if ($_GET['action'] == 'edit'): ?>
            <?php submit_button( __( 'Update News', 'esi' ), 'primary', 'submit new' ); ?>
        <?php else: ?>
            <?php submit_button( __( 'Add News', 'esi' ), 'primary', 'submit new' ); ?>
        <?php endif; ?>

</form>
</div>

<div id="esi-add-investment" style="display:none;">
    <div class="wrap">
        <h2>Add Investment</h2>
        <form method="post" id="esi_form_investment">

            <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_investment" name="entity_id">
                                <option value="0">Select a entity</option>
                                <?php foreach ($companies as $entity) : ?>
                                    <option value="<?= $entity->id ?>" <?= (isset($investment) && $investment->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 ?>" <?= (isset($investment) && $investment->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 ?>" <?= (isset($investorArray) && 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 (isset($investment))?esc_attr( $investment->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 (isset($investment))?esc_attr( explode(' ', $investment->investment_date)[0] ):''; ?>" required="required" />
                        </td>
                    </tr>
                </tbody>
            </table>

            <input type="hidden" name="action" value="esi_investment_ajax_action" />
            <input type="hidden" name="field_id" value="<?php echo (isset($investment))?$investment->id:0; ?>">

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

        </form>
    </div>
</div>



<div id="esi-add-entity" style="display:none;">
    <div class="wrap">
        <h2>Add entity</h2>

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

            <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="" 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"></textarea>
                        </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="" required="required" />
                        </td>
                    </tr>
                    <tr class="row-country">
                        <th scope="row">
                            <label for="url"><?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_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="" 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 _e( '', 'esi' ); ?></label>
                        </td>
                    </tr>
                </tbody>
            </table>


            <input type="hidden" name="action" value="esi_entity_ajax_action" />

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

        </form>
    </div>
</div>



<div id="esi-add-media" style="display:none;">
    <div class="wrap">
        <h1><?php _e( 'Add Media', 'esi' ); ?></h1>

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

            <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 media name', 'esi' ); ?>" required="required" />
                        </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 media URL', 'esi' ); ?>" required="required" />
                        </td>
                    </tr>

                </tbody>
            </table>

            <input type="hidden" name="action" value="esi_media_ajax_action" />

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

        </form>
    </div>
</div>
