<?php

if (!defined('ABSPATH')) exit; // Exit if accessed directly

$action              = (isset($_GET['action'])) ? sanitize_text_field($_GET['action']) : '';
$heading             = '';
$id                  = (isset($_GET['portfolio_attribute'])) ? absint(intval($_GET['portfolio_attribute'])) : null;

switch ( $action ) {
	case 'add':
		$heading = __('Add new attribute', 'portfolio-responsive-gallery');
		$portfolio_attribute = array(
			'id'        => $this->portfolio_attributes_obj->get_new_attr_id(),
			'name'      => '',
			'slug'      => '',
			'type'      => '',
			'published' => ''
		);
		break;
	case 'edit':
		$heading             = __('Edit attribute', 'portfolio-responsive-gallery');
		$portfolio_attribute = $this->portfolio_attributes_obj->get_portfolio_attribute_by_id($id);
		break;
}

if (isset($_POST['ays_submit'])) {
	$result = $this->portfolio_attributes_obj->add_or_edit_portfolio_attribute($_POST, $id);
}
if (isset($_POST['ays_apply'])) {
	$this->portfolio_attributes_obj->add_or_edit_portfolio_attribute($_POST, $id, "apply");
}

$loader_iamge = "<span class='display_none ays_prg_loader_box'><img src='" . esc_url( AYS_PRG_ADMIN_URL . '/images/loaders/loading.gif' ) . "'></span>";

?>

<div class="wrap">
    <div class="container-fluid">
        <?php echo esc_html( $heading ); ?>
        <hr/>
        <form class="ays-portfolio-attribute-form" id="ays-portfolio-attribute-form" method="post">

            <div class="form-group row">
                <div class="col-sm-2">
                    <label for='ays-attribute-name'><?php echo esc_html__('Name', 'portfolio-responsive-gallery'); ?></label>
                </div>
                <div class="col-sm-10">
                    <input class='ays-text-input' id='ays-attribute-name' name='ays_prg_attr_name' required type='text'
                           value='<?php echo (isset($portfolio_attribute['name'])) ? esc_attr(stripslashes($portfolio_attribute['name'])) : ''; ?>'>
                </div>
            </div>

            <hr/>
            <div class="form-group row">
                <div class="col-sm-2">
                    <label><?php echo esc_html__('Slug', 'portfolio-responsive-gallery'); ?></label>
                </div>
                <div class="col-sm-10">
                    <input class='ays-text-input' id='ays-attribute-slug' name='ays_prg_attr_slug' required readonly
                           type='text' value="<?php echo esc_attr( 'prg_attr_' . $portfolio_attribute['id'] ); ?>">
                </div>
            </div>

            <hr/>
            <div class="form-group row">
                <div class="col-sm-2">
                    <label><?php echo esc_html__('Type', 'portfolio-responsive-gallery'); ?></label>
                </div>
                <div class="col-sm-10">
                    <select class='ays-text-input ays-text-input-short' id='ays-attribute-type'
                            name='ays_prg_attr_type'>
                        <option value="text" <?php echo (isset($portfolio_attribute['type']) && $portfolio_attribute['type'] == 'text') ? 'selected' : ''; ?>><?php echo esc_html__('Text', 'portfolio-responsive-gallery'); ?></option>
                        <option value="textarea" <?php echo (isset($portfolio_attribute['type']) && $portfolio_attribute['type'] == 'textarea') ? 'selected' : ''; ?>><?php echo esc_html__('Textarea', 'portfolio-responsive-gallery'); ?></option>
                        <option value="email" <?php echo (isset($portfolio_attribute['type']) && $portfolio_attribute['type'] == 'email') ? 'selected' : ''; ?>><?php echo esc_html__('E-Mail', 'portfolio-responsive-gallery'); ?></option>
                        <option value="number" <?php echo (isset($portfolio_attribute['type']) && $portfolio_attribute['type'] == 'number') ? 'selected' : ''; ?>><?php echo esc_html__('Number', 'portfolio-responsive-gallery'); ?></option>
                        <option value="tel" <?php echo (isset($portfolio_attribute['type']) && $portfolio_attribute['type'] == 'tel') ? 'selected' : ''; ?>><?php echo esc_html__('Telephone', 'portfolio-responsive-gallery'); ?></option>
                        <option value="url" <?php echo (isset($portfolio_attribute['type']) && $portfolio_attribute['type'] == 'url') ? 'selected' : ''; ?>><?php echo esc_html__('URL', 'portfolio-responsive-gallery'); ?></option>
                        <option value="date" <?php echo (isset($portfolio_attribute['type']) && $portfolio_attribute['type'] == 'date') ? 'selected' : ''; ?>><?php echo esc_html__('Date', 'portfolio-responsive-gallery'); ?></option>
                    </select>
                </div>
            </div>

            <hr/>
            <div class="form-group row">
                <div class="col-sm-2">
                    <label><?php echo esc_html__('Attribute status', 'portfolio-responsive-gallery'); ?></label>
                </div>

                <div class="col-sm-3">
                    <div class="form-check form-check-inline">
                        <input type="radio" id="ays-publish" name="ays_prg_attr_publish"
                               value="1" <?php echo ($portfolio_attribute["published"] == '') ? "checked" : ""; ?> <?php echo ($portfolio_attribute['published'] == '1') ? 'checked' : ''; ?> />
                        <label class="form-check-label"
                               for="ays-publish"> <?php echo esc_html__('Published', 'portfolio-responsive-gallery'); ?> </label>
                    </div>
                    <div class="form-check form-check-inline">
                        <input type="radio" id="ays-unpublish" name="ays_prg_attr_publish"
                               value="0" <?php echo ($portfolio_attribute['published'] == '0') ? 'checked' : ''; ?> />
                        <label class="form-check-label"
                               for="ays-unpublish"> <?php echo esc_html__('Unpublished', 'portfolio-responsive-gallery'); ?> </label>
                    </div>
                </div>
            </div>

            <hr/>
			<?php
			    wp_nonce_field('portfolio_attribute_action', 'portfolio_attribute_action');			
			?>
            <div class="ays_submit_button">
                <input type="submit" name="ays_submit" class="button ays-submit ays-button button-primary ays-prg-save-comp" value="<?php echo esc_attr__("Save and close", 'portfolio-responsive-gallery');?>" prg_submit_name="ays-submit" />  
                <?php if($id != null){ ?>
                    <input type="submit" name="ays_apply" id="ays_submit_apply" class="button ays-button ays-submit ays-prg-save-comp" title="Ctrl + s" data-toggle="tooltip" data-delay='{"show":"1000"}' value="<?php echo esc_attr__("Save", 'portfolio-responsive-gallery');?>" gpg_submit_name="ays-apply"/>
                <?php } ?>

                <?php echo $loader_iamge; ?>
            </div>
        </form>
    </div>
</div>