DS = { fn: {} }

DS.fn.operateFormatter = function (value, row, index) {
    var operateBtns = [
        '<a class="edit operate-btn" title="Edit" data-target="#amazonItemModal" data-toggle="modal">',
        '<i class="glyphicon glyphicon-edit"></i>',
        '</a>',
        '<a class="remove operate-btn" title="Remove">',
        '<i class="glyphicon glyphicon-remove"></i>',
        '</a>'
    ];
    return operateBtns.join(' ');
}


DS.fn.amazonTableOperateEvents = {
    'click .edit': function (e, value, row, index) {
        $('#modalPreBooking').modal('show')
    },
    'click .remove': function (e, value, row, index) {
        if (confirm('삭제할까요?')) {
            // $.get(location.href + '/remove/' + value, {}, function (d) {
            //     if (d.ok) {
            //         $('.ecms-table').bootstrapTable('refresh')
            //     }
            // }, 'json');
        }
    }
}

DS.fn.AmazonItemImageFormatter = function (value, row, index) {
    return value ? '<img src="' + value + '" width="70">' : 'No image';
}

DS.fn.AmazonItemTitleFormatter = function (value, row, index) {
    return value ? '<a href="https://www.amazon.com/dp/' + row.asin + '" target="_blank">' + row.title + '</a>' : 'No Title';
}

DS.fn.AmazonItemCategoryFormatter = function (value, row, index) {
    return value ? '<a href="' + value + '" target="_blank">' + row.category + '</a>' : 'No Category';
}

DS.fn.AmazonItemVideoFormatter = function (value, row, index) {
    return value ? '<a href="' + value + '" target="_blank">' + row.video + '</a>' : 'No Video';
}

DS.fn.addItem = function (asinInput) {
    $('#show-table').bootstrapTable('showLoading')
    $.get('/dropshipping/add_asin/' + asinInput.value, {}, function (res) {
        if (res.code == 200) {
            // alert('Saved!')                        
            asinInput.value = '';
        } else {
            alert(res.message)
        }
        asinInput.readOnly = false;
        $('#show-table').bootstrapTable('refresh')
    })
}

$(function () {
    $('.input-daterange input').each(function () {
        $(this).datepicker({ format: 'yyyy-mm-dd' });
    });

    $('.ds-input-asin').on('keydown', function (e) {
        if (e.keyCode == 13) {
            this.readOnly = true;
            DS.fn.addItem(this)
        }
    })

    $('.ds-search-btn').on('click', function(e) {
        var options = SHOW.node('#show-table').bootstrapTable('getOptions');
        options.queryParams = function (a) {
            var params = {
                'keyword': SHOW.node('input[name=keyword]').val(),
                'app': $('.app-select').val(),
                'category_type': $('.category-type-select').val()
            }
            if (SHOW.node('.video-list-app-select').length) {
                if (SHOW.node('.video-list-app-select').val()) {
                    params.appid = SHOW.node('.video-list-app-select').val();
                }
            }
            if (SHOW.node('.video-list-category-select').length) {
                if (SHOW.node('.video-list-category-select').last().val()) {
                    params.category = SHOW.node('.video-list-category-select').last().val();
                }
            }
            return $.extend(a, params)
        }
        SHOW.node('#show-table').bootstrapTable('destroy').bootstrapTable(options)
    })
})