'use strict';


define([
    'underscore',
    'pim/job/common/edit/field/field',
    'dam/template/job/common/directory_asset',
    'pim/fetcher-registry',
    'jquery.select2',
], function (
    _,
    BaseField,
    fieldTemplate,
    fetcherRegistry
) {
    return BaseField.extend({
        fieldTemplate: _.template(fieldTemplate),
        events: {
            'change select': 'updateState',

        },
        empty: ['No Active Job Instance Found'],
        data: ['Select Active Credential'],

        /**
         * {@inheritdoc}
         */

        render: function () {          
            var self = this;
            var code = this.getFormData().code;
            var only_asset_check = this.getFormData().configuration.only_asset_check;
            
            if(!only_asset_check) {
                self.el.hidden = true;
            } else {
                self.el.hidden = false;
            }
            
            fetcherRegistry.getFetcher('asset-list').fetchAll()
            .then(function (asset) {   
          
                self.config.options = asset;
                self.config.show = only_asset_check;
                self.config.multiselect = '';
                
                BaseField.prototype.render.apply(self, arguments);
                var elem = self.$('.select2'); 
                elem.select2();
                elem.on("change", function (e) {
                    self.updateState();
                    self.parent.render(); 
                    
                });

            });
        
            

        },
        

        /**
         * Get the field dom value
         *
         * @return {string}
         */
        getFieldValue: function () {
            return this.$('select').val();
        },
        updateCredential: function() {
            console.log('hello');
        },
    });
});
