import { Controller } from 'stimulus';

const loader = require("./../components/loader.js");
const helper = require("./../components/helper.js");

/*
 * This is an example Stimulus controller!
 *
 * Any element with a data-controller="widgets" attribute will cause
 * this controller to be executed. The name "recaptcha" comes from the filename:
 * recaptcha_controller.js -> "recaptcha"
 *
 * Delete this file or adapt it for your use!
 */
export default class extends Controller {
    connect() {
        loader.loadSDK('widget').then(function(webSDK) {
            webSDK.on('init', function() {
                if (this.data.user.authenticated) {
                    runWebSDK();           
                }
            });
        })

        let runWebSDK = function () {
            this.securityCard();
            this.finance();
        }.bind(this)
    }

    finance() {
        var financeWidget = document.getElementById('finance-widget');

        window.webSDK.loadPaymentList({
            start: 0,
            length: 4
        }).then(function (resolve) {
            financeWidget.innerHTML = '';
            
            if (resolve.length > 0) {
                resolve.forEach(function(item) {
                    var div = document.createElement('div');
                    div.classList = 'payment-line';

                    var date = window.newDate(item.date);
                    var spanID = document.createElement('span');
                    spanID.classList = 'payment-line__id';
                    spanID.innerHTML = 'Счет № ' + item.id + ' от ' + window.dateFormat(date, 'd-m-Y');
                    div.append(spanID);

                    var spanPrice = document.createElement('span');
                    spanPrice.classList = 'payment-line__price';
                    spanPrice.innerHTML = item.amt + ' ₽';
                    div.append(spanPrice);
                    
                    financeWidget.append(div);
                });    
            } else {
                var item = document.createElement('span');
                item.innerHTML = 'Записей не найдено';
                item.classList = 'line-item';                     
                financeWidget.append(item);
            }
        }).catch(function (error) {
            helper.sendRequest({
                data: {'error': error, method: 'loadPaymentList'}
            }, helper.getHostname() + '/api/log', "POST", "json", true, "application/json");
        })
    }

    securityCard() {
        var securityCardWidget = document.getElementById('security-card-widget');
        var date = new Date();
        var lastDate = new Date(date.getFullYear(), date.getMonth() + 1, 0);
        
        if (window.webSDK.data.user.allows.caseHistory == true) {
            window.webSDK.loadTreatPlaceList({
                st: 20200101,
                en: window.dateFormat(lastDate),
                start: 0,
                length: 4
            }).then(function (resolve) {
                securityCardWidget.innerHTML = '';
                
                if (resolve.data.length > 0) {
                    resolve.data.forEach(function(el) {
                        var item = document.createElement('span');
                        item.innerHTML = el.protocolName.slice(0,37) + '...';
                        item.classList = 'line-item';

                        securityCardWidget.append(item);
                    });
                } else {
                    var item = document.createElement('span');
                    item.innerHTML = 'Записей не найдено';
                    item.classList = 'line-item';                     
                    securityCardWidget.append(item);
                }
            }).catch(function (error) {
                helper.sendRequest({
                    data: {'error': error, method: 'loadTreatPlaceList'}
                }, helper.getHostname() + '/api/log', "POST", "json", true, "application/json");
                
                securityCardWidget.innerHTML = '';
                var item = document.createElement('span');
                item.innerHTML = 'Записей не найдено';
                item.classList = 'line-item';                     
                securityCardWidget.append(item);
            });
        } else {
            securityCardWidget.innerHTML = '';
            var item = document.createElement('span');
            item.innerHTML = 'Записей не найдено';
            item.classList = 'line-item';                     
            securityCardWidget.append(item);
        }
    }
}
