<?php
/** @var \Magetop\Advancepreview\Helper\Data $preivewHelper */
$preivewHelper = $this->helper('Magetop\Advancepreview\Helper\Data');
//echo get_class($preivewHelper);
$industryOptions = $preivewHelper->getOpdSetting('advancepreview/register_setting/industry_value');
//echo $industryOptions;
$industryOptions = $industryOptions != '' ? explode(',',$industryOptions) : [];
$allStores = $preivewHelper->getAllStore();
$data = $block->getData('object');
$formData = $data->getData();
$custom = count($formData)>0? $formData['custom']:array();
$bigTypefileds = [
    'company_name'=>'Company name',
    'industry'=>'Industry',
    'street'=>'Street',
    'number'=>'House number',
    'postcode'=>'Postcode',
    'city'=>'City',
    'country'=>'Country',
    'phone'=>'Phone',
    'is_reseller'=>'Is reseller?',
    'taxvat'=>'Sales tax / VAT number',
    'preferred_language'=>'Preferred language'
];
/*$agencyTypefileds = [
    'company_name '=>'Company name ',
    'custom_industry'=>'Industry',
    'custom_street'=>'Street',
    'custom_number'=>'Home number',
    'custom_postcode'=>'Postcode',
    'custom_city'=>'City',
    'custom_country'=>'Country',
    'custom_phone'=>'Phone',
    'custom_preferred_language'=>'Preferred language',
    'custom_sales_tax'=>'Sales tax / VAT number',
];*/
?>

<div class="" id="custom-fields-big" style="display: none;">
    <legend class="custom-legend"><span><?php echo __('Company information') ?></span></legend>
    <?php foreach ($bigTypefileds as $key => $bigTypefiled) :

        ?>
        <div class="mgt-custom-additnation-filed" id="mgt-custom-additnation-filed-<?php echo $key; ?>">
            <label class="label" for="custom-<?php echo $key; ?>" style="display:<?php echo $key == 'preferred_language'? 'none':'black'; ?>">
                <span><?= $block->escapeHtml(__($bigTypefiled)) ?><span class="mand">*</span></span>
            </label>
            <div class="control">
                <?php if($key == 'industry') : ?>
                <select id="custom-<?php echo $key; ?>" name="custom[custom_<?php echo $key; ?>]" data-validate="{required:true}">
                    <?php if(count($industryOptions)) : ?>
                        <option value=""><?php echo __('Select') ?></option>
                        <?php foreach ($industryOptions as $industryOption) : ?>
                            <?php $vIndustryOption = str_replace(' ','',$industryOption) ?>
                            <option value="<?php echo $vIndustryOption ?>" <?php echo (count($custom)>0 && isset($custom['custom_'.$key]) && $custom['custom_'.$key] == $vIndustryOption)?'selected':'';?>><?php echo __($industryOption) ?></option>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </select>
                <?php elseif($key == 'preferred_language') : ?>
                    <select id="custom-<?php echo $key; ?>" style="display:<?php echo $key == 'preferred_language'? 'none':'black'; ?>" name="custom[custom_<?php echo $key; ?>]" data-validate="{required:true}">
                        <option value=""><?php echo __('Select') ?></option>
                        <?php if(count($allStores)) : ?>
                            <?php foreach ($allStores as $z => $allStore) : ?>
                                <option value="<?php echo $allStore['store_code'] ?>" <?php echo $z == 0? 'selected':'' ?>><?php echo $allStore['store_name']  ?></option>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </select>
                <?php elseif($key == 'is_reseller') : ?>
                    <label class="switch">
                        <input type="checkbox" id="txt-register-is-seller" name="custom[custom_<?php echo $key; ?>]" value="1" <?php echo (count($custom)>0 && isset($custom['custom_'.$key]))?'selected':'';?>>
                        <span class="slider round"></span>
                    </label>
                <?php else:  ?>
                <?php $typeInput = in_array($key,array('phone','postcode')) ? 'number' : 'text'; ?>
                <?php if($key == 'taxvat') : ?>
                        <input value="<?php echo (count($formData)>0 && isset($formData[$key]))?$formData[$key]:'';?>" type="<?php echo $typeInput; ?>" onkeypress="return characterWithSpaceAddress(event);" name="taxvat" maxlength="30" id="custom-<?php echo $key; ?>" value="" title="<?= $block->escapeHtmlAttr(__($bigTypefiled)) ?>" class="input-text" data-validate="{required:true}"
                       autocomplete="off"
                       maxlength="30">
                    <?php else : ?>
                        <input value="<?php echo (count($custom)>0 && isset($custom['custom_'.$key]))?$custom['custom_'.$key]:'';?>" <?php if($key == 'phone'){ ?> type="text" onkeypress="return validatePhone(event);" maxlength="18" onpaste="return false" <?php } else if($key == 'postcode') {?> type="<?php echo $typeInput; ?>" onkeypress="return validateQty(event);" maxlength="10" onpaste="return false" <?php } else{?> type="<?php echo $typeInput; ?>" onkeypress="return characterWithSpaceAddress(event);" maxlength="200" <?php } ?> name="custom[custom_<?php echo $key; ?>]" id="custom-<?php echo $key; ?>" value="" title="<?= $block->escapeHtmlAttr(__($bigTypefiled)) ?>" class="input-text" data-validate="{required:true}">

                    <?php endif; ?>
                <?php endif; ?>
            </div>

        </div>
    <?php endforeach; ?>
