<?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_radar( $id );

add_thickbox();
?>

<!-- This file should primarily consist of HTML with a little bit of PHP. -->
<div class="wrap">

    <div id="icon-options-general" class="icon32"></div>
    <h1><?php echo esc_html(get_admin_page_title()); ?></h1>
    <br class="clear"/>

    <div id="poststuff">
        <div id="post-body" class="metabox-holder">
            <div id="post-body-content">
                <form method="post" id="mtsc_form">
                    <div id="titlediv">
                        <div id="titlewrap">
                            <label class="screen-reader-text" id="title-prompt-text" for="title">Radar name</label>
                            <input type="text" name="name" size="30" value="<?php echo ($item)?esc_attr( $item->name ):''; ?>" id="title" spellcheck="true" autocomplete="off" placeholder="<?php echo esc_attr( '', 'esi' ); ?>" required="required">
                        </div>
                    </div>
                    <br class="clear"/>
                    <div class="esi_section">
                        <div style="width:48%;float:left;">
                            <h3>Industry
                                <a href="#TB_inline?width=600&height=350&inlineId=esi-add-industry"
                                class="page-title-action thickbox" id="esi_industry" style="float:right">
                                Edit Industry
                            </a>
                            <a href="#TB_inline?width=600&height=350&inlineId=esi-add-industry"
                            class="page-title-action thickbox" id="esi_industry" style="float:right">
                            Add New Industry
                        </a>
                    </h3>
                    <select id="esi_industry_table" name="esi_industry_table[]" multiple="multiple">
                        <?php $arrIndustry = $wpdb->get_results("SELECT * FROM " .$wpdb->prefix . "esi_industry WHERE parent=0"); ?>
                        <?php foreach ($arrIndustry as $valueParent) : ?>
                            <?php if(count($arrIndustry) == 0): ?>
                                <option value="<?= $valueParent->id ?>" data-section="top" <?= (in_array($valueParent->id, $arrSelectedFunction))?'selected="selected"':'' ?>><?= $valueParent->name ?></option>
                            <?php endif; ?>
                            <?php $arrChildIndustry = $wpdb->get_results("SELECT * FROM " .$wpdb->prefix . "esi_industry WHERE parent=". $valueParent->id); ?>
                            <?php foreach ($arrChildIndustry as $valueChild) : ?>
                                <option value="<?= $valueChild->id ?>" data-section="<?= $valueParent->name ?>" <?= (in_array($valueChild->id, $arrSelectedIndustry))?'selected="selected"':'' ?>><?= $valueChild->name ?></option>
                            <?php endforeach; ?>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div style="width:48%;float:right;">
                    <h3>Function
                        <a href="#TB_inline?width=600&height=350&inlineId=esi-add-industry"
                        class="page-title-action thickbox" id="esi_industry" style="float:right">
                        Edit Function
                    </a>
                    <a href="#TB_inline?width=600&height=350&inlineId=esi-add-industry"
                    class="page-title-action thickbox" id="esi_function" style="float:right">
                    Add New Function
                </a>
            </h3>
            <select id="esi_function_table" name="esi_function_table[]" multiple="multiple">
                <?php $arrFunction = $wpdb->get_results("SELECT * FROM " .$wpdb->prefix . "esi_function WHERE parent=0"); ?>
                <?php foreach ($arrFunction as $valueParent) : ?>
                    <?php $arrChildFunction = $wpdb->get_results("SELECT * FROM " .$wpdb->prefix . "esi_function WHERE parent=". $valueParent->id); ?>
                    <?php if(count($arrChildFunction) == 0): ?>
                        <option value="<?= $valueParent->id ?>" data-section="top" <?= (in_array($valueParent->id, $arrSelectedFunction))?'selected="selected"':'' ?>><?= $valueParent->name ?></option>
                    <?php endif; ?>
                    <?php foreach ($arrChildFunction as $valueChild) : ?>
                        <option value="<?= $valueChild->id ?>" data-section="<?= $valueParent->name ?>" <?= (in_array($valueChild->id, $arrSelectedFunction))?'selected="selected"':'' ?>><?= $valueChild->name ?></option>
                    <?php endforeach; ?>
                <?php endforeach; ?>
            </select>
        </div>
    </div>
    <br class="clear"/>
    <br class="clear"/>
    <div style="float:right" >

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

        <?php wp_nonce_field( '' ); ?>
        <?php submit_button( __( 'Add new radar', 'esi' ), 'primary', 'submit_radar' ); ?>
    </div>
</form>
</div>

</div>
<br class="clear">
<div id="esi-add-industry" style="display:none;">
    <div class="wrap">
        <h2>Add new <span id="esi_type">industry</span></h2>
        <form method="post" id="esi_form_entities">
            <table class="form-table">
                <tr valign="top">
                    <th scope="row">Parent</th>
                    <td>
                        <select name="parent" id="esi_form_entities_parents">
                            <option value="0" selected="selected">None</option>
                            <?php foreach ($arrIndustry as $valueParent) : ?>
                                <option value="<?= $valueParent->id ?>" data-type="industry"><?= $valueParent->name ?></option>
                            <?php endforeach; ?>
                            <?php foreach ($arrFunction as $valueParent) : ?>
                                <option value="<?= $valueParent->id ?>" data-type="function"><?= $valueParent->name ?></option>
                            <?php endforeach; ?>
                        </select>
                    </td>
                </tr>
                <tr valign="top">
                    <th scope="row">Name</th>
                    <td>
                        <input type="text" name="name" value="" style="width:100%" />
                    </td>
                </tr>
            </table>
            <br />
            <br />
            <br />
            <div style="float:right" >
                <input type="hidden" name="table" id="esi_table" value="" />
                <input type="hidden" name="action" value="esi_ajax_action" />
                <input type="submit" name="submit" id="esi_submit_final" class="button button-primary" value="Save Changes">
            </div>
        </form>
    </div>
</div>

</div>
</div>
