<?php
/**
 * Webkul Software.
 *
 * @category   Webkul
 * @package    Webkul_Agorae
 * @author     Webkul Software Private Limited
 * @copyright  Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license    https://store.webkul.com/license.html
 */

// @codingStandardsIgnoreFile

/**
 * Seller Pick attribute edit at seller product edit page
 *
 * @var $block \Webkul\Agorae\Block\Seller\Attributes
 */
$viewModel = $block->getData("viewModel");
$moduleManager = $viewModel->getModuleManager();
if ($moduleManager->isOutputEnabled('Webkul_Marketplace')) {
$helper = $this->helper('Webkul\Agorae\Helper\Data');
$mpHelper = $this->helper('Webkul\Marketplace\Helper\Data');
$isPartner = $mpHelper->isSeller();
$currencySymbol = $mpHelper->getCurrencySymbol();
$attributesCollection = $block->getAgoraeSellerAttributes();
$product = $block->getCurrentProduct();
if($isPartner == 1 && $attributesCollection && $product){
    foreach ($attributesCollection as $attribute) {
        $attributeCode = $attribute->getAttributeCode();
        $defaultvalues = $product->getResource()->getAttribute(
            $attributeCode
        )->getDefaultValue();
        if($attribute['is_user_defined']  == 1){
            $isRequired = '';
            if ($attribute['is_required']) {
                $isRequired = 'required-entry';
            }
            ?>
            <div class="field <?php if($attribute['is_required']) {echo 'required'; } ?>">
                <label class="label" id="<?php echo $attribute['attribute_code']?>">
                    <?php echo $attribute['frontend_label']; ?>
                    <?php if($attribute['frontend_input'] == 'price'){ ?>
                            <b><?php echo '('.$currencySymbol.')' ?></b>
                    <?php }?>:
                </label>
                <div class="control">
                    <?php if($attribute['frontend_input'] == 'select'
                            ||$attribute['frontend_input']=='multiselect'
                        ){
                            $attributeOptions = $attribute->getSource()->getAllOptions();
                            $fix="";
                            $multiselect="";
                            if($attribute['frontend_input']=='multiselect'){
                                $fix="[]";  
                                $multiselect="multiple='multiple'";
                            } ?>
                            <select class="<?php if($attribute['is_required']) {
                                            echo 'required-entry';
                                        } ?> input-text"
                                    name="<?php echo "product[$attributeCode]".$fix;?>"
                                    <?php echo $multiselect;?>>
                        <?php foreach($attributeOptions as $optionObj){
                                if(is_object($optionObj)){
                                    $option = (array)json_decode(json_encode($optionObj));
                                }else{
                                    $option = $optionObj;
                                }
                                $values=explode(',',$product[$attributeCode]);
                                if ($values[0] == 0) {
                                    $values= explode(',',$defaultvalues);
                                }
                                $selected="";                   
                                if(in_array($option["value"], $values)){
                                    $selected= "selected='selected'";
                                } ?>
                                <option <?php echo $selected; ?>
                                    value="<?php echo $option["value"]; ?>">
                                    <?php echo $option["label"]; ?>
                                </option>
                        <?php } ?>
                            </select>
                    <?php } ?>
                    <?php if($attribute['frontend_input']=='boolean'){ ?>
                            <select class="<?php if($attribute['is_required']) {
                                echo 'required-entry';
                                } ?> input-text"
                                name="<?php echo "product[$attributeCode]";?>">
                        <?php foreach($block->getBooleanAttributeOption() as $option){
                                $values=explode(',',$product[$attributeCode]);
                                $selected="";
                                if ($values[0] == 0) {
                                    $values= explode(',',$defaultvalues);
                                }
                                if(in_array($option["value"], $values)){
                                    $selected= "selected='selected'";
                                }
                                ?>
                                <option <?php echo $selected; ?> value="<?php echo $option["value"]; ?>">
                                    <?php echo $option["label"]; ?>
                                </option>
                            <?php
                            } ?>
                            </select>
                    <?php } ?>
                    <?php if($attribute['frontend_input'] == 'text'){ ?>
                        <input class="<?php echo $isRequired.' '.$attribute['frontend_class']; ?> input-text" type="text" name="<?php echo "product[$attributeCode]";?>" value="<?php echo $product[$attributeCode]==''?$defaultvalues:$product[$attributeCode]; ?>" />
                    <?php } ?>
                    <?php if($attribute['frontend_input'] == 'price'){ 
                        $product[$attributeCode] = $product[$attributeCode]==''?$defaultvalues:$product[$attributeCode];
                        ?>
                        <input class="<?php if($attribute['is_required']) {echo 'required-entry'; } ?> input-text <?php echo $attribute['frontend_class']; ?>" type="number" name="<?php echo "product[$attributeCode]";?>" value="<?php
                            echo $this->helper('Magento\Framework\Pricing\Helper\Data')->currency($product[$attributeCode], false, false);
                          ?>" />
                    <?php } ?>
                    <?php if($attribute['frontend_input'] == 'weight'){ ?>
                        <input class="<?php echo $isRequired.' '.$attribute['frontend_class']; ?> input-text" type="text" name="<?php echo "product[$attributeCode]";?>" value="<?php echo $product[$attributeCode]==''?$defaultvalues:$product[$attributeCode]; ?>" />
                    <?php } ?>
                    <?php if($attribute['frontend_input'] == 'date'){ 
                        $product[$attributeCode] = $product[$attributeCode]==''?$defaultvalues:$product[$attributeCode];
                        ?>
                        <input class="<?php echo $isRequired.' '.$attribute['frontend_class']; ?> input-text datetype" type="text" name="<?php echo "product[$attributeCode]";?>" value="<?php echo $block->formatDate($product[$attributeCode]); ?>" />
                    <?php } ?>
                    <?php if($attribute['frontend_input'] == 'textarea'){ ?>
                        <textarea class="<?php echo $isRequired.' '.$attribute['frontend_class']; ?> input-text" rows="5" cols="75" id="textar" name="<?php echo "product[$attributeCode]";?>" /><?php echo $product[$attributeCode]==''?$defaultvalues:$product[$attributeCode]; ?></textarea>
                    <?php } ?>
                    <?php if($attribute['frontend_input'] == 'media_image'){ ?>
                        <input class="input-text" type="file" name="<?php echo "product[$attributeCode]";?>" value="<?php echo $product[$attributeCode]==''?$defaultvalues:$product[$attributeCode]; ?>" />
                        <div id="wk_add_images_container">
                            <img class='wk_images' width='75' height='75' alt='image' src="<?php echo $helper->getMediaUrl() ?>catalog/product<?php echo  $product[$attributeCode]==''?$defaultvalues:$product[$attributeCode]; ?>"/>
                        </div>
                        
                    <?php } ?>
                    <?php if($attribute['frontend_input'] == 'gallery'){ ?>
                        <input class="<?php if($attribute['is_required']) {echo 'required-entry'; } ?> input-text <?php echo $attribute['frontend_class']; ?>" type="file" name="<?php echo "product[$attributeCode]";?>" />
                    <?php } ?>
                </div>
            </div>
        <?php }
    }
}
} ?>