</div>
<style>
    .switch {
        position: relative;
        display: inline-block;
        width: 60px;
        height: 34px;
    }

    .switch input {
        opacity: 0;
        width: 0;
        height: 0;
    }

    .slider {
        position: absolute;
        cursor: pointer;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: #ccc;
        -webkit-transition: .4s;
        transition: .4s;
    }

    .slider:before {
        position: absolute;
        content: "";
        height: 26px;
        width: 26px;
        left: 4px;
        bottom: 4px;
        background-color: white;
        -webkit-transition: .4s;
        transition: .4s;
    }

    input:checked + .slider {
        background-color: #2196F3;
    }

    input:focus + .slider {
        box-shadow: 0 0 1px #2196F3;
    }

    input:checked + .slider:before {
        -webkit-transform: translateX(26px);
        -ms-transform: translateX(26px);
        transform: translateX(26px);
    }

    /* Rounded sliders */
    .slider.round {
        border-radius: 34px;
    }

    .slider.round:before {
        border-radius: 50%;
    }
    .mand{
        color: #e02b27;
        font-size: 1.2rem;
        margin: 0 0 0 6px;
    }
    #mgt-custom-additnation-filed-preferred_language{
        margin-bottom: 30px;
    }
    .field.choice.newsletter{
        margin-bottom: 12px;
    }
    .fieldset.create.account{
        padding-bottom: 75px !important;
    }
</style>
<script>
    // document.querySelector("#custom-phone").addEventListener("keypress", function (evt) {
    //     if (evt.which != 8 && evt.which != 0 && evt.which < 48 || evt.which > 57)
    //     {
    //         evt.preventDefault();
    //     }
    // });
    // document.querySelector("#custom-postcode").addEventListener("keypress", function (evt) {
    //     if (evt.which != 8 && evt.which != 0 && evt.which < 48 || evt.which > 57)
    //     {
    //         evt.preventDefault();
    //     }
    // });
    function characterWithSpaceAddress(event) {
        var regex = /^[@#$%<>"!`_~:;?\/\^\]&\[*}{)(=\\]*$/;
        var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
        console.log(key);
        if (regex.test(key)) {
            event.preventDefault();
            return false;
        }
    };
    function validateQty(event) {
        var key = window.event ? event.keyCode : event.which;
        if (event.keyCode == 8 || event.keyCode == 46
         || event.keyCode == 39) {
            return true;
        }
        else if ( key < 48 || key > 57 ) {
            return false;
        }
        else return true;
    };
    function validatePhone(event) {
        var key = window.event ? event.keyCode : event.which;
        if (event.keyCode == 8 || event.keyCode == 46 || event.keyCode == 39 || event.keyCode == 40 || event.keyCode == 41 || event.keyCode == 43 || event.keyCode == 45) {
            return true;
        }
        else if ( key < 48 || key > 57 ) {
            return false;
        }
        else return true;
    };
    var custom_customer_type = "<?php echo (count($formData)>0 && isset($formData['txt_customer_group']))?$formData['txt_customer_group']:'';?>";
    setTimeout(() => {
        if(custom_customer_type!='')
        {
            document.getElementById(custom_customer_type==2?"custom-customer-type-big":"custom-customer-type-b2c").click();
        }
    }, 1000);
</script>
