// ==UserScript==
// @name         Snipe-IT Asset List Warranty Link
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  try to take over the world!
// @author       You
// @match        https://cbnva.snipe-it.io/hardware
// @match        https://cbnva.snipe-it.io/models/*
// @grant        none
// @updateURL    https://gist.github.com/JeremyLee/00b62ff8f8d4c497e502cc204a23137f/raw/SnipeITAssetListWarrantyLink.user.js
// @downloadURL  https://gist.github.com/JeremyLee/00b62ff8f8d4c497e502cc204a23137f/raw/SnipeITAssetListWarrantyLink.user.js
// ==/UserScript==

(function() {
    'use strict';

    window.hardwareWarrantyFormatter = function hardwareWarrantyFormatter(value, row) {
        if((!row.manufacturer) || (typeof row.manufacturer.name !== "string") || row.manufacturer.name.toLowerCase().indexOf("dell") === -1) {
            return value;
        } else {
            if(value === null || ("string" === typeof value && value.length === 0)) {
                return '<a target="_blank" href="https://www.dell.com/support/home/us/en/04/product-support/servicetag/' + row.serial + '/warranty?ref=captchasuccess">Warranty</a>';
            } else {
                return '<a target="_blank" href="https://www.dell.com/support/home/us/en/04/product-support/servicetag/' + row.serial + '/warranty?ref=captchasuccess">' + value + '</a>';
            }
        }

    };
    window.hardwareNameFormatterWithRemote = function hardwareNameFormatterWithRemote(value, row) {
        var html = ''
        if(row && row.custom_fields && row.custom_fields["Remote Access"] && row.custom_fields["Remote Access"].value) {
            html += '<a href="st-business://com.splashtop.business?uuid=' + row.custom_fields["Remote Access"].value + '&sessiontype=remote"><img style="width:16px; height:16px;" src="https://theme.zdassets.com/theme_assets/294070/7205f6bf4ce931b326f56a3b0395092ca05dcc74.png"> </a>'
        }
        if (value) {
            html += '<a href="https://cbnva.snipe-it.io/hardware/' + row.id + '"> ' + value + '</a>';
        }
        return html;

    };

    var table = $("#assetsListingTable");
    if(table.length == 0) {
        table = $("#assetListingTable");
    }
    var options = table.bootstrapTable('getOptions');
    options.columns[0].forEach(function(column) {
        if(column.field==="warranty_months") {
            column.formatter="hardwareWarrantyFormatter";
        }
    });
    options.columns[0].forEach(function(column) {
        if(column.field==="name") {
            column.formatter="hardwareNameFormatterWithRemote";
        }
    });
    table.bootstrapTable('refreshOptions', {});
    //debugger;
})();